Try to survive unavailability of local storage

Bug: 6035465
Change-Id: Ibe2d542349ec598824f78b91d374a977082fc2d1
This commit is contained in:
Jean Chalard 2012-02-29 19:21:42 +09:00
parent 5a0661eae8
commit f3f90e6d35
2 changed files with 45 additions and 13 deletions

View file

@ -212,7 +212,8 @@ public class UserBigramDictionary extends ExpandableDictionary {
@Override @Override
public void loadDictionaryAsync() { public void loadDictionaryAsync() {
// Load the words that correspond to the current input locale // Load the words that correspond to the current input locale
Cursor cursor = query(MAIN_COLUMN_LOCALE + "=?", new String[] { mLocale }); final Cursor cursor = query(MAIN_COLUMN_LOCALE + "=?", new String[] { mLocale });
if (null == cursor) return;
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1); int word1Index = cursor.getColumnIndex(MAIN_COLUMN_WORD1);
@ -249,11 +250,17 @@ public class UserBigramDictionary extends ExpandableDictionary {
qb.setProjectionMap(sDictProjectionMap); qb.setProjectionMap(sDictProjectionMap);
// Get the database and run the query // Get the database and run the query
try {
SQLiteDatabase db = sOpenHelper.getReadableDatabase(); SQLiteDatabase db = sOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, Cursor c = qb.query(db,
new String[] { MAIN_COLUMN_WORD1, MAIN_COLUMN_WORD2, FREQ_COLUMN_FREQUENCY }, new String[] { MAIN_COLUMN_WORD1, MAIN_COLUMN_WORD2, FREQ_COLUMN_FREQUENCY },
selection, selectionArgs, null, null, null); selection, selectionArgs, null, null, null);
return c; return c;
} catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) {
// Can't open the database : presumably we can't access storage. That may happen
// when the device is wedged; do a best effort to still start the keyboard.
return null;
}
} }
/** /**
@ -344,7 +351,18 @@ public class UserBigramDictionary extends ExpandableDictionary {
@Override @Override
protected Void doInBackground(Void... v) { protected Void doInBackground(Void... v) {
SQLiteDatabase db = mDbHelper.getWritableDatabase(); SQLiteDatabase db = null;
try {
db = mDbHelper.getWritableDatabase();
} catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) {
// If we can't open the db, don't do anything. Exit through the next test
// for non-nullity of the db variable.
}
if (null == db) {
// Not much we can do. Just exit.
sUpdatingDB = false;
return null;
}
db.execSQL("PRAGMA foreign_keys = ON;"); db.execSQL("PRAGMA foreign_keys = ON;");
// Write all the entries to the db // Write all the entries to the db
Iterator<Bigram> iterator = mMap.iterator(); Iterator<Bigram> iterator = mMap.iterator();

View file

@ -121,7 +121,8 @@ public class UserUnigramDictionary extends ExpandableDictionary {
public void loadDictionaryAsync() { public void loadDictionaryAsync() {
if (!ENABLE_USER_UNIGRAM_DICTIONARY) return; if (!ENABLE_USER_UNIGRAM_DICTIONARY) return;
// Load the words that correspond to the current input locale // Load the words that correspond to the current input locale
Cursor cursor = query(COLUMN_LOCALE + "=?", new String[] { mLocale }); final Cursor cursor = query(COLUMN_LOCALE + "=?", new String[] { mLocale });
if (null == cursor) return;
try { try {
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
int wordIndex = cursor.getColumnIndex(COLUMN_WORD); int wordIndex = cursor.getColumnIndex(COLUMN_WORD);
@ -212,10 +213,16 @@ public class UserUnigramDictionary extends ExpandableDictionary {
qb.setProjectionMap(sDictProjectionMap); qb.setProjectionMap(sDictProjectionMap);
// Get the database and run the query // Get the database and run the query
try {
SQLiteDatabase db = sOpenHelper.getReadableDatabase(); SQLiteDatabase db = sOpenHelper.getReadableDatabase();
Cursor c = qb.query(db, null, selection, selectionArgs, null, null, Cursor c = qb.query(db, null, selection, selectionArgs, null, null,
DEFAULT_SORT_ORDER); DEFAULT_SORT_ORDER);
return c; return c;
} catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) {
// Can't open the database : presumably we can't access storage. That may happen
// when the device is wedged; do a best effort to still start the keyboard.
return null;
}
} }
/** /**
@ -236,7 +243,14 @@ public class UserUnigramDictionary extends ExpandableDictionary {
@Override @Override
protected Void doInBackground(Void... v) { protected Void doInBackground(Void... v) {
SQLiteDatabase db = mDbHelper.getWritableDatabase(); SQLiteDatabase db = null;
try {
db = mDbHelper.getWritableDatabase();
} catch (android.database.sqlite.SQLiteCantOpenDatabaseException e) {
// With no access to the DB, this is moot. Do nothing: we'll exit through the
// test for null == db.
}
if (null == db) return null;
// Write all the entries to the db // Write all the entries to the db
Set<Entry<String,Integer>> mEntries = mMap.entrySet(); Set<Entry<String,Integer>> mEntries = mMap.entrySet();
for (Entry<String,Integer> entry : mEntries) { for (Entry<String,Integer> entry : mEntries) {