diff options
Diffstat (limited to 'java/src/com/android/inputmethod/dictionarypack')
9 files changed, 156 insertions, 71 deletions
diff --git a/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java b/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java index df4a52f4e..faf5d3c87 100644 --- a/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java +++ b/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java @@ -174,7 +174,7 @@ public final class ActionBatch { final long downloadId = UpdateHandler.registerDownloadRequest(manager, request, db, mWordList.mId, mWordList.mVersion); Utils.l("Starting download of", uri, "with id", downloadId); - PrivateLog.log("Starting download of " + uri + ", id : " + downloadId, context); + PrivateLog.log("Starting download of " + uri + ", id : " + downloadId); } } @@ -333,7 +333,7 @@ public final class ActionBatch { mWordList.mRemoteFilename, mWordList.mLastUpdate, mWordList.mChecksum, mWordList.mFileSize, mWordList.mVersion, mWordList.mFormatVersion); PrivateLog.log("Insert 'available' record for " + mWordList.mDescription - + " and locale " + mWordList.mLocale, context); + + " and locale " + mWordList.mLocale); db.insert(MetadataDbHelper.METADATA_TABLE_NAME, null, values); } } @@ -383,7 +383,7 @@ public final class ActionBatch { mWordList.mChecksum, mWordList.mFileSize, mWordList.mVersion, mWordList.mFormatVersion); PrivateLog.log("Insert 'preinstalled' record for " + mWordList.mDescription - + " and locale " + mWordList.mLocale, context); + + " and locale " + mWordList.mLocale); db.insert(MetadataDbHelper.METADATA_TABLE_NAME, null, values); } } @@ -424,7 +424,7 @@ public final class ActionBatch { mWordList.mRemoteFilename, mWordList.mLastUpdate, mWordList.mChecksum, mWordList.mFileSize, mWordList.mVersion, mWordList.mFormatVersion); PrivateLog.log("Updating record for " + mWordList.mDescription - + " and locale " + mWordList.mLocale, context); + + " and locale " + mWordList.mLocale); db.update(MetadataDbHelper.METADATA_TABLE_NAME, values, MetadataDbHelper.WORDLISTID_COLUMN + " = ? AND " + MetadataDbHelper.VERSION_COLUMN + " = ?", diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryProvider.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryProvider.java index f8d1c4fc9..4fbe16233 100644 --- a/java/src/com/android/inputmethod/dictionarypack/DictionaryProvider.java +++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryProvider.java @@ -189,7 +189,7 @@ public final class DictionaryProvider extends ContentProvider { */ @Override public String getType(final Uri uri) { - PrivateLog.log("Asked for type of : " + uri, this); + PrivateLog.log("Asked for type of : " + uri); final int match = matchUri(uri); switch (match) { case NO_MATCH: return null; @@ -220,7 +220,7 @@ public final class DictionaryProvider extends ContentProvider { public Cursor query(final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) { Utils.l("Uri =", uri); - PrivateLog.log("Query : " + uri, this); + PrivateLog.log("Query : " + uri); final String clientId = getClientId(uri); final int match = matchUri(uri); switch (match) { @@ -228,7 +228,7 @@ public final class DictionaryProvider extends ContentProvider { case DICTIONARY_V2_WHOLE_LIST: final Cursor c = MetadataDbHelper.queryDictionaries(getContext(), clientId); Utils.l("List of dictionaries with count", c.getCount()); - PrivateLog.log("Returned a list of " + c.getCount() + " items", this); + PrivateLog.log("Returned a list of " + c.getCount() + " items"); return c; case DICTIONARY_V2_DICT_INFO: // In protocol version 2, we return null if the client is unknown. Otherwise @@ -248,10 +248,10 @@ public final class DictionaryProvider extends ContentProvider { // TODO: pass clientId to the following function DictionaryService.updateNowIfNotUpdatedInAVeryLongTime(getContext()); if (null != dictFiles && dictFiles.size() > 0) { - PrivateLog.log("Returned " + dictFiles.size() + " files", this); + PrivateLog.log("Returned " + dictFiles.size() + " files"); return new ResourcePathCursor(dictFiles); } else { - PrivateLog.log("No dictionary files for this URL", this); + PrivateLog.log("No dictionary files for this URL"); return new ResourcePathCursor(Collections.<WordListInfo>emptyList()); } // V2_METADATA and V2_DATAFILE are not supported for query() @@ -488,7 +488,7 @@ public final class DictionaryProvider extends ContentProvider { public Uri insert(final Uri uri, final ContentValues values) throws UnsupportedOperationException { if (null == uri || null == values) return null; // Should never happen but let's be safe - PrivateLog.log("Insert, uri = " + uri.toString(), this); + PrivateLog.log("Insert, uri = " + uri.toString()); final String clientId = getClientId(uri); switch (matchUri(uri)) { case DICTIONARY_V2_METADATA: @@ -517,7 +517,7 @@ public final class DictionaryProvider extends ContentProvider { break; case DICTIONARY_V1_WHOLE_LIST: case DICTIONARY_V1_DICT_INFO: - PrivateLog.log("Attempt to insert : " + uri, this); + PrivateLog.log("Attempt to insert : " + uri); throw new UnsupportedOperationException( "Insertion in the dictionary is not supported in this version"); } @@ -532,7 +532,7 @@ public final class DictionaryProvider extends ContentProvider { @Override public int update(final Uri uri, final ContentValues values, final String selection, final String[] selectionArgs) throws UnsupportedOperationException { - PrivateLog.log("Attempt to update : " + uri, this); + PrivateLog.log("Attempt to update : " + uri); throw new UnsupportedOperationException("Updating dictionary words is not supported"); } } diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java index 5817eb498..46bb5543a 100644 --- a/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java +++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryService.java @@ -21,7 +21,6 @@ import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; -import android.content.SharedPreferences; import android.os.IBinder; import android.text.format.DateUtils; import android.util.Log; @@ -190,7 +189,7 @@ public final class DictionaryService extends Service { // is still more recent than UPDATE_FREQUENCY, do nothing. if (!isLastUpdateAtLeastThisOld(context, UPDATE_FREQUENCY)) return; - PrivateLog.log("Date changed - registering alarm", context); + PrivateLog.log("Date changed - registering alarm"); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); // Best effort to wake between midnight and MAX_ALARM_DELAY in the morning. @@ -215,7 +214,7 @@ public final class DictionaryService extends Service { private static boolean isLastUpdateAtLeastThisOld(final Context context, final long time) { final long now = System.currentTimeMillis(); final long lastUpdate = MetadataDbHelper.getOldestUpdateTime(context); - PrivateLog.log("Last update was " + lastUpdate, context); + PrivateLog.log("Last update was " + lastUpdate); return lastUpdate + time < now; } diff --git a/java/src/com/android/inputmethod/dictionarypack/LogProblemReporter.java b/java/src/com/android/inputmethod/dictionarypack/LogProblemReporter.java index c127ad540..c9e128d70 100644 --- a/java/src/com/android/inputmethod/dictionarypack/LogProblemReporter.java +++ b/java/src/com/android/inputmethod/dictionarypack/LogProblemReporter.java @@ -28,7 +28,8 @@ final class LogProblemReporter implements ProblemReporter { TAG = tag; } + @Override public void report(final Exception e) { - Log.e(TAG, "Reporting problem : " + e); + Log.e(TAG, "Reporting problem", e); } } diff --git a/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java b/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java index 55f545aad..03ed267c3 100644 --- a/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java +++ b/java/src/com/android/inputmethod/dictionarypack/MetadataDbHelper.java @@ -16,13 +16,11 @@ package com.android.inputmethod.dictionarypack; -import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; -import android.provider.Settings; import android.text.TextUtils; import android.util.Log; @@ -47,16 +45,16 @@ public class MetadataDbHelper extends SQLiteOpenHelper { private static final int METADATA_DATABASE_INITIAL_VERSION = 3; // This is the first released version of the database that implements CLIENTID. It is // used to identify the versions for upgrades. This should never change going forward. - private static final int METADATA_DATABASE_VERSION_WITH_CLIENTID = 5; + private static final int METADATA_DATABASE_VERSION_WITH_CLIENTID = 6; // This is the current database version. It should be updated when the database schema // gets updated. It is passed to the framework constructor of SQLiteOpenHelper, so // that's what the framework uses to track our database version. - private static final int METADATA_DATABASE_VERSION = 5; + private static final int METADATA_DATABASE_VERSION = 6; private final static long NOT_A_DOWNLOAD_ID = -1; public static final String METADATA_TABLE_NAME = "pendingUpdates"; - private static final String CLIENT_TABLE_NAME = "clients"; + static final String CLIENT_TABLE_NAME = "clients"; public static final String PENDINGID_COLUMN = "pendingid"; // Download Manager ID public static final String TYPE_COLUMN = "type"; public static final String STATUS_COLUMN = "status"; @@ -75,6 +73,7 @@ public class MetadataDbHelper extends SQLiteOpenHelper { private static final String CLIENT_CLIENT_ID_COLUMN = "clientid"; private static final String CLIENT_METADATA_URI_COLUMN = "uri"; + private static final String CLIENT_METADATA_ADDITIONAL_ID_COLUMN = "additionalid"; private static final String CLIENT_LAST_UPDATE_DATE_COLUMN = "lastupdate"; private static final String CLIENT_PENDINGID_COLUMN = "pendingid"; // Download Manager ID @@ -130,6 +129,7 @@ public class MetadataDbHelper extends SQLiteOpenHelper { "CREATE TABLE IF NOT EXISTS " + CLIENT_TABLE_NAME + " (" + CLIENT_CLIENT_ID_COLUMN + " TEXT, " + CLIENT_METADATA_URI_COLUMN + " TEXT, " + + CLIENT_METADATA_ADDITIONAL_ID_COLUMN + " TEXT, " + CLIENT_LAST_UPDATE_DATE_COLUMN + " INTEGER NOT NULL DEFAULT 0, " + CLIENT_PENDINGID_COLUMN + " INTEGER, " + FLAGS_COLUMN + " INTEGER, " @@ -284,14 +284,15 @@ public class MetadataDbHelper extends SQLiteOpenHelper { * @return the string representation of the URI */ public static String getMetadataUriAsString(final Context context, final String clientId) { - SQLiteDatabase defaultDb = getDb(context, null); - final Cursor cursor = defaultDb.query(CLIENT_TABLE_NAME, - new String[] { CLIENT_METADATA_URI_COLUMN }, - CLIENT_CLIENT_ID_COLUMN + " = ?", new String[] { clientId }, + SQLiteDatabase defaultDb = MetadataDbHelper.getDb(context, null); + final Cursor cursor = defaultDb.query(MetadataDbHelper.CLIENT_TABLE_NAME, + new String[] { MetadataDbHelper.CLIENT_METADATA_URI_COLUMN, + MetadataDbHelper.CLIENT_METADATA_ADDITIONAL_ID_COLUMN }, + MetadataDbHelper.CLIENT_CLIENT_ID_COLUMN + " = ?", new String[] { clientId }, null, null, null, null); try { if (!cursor.moveToFirst()) return null; - return cursor.getString(0); // Only one column, return it + return MetadataUriGetter.getUri(context, cursor.getString(0), cursor.getString(1)); } finally { cursor.close(); } @@ -300,7 +301,8 @@ public class MetadataDbHelper extends SQLiteOpenHelper { /** * Update the last metadata update time for all clients using a particular URI. * - * All clients using this metadata URI will be indicated as having been updated now. + * This method searches for all clients using a particular URI and updates the last + * update time for this client. * The current time is used as the latest update time. This saved date will be what * is returned henceforth by {@link #getLastUpdateDateForClient(Context, String)}, * until this method is called again. @@ -309,13 +311,26 @@ public class MetadataDbHelper extends SQLiteOpenHelper { * @param uri the metadata URI we just downloaded */ public static void saveLastUpdateTimeOfUri(final Context context, final String uri) { - PrivateLog.log("Save last update time of URI : " + uri + " " + System.currentTimeMillis(), - context); + PrivateLog.log("Save last update time of URI : " + uri + " " + System.currentTimeMillis()); final ContentValues values = new ContentValues(); values.put(CLIENT_LAST_UPDATE_DATE_COLUMN, System.currentTimeMillis()); final SQLiteDatabase defaultDb = getDb(context, null); - defaultDb.update(CLIENT_TABLE_NAME, values, - CLIENT_METADATA_URI_COLUMN + " = ?", new String[] { uri }); + final Cursor cursor = MetadataDbHelper.queryClientIds(context); + if (null == cursor) return; + try { + if (!cursor.moveToFirst()) return; + do { + final String clientId = cursor.getString(0); + final String metadataUri = + MetadataDbHelper.getMetadataUriAsString(context, clientId); + if (metadataUri.equals(uri)) { + defaultDb.update(CLIENT_TABLE_NAME, values, + CLIENT_CLIENT_ID_COLUMN + " = ?", new String[] { clientId }); + } + } while (cursor.moveToNext()); + } finally { + cursor.close(); + } } /** @@ -730,11 +745,13 @@ public class MetadataDbHelper extends SQLiteOpenHelper { /** * Updates information relative to a specific client. * - * Updatable information includes only the metadata URI, but may be expanded in the future. + * Updatable information includes the metadata URI and the additional ID column. It may be + * expanded in the future. * The passed values must include a client ID in the key CLIENT_CLIENT_ID_COLUMN, and it must - * be equal to the string passed as an argument for clientId. - * The passed values must also include a non-empty metadata URI in the - * CLIENT_METADATA_URI_COLUMN column. + * be equal to the string passed as an argument for clientId. It may not be empty. + * The passed values must also include a non-null metadata URI in the + * CLIENT_METADATA_URI_COLUMN column, as well as a non-null additional ID in the + * CLIENT_METADATA_ADDITIONAL_ID_COLUMN. Both these strings may be empty. * If any of the above is not complied with, this function returns without updating data. * * @param context the context, to open the database @@ -746,10 +763,16 @@ public class MetadataDbHelper extends SQLiteOpenHelper { // Sanity check the content values final String valuesClientId = values.getAsString(CLIENT_CLIENT_ID_COLUMN); final String valuesMetadataUri = values.getAsString(CLIENT_METADATA_URI_COLUMN); - // Empty string is a valid client ID, but external apps may not configure it. - // Empty string is a valid metadata URI if the client does not want updates. - if (TextUtils.isEmpty(valuesClientId) || null == valuesMetadataUri) { - // We need both these columns to be filled in + final String valuesMetadataAdditionalId = + values.getAsString(CLIENT_METADATA_ADDITIONAL_ID_COLUMN); + // Empty string is a valid client ID, but external apps may not configure it, so disallow + // both null and empty string. + // Empty string is a valid metadata URI if the client does not want updates, so allow + // empty string but disallow null. + // Empty string is a valid additional ID so allow empty string but disallow null. + if (TextUtils.isEmpty(valuesClientId) || null == valuesMetadataUri + || null == valuesMetadataAdditionalId) { + // We need all these columns to be filled in Utils.l("Missing parameter for updateClientInfo"); return; } @@ -780,8 +803,9 @@ public class MetadataDbHelper extends SQLiteOpenHelper { * Register a download ID for a specific metadata URI. * * This method should be called when a download for a metadata URI is starting. It will - * register the download ID for all clients using this metadata URI into the database - * for later retrieval by {@link #getDownloadRecordsForDownloadId(Context, long)}. + * search for all clients using this metadata URI and will register for each of them + * the download ID into the database for later retrieval by + * {@link #getDownloadRecordsForDownloadId(Context, long)}. * * @param context a context for opening databases * @param uri the metadata URI @@ -792,8 +816,22 @@ public class MetadataDbHelper extends SQLiteOpenHelper { final ContentValues values = new ContentValues(); values.put(CLIENT_PENDINGID_COLUMN, downloadId); final SQLiteDatabase defaultDb = getDb(context, ""); - defaultDb.update(CLIENT_TABLE_NAME, values, - CLIENT_METADATA_URI_COLUMN + " = ?", new String[] { uri }); + final Cursor cursor = MetadataDbHelper.queryClientIds(context); + if (null == cursor) return; + try { + if (!cursor.moveToFirst()) return; + do { + final String clientId = cursor.getString(0); + final String metadataUri = + MetadataDbHelper.getMetadataUriAsString(context, clientId); + if (metadataUri.equals(uri)) { + defaultDb.update(CLIENT_TABLE_NAME, values, + CLIENT_CLIENT_ID_COLUMN + " = ?", new String[] { clientId }); + } + } while (cursor.moveToNext()); + } finally { + cursor.close(); + } } /** diff --git a/java/src/com/android/inputmethod/dictionarypack/MetadataUriGetter.java b/java/src/com/android/inputmethod/dictionarypack/MetadataUriGetter.java new file mode 100644 index 000000000..ed817658e --- /dev/null +++ b/java/src/com/android/inputmethod/dictionarypack/MetadataUriGetter.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2013 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.android.inputmethod.dictionarypack; + +import android.content.Context; + +/** + * Helper to get the metadata URI from its base URI and the additional ID, if any. + */ +public class MetadataUriGetter { + private MetadataUriGetter() { + // This helper class is not instantiable. + } + + public static String getUri(final Context context, final String baseUri, + final String additionalId) { + return baseUri; + } +} diff --git a/java/src/com/android/inputmethod/dictionarypack/PrivateLog.java b/java/src/com/android/inputmethod/dictionarypack/PrivateLog.java index 8593c1c9b..67dd7b9b7 100644 --- a/java/src/com/android/inputmethod/dictionarypack/PrivateLog.java +++ b/java/src/com/android/inputmethod/dictionarypack/PrivateLog.java @@ -16,7 +16,6 @@ package com.android.inputmethod.dictionarypack; -import android.content.ContentProvider; import android.content.ContentValues; import android.content.Context; import android.database.sqlite.SQLiteDatabase; @@ -24,6 +23,7 @@ import android.database.sqlite.SQLiteOpenHelper; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.Locale; /** * Class to keep long-term log. This is inactive in production, and is only for debug purposes. @@ -44,10 +44,10 @@ public class PrivateLog { + COLUMN_EVENT + " TEXT);"; private static final SimpleDateFormat sDateFormat = - new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US); private static PrivateLog sInstance = new PrivateLog(); - private static DebugHelper mDebugHelper = null; + private static DebugHelper sDebugHelper = null; private PrivateLog() { } @@ -55,8 +55,8 @@ public class PrivateLog { public static synchronized PrivateLog getInstance(final Context context) { if (!DEBUG) return sInstance; synchronized(PrivateLog.class) { - if (sInstance.mDebugHelper == null) { - sInstance.mDebugHelper = new DebugHelper(context); + if (sDebugHelper == null) { + sDebugHelper = new DebugHelper(context); } return sInstance; } @@ -94,16 +94,9 @@ public class PrivateLog { } - public static void log(String event, Context context) { + public static void log(String event) { if (!DEBUG) return; - final SQLiteDatabase l = getInstance(context).mDebugHelper.getWritableDatabase(); - mDebugHelper.insert(l, event); - } - - public static void log(String event, ContentProvider provider) { - if (!DEBUG) return; - final SQLiteDatabase l = - getInstance(provider.getContext()).mDebugHelper.getWritableDatabase(); - mDebugHelper.insert(l, event); + final SQLiteDatabase l = sDebugHelper.getWritableDatabase(); + DebugHelper.insert(l, event); } } diff --git a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java index 3173e911b..a59660954 100644 --- a/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java +++ b/java/src/com/android/inputmethod/dictionarypack/UpdateHandler.java @@ -183,7 +183,7 @@ public final class UpdateHandler { final String clientId = cursor.getString(0); final String metadataUri = MetadataDbHelper.getMetadataUriAsString(context, clientId); - PrivateLog.log("Update for clientId " + Utils.s(clientId), context); + PrivateLog.log("Update for clientId " + Utils.s(clientId)); Utils.l("Update for clientId", clientId, " which uses URI ", metadataUri); uris.add(metadataUri); } while (cursor.moveToNext()); @@ -211,7 +211,7 @@ public final class UpdateHandler { */ private static void updateClientsWithMetadataUri(final Context context, final boolean updateNow, final String metadataUri) { - PrivateLog.log("Update for metadata URI " + Utils.s(metadataUri), context); + PrivateLog.log("Update for metadata URI " + Utils.s(metadataUri)); final Request metadataRequest = new Request(Uri.parse(metadataUri)); Utils.l("Request =", metadataRequest); @@ -257,7 +257,7 @@ public final class UpdateHandler { // method will ignore it. writeMetadataDownloadId(context, metadataUri, downloadId); } - PrivateLog.log("Requested download with id " + downloadId, context); + PrivateLog.log("Requested download with id " + downloadId); } /** @@ -404,7 +404,7 @@ public final class UpdateHandler { /* package */ static void downloadFinished(final Context context, final Intent intent) { // Get and check the ID of the file that was downloaded final long fileId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, NOT_AN_ID); - PrivateLog.log("Download finished with id " + fileId, context); + PrivateLog.log("Download finished with id " + fileId); Utils.l("DownloadFinished with id", fileId); if (NOT_AN_ID == fileId) return; // Spurious wake-up: ignore @@ -491,7 +491,7 @@ public final class UpdateHandler { private static void publishUpdateCycleCompletedEvent(final Context context) { // Even if this is not successful, we have to publish the new state. - PrivateLog.log("Publishing update cycle completed event", context); + PrivateLog.log("Publishing update cycle completed event"); Utils.l("Publishing update cycle completed event"); for (UpdateEventListener listener : linkedCopyOfList(sUpdateEventListeners)) { listener.updateCycleCompleted(); @@ -582,7 +582,7 @@ public final class UpdateHandler { } Utils.l("Downloaded metadata :", newMetadata); - PrivateLog.log("Downloaded metadata\n" + newMetadata, context); + PrivateLog.log("Downloaded metadata\n" + newMetadata); final ActionBatch actions = computeUpgradeTo(context, clientId, newMetadata); // TODO: Check with UX how we should report to the user @@ -610,7 +610,7 @@ public final class UpdateHandler { MetadataDbHelper.DESCRIPTION_COLUMN), "for", downloadRecord.mClientId); PrivateLog.log("Downloaded a new word list with description : " + downloadRecord.mAttributes.getAsString(MetadataDbHelper.DESCRIPTION_COLUMN) - + " for " + downloadRecord.mClientId, context); + + " for " + downloadRecord.mClientId); final String locale = downloadRecord.mAttributes.getAsString(MetadataDbHelper.LOCALE_COLUMN); diff --git a/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java b/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java index 0d923ae01..93f12d53e 100644 --- a/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java +++ b/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java @@ -23,7 +23,9 @@ import android.preference.DialogPreference; import android.util.Log; import android.view.View; import android.view.ViewGroup; +import android.view.ViewParent; import android.widget.Button; +import android.widget.ListView; import com.android.inputmethod.latin.R; @@ -42,6 +44,7 @@ public final class WordListPreference extends DialogPreference { // What to display in the "status" field when we receive unknown data as a status from // the content provider. Empty string sounds sensible. static final private String NO_STATUS_MESSAGE = ""; + static final private int NOT_AN_INDEX = -1; /// Actions static final private int ACTION_UNKNOWN = 0; @@ -64,7 +67,8 @@ public final class WordListPreference extends DialogPreference { static final private int ANIMATION_IN = 1; static final private int ANIMATION_OUT = 2; - private static Button sLastClickedActionButton = null; + private static int sLastClickedIndex = NOT_AN_INDEX; + private static String sLastClickedWordlistId = null; private final OnWordListPreferenceClick mPreferenceClickHandler = new OnWordListPreferenceClick(); private final OnActionButtonClick mActionButtonClickHandler = @@ -196,7 +200,8 @@ public final class WordListPreference extends DialogPreference { ((ViewGroup)view).setLayoutTransition(null); final Button button = (Button)view.findViewById(R.id.wordlist_button); button.setText(getButtonLabel(mStatus)); - button.setVisibility(View.INVISIBLE); + // String identity match. This is an ==, not an .equals, on purpose. + button.setVisibility(mWordlistId == sLastClickedWordlistId ? View.VISIBLE : View.INVISIBLE); button.setOnClickListener(mActionButtonClickHandler); view.setOnClickListener(mPreferenceClickHandler); } @@ -205,15 +210,31 @@ public final class WordListPreference extends DialogPreference { @Override public void onClick(final View v) { final Button button = (Button)v.findViewById(R.id.wordlist_button); - if (null != sLastClickedActionButton) { - animateButton(sLastClickedActionButton, ANIMATION_OUT); - } animateButton(button, ANIMATION_IN); - sLastClickedActionButton = button; + final ViewParent parent = v.getParent(); + // Just in case something changed in the framework, test for the concrete class + if (!(parent instanceof ListView)) return; + final ListView listView = (ListView)parent; + final int myIndex = listView.indexOfChild(v) + listView.getFirstVisiblePosition(); + if (NOT_AN_INDEX != sLastClickedIndex) { + animateButton(getButtonForIndex(listView, sLastClickedIndex), ANIMATION_OUT); + } + sLastClickedIndex = myIndex; + sLastClickedWordlistId = mWordlistId; + } + } + + private Button getButtonForIndex(final ListView listView, final int index) { + final int indexInChildren = index - listView.getFirstVisiblePosition(); + if (indexInChildren < 0 || index > listView.getLastVisiblePosition()) { + // The view is offscreen. + return null; } + return (Button)listView.getChildAt(indexInChildren).findViewById(R.id.wordlist_button); } private void animateButton(final Button button, final int direction) { + if (null == button) return; final float outerX = ((View)button.getParent()).getWidth(); final float innerX = button.getX() - button.getTranslationX(); if (View.INVISIBLE == button.getVisibility()) { |