Merge "Do not throw NPE if the dictionary info is not available on db"

main
Mohammadinamul Sheik 2015-04-03 01:09:53 +00:00 committed by Android (Google) Code Review
commit ceaafa2ff9
4 changed files with 22 additions and 2 deletions

View File

@ -34,6 +34,8 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.TreeMap; import java.util.TreeMap;
import javax.annotation.Nullable;
/** /**
* Various helper functions for the state database * Various helper functions for the state database
*/ */
@ -705,6 +707,7 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
* @param version the word list version. * @param version the word list version.
* @return the metadata about this word list. * @return the metadata about this word list.
*/ */
@Nullable
public static ContentValues getContentValuesByWordListId(final SQLiteDatabase db, public static ContentValues getContentValuesByWordListId(final SQLiteDatabase db,
final String id, final int version) { final String id, final int version) {
final Cursor cursor = db.query(METADATA_TABLE_NAME, final Cursor cursor = db.query(METADATA_TABLE_NAME,

View File

@ -19,6 +19,7 @@ package com.android.inputmethod.dictionarypack;
import android.content.ContentValues; import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.util.Log;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -30,10 +31,13 @@ import java.util.List;
* Helper class to easy up manipulation of dictionary pack metadata. * Helper class to easy up manipulation of dictionary pack metadata.
*/ */
public class MetadataHandler { public class MetadataHandler {
public static final String TAG = MetadataHandler.class.getSimpleName();
// The canonical file name for metadata. This is not the name of a real file on the // The canonical file name for metadata. This is not the name of a real file on the
// device, but a symbolic name used in the database and in metadata handling. It is never // device, but a symbolic name used in the database and in metadata handling. It is never
// tested against, only used for human-readability as the file name for the metadata. // tested against, only used for human-readability as the file name for the metadata.
public final static String METADATA_FILENAME = "metadata.json"; public static final String METADATA_FILENAME = "metadata.json";
/** /**
* Reads the data from the cursor and store it in metadata objects. * Reads the data from the cursor and store it in metadata objects.
@ -114,6 +118,14 @@ public class MetadataHandler {
final String clientId, final String wordListId, final int version) { final String clientId, final String wordListId, final int version) {
final ContentValues contentValues = MetadataDbHelper.getContentValuesByWordListId( final ContentValues contentValues = MetadataDbHelper.getContentValuesByWordListId(
MetadataDbHelper.getDb(context, clientId), wordListId, version); MetadataDbHelper.getDb(context, clientId), wordListId, version);
if (contentValues == null) {
// TODO: Figure out why this would happen.
// Check if this happens when the metadata gets updated in the background.
Log.e(TAG, String.format( "Unable to find the current metadata for wordlist "
+ "(clientId=%s, wordListId=%s, version=%d) on the database",
clientId, wordListId, version));
return null;
}
return WordListMetadata.createFromContentValues(contentValues); return WordListMetadata.createFromContentValues(contentValues);
} }

View File

@ -1143,6 +1143,9 @@ public final class UpdateHandler {
} }
final WordListMetadata wordListMetaData = MetadataHandler.getCurrentMetadataForWordList( final WordListMetadata wordListMetaData = MetadataHandler.getCurrentMetadataForWordList(
context, clientId, wordlistId, version); context, clientId, wordlistId, version);
if (wordListMetaData == null) {
return;
}
final ActionBatch actions = new ActionBatch(); final ActionBatch actions = new ActionBatch();
actions.add(new ActionBatch.StartDownloadAction( actions.add(new ActionBatch.StartDownloadAction(

View File

@ -18,6 +18,8 @@ package com.android.inputmethod.dictionarypack;
import android.content.ContentValues; import android.content.ContentValues;
import javax.annotation.Nonnull;
/** /**
* The metadata for a single word list. * The metadata for a single word list.
* *
@ -77,7 +79,7 @@ public class WordListMetadata {
* *
* If this lacks any required field, IllegalArgumentException is thrown. * If this lacks any required field, IllegalArgumentException is thrown.
*/ */
public static WordListMetadata createFromContentValues(final ContentValues values) { public static WordListMetadata createFromContentValues(@Nonnull final ContentValues values) {
final String id = values.getAsString(MetadataDbHelper.WORDLISTID_COLUMN); final String id = values.getAsString(MetadataDbHelper.WORDLISTID_COLUMN);
final Integer type = values.getAsInteger(MetadataDbHelper.TYPE_COLUMN); final Integer type = values.getAsInteger(MetadataDbHelper.TYPE_COLUMN);
final String description = values.getAsString(MetadataDbHelper.DESCRIPTION_COLUMN); final String description = values.getAsString(MetadataDbHelper.DESCRIPTION_COLUMN);