diff options
Diffstat (limited to 'java/src')
48 files changed, 2454 insertions, 475 deletions
diff --git a/java/src/com/android/inputmethod/compat/TextViewCompatUtils.java b/java/src/com/android/inputmethod/compat/TextViewCompatUtils.java index d4f1ea830..f8e1902c0 100644 --- a/java/src/com/android/inputmethod/compat/TextViewCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/TextViewCompatUtils.java @@ -22,23 +22,23 @@ import android.widget.TextView; import java.lang.reflect.Method; public final class TextViewCompatUtils { - // Note that TextView.setCompoundDrawablesRelative(Drawable,Drawable,Drawable,Drawable) has - // been introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1). - private static final Method METHOD_setCompoundDrawablesRelative = CompatUtils.getMethod( - TextView.class, "setCompoundDrawablesRelative", + // Note that TextView.setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,Drawable, + // Drawable,Drawable) has been introduced in API level 17 (Build.VERSION_CODE.JELLY_BEAN_MR1). + private static final Method METHOD_setCompoundDrawablesRelativeWithIntrinsicBounds = + CompatUtils.getMethod(TextView.class, "setCompoundDrawablesRelativeWithIntrinsicBounds", Drawable.class, Drawable.class, Drawable.class, Drawable.class); private TextViewCompatUtils() { // This utility class is not publicly instantiable. } - public static void setCompoundDrawablesRelative(final TextView textView, final Drawable start, - final Drawable top, final Drawable end, final Drawable bottom) { - if (METHOD_setCompoundDrawablesRelative == null) { - textView.setCompoundDrawables(start, top, end, bottom); + public static void setCompoundDrawablesRelativeWithIntrinsicBounds(final TextView textView, + final Drawable start, final Drawable top, final Drawable end, final Drawable bottom) { + if (METHOD_setCompoundDrawablesRelativeWithIntrinsicBounds == null) { + textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom); return; } - CompatUtils.invoke(textView, null, METHOD_setCompoundDrawablesRelative, + CompatUtils.invoke(textView, null, METHOD_setCompoundDrawablesRelativeWithIntrinsicBounds, start, top, end, bottom); } } diff --git a/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java b/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java index ff6561c58..a0d76415c 100644 --- a/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java +++ b/java/src/com/android/inputmethod/compat/UserDictionaryCompatUtils.java @@ -28,6 +28,7 @@ public final class UserDictionaryCompatUtils { private static final Method METHOD_addWord = CompatUtils.getMethod(Words.class, "addWord", Context.class, String.class, Integer.TYPE, String.class, Locale.class); + @SuppressWarnings("deprecation") public static void addWord(final Context context, final String word, final int freq, final String shortcut, final Locale locale) { if (hasNewerAddWord()) { @@ -39,13 +40,18 @@ public final class UserDictionaryCompatUtils { if (null == locale) { localeType = Words.LOCALE_TYPE_ALL; } else { - localeType = Words.LOCALE_TYPE_CURRENT; + final Locale currentLocale = context.getResources().getConfiguration().locale; + if (locale.equals(currentLocale)) { + localeType = Words.LOCALE_TYPE_CURRENT; + } else { + localeType = Words.LOCALE_TYPE_ALL; + } } Words.addWord(context, word, freq, localeType); } } - private static final boolean hasNewerAddWord() { + public static final boolean hasNewerAddWord() { return null != METHOD_addWord; } } diff --git a/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java b/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java index df4a52f4e..bf2230553 100644 --- a/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java +++ b/java/src/com/android/inputmethod/dictionarypack/ActionBatch.java @@ -138,7 +138,12 @@ public final class ActionBatch { if (null == manager) return; // This is an upgraded word list: we should download it. - final Uri uri = Uri.parse(mWordList.mRemoteFilename); + // Adding a disambiguator to circumvent a bug in older versions of DownloadManager. + // DownloadManager also stupidly cuts the extension to replace with its own that it + // gets from the content-type. We need to circumvent this. + final String disambiguator = "#" + System.currentTimeMillis() + + com.android.inputmethod.latin.Utils.getVersionName(context) + ".dict"; + final Uri uri = Uri.parse(mWordList.mRemoteFilename + disambiguator); final Request request = new Request(uri); final Resources res = context.getResources(); @@ -174,7 +179,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 +338,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 +388,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 +429,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 + " = ?", @@ -478,13 +483,14 @@ public final class ActionBatch { if (MetadataDbHelper.STATUS_INSTALLED == status || MetadataDbHelper.STATUS_DISABLED == status || MetadataDbHelper.STATUS_DELETING == status) { - // If it is installed or disabled, then we cannot remove the entry lest the user - // lose the ability to delete the file or otherwise administrate it. We will thus - // leave it as is, but remove the URI from the database since it is not supposed to - // be accessible any more. + // If it is installed or disabled, we need to mark it as deleted so that LatinIME + // will remove it next time it enquires for dictionaries. // If it is deleting and we don't have a new version, then we have to wait until - // Android Keyboard actually has deleted it before we can remove its metadata. + // LatinIME actually has deleted it before we can remove its metadata. + // In both cases, remove the URI from the database since it is not supposed to + // be accessible any more. values.put(MetadataDbHelper.REMOTE_FILENAME_COLUMN, ""); + values.put(MetadataDbHelper.STATUS_COLUMN, MetadataDbHelper.STATUS_DELETING); db.update(MetadataDbHelper.METADATA_TABLE_NAME, values, MetadataDbHelper.WORDLISTID_COLUMN + " = ? AND " + MetadataDbHelper.VERSION_COLUMN + " = ?", diff --git a/java/src/com/android/inputmethod/dictionarypack/ButtonSwitcher.java b/java/src/com/android/inputmethod/dictionarypack/ButtonSwitcher.java new file mode 100644 index 000000000..5ab94a429 --- /dev/null +++ b/java/src/com/android/inputmethod/dictionarypack/ButtonSwitcher.java @@ -0,0 +1,155 @@ +/** + * 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.animation.Animator; +import android.animation.AnimatorListenerAdapter; +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewPropertyAnimator; +import android.widget.Button; +import android.widget.FrameLayout; + +import com.android.inputmethod.latin.R; + +/** + * A view that handles buttons inside it according to a status. + */ +public class ButtonSwitcher extends FrameLayout { + public static final int NOT_INITIALIZED = -1; + public static final int STATUS_NO_BUTTON = 0; + public static final int STATUS_INSTALL = 1; + public static final int STATUS_CANCEL = 2; + public static final int STATUS_DELETE = 3; + // One of the above + private int mStatus = NOT_INITIALIZED; + private int mAnimateToStatus = NOT_INITIALIZED; + + // Animation directions + public static final int ANIMATION_IN = 1; + public static final int ANIMATION_OUT = 2; + + private Button mInstallButton; + private Button mCancelButton; + private Button mDeleteButton; + private OnClickListener mOnClickListener; + + public ButtonSwitcher(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public ButtonSwitcher(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + protected void onLayout(final boolean changed, final int left, final int top, final int right, + final int bottom) { + super.onLayout(changed, left, top, right, bottom); + mInstallButton = (Button)findViewById(R.id.dict_install_button); + mCancelButton = (Button)findViewById(R.id.dict_cancel_button); + mDeleteButton = (Button)findViewById(R.id.dict_delete_button); + mInstallButton.setOnClickListener(mOnClickListener); + mCancelButton.setOnClickListener(mOnClickListener); + mDeleteButton.setOnClickListener(mOnClickListener); + setButtonPositionWithoutAnimation(mStatus); + if (mAnimateToStatus != NOT_INITIALIZED) { + // We have been asked to animate before we were ready, so we took a note of it. + // We are now ready: launch the animation. + animateButtonPosition(mStatus, mAnimateToStatus); + mStatus = mAnimateToStatus; + mAnimateToStatus = NOT_INITIALIZED; + } + } + + private Button getButton(final int status) { + switch(status) { + case STATUS_INSTALL: + return mInstallButton; + case STATUS_CANCEL: + return mCancelButton; + case STATUS_DELETE: + return mDeleteButton; + default: + return null; + } + } + + public void setStatusAndUpdateVisuals(final int status) { + if (mStatus == NOT_INITIALIZED) { + setButtonPositionWithoutAnimation(status); + mStatus = status; + } else { + if (null == mInstallButton) { + // We may come here before we have been layout. In this case we don't know our + // size yet so we can't start animations so we need to remember what animation to + // start once layout has gone through. + mAnimateToStatus = status; + } else { + animateButtonPosition(mStatus, status); + mStatus = status; + } + } + } + + private void setButtonPositionWithoutAnimation(final int status) { + // This may be called by setStatus() before the layout has come yet. + if (null == mInstallButton) return; + final int width = getWidth(); + // Set to out of the screen if that's not the currently displayed status + mInstallButton.setTranslationX(STATUS_INSTALL == status ? 0 : width); + mCancelButton.setTranslationX(STATUS_CANCEL == status ? 0 : width); + mDeleteButton.setTranslationX(STATUS_DELETE == status ? 0 : width); + } + + private void animateButtonPosition(final int oldStatus, final int newStatus) { + final View oldButton = getButton(oldStatus); + final View newButton = getButton(newStatus); + if (null != oldButton && null != newButton) { + // Transition between two buttons : animate out, then in + animateButton(oldButton, ANIMATION_OUT).setListener( + new AnimatorListenerAdapter() { + @Override + public void onAnimationEnd(final Animator animation) { + if (newStatus != mStatus) return; + animateButton(newButton, ANIMATION_IN); + } + }); + } else if (null != oldButton) { + animateButton(oldButton, ANIMATION_OUT); + } else if (null != newButton) { + animateButton(newButton, ANIMATION_IN); + } + } + + public void setInternalOnClickListener(final OnClickListener listener) { + mOnClickListener = listener; + } + + private ViewPropertyAnimator animateButton(final View button, final int direction) { + final float outerX = getWidth(); + final float innerX = button.getX() - button.getTranslationX(); + if (ANIMATION_IN == direction) { + button.setClickable(true); + return button.animate().translationX(0); + } else { + button.setClickable(false); + return button.animate().translationX(outerX - innerX); + } + } +} diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryDownloadProgressBar.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryDownloadProgressBar.java new file mode 100644 index 000000000..88b5032e3 --- /dev/null +++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryDownloadProgressBar.java @@ -0,0 +1,178 @@ +/** + * 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.app.DownloadManager; +import android.app.DownloadManager.Query; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.Handler; +import android.util.AttributeSet; +import android.util.Log; +import android.view.View; +import android.widget.ProgressBar; + +public class DictionaryDownloadProgressBar extends ProgressBar { + @SuppressWarnings("unused") + private static final String TAG = DictionaryDownloadProgressBar.class.getSimpleName(); + private static final int NOT_A_DOWNLOADMANAGER_PENDING_ID = 0; + + private String mClientId; + private String mWordlistId; + private boolean mIsCurrentlyAttachedToWindow = false; + private Thread mReporterThread = null; + + public DictionaryDownloadProgressBar(final Context context) { + super(context); + } + + public DictionaryDownloadProgressBar(final Context context, final AttributeSet attrs) { + super(context, attrs); + } + + public void setIds(final String clientId, final String wordlistId) { + mClientId = clientId; + mWordlistId = wordlistId; + } + + static private int getDownloadManagerPendingIdFromWordlistId(final Context context, + final String clientId, final String wordlistId) { + final SQLiteDatabase db = MetadataDbHelper.getDb(context, clientId); + final ContentValues wordlistValues = + MetadataDbHelper.getContentValuesOfLatestAvailableWordlistById(db, wordlistId); + if (null == wordlistValues) { + // We don't know anything about a word list with this id. Bug? This should never + // happen, but still return to prevent a crash. + Log.e(TAG, "Unexpected word list ID: " + wordlistId); + return NOT_A_DOWNLOADMANAGER_PENDING_ID; + } + return wordlistValues.getAsInteger(MetadataDbHelper.PENDINGID_COLUMN); + } + + /* + * This method will stop any running updater thread for this progress bar and create and run + * a new one only if the progress bar is visible. + * Hence, as a result of calling this method, the progress bar will have an updater thread + * running if and only if the progress bar is visible. + */ + private void updateReporterThreadRunningStatusAccordingToVisibility() { + if (null != mReporterThread) mReporterThread.interrupt(); + if (mIsCurrentlyAttachedToWindow && View.VISIBLE == getVisibility()) { + final int downloadManagerPendingId = + getDownloadManagerPendingIdFromWordlistId(getContext(), mClientId, mWordlistId); + if (NOT_A_DOWNLOADMANAGER_PENDING_ID == downloadManagerPendingId) { + // Can't get the ID. This is never supposed to happen, but still clear the updater + // thread and return to avoid a crash. + mReporterThread = null; + return; + } + final UpdaterThread updaterThread = + new UpdaterThread(getContext(), downloadManagerPendingId); + updaterThread.start(); + mReporterThread = updaterThread; + } else { + // We're not going to restart the thread anyway, so we may as well garbage collect it. + mReporterThread = null; + } + } + + @Override + protected void onAttachedToWindow() { + mIsCurrentlyAttachedToWindow = true; + updateReporterThreadRunningStatusAccordingToVisibility(); + } + + @Override + protected void onDetachedFromWindow() { + mIsCurrentlyAttachedToWindow = false; + updateReporterThreadRunningStatusAccordingToVisibility(); + } + + private class UpdaterThread extends Thread { + private final static int REPORT_PERIOD = 150; // how often to report progress, in ms + final DownloadManager mDownloadManager; + final int mId; + public UpdaterThread(final Context context, final int id) { + super(); + mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); + mId = id; + } + @Override + public void run() { + try { + // It's almost impossible that mDownloadManager is null (it would mean it has been + // disabled between pressing the 'install' button and displaying the progress + // bar), but just in case. + if (null == mDownloadManager) return; + final UpdateHelper updateHelper = new UpdateHelper(); + final Query query = new Query().setFilterById(mId); + int lastProgress = 0; + setIndeterminate(true); + while (!isInterrupted()) { + final Cursor cursor = mDownloadManager.query(query); + if (null == cursor) { + // Can't contact DownloadManager: this should never happen. + return; + } + try { + if (cursor.moveToNext()) { + final int columnBytesDownloadedSoFar = cursor.getColumnIndex( + DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR); + final int bytesDownloadedSoFar = + cursor.getInt(columnBytesDownloadedSoFar); + updateHelper.setProgressFromAnotherThread(bytesDownloadedSoFar); + } else { + // Download has finished and DownloadManager has already been asked to + // clean up the db entry. + updateHelper.setProgressFromAnotherThread(getMax()); + return; + } + } finally { + cursor.close(); + } + Thread.sleep(REPORT_PERIOD); + } + } catch (InterruptedException e) { + // Do nothing and terminate normally. + } + } + + private class UpdateHelper implements Runnable { + private int mProgress; + @Override + public void run() { + setIndeterminate(false); + setProgress(mProgress); + } + public void setProgressFromAnotherThread(final int progress) { + if (mProgress != progress) { + mProgress = progress; + // For some unknown reason, setProgress just does not work from a separate + // thread, although the code in ProgressBar looks like it should. Thus, we + // resort to a runnable posted to the handler of the view. + final Handler handler = getHandler(); + // It's possible to come here before this view has been laid out. If so, + // just ignore the call - it will be updated again later. + if (null == handler) return; + handler.post(this); + } + } + } + } +} diff --git a/java/src/com/android/inputmethod/dictionarypack/DictionaryListInterfaceState.java b/java/src/com/android/inputmethod/dictionarypack/DictionaryListInterfaceState.java new file mode 100644 index 000000000..de3711c27 --- /dev/null +++ b/java/src/com/android/inputmethod/dictionarypack/DictionaryListInterfaceState.java @@ -0,0 +1,67 @@ +/** + * 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 com.android.inputmethod.latin.CollectionUtils; + +import java.util.HashMap; + +/** + * Helper class to maintain the interface state of word list preferences. + * + * This is necessary because the views are created on-demand by calling code. There are many + * situations where views are renewed with little relation with user interaction. For example, + * when scrolling, the view is reused so it doesn't keep its state, which means we need to keep + * it separately. Also whenever the underlying dictionary list undergoes a change (for example, + * update the metadata, or finish downloading) the whole list has to be thrown out and recreated + * in case some dictionaries appeared, disappeared, changed states etc. + */ +public class DictionaryListInterfaceState { + private static class State { + public boolean mOpen = false; + public int mStatus = MetadataDbHelper.STATUS_UNKNOWN; + } + + private HashMap<String, State> mWordlistToState = CollectionUtils.newHashMap(); + + public boolean isOpen(final String wordlistId) { + final State state = mWordlistToState.get(wordlistId); + if (null == state) return false; + return state.mOpen; + } + + public int getStatus(final String wordlistId) { + final State state = mWordlistToState.get(wordlistId); + if (null == state) return MetadataDbHelper.STATUS_UNKNOWN; + return state.mStatus; + } + + public void setOpen(final String wordlistId, final int status) { + final State newState; + final State state = mWordlistToState.get(wordlistId); + newState = null == state ? new State() : state; + newState.mOpen = true; + newState.mStatus = status; + mWordlistToState.put(wordlistId, newState); + } + + public void closeAll() { + for (final State state : mWordlistToState.values()) { + state.mOpen = false; + } + } +} 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/DictionarySettingsFragment.java b/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsFragment.java index 9e27c1f3f..fb75d6dc0 100644 --- a/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsFragment.java +++ b/java/src/com/android/inputmethod/dictionarypack/DictionarySettingsFragment.java @@ -64,6 +64,8 @@ public final class DictionarySettingsFragment extends PreferenceFragment private ConnectivityManager mConnectivityManager; private MenuItem mUpdateNowMenu; private boolean mChangedSettings; + private DictionaryListInterfaceState mDictionaryListInterfaceState = + new DictionaryListInterfaceState(); private final BroadcastReceiver mConnectivityChangedReceiver = new BroadcastReceiver() { @Override @@ -283,6 +285,7 @@ public final class DictionarySettingsFragment extends PreferenceFragment final int localeIndex = cursor.getColumnIndex(MetadataDbHelper.LOCALE_COLUMN); final int descriptionIndex = cursor.getColumnIndex(MetadataDbHelper.DESCRIPTION_COLUMN); final int statusIndex = cursor.getColumnIndex(MetadataDbHelper.STATUS_COLUMN); + final int filesizeIndex = cursor.getColumnIndex(MetadataDbHelper.FILESIZE_COLUMN); do { final String wordlistId = cursor.getString(idIndex); final int version = cursor.getInt(versionIndex); @@ -292,13 +295,15 @@ public final class DictionarySettingsFragment extends PreferenceFragment final int status = cursor.getInt(statusIndex); final int matchLevel = LocaleUtils.getMatchLevel(systemLocaleString, localeString); final String matchLevelString = LocaleUtils.getMatchLevelSortedString(matchLevel); + final int filesize = cursor.getInt(filesizeIndex); // The key is sorted in lexicographic order, according to the match level, then // the description. final String key = matchLevelString + "." + description + "." + wordlistId; final WordListPreference existingPref = prefList.get(key); if (null == existingPref || hasPriority(status, existingPref.mStatus)) { - final WordListPreference pref = new WordListPreference(activity, mClientId, - wordlistId, version, locale, description, status); + final WordListPreference pref = new WordListPreference(activity, + mDictionaryListInterfaceState, mClientId, wordlistId, version, locale, + description, status, filesize); prefList.put(key, pref); } } while (cursor.moveToNext()); 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..3f917f13f 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,8 +211,13 @@ 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); - final Request metadataRequest = new Request(Uri.parse(metadataUri)); + PrivateLog.log("Update for metadata URI " + Utils.s(metadataUri)); + // Adding a disambiguator to circumvent a bug in older versions of DownloadManager. + // DownloadManager also stupidly cuts the extension to replace with its own that it + // gets from the content-type. We need to circumvent this. + final String disambiguator = "#" + System.currentTimeMillis() + + com.android.inputmethod.latin.Utils.getVersionName(context) + ".json"; + final Request metadataRequest = new Request(Uri.parse(metadataUri + disambiguator)); Utils.l("Request =", metadataRequest); final Resources res = context.getResources(); @@ -257,7 +262,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); } /** @@ -351,7 +356,13 @@ public final class UpdateHandler { final int columnUri = cursor.getColumnIndex(DownloadManager.COLUMN_URI); final int error = cursor.getInt(columnError); status = cursor.getInt(columnStatus); - uri = cursor.getString(columnUri); + final String uriWithAnchor = cursor.getString(columnUri); + int anchorIndex = uriWithAnchor.indexOf('#'); + if (anchorIndex != -1) { + uri = uriWithAnchor.substring(0, anchorIndex); + } else { + uri = uriWithAnchor; + } if (DownloadManager.STATUS_SUCCESSFUL != status) { Log.e(TAG, "Permanent failure of download " + downloadId + " with error code: " + error); @@ -404,7 +415,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 +502,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 +593,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 +621,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..29015d61b 100644 --- a/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java +++ b/java/src/com/android/inputmethod/dictionarypack/WordListPreference.java @@ -16,14 +16,15 @@ package com.android.inputmethod.dictionarypack; -import android.app.Dialog; import android.content.Context; import android.content.SharedPreferences; -import android.preference.DialogPreference; +import android.preference.Preference; import android.util.Log; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; +import android.view.ViewParent; +import android.widget.ListView; +import android.widget.TextView; import com.android.inputmethod.latin.R; @@ -36,7 +37,7 @@ import java.util.Locale; * pack. Upon being pressed, it displays a menu to allow the user to install, disable, * enable or delete it as appropriate for the current state of the word list. */ -public final class WordListPreference extends DialogPreference { +public final class WordListPreference extends Preference { static final private String TAG = WordListPreference.class.getSimpleName(); // What to display in the "status" field when we receive unknown data as a status from @@ -59,24 +60,26 @@ public final class WordListPreference extends DialogPreference { public final int mVersion; // The status public int mStatus; + // The size of the dictionary file + private final int mFilesize; - // Animation directions - static final private int ANIMATION_IN = 1; - static final private int ANIMATION_OUT = 2; - - private static Button sLastClickedActionButton = null; + private final DictionaryListInterfaceState mInterfaceState; private final OnWordListPreferenceClick mPreferenceClickHandler = new OnWordListPreferenceClick(); private final OnActionButtonClick mActionButtonClickHandler = new OnActionButtonClick(); - public WordListPreference(final Context context, final String clientId, final String wordlistId, - final int version, final Locale locale, final String description, final int status) { + public WordListPreference(final Context context, + final DictionaryListInterfaceState dictionaryListInterfaceState, final String clientId, + final String wordlistId, final int version, final Locale locale, + final String description, final int status, final int filesize) { super(context, null); mContext = context; + mInterfaceState = dictionaryListInterfaceState; mClientId = clientId; mVersion = version; mWordlistId = wordlistId; + mFilesize = filesize; setLayoutResource(R.layout.dictionary_line); @@ -89,12 +92,6 @@ public final class WordListPreference extends DialogPreference { if (status == mStatus) return; mStatus = status; setSummary(getSummary(status)); - // If we are currently displaying the dialog, we should update it, or at least - // dismiss it. - final Dialog dialog = getDialog(); - if (null != dialog) { - dialog.dismiss(); - } } private String getSummary(final int status) { @@ -117,29 +114,31 @@ public final class WordListPreference extends DialogPreference { } } + // The table below needs to be kept in sync with MetadataDbHelper.STATUS_* since it uses + // the values as indices. private static final int sStatusActionList[][] = { // MetadataDbHelper.STATUS_UNKNOWN {}, // MetadataDbHelper.STATUS_AVAILABLE - { R.string.install_dict, ACTION_ENABLE_DICT }, + { ButtonSwitcher.STATUS_INSTALL, ACTION_ENABLE_DICT }, // MetadataDbHelper.STATUS_DOWNLOADING - { R.string.cancel_download_dict, ACTION_DISABLE_DICT }, + { ButtonSwitcher.STATUS_CANCEL, ACTION_DISABLE_DICT }, // MetadataDbHelper.STATUS_INSTALLED - { R.string.delete_dict, ACTION_DELETE_DICT }, + { ButtonSwitcher.STATUS_DELETE, ACTION_DELETE_DICT }, // MetadataDbHelper.STATUS_DISABLED - { R.string.delete_dict, ACTION_DELETE_DICT }, + { ButtonSwitcher.STATUS_DELETE, ACTION_DELETE_DICT }, // MetadataDbHelper.STATUS_DELETING // We show 'install' because the file is supposed to be deleted. // The user may reinstall it. - { R.string.install_dict, ACTION_ENABLE_DICT } + { ButtonSwitcher.STATUS_INSTALL, ACTION_ENABLE_DICT } }; - private CharSequence getButtonLabel(final int status) { + private int getButtonSwitcherStatus(final int status) { if (status >= sStatusActionList.length) { Log.e(TAG, "Unknown status " + status); - return ""; + return ButtonSwitcher.STATUS_NO_BUTTON; } - return mContext.getString(sStatusActionList[status][0]); + return sStatusActionList[status][0]; } private static int getActionIdFromStatusAndMenuEntry(final int status) { @@ -194,36 +193,70 @@ public final class WordListPreference extends DialogPreference { protected void onBindView(final View view) { super.onBindView(view); ((ViewGroup)view).setLayoutTransition(null); - final Button button = (Button)view.findViewById(R.id.wordlist_button); - button.setText(getButtonLabel(mStatus)); - button.setVisibility(View.INVISIBLE); - button.setOnClickListener(mActionButtonClickHandler); + + final DictionaryDownloadProgressBar progressBar = + (DictionaryDownloadProgressBar)view.findViewById(R.id.dictionary_line_progress_bar); + final TextView status = (TextView)view.findViewById(android.R.id.summary); + progressBar.setIds(mClientId, mWordlistId); + progressBar.setMax(mFilesize); + final boolean showProgressBar = (MetadataDbHelper.STATUS_DOWNLOADING == mStatus); + status.setVisibility(showProgressBar ? View.INVISIBLE : View.VISIBLE); + progressBar.setVisibility(showProgressBar ? View.VISIBLE : View.INVISIBLE); + + final ButtonSwitcher buttonSwitcher = + (ButtonSwitcher)view.findViewById(R.id.wordlist_button_switcher); + if (mInterfaceState.isOpen(mWordlistId)) { + // The button is open. + final int previousStatus = mInterfaceState.getStatus(mWordlistId); + buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(previousStatus)); + if (previousStatus != mStatus) { + // We come here if the status has changed since last time. We need to animate + // the transition. + buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(mStatus)); + mInterfaceState.setOpen(mWordlistId, mStatus); + } + } else { + // The button is closed. + buttonSwitcher.setStatusAndUpdateVisuals(ButtonSwitcher.STATUS_NO_BUTTON); + } + buttonSwitcher.setInternalOnClickListener(mActionButtonClickHandler); view.setOnClickListener(mPreferenceClickHandler); } private class OnWordListPreferenceClick implements View.OnClickListener { @Override public void onClick(final View v) { - final Button button = (Button)v.findViewById(R.id.wordlist_button); - if (null != sLastClickedActionButton) { - animateButton(sLastClickedActionButton, ANIMATION_OUT); + // Note : v is the preference view + 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 indexToOpen; + // Close all first, we'll open back any item that needs to be open. + final boolean wasOpen = mInterfaceState.isOpen(mWordlistId); + mInterfaceState.closeAll(); + if (wasOpen) { + // This button being shown. Take note that we don't want to open any button in the + // loop below. + indexToOpen = -1; + } else { + // This button was not being shown. Open it, and remember the index of this + // child as the one to open in the following loop. + mInterfaceState.setOpen(mWordlistId, mStatus); + indexToOpen = listView.indexOfChild(v); + } + final int lastDisplayedIndex = + listView.getLastVisiblePosition() - listView.getFirstVisiblePosition(); + // The "lastDisplayedIndex" is actually displayed, hence the <= + for (int i = 0; i <= lastDisplayedIndex; ++i) { + final ButtonSwitcher buttonSwitcher = (ButtonSwitcher)listView.getChildAt(i) + .findViewById(R.id.wordlist_button_switcher); + if (i == indexToOpen) { + buttonSwitcher.setStatusAndUpdateVisuals(getButtonSwitcherStatus(mStatus)); + } else { + buttonSwitcher.setStatusAndUpdateVisuals(ButtonSwitcher.STATUS_NO_BUTTON); + } } - animateButton(button, ANIMATION_IN); - sLastClickedActionButton = button; - } - } - - private void animateButton(final Button button, final int direction) { - final float outerX = ((View)button.getParent()).getWidth(); - final float innerX = button.getX() - button.getTranslationX(); - if (View.INVISIBLE == button.getVisibility()) { - button.setTranslationX(outerX - innerX); - button.setVisibility(View.VISIBLE); - } - if (ANIMATION_IN == direction) { - button.animate().translationX(0); - } else { - button.animate().translationX(outerX - innerX); } } diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index ba78d014a..a0ac47535 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -333,6 +333,10 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack private static void cancelAndStartAnimators(final ObjectAnimator animatorToCancel, final ObjectAnimator animatorToStart) { + if (animatorToCancel == null || animatorToStart == null) { + // TODO: Stop using null as a no-operation animator. + return; + } float startFraction = 0.0f; if (animatorToCancel.isStarted()) { animatorToCancel.cancel(); @@ -366,7 +370,9 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack // When user hits the space or the enter key, just cancel the while-typing timer. final int typedCode = typedKey.mCode; if (typedCode == Constants.CODE_SPACE || typedCode == Constants.CODE_ENTER) { - startWhileTypingFadeinAnimation(keyboardView); + if (isTyping) { + startWhileTypingFadeinAnimation(keyboardView); + } return; } @@ -581,6 +587,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack private ObjectAnimator loadObjectAnimator(final int resId, final Object target) { if (resId == 0) { + // TODO: Stop returning null. return null; } final ObjectAnimator animator = (ObjectAnimator)AnimatorInflater.loadAnimator( @@ -609,8 +616,18 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack @ExternallyReferenced public void setAltCodeKeyWhileTypingAnimAlpha(final int alpha) { + if (mAltCodeKeyWhileTypingAnimAlpha == alpha) { + return; + } + // Update the visual of alt-code-key-while-typing. mAltCodeKeyWhileTypingAnimAlpha = alpha; - updateAltCodeKeyWhileTyping(); + final Keyboard keyboard = getKeyboard(); + if (keyboard == null) { + return; + } + for (final Key key : keyboard.mAltCodeKeysWhileTyping) { + invalidateKey(key); + } } public void setKeyboardActionListener(final KeyboardActionListener listener) { @@ -1054,6 +1071,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack @Override public void onShowMoreKeysPanel(final MoreKeysPanel panel) { + locatePreviewPlacerView(); if (isShowingMoreKeysPanel()) { onDismissMoreKeysPanel(); } @@ -1189,10 +1207,12 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack if (ENABLE_USABILITY_STUDY_LOG) { writeUsabilityStudyLog(me, action, eventTime, i, pointerId, px, py); } - if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { - ResearchLogger.mainKeyboardView_processMotionEvent( - me, action, eventTime, i, pointerId, px, py); - } + // TODO: This seems to be no longer necessary, and confusing because it leads to + // duplicate MotionEvents being recorded. + // if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { + // ResearchLogger.mainKeyboardView_processMotionEvent( + // me, action, eventTime, i, pointerId, px, py); + // } } } else { final PointerTracker tracker = PointerTracker.getPointerTracker(id, this); @@ -1274,16 +1294,6 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack invalidateKey(shortcutKey); } - private void updateAltCodeKeyWhileTyping() { - final Keyboard keyboard = getKeyboard(); - if (keyboard == null) { - return; - } - for (final Key key : keyboard.mAltCodeKeysWhileTyping) { - invalidateKey(key); - } - } - public void startDisplayLanguageOnSpacebar(final boolean subtypeChanged, final boolean needsToDisplayLanguage, final boolean hasMultipleEnabledIMEsOrSubtypes) { mNeedsToDisplayLanguage = needsToDisplayLanguage; diff --git a/java/src/com/android/inputmethod/keyboard/PointerTracker.java b/java/src/com/android/inputmethod/keyboard/PointerTracker.java index 91c4319e7..c1b148dbf 100644 --- a/java/src/com/android/inputmethod/keyboard/PointerTracker.java +++ b/java/src/com/android/inputmethod/keyboard/PointerTracker.java @@ -318,8 +318,8 @@ public final class PointerTracker implements PointerTrackerQueue.Element { // true if keyboard layout has been changed. private boolean mKeyboardLayoutHasBeenChanged; - // true if this pointer is no longer tracking touch event. - private boolean mIsTrackingCanceled; + // true if this pointer is no longer triggering any action because it has been canceled. + private boolean mIsTrackingForActionDisabled; // the more keys panel currently being shown. equals null if no panel is active. private MoreKeysPanel mMoreKeysPanel; @@ -441,7 +441,11 @@ public final class PointerTracker implements PointerTrackerQueue.Element { // Returns true if keyboard has been changed by this callback. private boolean callListenerOnPressAndCheckKeyboardLayoutChange(final Key key) { - if (sInGesture || mIsDetectingGesture) { + // While gesture input is going on, this method should be a no-operation. But when gesture + // input has been canceled, <code>sInGesture</code> and <code>mIsDetectingGesture</code> + // are set to false. To keep this method is a no-operation, + // <code>mIsTrackingForActionDisabled</code> should also be taken account of. + if (sInGesture || mIsDetectingGesture || mIsTrackingForActionDisabled) { return false; } final boolean ignoreModifierKey = mIsInSlidingKeyInput && key.isModifier(); @@ -500,7 +504,8 @@ public final class PointerTracker implements PointerTrackerQueue.Element { // primaryCode is different from {@link Key#mCode}. private void callListenerOnRelease(final Key key, final int primaryCode, final boolean withSliding) { - if (sInGesture || mIsDetectingGesture) { + // See the comment at {@link #callListenerOnPressAndCheckKeyboardLayoutChange(Key}}. + if (sInGesture || mIsDetectingGesture || mIsTrackingForActionDisabled) { return; } final boolean ignoreModifierKey = mIsInSlidingKeyInput && key.isModifier(); @@ -745,7 +750,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { if (key != null) { updateBatchInput(eventTime); } - if (mIsTrackingCanceled) { + if (mIsTrackingForActionDisabled) { return; } mDrawingProxy.showGesturePreviewTrail(this); @@ -777,7 +782,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { sInGesture = false; sTimeRecorder.onEndBatchInput(eventTime); mTimerProxy.cancelAllUpdateBatchInputTimers(); - if (!mIsTrackingCanceled) { + if (!mIsTrackingForActionDisabled) { if (DEBUG_LISTENER) { Log.d(TAG, String.format("[%d] onEndBatchInput : batchPoints=%d", mPointerId, sAggregratedPointers.getPointerSize())); @@ -786,7 +791,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { } } } - if (mIsTrackingCanceled) { + if (mIsTrackingForActionDisabled) { return; } mDrawingProxy.showGesturePreviewTrail(this); @@ -846,7 +851,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { if (ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { ResearchLogger.pointerTracker_onDownEvent(deltaT, distance * distance); } - cancelTracking(); + cancelTrackingForAction(); return; } } @@ -887,7 +892,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { || (key != null && key.isModifier()) || mKeyDetector.alwaysAllowsSlidingInput(); mKeyboardLayoutHasBeenChanged = false; - mIsTrackingCanceled = false; + mIsTrackingForActionDisabled = false; resetSlidingKeyInput(); if (key != null) { // This onPress call may have changed keyboard layout. Those cases are detected at @@ -947,7 +952,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { if (DEBUG_MOVE_EVENT) { printTouchEvent("onMoveEvent:", x, y, eventTime); } - if (mIsTrackingCanceled) { + if (mIsTrackingForActionDisabled) { return; } @@ -985,6 +990,9 @@ public final class PointerTracker implements PointerTrackerQueue.Element { key = onMoveKey(x, y); } onMoveToNewKey(key, x, y); + if (mIsTrackingForActionDisabled) { + return; + } startLongPressTimer(key); setPressedKeyGraphics(key, eventTime); } @@ -1069,11 +1077,11 @@ public final class PointerTracker implements PointerTrackerQueue.Element { + " detected sliding finger while multi touching", mPointerId)); } onUpEvent(x, y, eventTime); - cancelTracking(); + cancelTrackingForAction(); setReleasedKeyGraphics(oldKey); } else { if (!mIsDetectingGesture) { - cancelTracking(); + cancelTrackingForAction(); } setReleasedKeyGraphics(oldKey); } @@ -1087,7 +1095,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { onMoveToNewKey(null, x, y); } else { if (!mIsDetectingGesture) { - cancelTracking(); + cancelTrackingForAction(); } } } @@ -1155,7 +1163,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { return; } onUpEventInternal(mLastX, mLastY, eventTime); - cancelTracking(); + cancelTrackingForAction(); } private void onUpEventInternal(final int x, final int y, final long eventTime) { @@ -1168,7 +1176,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { setReleasedKeyGraphics(currentKey); if (isShowingMoreKeysPanel()) { - if (!mIsTrackingCanceled) { + if (!mIsTrackingForActionDisabled) { final int translatedX = mMoreKeysPanel.translateX(x); final int translatedY = mMoreKeysPanel.translateY(y); mMoreKeysPanel.onUpEvent(translatedX, translatedY, mPointerId, eventTime); @@ -1186,7 +1194,7 @@ public final class PointerTracker implements PointerTrackerQueue.Element { return; } - if (mIsTrackingCanceled) { + if (mIsTrackingForActionDisabled) { return; } if (currentKey != null && !currentKey.isRepeatable()) { @@ -1203,16 +1211,16 @@ public final class PointerTracker implements PointerTrackerQueue.Element { } @Override - public void cancelTracking() { + public void cancelTrackingForAction() { if (isShowingMoreKeysPanel()) { return; } - mIsTrackingCanceled = true; + mIsTrackingForActionDisabled = true; } public void onLongPressed() { resetSlidingKeyInput(); - cancelTracking(); + cancelTrackingForAction(); setReleasedKeyGraphics(mCurrentKey); sPointerTrackerQueue.remove(this); } @@ -1316,6 +1324,6 @@ public final class PointerTracker implements PointerTrackerQueue.Element { final Key key = mKeyDetector.detectHitKey(x, y); final String code = KeyDetector.printableCode(key); Log.d(TAG, String.format("[%d]%s%s %4d %4d %5d %s", mPointerId, - (mIsTrackingCanceled ? "-" : " "), title, x, y, eventTime, code)); + (mIsTrackingForActionDisabled ? "-" : " "), title, x, y, eventTime, code)); } } diff --git a/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java index 6bc6acc0f..8901f99b7 100644 --- a/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java +++ b/java/src/com/android/inputmethod/keyboard/internal/PointerTrackerQueue.java @@ -30,7 +30,7 @@ public final class PointerTrackerQueue { public boolean isModifier(); public boolean isInSlidingKeyInput(); public void onPhantomUpEvent(long eventTime); - public void cancelTracking(); + public void cancelTrackingForAction(); } private static final int INITIAL_CAPACITY = 10; @@ -206,7 +206,7 @@ public final class PointerTrackerQueue { final int arraySize = mArraySize; for (int index = 0; index < arraySize; index++) { final Element element = expandableArray.get(index); - element.cancelTracking(); + element.cancelTrackingForAction(); } } } diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionary.java b/java/src/com/android/inputmethod/latin/BinaryDictionary.java index 18e712212..c8c7bb456 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionary.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionary.java @@ -139,19 +139,31 @@ public final class BinaryDictionary extends Dictionary { inputSize, 0 /* commitPoint */, isGesture, prevWordCodePointArray, mUseFullEditDistance, mOutputCodePoints, mOutputScores, mSpaceIndices, mOutputTypes); + final boolean blockPotentiallyOffensive = + Settings.getInstance().getBlockPotentiallyOffensive(); final ArrayList<SuggestedWordInfo> suggestions = CollectionUtils.newArrayList(); for (int j = 0; j < count; ++j) { - if (composerSize > 0 && mOutputScores[j] < 1) break; final int start = j * MAX_WORD_LENGTH; int len = 0; while (len < MAX_WORD_LENGTH && mOutputCodePoints[start + len] != 0) { ++len; } if (len > 0) { - final int score = SuggestedWordInfo.KIND_WHITELIST == mOutputTypes[j] + final int flags = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_FLAGS; + if (blockPotentiallyOffensive + && 0 != (flags & SuggestedWordInfo.KIND_FLAG_POSSIBLY_OFFENSIVE) + && 0 == (flags & SuggestedWordInfo.KIND_FLAG_EXACT_MATCH)) { + // If we block potentially offensive words, and if the word is possibly + // offensive, then we don't output it unless it's also an exact match. + continue; + } + final int kind = mOutputTypes[j] & SuggestedWordInfo.KIND_MASK_KIND; + final int score = SuggestedWordInfo.KIND_WHITELIST == kind ? SuggestedWordInfo.MAX_SCORE : mOutputScores[j]; + // TODO: check that all users of the `kind' parameter are ready to accept + // flags too and pass mOutputTypes[j] instead of kind suggestions.add(new SuggestedWordInfo(new String(mOutputCodePoints, start, len), - score, mOutputTypes[j], mDictType)); + score, kind, mDictType)); } } return suggestions; diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java index 42f713697..a9b58de44 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryFileDumper.java @@ -81,6 +81,7 @@ public final class BinaryDictionaryFileDumper { private static final String QUERY_PATH_METADATA = "metadata"; private static final String INSERT_METADATA_CLIENT_ID_COLUMN = "clientid"; private static final String INSERT_METADATA_METADATA_URI_COLUMN = "uri"; + private static final String INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN = "additionalid"; // Prevents this class to be accidentally instantiated. private BinaryDictionaryFileDumper() { @@ -209,7 +210,7 @@ public final class BinaryDictionaryFileDumper { * to the cache file name designated by its id and locale, overwriting it if already present * and creating it (and its containing directory) if necessary. */ - private static AssetFileAddress cacheWordList(final String wordlistId, final String locale, + private static void cacheWordList(final String wordlistId, final String locale, final ContentProviderClient providerClient, final Context context) { final int COMPRESSED_CRYPTED_COMPRESSED = 0; final int CRYPTED_COMPRESSED = 1; @@ -227,7 +228,7 @@ public final class BinaryDictionaryFileDumper { providerClient, QUERY_PATH_DATAFILE, wordlistId /* extraPath */); } catch (RemoteException e) { Log.e(TAG, "Can't communicate with the dictionary pack", e); - return null; + return; } final String finalFileName = DictionaryInfoUtils.getCacheFileName(wordlistId, locale, context); @@ -236,11 +237,11 @@ public final class BinaryDictionaryFileDumper { tempFileName = BinaryDictionaryGetter.getTempFileName(wordlistId, context); } catch (IOException e) { Log.e(TAG, "Can't open the temporary file", e); - return null; + return; } for (int mode = MODE_MIN; mode <= MODE_MAX; ++mode) { - InputStream originalSourceStream = null; + final InputStream originalSourceStream; InputStream inputStream = null; InputStream uncompressedStream = null; InputStream decryptedStream = null; @@ -253,7 +254,7 @@ public final class BinaryDictionaryFileDumper { // Open input. afd = openAssetFileDescriptor(providerClient, wordListUri); // If we can't open it at all, don't even try a number of times. - if (null == afd) return null; + if (null == afd) return; originalSourceStream = afd.createInputStream(); // Open output. outputFile = new File(tempFileName); @@ -304,7 +305,7 @@ public final class BinaryDictionaryFileDumper { } BinaryDictionaryGetter.removeFilesWithIdExcept(context, wordlistId, finalFile); // Success! Close files (through the finally{} clause) and return. - return AssetFileAddress.makeFromFileName(finalFileName); + return; } catch (Exception e) { if (DEBUG) { Log.i(TAG, "Can't open word list in mode " + mode, e); @@ -319,7 +320,7 @@ public final class BinaryDictionaryFileDumper { } finally { // Ignore exceptions while closing files. try { - // inputStream.close() will close afd, we should not call afd.close(). + if (null != afd) afd.close(); if (null != inputStream) inputStream.close(); if (null != uncompressedStream) uncompressedStream.close(); if (null != decryptedStream) decryptedStream.close(); @@ -349,7 +350,6 @@ public final class BinaryDictionaryFileDumper { } catch (RemoteException e) { Log.e(TAG, "In addition, communication with the dictionary provider was cut", e); } - return null; } /** @@ -358,30 +358,23 @@ public final class BinaryDictionaryFileDumper { * This will query a content provider for word list data for a given locale, and copy the * files locally so that they can be mmap'ed. This may overwrite previously cached word lists * with newer versions if a newer version is made available by the content provider. - * @returns the addresses of the word list files, or null if no data could be obtained. * @throw FileNotFoundException if the provider returns non-existent data. * @throw IOException if the provider-returned data could not be read. */ - public static List<AssetFileAddress> cacheWordListsFromContentProvider(final Locale locale, + public static void cacheWordListsFromContentProvider(final Locale locale, final Context context, final boolean hasDefaultWordList) { final ContentProviderClient providerClient = context.getContentResolver(). acquireContentProviderClient(getProviderUriBuilder("").build()); if (null == providerClient) { Log.e(TAG, "Can't establish communication with the dictionary provider"); - return CollectionUtils.newArrayList(); + return; } try { final List<WordListInfo> idList = getWordListWordListInfos(locale, context, hasDefaultWordList); - final ArrayList<AssetFileAddress> fileAddressList = CollectionUtils.newArrayList(); for (WordListInfo id : idList) { - final AssetFileAddress afd = - cacheWordList(id.mId, id.mLocale, providerClient, context); - if (null != afd) { - fileAddressList.add(afd); - } + cacheWordList(id.mId, id.mLocale, providerClient, context); } - return fileAddressList; } finally { providerClient.release(); } @@ -423,6 +416,7 @@ public final class BinaryDictionaryFileDumper { private static void reinitializeClientRecordInDictionaryContentProvider(final Context context, final ContentProviderClient client, final String clientId) throws RemoteException { final String metadataFileUri = MetadataFileUriGetter.getMetadataUri(context); + final String metadataAdditionalId = MetadataFileUriGetter.getMetadataAdditionalId(context); if (TextUtils.isEmpty(metadataFileUri)) return; // Tell the content provider to reset all information about this client id final Uri metadataContentUri = getProviderUriBuilder(clientId) @@ -434,6 +428,7 @@ public final class BinaryDictionaryFileDumper { final ContentValues metadataValues = new ContentValues(); metadataValues.put(INSERT_METADATA_CLIENT_ID_COLUMN, clientId); metadataValues.put(INSERT_METADATA_METADATA_URI_COLUMN, metadataFileUri); + metadataValues.put(INSERT_METADATA_METADATA_ADDITIONAL_ID_COLUMN, metadataAdditionalId); client.insert(metadataContentUri, metadataValues); // Update the dictionary list. diff --git a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java index 294312843..98eadcacb 100644 --- a/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java +++ b/java/src/com/android/inputmethod/latin/BinaryDictionaryGetter.java @@ -72,10 +72,16 @@ final class BinaryDictionaryGetter { public static String getTempFileName(final String id, final Context context) throws IOException { final String safeId = DictionaryInfoUtils.replaceFileNameDangerousCharacters(id); + final File directory = new File(DictionaryInfoUtils.getWordListTempDirectory(context)); + if (!directory.exists()) { + if (!directory.mkdirs()) { + Log.e(TAG, "Could not create the temporary directory"); + } + } // If the first argument is less than three chars, createTempFile throws a // RuntimeException. We don't really care about what name we get, so just // put a three-chars prefix makes us safe. - return File.createTempFile("xxx" + safeId, null).getAbsolutePath(); + return File.createTempFile("xxx" + safeId, null, directory).getAbsolutePath(); } /** @@ -89,8 +95,16 @@ final class BinaryDictionaryGetter { + fallbackResId); return null; } - return AssetFileAddress.makeFromFileNameAndOffset( - context.getApplicationInfo().sourceDir, afd.getStartOffset(), afd.getLength()); + try { + return AssetFileAddress.makeFromFileNameAndOffset( + context.getApplicationInfo().sourceDir, afd.getStartOffset(), afd.getLength()); + } finally { + try { + afd.close(); + } catch (IOException e) { + // Ignored + } + } } private static final class DictPackSettings { @@ -276,9 +290,6 @@ final class BinaryDictionaryGetter { final Context context) { final boolean hasDefaultWordList = DictionaryFactory.isDictionaryAvailable(context, locale); - // cacheWordListsFromContentProvider returns the list of files it copied to local - // storage, but we don't really care about what was copied NOW: what we want is the - // list of everything we ever cached, so we ignore the return value. // TODO: The development-only-diagnostic version is not supported by the Dictionary Pack // Service yet if (!ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS) { diff --git a/java/src/com/android/inputmethod/latin/DebugSettings.java b/java/src/com/android/inputmethod/latin/DebugSettings.java index c2aade64d..5969a63de 100644 --- a/java/src/com/android/inputmethod/latin/DebugSettings.java +++ b/java/src/com/android/inputmethod/latin/DebugSettings.java @@ -121,18 +121,8 @@ public final class DebugSettings extends PreferenceFragment return; } boolean isDebugMode = mDebugMode.isChecked(); - String version = ""; - try { - final Context context = getActivity(); - if (context == null) { - return; - } - final String packageName = context.getPackageName(); - PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); - version = "Version " + info.versionName; - } catch (NameNotFoundException e) { - Log.e(TAG, "Could not find version info."); - } + final String version = getResources().getString( + R.string.version_text, Utils.getVersionName(getActivity())); if (!isDebugMode) { mDebugMode.setTitle(version); mDebugMode.setSummary(""); diff --git a/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java b/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java index dcfa483f8..df7bad8d0 100644 --- a/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java +++ b/java/src/com/android/inputmethod/latin/DictionaryInfoUtils.java @@ -129,6 +129,13 @@ public class DictionaryInfoUtils { } /** + * Helper method to get the top level temp directory. + */ + public static String getWordListTempDirectory(final Context context) { + return context.getFilesDir() + File.separator + "tmp"; + } + + /** * Reverse escaping done by replaceFileNameDangerousCharacters. */ public static String getWordListIdFromFileName(final String fname) { diff --git a/java/src/com/android/inputmethod/latin/FeedbackUtils.java b/java/src/com/android/inputmethod/latin/FeedbackUtils.java index 1e5260e34..0582763fe 100644 --- a/java/src/com/android/inputmethod/latin/FeedbackUtils.java +++ b/java/src/com/android/inputmethod/latin/FeedbackUtils.java @@ -17,6 +17,7 @@ package com.android.inputmethod.latin; import android.content.Context; +import android.content.Intent; public class FeedbackUtils { public static boolean isFeedbackFormSupported() { @@ -25,4 +26,12 @@ public class FeedbackUtils { public static void showFeedbackForm(Context context) { } + + public static int getAboutKeyboardTitleResId() { + return 0; + } + + public static Intent getAboutKeyboardIntent(Context context) { + return null; + } } diff --git a/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java b/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java index e6dc6db8f..a98ecc7b6 100644 --- a/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java +++ b/java/src/com/android/inputmethod/latin/MetadataFileUriGetter.java @@ -19,10 +19,18 @@ package com.android.inputmethod.latin; import android.content.Context; /** - * Helper class to get the metadata URI. + * Helper class to get the metadata URI and the additional ID. */ public class MetadataFileUriGetter { - public static String getMetadataUri(Context context) { + private MetadataFileUriGetter() { + // This helper class is not instantiable. + } + + public static String getMetadataUri(final Context context) { return context.getString(R.string.dictionary_pack_metadata_uri); } + + public static String getMetadataAdditionalId(final Context context) { + return ""; + } } diff --git a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java index e39aae958..3f7be99e5 100644 --- a/java/src/com/android/inputmethod/latin/RichInputMethodManager.java +++ b/java/src/com/android/inputmethod/latin/RichInputMethodManager.java @@ -22,6 +22,7 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.IBinder; import android.preference.PreferenceManager; +import android.util.Log; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodSubtype; @@ -46,6 +47,8 @@ public final class RichInputMethodManager { private InputMethodManagerCompatWrapper mImmWrapper; private InputMethodInfo mInputMethodInfoOfThisIme; + private static final int INDEX_NOT_FOUND = -1; + public static RichInputMethodManager getInstance() { sInstance.checkInitialized(); return sInstance; @@ -98,11 +101,100 @@ public final class RichInputMethodManager { } public boolean switchToNextInputMethod(final IBinder token, final boolean onlyCurrentIme) { - final boolean result = mImmWrapper.switchToNextInputMethod(token, onlyCurrentIme); - if (!result) { - mImmWrapper.mImm.switchToLastInputMethod(token); + if (mImmWrapper.switchToNextInputMethod(token, onlyCurrentIme)) { + return true; + } + // Was not able to call {@link InputMethodManager#switchToNextInputMethodIBinder,boolean)} + // because the current device is running ICS or previous and lacks the API. + if (switchToNextInputSubtypeInThisIme(token, onlyCurrentIme)) { + return true; + } + return switchToNextInputMethodAndSubtype(token); + } + + private boolean switchToNextInputSubtypeInThisIme(final IBinder token, + final boolean onlyCurrentIme) { + final InputMethodManager imm = mImmWrapper.mImm; + final InputMethodSubtype currentSubtype = imm.getCurrentInputMethodSubtype(); + final List<InputMethodSubtype> enabledSubtypes = imm.getEnabledInputMethodSubtypeList( + mInputMethodInfoOfThisIme, true /* allowsImplicitlySelectedSubtypes */); + final int currentIndex = getSubtypeIndexInList(currentSubtype, enabledSubtypes); + if (currentIndex == INDEX_NOT_FOUND) { + Log.w(TAG, "Can't find current subtype in enabled subtypes: subtype=" + + SubtypeLocale.getSubtypeDisplayName(currentSubtype)); + return false; + } + final int nextIndex = (currentIndex + 1) % enabledSubtypes.size(); + if (nextIndex <= currentIndex && !onlyCurrentIme) { + // The current subtype is the last or only enabled one and it needs to switch to + // next IME. return false; } + final InputMethodSubtype nextSubtype = enabledSubtypes.get(nextIndex); + setInputMethodAndSubtype(token, nextSubtype); + return true; + } + + private boolean switchToNextInputMethodAndSubtype(final IBinder token) { + final InputMethodManager imm = mImmWrapper.mImm; + final List<InputMethodInfo> enabledImis = imm.getEnabledInputMethodList(); + final int currentIndex = getImiIndexInList(mInputMethodInfoOfThisIme, enabledImis); + if (currentIndex == INDEX_NOT_FOUND) { + Log.w(TAG, "Can't find current IME in enabled IMEs: IME package=" + + mInputMethodInfoOfThisIme.getPackageName()); + return false; + } + final InputMethodInfo nextImi = getNextNonAuxiliaryIme(currentIndex, enabledImis); + final List<InputMethodSubtype> enabledSubtypes = imm.getEnabledInputMethodSubtypeList( + nextImi, true /* allowsImplicitlySelectedSubtypes */); + if (enabledSubtypes.isEmpty()) { + // The next IME has no subtype. + imm.setInputMethod(token, nextImi.getId()); + return true; + } + final InputMethodSubtype firstSubtype = enabledSubtypes.get(0); + imm.setInputMethodAndSubtype(token, nextImi.getId(), firstSubtype); + return true; + } + + private static int getImiIndexInList(final InputMethodInfo inputMethodInfo, + final List<InputMethodInfo> imiList) { + final int count = imiList.size(); + for (int index = 0; index < count; index++) { + final InputMethodInfo imi = imiList.get(index); + if (imi.equals(inputMethodInfo)) { + return index; + } + } + return INDEX_NOT_FOUND; + } + + // This method mimics {@link InputMethodManager#switchToNextInputMethod(IBinder,boolean)}. + private static InputMethodInfo getNextNonAuxiliaryIme(final int currentIndex, + final List<InputMethodInfo> imiList) { + final int count = imiList.size(); + for (int i = 1; i < count; i++) { + final int nextIndex = (currentIndex + i) % count; + final InputMethodInfo nextImi = imiList.get(nextIndex); + if (!isAuxiliaryIme(nextImi)) { + return nextImi; + } + } + return imiList.get(currentIndex); + } + + // Copied from {@link InputMethodInfo}. See how auxiliary of IME is determined. + private static boolean isAuxiliaryIme(final InputMethodInfo imi) { + final int count = imi.getSubtypeCount(); + if (count == 0) { + return false; + } + for (int index = 0; index < count; index++) { + final InputMethodSubtype subtype = imi.getSubtypeAt(index); + if (!subtype.isAuxiliary()) { + return false; + } + } return true; } @@ -136,24 +228,35 @@ public final class RichInputMethodManager { private static boolean checkIfSubtypeBelongsToList(final InputMethodSubtype subtype, final List<InputMethodSubtype> subtypes) { - for (final InputMethodSubtype ims : subtypes) { + return getSubtypeIndexInList(subtype, subtypes) != INDEX_NOT_FOUND; + } + + private static int getSubtypeIndexInList(final InputMethodSubtype subtype, + final List<InputMethodSubtype> subtypes) { + final int count = subtypes.size(); + for (int index = 0; index < count; index++) { + final InputMethodSubtype ims = subtypes.get(index); if (ims.equals(subtype)) { - return true; + return index; } } - return false; + return INDEX_NOT_FOUND; } public boolean checkIfSubtypeBelongsToThisIme(final InputMethodSubtype subtype) { - final InputMethodInfo myImi = mInputMethodInfoOfThisIme; - final int count = myImi.getSubtypeCount(); - for (int i = 0; i < count; i++) { - final InputMethodSubtype ims = myImi.getSubtypeAt(i); + return getSubtypeIndexInIme(subtype, mInputMethodInfoOfThisIme) != INDEX_NOT_FOUND; + } + + private static int getSubtypeIndexInIme(final InputMethodSubtype subtype, + final InputMethodInfo imi) { + final int count = imi.getSubtypeCount(); + for (int index = 0; index < count; index++) { + final InputMethodSubtype ims = imi.getSubtypeAt(index); if (ims.equals(subtype)) { - return true; + return index; } } - return false; + return INDEX_NOT_FOUND; } public InputMethodSubtype getCurrentInputMethodSubtype( diff --git a/java/src/com/android/inputmethod/latin/Settings.java b/java/src/com/android/inputmethod/latin/Settings.java index 72e08700a..9fefb58a6 100644 --- a/java/src/com/android/inputmethod/latin/Settings.java +++ b/java/src/com/android/inputmethod/latin/Settings.java @@ -36,6 +36,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang public static final String PREF_POPUP_ON = "popup_on"; public static final String PREF_VOICE_MODE = "voice_mode"; public static final String PREF_CORRECTION_SETTINGS = "correction_settings"; + public static final String PREF_EDIT_PERSONAL_DICTIONARY = "edit_personal_dictionary"; public static final String PREF_CONFIGURE_DICTIONARIES_KEY = "configure_dictionaries_key"; public static final String PREF_AUTO_CORRECTION_THRESHOLD = "auto_correction_threshold"; public static final String PREF_SHOW_SUGGESTIONS_SETTING = "show_suggestions_setting"; @@ -46,6 +47,8 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang public static final String PREF_KEY_USE_CONTACTS_DICT = "pref_key_use_contacts_dict"; public static final String PREF_KEY_USE_DOUBLE_SPACE_PERIOD = "pref_key_use_double_space_period"; + public static final String PREF_BLOCK_POTENTIALLY_OFFENSIVE = + "pref_key_block_potentially_offensive"; public static final String PREF_SHOW_LANGUAGE_SWITCH_KEY = "pref_show_language_switch_key"; public static final String PREF_INCLUDE_OTHER_IMES_IN_LANGUAGE_SWITCH_LIST = @@ -78,6 +81,7 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang "pref_suppress_language_switch_key"; public static final String PREF_SEND_FEEDBACK = "send_feedback"; + public static final String PREF_ABOUT_KEYBOARD = "about_keyboard"; private Resources mRes; private SharedPreferences mPrefs; @@ -142,6 +146,10 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang return mCurrentLocale; } + public boolean getBlockPotentiallyOffensive() { + return mSettingsValues.mBlockPotentiallyOffensive; + } + // Accessed from the settings interface, hence public public static boolean readKeypressSoundEnabled(final SharedPreferences prefs, final Resources res) { @@ -163,6 +171,12 @@ public final class Settings implements SharedPreferences.OnSharedPreferenceChang return !currentAutoCorrectionSetting.equals(autoCorrectionOff); } + public static boolean readBlockPotentiallyOffensive(final SharedPreferences prefs, + final Resources res) { + return prefs.getBoolean(Settings.PREF_BLOCK_POTENTIALLY_OFFENSIVE, + res.getBoolean(R.bool.config_block_potentially_offensive)); + } + public static boolean readFromBuildConfigIfGestureInputEnabled(final Resources res) { return res.getBoolean(R.bool.config_gesture_input_enabled_by_build_config); } diff --git a/java/src/com/android/inputmethod/latin/SettingsFragment.java b/java/src/com/android/inputmethod/latin/SettingsFragment.java index a96c997c8..835ef7b46 100644 --- a/java/src/com/android/inputmethod/latin/SettingsFragment.java +++ b/java/src/com/android/inputmethod/latin/SettingsFragment.java @@ -16,10 +16,13 @@ package com.android.inputmethod.latin; +import android.app.Activity; import android.app.backup.BackupManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; import android.content.res.Resources; import android.media.AudioManager; import android.os.Bundle; @@ -31,13 +34,19 @@ import android.preference.PreferenceGroup; import android.preference.PreferenceScreen; import android.view.inputmethod.InputMethodSubtype; +import java.util.TreeSet; + import com.android.inputmethod.dictionarypack.DictionarySettingsActivity; import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.setup.LauncherIconVisibilityManager; +import com.android.inputmethod.latin.userdictionary.UserDictionaryList; +import com.android.inputmethod.latin.userdictionary.UserDictionarySettings; import com.android.inputmethodcommon.InputMethodSettingsFragment; public final class SettingsFragment extends InputMethodSettingsFragment implements SharedPreferences.OnSharedPreferenceChangeListener { + private static final boolean DBG_USE_INTERNAL_USER_DICTIONARY_SETTINGS = false; + private ListPreference mVoicePreference; private ListPreference mShowCorrectionSuggestionsPreference; private ListPreference mAutoCorrectionThresholdPreference; @@ -77,10 +86,13 @@ public final class SettingsFragment extends InputMethodSettingsFragment final Resources res = getResources(); final Context context = getActivity(); - // When we are called from the Settings application but we are not already running, the - // {@link SubtypeLocale} class may not have been initialized. It is safe to call - // {@link SubtypeLocale#init(Context)} multiple times. + // When we are called from the Settings application but we are not already running, some + // singleton and utility classes may not have been initialized. We have to call + // initialization method of these classes here. See {@link LatinIME#onCreate()}. + SubtypeSwitcher.init(context); SubtypeLocale.init(context); + AudioAndHapticFeedbackManager.init(context); + mVoicePreference = (ListPreference) findPreference(Settings.PREF_VOICE_MODE); mShowCorrectionSuggestionsPreference = (ListPreference) findPreference(Settings.PREF_SHOW_SUGGESTIONS_SETTING); @@ -110,6 +122,7 @@ public final class SettingsFragment extends InputMethodSettingsFragment } final Preference feedbackSettings = findPreference(Settings.PREF_SEND_FEEDBACK); + final Preference aboutSettings = findPreference(Settings.PREF_ABOUT_KEYBOARD); if (feedbackSettings != null) { if (FeedbackUtils.isFeedbackFormSupported()) { feedbackSettings.setOnPreferenceClickListener(new OnPreferenceClickListener() { @@ -119,8 +132,11 @@ public final class SettingsFragment extends InputMethodSettingsFragment return true; } }); + aboutSettings.setTitle(FeedbackUtils.getAboutKeyboardTitleResId()); + aboutSettings.setIntent(FeedbackUtils.getAboutKeyboardIntent(getActivity())); } else { miscSettings.removePreference(feedbackSettings); + miscSettings.removePreference(aboutSettings); } } @@ -180,6 +196,15 @@ public final class SettingsFragment extends InputMethodSettingsFragment textCorrectionGroup.removePreference(dictionaryLink); } + final Preference editPersonalDictionary = + findPreference(Settings.PREF_EDIT_PERSONAL_DICTIONARY); + final Intent editPersonalDictionaryIntent = editPersonalDictionary.getIntent(); + final ResolveInfo ri = context.getPackageManager().resolveActivity( + editPersonalDictionaryIntent, PackageManager.MATCH_DEFAULT_ONLY); + if (DBG_USE_INTERNAL_USER_DICTIONARY_SETTINGS || ri == null) { + updateUserDictionaryPreference(editPersonalDictionary); + } + if (!Settings.readFromBuildConfigIfGestureInputEnabled(res)) { removePreference(Settings.PREF_GESTURE_SETTINGS, getPreferenceScreen()); } @@ -386,4 +411,28 @@ public final class SettingsFragment extends InputMethodSettingsFragment } }); } + + private void updateUserDictionaryPreference(Preference userDictionaryPreference) { + final Activity activity = getActivity(); + final TreeSet<String> localeList = UserDictionaryList.getUserDictionaryLocalesSet(activity); + if (null == localeList) { + // The locale list is null if and only if the user dictionary service is + // not present or disabled. In this case we need to remove the preference. + getPreferenceScreen().removePreference(userDictionaryPreference); + } else if (localeList.size() <= 1) { + userDictionaryPreference.setFragment(UserDictionarySettings.class.getName()); + // If the size of localeList is 0, we don't set the locale parameter in the + // extras. This will be interpreted by the UserDictionarySettings class as + // meaning "the current locale". + // Note that with the current code for UserDictionaryList#getUserDictionaryLocalesSet() + // the locale list always has at least one element, since it always includes the current + // locale explicitly. @see UserDictionaryList.getUserDictionaryLocalesSet(). + if (localeList.size() == 1) { + final String locale = (String)localeList.toArray()[0]; + userDictionaryPreference.getExtras().putString("locale", locale); + } + } else { + userDictionaryPreference.setFragment(UserDictionaryList.class.getName()); + } + } } diff --git a/java/src/com/android/inputmethod/latin/SettingsValues.java b/java/src/com/android/inputmethod/latin/SettingsValues.java index f77a92885..615b2dfab 100644 --- a/java/src/com/android/inputmethod/latin/SettingsValues.java +++ b/java/src/com/android/inputmethod/latin/SettingsValues.java @@ -34,6 +34,9 @@ import java.util.Arrays; */ public final class SettingsValues { private static final String TAG = SettingsValues.class.getSimpleName(); + // "floatNegativeInfinity" is a special marker string for Float.NEGATIVE_INFINITE + // currently used for auto-correction + private static final String FLOAT_NEGATIVE_INFINITY_MARKER_STRING = "floatNegativeInfinity"; // From resources: public final int mDelayUpdateOldSuggestions; @@ -54,6 +57,7 @@ public final class SettingsValues { public final boolean mShowsLanguageSwitchKey; public final boolean mUseContactsDict; public final boolean mUseDoubleSpacePeriod; + public final boolean mBlockPotentiallyOffensive; // Use bigrams to predict the next word when there is no input for it yet public final boolean mBigramPredictionEnabled; public final boolean mGestureInputEnabled; @@ -123,6 +127,7 @@ public final class SettingsValues { mShowsLanguageSwitchKey = Settings.readShowsLanguageSwitchKey(prefs); mUseContactsDict = prefs.getBoolean(Settings.PREF_KEY_USE_CONTACTS_DICT, true); mUseDoubleSpacePeriod = prefs.getBoolean(Settings.PREF_KEY_USE_DOUBLE_SPACE_PERIOD, true); + mBlockPotentiallyOffensive = Settings.readBlockPotentiallyOffensive(prefs, res); mAutoCorrectEnabled = Settings.readAutoCorrectEnabled(autoCorrectionThresholdRawValue, res); mBigramPredictionEnabled = readBigramPredictionEnabled(prefs, res); @@ -266,8 +271,12 @@ public final class SettingsValues { try { final int arrayIndex = Integer.valueOf(currentAutoCorrectionSetting); if (arrayIndex >= 0 && arrayIndex < autoCorrectionThresholdValues.length) { - autoCorrectionThreshold = Float.parseFloat( - autoCorrectionThresholdValues[arrayIndex]); + final String val = autoCorrectionThresholdValues[arrayIndex]; + if (FLOAT_NEGATIVE_INFINITY_MARKER_STRING.equals(val)) { + autoCorrectionThreshold = Float.NEGATIVE_INFINITY; + } else { + autoCorrectionThreshold = Float.parseFloat(val); + } } } catch (NumberFormatException e) { // Whenever the threshold settings are correct, never come here. @@ -275,7 +284,7 @@ public final class SettingsValues { Log.w(TAG, "Cannot load auto correction threshold setting." + " currentAutoCorrectionSetting: " + currentAutoCorrectionSetting + ", autoCorrectionThresholdValues: " - + Arrays.toString(autoCorrectionThresholdValues)); + + Arrays.toString(autoCorrectionThresholdValues), e); } return autoCorrectionThreshold; } diff --git a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java index 2f9e34ff1..bef8a3cf1 100644 --- a/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java +++ b/java/src/com/android/inputmethod/latin/SubtypeSwitcher.java @@ -80,6 +80,7 @@ public final class SubtypeSwitcher { public static void init(final Context context) { SubtypeLocale.init(context); + RichInputMethodManager.init(context); sInstance.initialize(context); } @@ -87,10 +88,13 @@ public final class SubtypeSwitcher { // Intentional empty constructor for singleton. } - private void initialize(final Context service) { - mResources = service.getResources(); + private void initialize(final Context context) { + if (mResources != null) { + return; + } + mResources = context.getResources(); mRichImm = RichInputMethodManager.getInstance(); - mConnectivityManager = (ConnectivityManager) service.getSystemService( + mConnectivityManager = (ConnectivityManager) context.getSystemService( Context.CONNECTIVITY_SERVICE); mNoLanguageSubtype = mRichImm.findSubtypeByLocaleAndKeyboardLayoutSet( SubtypeLocale.NO_LANGUAGE, SubtypeLocale.QWERTY); diff --git a/java/src/com/android/inputmethod/latin/SuggestedWords.java b/java/src/com/android/inputmethod/latin/SuggestedWords.java index 616e1911b..dfddb0ffe 100644 --- a/java/src/com/android/inputmethod/latin/SuggestedWords.java +++ b/java/src/com/android/inputmethod/latin/SuggestedWords.java @@ -122,6 +122,7 @@ public final class SuggestedWords { public static final class SuggestedWordInfo { public static final int MAX_SCORE = Integer.MAX_VALUE; + public static final int KIND_MASK_KIND = 0xFF; // Mask to get only the kind public static final int KIND_TYPED = 0; // What user typed public static final int KIND_CORRECTION = 1; // Simple correction/suggestion public static final int KIND_COMPLETION = 2; // Completion (suggestion with appended chars) @@ -132,6 +133,11 @@ public final class SuggestedWords { public static final int KIND_SHORTCUT = 7; // A shortcut public static final int KIND_PREDICTION = 8; // A prediction (== a suggestion with no input) public static final int KIND_RESUMED = 9; // A resumed suggestion (comes from a span) + + public static final int KIND_MASK_FLAGS = 0xFFFFFF00; // Mask to get the flags + public static final int KIND_FLAG_POSSIBLY_OFFENSIVE = 0x80000000; + public static final int KIND_FLAG_EXACT_MATCH = 0x40000000; + public final String mWord; public final int mScore; public final int mKind; // one of the KIND_* constants above diff --git a/java/src/com/android/inputmethod/latin/Utils.java b/java/src/com/android/inputmethod/latin/Utils.java index aff5d17d7..0f96c54dc 100644 --- a/java/src/com/android/inputmethod/latin/Utils.java +++ b/java/src/com/android/inputmethod/latin/Utils.java @@ -21,6 +21,7 @@ import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.inputmethodservice.InputMethodService; @@ -473,4 +474,18 @@ public final class Utils { } return 0; } + + public static String getVersionName(Context context) { + try { + if (context == null) { + return ""; + } + final String packageName = context.getPackageName(); + PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0); + return info.versionName; + } catch (NameNotFoundException e) { + Log.e(TAG, "Could not find version info.", e); + } + return ""; + } } diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java index 15d0bac37..044180bd6 100644 --- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java +++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java @@ -17,18 +17,21 @@ package com.android.inputmethod.latin.setup; import android.app.Activity; +import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.res.Resources; -import android.graphics.PorterDuff; -import android.graphics.drawable.Drawable; +import android.media.MediaPlayer; +import android.net.Uri; import android.os.Bundle; import android.os.Message; import android.provider.Settings; +import android.util.Log; import android.view.View; import android.view.inputmethod.InputMethodInfo; import android.view.inputmethod.InputMethodManager; import android.widget.TextView; +import android.widget.VideoView; import com.android.inputmethod.compat.TextViewCompatUtils; import com.android.inputmethod.compat.ViewCompatUtils; @@ -38,16 +41,28 @@ import com.android.inputmethod.latin.RichInputMethodManager; import com.android.inputmethod.latin.SettingsActivity; import com.android.inputmethod.latin.StaticInnerHandlerWrapper; -import java.util.HashMap; - -public final class SetupActivity extends Activity { - private SetupStepIndicatorView mStepIndicatorView; - private final SetupStepGroup mSetupSteps = new SetupStepGroup(); +import java.util.ArrayList; + +// TODO: Use Fragment to implement welcome screen and setup steps. +public final class SetupActivity extends Activity implements View.OnClickListener { + private static final String TAG = SetupActivity.class.getSimpleName(); + + private View mWelcomeScreen; + private View mSetupScreen; + private Uri mWelcomeVideoUri; + private VideoView mWelcomeVideoView; + private View mActionStart; + private View mActionNext; + private TextView mStep1Bullet; + private TextView mActionFinish; + private SetupStepGroup mSetupStepGroup; private static final String STATE_STEP = "step"; private int mStepNumber; + private static final int STEP_0 = 0; private static final int STEP_1 = 1; private static final int STEP_2 = 2; private static final int STEP_3 = 3; + private boolean mWasLanguageAndInputSettingsInvoked; private final SettingsPoolingHandler mHandler = new SettingsPoolingHandler(this); @@ -109,17 +124,26 @@ public final class SetupActivity extends Activity { return; } - // TODO: Use sans-serif-thin font family depending on the system locale white list and - // the SDK version. - final TextView titleView = (TextView)findViewById(R.id.setup_title); - final int appName = getApplicationInfo().labelRes; - titleView.setText(getString(R.string.setup_title, getString(appName))); - - mStepIndicatorView = (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator); - - final SetupStep step1 = new SetupStep(findViewById(R.id.setup_step1), - appName, R.string.setup_step1_title, R.string.setup_step1_instruction, - R.drawable.ic_settings_language, R.string.language_settings); + final String applicationName = getResources().getString(getApplicationInfo().labelRes); + mWelcomeScreen = findViewById(R.id.setup_welcome_screen); + final TextView welcomeTitle = (TextView)findViewById(R.id.setup_welcome_title); + welcomeTitle.setText(getString(R.string.setup_welcome_title, applicationName)); + + mSetupScreen = findViewById(R.id.setup_steps_screen); + final TextView stepsTitle = (TextView)findViewById(R.id.setup_title); + stepsTitle.setText(getString(R.string.setup_steps_title, applicationName)); + + final SetupStepIndicatorView indicatorView = + (SetupStepIndicatorView)findViewById(R.id.setup_step_indicator); + mSetupStepGroup = new SetupStepGroup(indicatorView); + + mStep1Bullet = (TextView)findViewById(R.id.setup_step1_bullet); + mStep1Bullet.setOnClickListener(this); + final SetupStep step1 = new SetupStep(STEP_1, applicationName, + mStep1Bullet, findViewById(R.id.setup_step1), + R.string.setup_step1_title, R.string.setup_step1_instruction, + R.string.setup_step1_finished_instruction, R.drawable.ic_setup_step1, + R.string.setup_step1_action); step1.setAction(new Runnable() { @Override public void run() { @@ -127,11 +151,13 @@ public final class SetupActivity extends Activity { mHandler.startPollingImeSettings(); } }); - mSetupSteps.addStep(STEP_1, step1); + mSetupStepGroup.addStep(step1); - final SetupStep step2 = new SetupStep(findViewById(R.id.setup_step2), - appName, R.string.setup_step2_title, R.string.setup_step2_instruction, - 0 /* actionIcon */, R.string.select_input_method); + final SetupStep step2 = new SetupStep(STEP_2, applicationName, + (TextView)findViewById(R.id.setup_step2_bullet), findViewById(R.id.setup_step2), + R.string.setup_step2_title, R.string.setup_step2_instruction, + 0 /* finishedInstruction */, R.drawable.ic_setup_step2, + R.string.setup_step2_action); step2.setAction(new Runnable() { @Override public void run() { @@ -140,18 +166,81 @@ public final class SetupActivity extends Activity { .showInputMethodPicker(); } }); - mSetupSteps.addStep(STEP_2, step2); + mSetupStepGroup.addStep(step2); - final SetupStep step3 = new SetupStep(findViewById(R.id.setup_step3), - appName, R.string.setup_step3_title, 0 /* instruction */, - R.drawable.sym_keyboard_language_switch, R.string.setup_step3_instruction); + final SetupStep step3 = new SetupStep(STEP_3, applicationName, + (TextView)findViewById(R.id.setup_step3_bullet), findViewById(R.id.setup_step3), + R.string.setup_step3_title, R.string.setup_step3_instruction, + 0 /* finishedInstruction */, R.drawable.ic_setup_step3, + R.string.setup_step3_action); step3.setAction(new Runnable() { @Override public void run() { invokeSubtypeEnablerOfThisIme(); } }); - mSetupSteps.addStep(STEP_3, step3); + mSetupStepGroup.addStep(step3); + + mWelcomeVideoUri = new Uri.Builder() + .scheme(ContentResolver.SCHEME_ANDROID_RESOURCE) + .authority(getPackageName()) + .path(Integer.toString(R.raw.setup_welcome_video)) + .build(); + mWelcomeVideoView = (VideoView)findViewById(R.id.setup_welcome_video); + mWelcomeVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { + @Override + public void onCompletion(final MediaPlayer mp) { + mp.start(); + } + }); + mWelcomeVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { + @Override + public void onPrepared(final MediaPlayer mp) { + // Now VideoView has been laid-out and ready to play, remove background of it to + // reveal the video. + mWelcomeVideoView.setBackgroundResource(0); + } + }); + mWelcomeVideoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { + @Override + public boolean onError(final MediaPlayer mp, final int what, final int extra) { + Log.e(TAG, "Playing welcome video causes error: what=" + what + " extra=" + extra); + mWelcomeVideoView.setVisibility(View.GONE); + return true; + } + }); + + mActionStart = findViewById(R.id.setup_start_label); + mActionStart.setOnClickListener(this); + mActionNext = findViewById(R.id.setup_next); + mActionNext.setOnClickListener(this); + mActionFinish = (TextView)findViewById(R.id.setup_finish); + TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds(mActionFinish, + getResources().getDrawable(R.drawable.ic_setup_finish), null, null, null); + mActionFinish.setOnClickListener(this); + } + + @Override + public void onClick(final View v) { + if (v == mActionFinish) { + finish(); + return; + } + final int stepState = determineSetupState(); + final int nextStep; + if (v == mActionStart) { + nextStep = STEP_1; + } else if (v == mActionNext) { + nextStep = mStepNumber + 1; + } else if (v == mStep1Bullet && stepState == STEP_2) { + nextStep = STEP_1; + } else { + nextStep = mStepNumber; + } + if (mStepNumber != nextStep) { + mStepNumber = nextStep; + updateSetupStepView(); + } } private void invokeSetupWizardOfThisIme() { @@ -175,6 +264,7 @@ public final class SetupActivity extends Activity { intent.setAction(Settings.ACTION_INPUT_METHOD_SETTINGS); intent.addCategory(Intent.CATEGORY_DEFAULT); startActivity(intent); + mWasLanguageAndInputSettingsInvoked = true; } private void invokeSubtypeEnablerOfThisIme() { @@ -222,7 +312,7 @@ public final class SetupActivity extends Activity { return myImi.getId().equals(currentImeId); } - private int determineSetupStepNumber() { + private int determineSetupState() { mHandler.cancelPollingImeSettings(); if (!isThisImeEnabled(this)) { return STEP_1; @@ -233,6 +323,14 @@ public final class SetupActivity extends Activity { return STEP_3; } + private int determineSetupStepNumber() { + final int stepState = determineSetupState(); + if (stepState == STEP_1) { + return mWasLanguageAndInputSettingsInvoked ? STEP_1 : STEP_0; + } + return stepState; + } + @Override protected void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); @@ -264,6 +362,22 @@ public final class SetupActivity extends Activity { } @Override + public void onBackPressed() { + if (mStepNumber == STEP_1) { + mStepNumber = STEP_0; + updateSetupStepView(); + return; + } + super.onBackPressed(); + } + + @Override + protected void onPause() { + mWelcomeVideoView.stopPlayback(); + super.onPause(); + } + + @Override public void onWindowFocusChanged(final boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (!hasFocus) { @@ -274,57 +388,67 @@ public final class SetupActivity extends Activity { } private void updateSetupStepView() { - final int layoutDirection = ViewCompatUtils.getLayoutDirection(mStepIndicatorView); - mStepIndicatorView.setIndicatorPosition( - getIndicatorPosition(mStepNumber, mSetupSteps.getTotalStep(), layoutDirection)); - mSetupSteps.enableStep(mStepNumber); - } - - private static float getIndicatorPosition(final int step, final int totalStep, - final int layoutDirection) { - final float pos = ((step - STEP_1) * 2 + 1) / (float)(totalStep * 2); - return (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos; + final boolean welcomeScreen = (mStepNumber == STEP_0); + mWelcomeScreen.setVisibility(welcomeScreen ? View.VISIBLE : View.GONE); + mSetupScreen.setVisibility(welcomeScreen ? View.GONE: View.VISIBLE); + if (welcomeScreen) { + mWelcomeVideoView.setVideoURI(mWelcomeVideoUri); + mWelcomeVideoView.start(); + return; + } + mWelcomeVideoView.stopPlayback(); + final boolean isStepActionAlreadyDone = mStepNumber < determineSetupState(); + mSetupStepGroup.enableStep(mStepNumber, isStepActionAlreadyDone); + mActionNext.setVisibility(isStepActionAlreadyDone ? View.VISIBLE : View.GONE); + mActionFinish.setVisibility((mStepNumber == STEP_3) ? View.VISIBLE : View.GONE); } static final class SetupStep implements View.OnClickListener { - private final View mRootView; + public final int mStepNo; + private final View mStepView; + private final TextView mBulletView; + private final int mActivatedColor; + private final int mDeactivatedColor; + private final String mInstruction; + private final String mFinishedInstruction; private final TextView mActionLabel; private Runnable mAction; - public SetupStep(final View rootView, final int appName, final int title, - final int instruction, final int actionIcon, final int actionLabel) { - mRootView = rootView; - final Resources res = rootView.getResources(); - final String applicationName = res.getString(appName); - - final TextView titleView = (TextView)rootView.findViewById(R.id.setup_step_title); + public SetupStep(final int stepNo, final String applicationName, final TextView bulletView, + final View stepView, final int title, final int instruction, + final int finishedInstruction,final int actionIcon, final int actionLabel) { + mStepNo = stepNo; + mStepView = stepView; + mBulletView = bulletView; + final Resources res = stepView.getResources(); + mActivatedColor = res.getColor(R.color.setup_text_action); + mDeactivatedColor = res.getColor(R.color.setup_text_dark); + + final TextView titleView = (TextView)mStepView.findViewById(R.id.setup_step_title); titleView.setText(res.getString(title, applicationName)); + mInstruction = (instruction == 0) ? null + : res.getString(instruction, applicationName); + mFinishedInstruction = (finishedInstruction == 0) ? null + : res.getString(finishedInstruction, applicationName); - final TextView instructionView = (TextView)rootView.findViewById( - R.id.setup_step_instruction); - if (instruction == 0) { - instructionView.setVisibility(View.GONE); - } else { - instructionView.setText(res.getString(instruction, applicationName)); - } - - mActionLabel = (TextView)rootView.findViewById(R.id.setup_step_action_label); + mActionLabel = (TextView)mStepView.findViewById(R.id.setup_step_action_label); mActionLabel.setText(res.getString(actionLabel)); if (actionIcon == 0) { final int paddingEnd = ViewCompatUtils.getPaddingEnd(mActionLabel); ViewCompatUtils.setPaddingRelative(mActionLabel, paddingEnd, 0, paddingEnd, 0); } else { - final int overrideColor = res.getColor(R.color.setup_text_action); - final Drawable icon = res.getDrawable(actionIcon); - icon.setColorFilter(overrideColor, PorterDuff.Mode.MULTIPLY); - icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon.getIntrinsicHeight()); - TextViewCompatUtils.setCompoundDrawablesRelative( - mActionLabel, icon, null, null, null); + TextViewCompatUtils.setCompoundDrawablesRelativeWithIntrinsicBounds( + mActionLabel, res.getDrawable(actionIcon), null, null, null); } } - public void setEnabled(final boolean enabled) { - mRootView.setVisibility(enabled ? View.VISIBLE : View.GONE); + public void setEnabled(final boolean enabled, final boolean isStepActionAlreadyDone) { + mStepView.setVisibility(enabled ? View.VISIBLE : View.GONE); + mBulletView.setTextColor(enabled ? mActivatedColor : mDeactivatedColor); + final TextView instructionView = (TextView)mStepView.findViewById( + R.id.setup_step_instruction); + instructionView.setText(isStepActionAlreadyDone ? mFinishedInstruction : mInstruction); + mActionLabel.setVisibility(isStepActionAlreadyDone ? View.GONE : View.VISIBLE); } public void setAction(final Runnable action) { @@ -334,28 +458,30 @@ public final class SetupActivity extends Activity { @Override public void onClick(final View v) { - if (mAction != null) { + if (v == mActionLabel && mAction != null) { mAction.run(); + return; } } } static final class SetupStepGroup { - private final HashMap<Integer, SetupStep> mGroup = CollectionUtils.newHashMap(); + private final SetupStepIndicatorView mIndicatorView; + private final ArrayList<SetupStep> mGroup = CollectionUtils.newArrayList(); - public void addStep(final int stepNo, final SetupStep step) { - mGroup.put(stepNo, step); + public SetupStepGroup(final SetupStepIndicatorView indicatorView) { + mIndicatorView = indicatorView; } - public void enableStep(final int enableStepNo) { - for (final Integer stepNo : mGroup.keySet()) { - final SetupStep step = mGroup.get(stepNo); - step.setEnabled(stepNo == enableStepNo); - } + public void addStep(final SetupStep step) { + mGroup.add(step); } - public int getTotalStep() { - return mGroup.size(); + public void enableStep(final int enableStepNo, final boolean isStepActionAlreadyDone) { + for (final SetupStep step : mGroup) { + step.setEnabled(step.mStepNo == enableStepNo, isStepActionAlreadyDone); + } + mIndicatorView.setIndicatorPosition(enableStepNo - STEP_1, mGroup.size()); } } } diff --git a/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java b/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java new file mode 100644 index 000000000..ca974f6b8 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/setup/SetupStartIndicatorView.java @@ -0,0 +1,108 @@ +/* + * 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.latin.setup; + +import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.graphics.Path; +import android.util.AttributeSet; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.LinearLayout; +import android.widget.TextView; + +import com.android.inputmethod.compat.ViewCompatUtils; +import com.android.inputmethod.latin.R; + +public final class SetupStartIndicatorView extends LinearLayout { + public SetupStartIndicatorView(final Context context, final AttributeSet attrs) { + super(context, attrs); + setOrientation(HORIZONTAL); + LayoutInflater.from(context).inflate(R.layout.setup_start_indicator_label, this); + + final LabelView labelView = (LabelView)findViewById(R.id.setup_start_label); + labelView.setIndicatorView(findViewById(R.id.setup_start_indicator)); + } + + public static final class LabelView extends TextView { + private View mIndicatorView; + + public LabelView(final Context context, final AttributeSet attrs) { + super(context, attrs); + } + + public void setIndicatorView(final View indicatorView) { + mIndicatorView = indicatorView; + } + + @Override + public void setPressed(final boolean pressed) { + super.setPressed(pressed); + if (mIndicatorView != null) { + mIndicatorView.setPressed(pressed); + } + } + } + + public static final class IndicatorView extends View { + private final Path mIndicatorPath = new Path(); + private final Paint mIndicatorPaint = new Paint(); + private final ColorStateList mIndicatorColor; + + public IndicatorView(final Context context, final AttributeSet attrs) { + super(context, attrs); + mIndicatorColor = getResources().getColorStateList( + R.color.setup_step_action_background); + mIndicatorPaint.setStyle(Paint.Style.FILL); + } + + @Override + public void setPressed(final boolean pressed) { + super.setPressed(pressed); + invalidate(); + } + + @Override + protected void onDraw(final Canvas canvas) { + super.onDraw(canvas); + final int layoutDirection = ViewCompatUtils.getLayoutDirection(this); + final int width = getWidth(); + final int height = getHeight(); + final float halfHeight = height / 2.0f; + final Path path = mIndicatorPath; + path.rewind(); + if (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) { + // Left arrow + path.moveTo(width, 0.0f); + path.lineTo(0.0f, halfHeight); + path.lineTo(width, height); + } else { // LAYOUT_DIRECTION_LTR + // Right arrow + path.moveTo(0.0f, 0.0f); + path.lineTo(width, halfHeight); + path.lineTo(0.0f, height); + } + path.close(); + final int[] stateSet = getDrawableState(); + final int color = mIndicatorColor.getColorForState(stateSet, 0); + mIndicatorPaint.setColor(color); + canvas.drawPath(path, mIndicatorPaint); + } + } +} diff --git a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java b/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java index 077a21793..c909507c6 100644 --- a/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java +++ b/java/src/com/android/inputmethod/latin/setup/SetupStepIndicatorView.java @@ -23,6 +23,7 @@ import android.graphics.Path; import android.util.AttributeSet; import android.view.View; +import com.android.inputmethod.compat.ViewCompatUtils; import com.android.inputmethod.latin.R; public final class SetupStepIndicatorView extends View { @@ -36,8 +37,13 @@ public final class SetupStepIndicatorView extends View { mIndicatorPaint.setStyle(Paint.Style.FILL); } - public void setIndicatorPosition(final float xRatio) { - mXRatio = xRatio; + public void setIndicatorPosition(final int stepPos, final int totalStepNum) { + final int layoutDirection = ViewCompatUtils.getLayoutDirection(this); + // The indicator position is the center of the partition that is equally divided into + // the total step number. + final float partionWidth = 1.0f / totalStepNum; + final float pos = stepPos * partionWidth + partionWidth / 2.0f; + mXRatio = (layoutDirection == ViewCompatUtils.LAYOUT_DIRECTION_RTL) ? 1.0f - pos : pos; invalidate(); } diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java new file mode 100644 index 000000000..2b6fda381 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordContents.java @@ -0,0 +1,261 @@ +/* + * 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.latin.userdictionary; + +import com.android.inputmethod.compat.UserDictionaryCompatUtils; +import com.android.inputmethod.latin.LocaleUtils; +import com.android.inputmethod.latin.R; + +import android.app.Activity; +import android.content.ContentResolver; +import android.content.Context; +import android.database.Cursor; +import android.os.Bundle; +import android.provider.UserDictionary; +import android.text.TextUtils; +import android.view.View; +import android.widget.EditText; + +import java.util.ArrayList; +import java.util.Locale; +import java.util.TreeSet; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordContents.java +// in order to deal with some devices that have issues with the user dictionary handling + +/** + * A container class to factor common code to UserDictionaryAddWordFragment + * and UserDictionaryAddWordActivity. + */ +public class UserDictionaryAddWordContents { + public static final String EXTRA_MODE = "mode"; + public static final String EXTRA_WORD = "word"; + public static final String EXTRA_SHORTCUT = "shortcut"; + public static final String EXTRA_LOCALE = "locale"; + public static final String EXTRA_ORIGINAL_WORD = "originalWord"; + public static final String EXTRA_ORIGINAL_SHORTCUT = "originalShortcut"; + + public static final int MODE_EDIT = 0; + public static final int MODE_INSERT = 1; + + /* package */ static final int CODE_WORD_ADDED = 0; + /* package */ static final int CODE_CANCEL = 1; + /* package */ static final int CODE_ALREADY_PRESENT = 2; + + private static final int FREQUENCY_FOR_USER_DICTIONARY_ADDS = 250; + + private final int mMode; // Either MODE_EDIT or MODE_INSERT + private final EditText mWordEditText; + private final EditText mShortcutEditText; + private String mLocale; + private final String mOldWord; + private final String mOldShortcut; + + /* package */ UserDictionaryAddWordContents(final View view, final Bundle args) { + mWordEditText = (EditText)view.findViewById(R.id.user_dictionary_add_word_text); + mShortcutEditText = (EditText)view.findViewById(R.id.user_dictionary_add_shortcut); + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + mShortcutEditText.setVisibility(View.GONE); + view.findViewById(R.id.user_dictionary_add_shortcut_label).setVisibility(View.GONE); + } + final String word = args.getString(EXTRA_WORD); + if (null != word) { + mWordEditText.setText(word); + mWordEditText.setSelection(word.length()); + } + final String shortcut; + if (UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + shortcut = args.getString(EXTRA_SHORTCUT); + if (null != shortcut && null != mShortcutEditText) { + mShortcutEditText.setText(shortcut); + } + mOldShortcut = args.getString(EXTRA_SHORTCUT); + } else { + shortcut = null; + mOldShortcut = null; + } + mMode = args.getInt(EXTRA_MODE); // default return value for #getInt() is 0 = MODE_EDIT + mOldWord = args.getString(EXTRA_WORD); + updateLocale(args.getString(EXTRA_LOCALE)); + } + + // locale may be null, this means default locale + // It may also be the empty string, which means "all locales" + /* package */ void updateLocale(final String locale) { + mLocale = null == locale ? Locale.getDefault().toString() : locale; + } + + /* package */ void saveStateIntoBundle(final Bundle outState) { + outState.putString(EXTRA_WORD, mWordEditText.getText().toString()); + outState.putString(EXTRA_ORIGINAL_WORD, mOldWord); + if (null != mShortcutEditText) { + outState.putString(EXTRA_SHORTCUT, mShortcutEditText.getText().toString()); + } + if (null != mOldShortcut) { + outState.putString(EXTRA_ORIGINAL_SHORTCUT, mOldShortcut); + } + outState.putString(EXTRA_LOCALE, mLocale); + } + + /* package */ void delete(final Context context) { + if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { + // Mode edit: remove the old entry. + final ContentResolver resolver = context.getContentResolver(); + UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); + } + // If we are in add mode, nothing was added, so we don't need to do anything. + } + + /* package */ + int apply(final Context context, final Bundle outParameters) { + if (null != outParameters) saveStateIntoBundle(outParameters); + final ContentResolver resolver = context.getContentResolver(); + if (MODE_EDIT == mMode && !TextUtils.isEmpty(mOldWord)) { + // Mode edit: remove the old entry. + UserDictionarySettings.deleteWord(mOldWord, mOldShortcut, resolver); + } + final String newWord = mWordEditText.getText().toString(); + final String newShortcut; + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + newShortcut = null; + } else if (null == mShortcutEditText) { + newShortcut = null; + } else { + final String tmpShortcut = mShortcutEditText.getText().toString(); + if (TextUtils.isEmpty(tmpShortcut)) { + newShortcut = null; + } else { + newShortcut = tmpShortcut; + } + } + if (TextUtils.isEmpty(newWord)) { + // If the word is somehow empty, don't insert it. + return CODE_CANCEL; + } + // If there is no shortcut, and the word already exists in the database, then we + // should not insert, because either A. the word exists with no shortcut, in which + // case the exact same thing we want to insert is already there, or B. the word + // exists with at least one shortcut, in which case it has priority on our word. + if (hasWord(newWord, context)) return CODE_ALREADY_PRESENT; + + // Disallow duplicates. If the same word with no shortcut is defined, remove it; if + // the same word with the same shortcut is defined, remove it; but we don't mind if + // there is the same word with a different, non-empty shortcut. + UserDictionarySettings.deleteWord(newWord, null, resolver); + if (!TextUtils.isEmpty(newShortcut)) { + // If newShortcut is empty we just deleted this, no need to do it again + UserDictionarySettings.deleteWord(newWord, newShortcut, resolver); + } + + // In this class we use the empty string to represent 'all locales' and mLocale cannot + // be null. However the addWord method takes null to mean 'all locales'. + UserDictionaryCompatUtils.addWord(context, newWord.toString(), + FREQUENCY_FOR_USER_DICTIONARY_ADDS, newShortcut, TextUtils.isEmpty(mLocale) ? + null : LocaleUtils.constructLocaleFromString(mLocale)); + + return CODE_WORD_ADDED; + } + + private static final String[] HAS_WORD_PROJECTION = { UserDictionary.Words.WORD }; + private static final String HAS_WORD_SELECTION_ONE_LOCALE = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.LOCALE + "=?"; + private static final String HAS_WORD_SELECTION_ALL_LOCALES = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.LOCALE + " is null"; + private boolean hasWord(final String word, final Context context) { + final Cursor cursor; + // mLocale == "" indicates this is an entry for all languages. Here, mLocale can't + // be null at all (it's ensured by the updateLocale method). + if ("".equals(mLocale)) { + cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, + HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ALL_LOCALES, + new String[] { word }, null /* sort order */); + } else { + cursor = context.getContentResolver().query(UserDictionary.Words.CONTENT_URI, + HAS_WORD_PROJECTION, HAS_WORD_SELECTION_ONE_LOCALE, + new String[] { word, mLocale }, null /* sort order */); + } + try { + if (null == cursor) return false; + return cursor.getCount() > 0; + } finally { + if (null != cursor) cursor.close(); + } + } + + public static class LocaleRenderer { + private final String mLocaleString; + private final String mDescription; + // LocaleString may NOT be null. + public LocaleRenderer(final Context context, final String localeString) { + mLocaleString = localeString; + if (null == localeString) { + mDescription = context.getString(R.string.user_dict_settings_more_languages); + } else if ("".equals(localeString)) { + mDescription = context.getString(R.string.user_dict_settings_all_languages); + } else { + mDescription = LocaleUtils.constructLocaleFromString(localeString).getDisplayName(); + } + } + @Override + public String toString() { + return mDescription; + } + public String getLocaleString() { + return mLocaleString; + } + // "More languages..." is null ; "All languages" is the empty string. + public boolean isMoreLanguages() { + return null == mLocaleString; + } + } + + private static void addLocaleDisplayNameToList(final Context context, + final ArrayList<LocaleRenderer> list, final String locale) { + if (null != locale) { + list.add(new LocaleRenderer(context, locale)); + } + } + + // Helper method to get the list of locales to display for this word + public ArrayList<LocaleRenderer> getLocalesList(final Activity activity) { + final TreeSet<String> locales = UserDictionaryList.getUserDictionaryLocalesSet(activity); + // Remove our locale if it's in, because we're always gonna put it at the top + locales.remove(mLocale); // mLocale may not be null + final String systemLocale = Locale.getDefault().toString(); + // The system locale should be inside. We want it at the 2nd spot. + locales.remove(systemLocale); // system locale may not be null + locales.remove(""); // Remove the empty string if it's there + final ArrayList<LocaleRenderer> localesList = new ArrayList<LocaleRenderer>(); + // Add the passed locale, then the system locale at the top of the list. Add an + // "all languages" entry at the bottom of the list. + addLocaleDisplayNameToList(activity, localesList, mLocale); + if (!systemLocale.equals(mLocale)) { + addLocaleDisplayNameToList(activity, localesList, systemLocale); + } + for (final String l : locales) { + // TODO: sort in unicode order + addLocaleDisplayNameToList(activity, localesList, l); + } + if (!"".equals(mLocale)) { + // If mLocale is "", then we already inserted the "all languages" item, so don't do it + addLocaleDisplayNameToList(activity, localesList, ""); // meaning: all languages + } + localesList.add(new LocaleRenderer(activity, null)); // meaning: select another locale + return localesList; + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java new file mode 100644 index 000000000..5f4c44636 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryAddWordFragment.java @@ -0,0 +1,162 @@ +/* + * 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.latin.userdictionary; + +import com.android.inputmethod.latin.R; +import com.android.inputmethod.latin.userdictionary.UserDictionaryAddWordContents.LocaleRenderer; +import com.android.inputmethod.latin.userdictionary.UserDictionaryLocalePicker.LocationChangedListener; + +import android.app.Fragment; +import android.os.Bundle; +import android.preference.PreferenceActivity; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.ArrayAdapter; +import android.widget.Spinner; + +import java.util.ArrayList; +import java.util.Locale; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryAddWordFragment.java +// in order to deal with some devices that have issues with the user dictionary handling + +/** + * Fragment to add a word/shortcut to the user dictionary. + * + * As opposed to the UserDictionaryActivity, this is only invoked within Settings + * from the UserDictionarySettings. + */ +public class UserDictionaryAddWordFragment extends Fragment + implements AdapterView.OnItemSelectedListener, LocationChangedListener { + + private static final int OPTIONS_MENU_ADD = Menu.FIRST; + private static final int OPTIONS_MENU_DELETE = Menu.FIRST + 1; + + private UserDictionaryAddWordContents mContents; + private View mRootView; + private boolean mIsDeleting = false; + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) { + mRootView = inflater.inflate(R.layout.user_dictionary_add_word_fullscreen, null); + mIsDeleting = false; + if (null == mContents) { + mContents = new UserDictionaryAddWordContents(mRootView, getArguments()); + } + return mRootView; + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + final MenuItem actionItemDelete = menu.add(0, OPTIONS_MENU_DELETE, 0, + R.string.user_dict_settings_delete).setIcon(android.R.drawable.ic_menu_delete); + actionItemDelete.setShowAsAction( + MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + final MenuItem actionItemAdd = menu.add(0, OPTIONS_MENU_ADD, 0, + R.string.user_dict_settings_delete).setIcon(R.drawable.ic_menu_add); + actionItemAdd.setShowAsAction( + MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + + /** + * Callback for the framework when a menu option is pressed. + * + * @param MenuItem the item that was pressed + * @return false to allow normal menu processing to proceed, true to consume it here + */ + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == OPTIONS_MENU_ADD) { + // added the entry in "onPause" + getActivity().onBackPressed(); + return true; + } + if (item.getItemId() == OPTIONS_MENU_DELETE) { + mContents.delete(getActivity()); + mIsDeleting = true; + getActivity().onBackPressed(); + return true; + } + return false; + } + + @Override + public void onResume() { + super.onResume(); + // We are being shown: display the word + updateSpinner(); + } + + private void updateSpinner() { + final ArrayList<LocaleRenderer> localesList = mContents.getLocalesList(getActivity()); + + final Spinner localeSpinner = + (Spinner)mRootView.findViewById(R.id.user_dictionary_add_locale); + final ArrayAdapter<LocaleRenderer> adapter = new ArrayAdapter<LocaleRenderer>(getActivity(), + android.R.layout.simple_spinner_item, localesList); + adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + localeSpinner.setAdapter(adapter); + localeSpinner.setOnItemSelectedListener(this); + } + + @Override + public void onPause() { + super.onPause(); + // We are being hidden: commit changes to the user dictionary, unless we were deleting it + if (!mIsDeleting) { + mContents.apply(getActivity(), null); + } + } + + @Override + public void onItemSelected(final AdapterView<?> parent, final View view, final int pos, + final long id) { + final LocaleRenderer locale = (LocaleRenderer)parent.getItemAtPosition(pos); + if (locale.isMoreLanguages()) { + PreferenceActivity preferenceActivity = (PreferenceActivity)getActivity(); + preferenceActivity.startPreferenceFragment(new UserDictionaryLocalePicker(), true); + } else { + mContents.updateLocale(locale.getLocaleString()); + } + } + + @Override + public void onNothingSelected(final AdapterView<?> parent) { + // I'm not sure we can come here, but if we do, that's the right thing to do. + final Bundle args = getArguments(); + mContents.updateLocale(args.getString(UserDictionaryAddWordContents.EXTRA_LOCALE)); + } + + // Called by the locale picker + @Override + public void onLocaleSelected(final Locale locale) { + mContents.updateLocale(locale.toString()); + getActivity().onBackPressed(); + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java new file mode 100644 index 000000000..6e64882b6 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryList.java @@ -0,0 +1,127 @@ +/* + * 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.latin.userdictionary; + +import com.android.inputmethod.latin.LocaleUtils; +import com.android.inputmethod.latin.R; + +import android.app.Activity; +import android.content.Intent; +import android.database.Cursor; +import android.os.Bundle; +import android.preference.Preference; +import android.preference.PreferenceFragment; +import android.preference.PreferenceGroup; +import android.provider.UserDictionary; +import android.text.TextUtils; + +import java.util.Locale; +import java.util.TreeSet; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryList.java +// in order to deal with some devices that have issues with the user dictionary handling + +public class UserDictionaryList extends PreferenceFragment { + + public static final String USER_DICTIONARY_SETTINGS_INTENT_ACTION = + "android.settings.USER_DICTIONARY_SETTINGS"; + + @Override + public void onCreate(Bundle icicle) { + super.onCreate(icicle); + setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getActivity())); + } + + public static TreeSet<String> getUserDictionaryLocalesSet(Activity activity) { + @SuppressWarnings("deprecation") + final Cursor cursor = activity.managedQuery(UserDictionary.Words.CONTENT_URI, + new String[] { UserDictionary.Words.LOCALE }, + null, null, null); + final TreeSet<String> localeList = new TreeSet<String>(); + boolean addedAllLocale = false; + if (null == cursor) { + // The user dictionary service is not present or disabled. Return null. + return null; + } else if (cursor.moveToFirst()) { + final int columnIndex = cursor.getColumnIndex(UserDictionary.Words.LOCALE); + do { + final String locale = cursor.getString(columnIndex); + final boolean allLocale = TextUtils.isEmpty(locale); + localeList.add(allLocale ? "" : locale); + if (allLocale) { + addedAllLocale = true; + } + } while (cursor.moveToNext()); + } + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED && !addedAllLocale) { + // For ICS, we need to show "For all languages" in case that the keyboard locale + // is different from the system locale + localeList.add(""); + } + localeList.add(Locale.getDefault().toString()); + return localeList; + } + + /** + * Creates the entries that allow the user to go into the user dictionary for each locale. + * @param userDictGroup The group to put the settings in. + */ + protected void createUserDictSettings(PreferenceGroup userDictGroup) { + final Activity activity = getActivity(); + userDictGroup.removeAll(); + final TreeSet<String> localeList = + UserDictionaryList.getUserDictionaryLocalesSet(activity); + + if (localeList.isEmpty()) { + userDictGroup.addPreference(createUserDictionaryPreference(null, activity)); + } else { + for (String locale : localeList) { + userDictGroup.addPreference(createUserDictionaryPreference(locale, activity)); + } + } + } + + /** + * Create a single User Dictionary Preference object, with its parameters set. + * @param locale The locale for which this user dictionary is for. + * @return The corresponding preference. + */ + protected Preference createUserDictionaryPreference(String locale, Activity activity) { + final Preference newPref = new Preference(getActivity()); + final Intent intent = new Intent(USER_DICTIONARY_SETTINGS_INTENT_ACTION); + if (null == locale) { + newPref.setTitle(Locale.getDefault().getDisplayName()); + } else { + if ("".equals(locale)) + newPref.setTitle(getString(R.string.user_dict_settings_all_languages)); + else + newPref.setTitle(LocaleUtils.constructLocaleFromString(locale).getDisplayName()); + intent.putExtra("locale", locale); + newPref.getExtras().putString("locale", locale); + } + newPref.setIntent(intent); + newPref.setFragment(UserDictionarySettings.class.getName()); + return newPref; + } + + @Override + public void onResume() { + super.onResume(); + createUserDictSettings(getPreferenceScreen()); + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java new file mode 100644 index 000000000..58d3fb91c --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionaryLocalePicker.java @@ -0,0 +1,36 @@ +/* + * 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.latin.userdictionary; + +import android.app.Fragment; + +import java.util.Locale; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionaryLocalePicker.java +// in order to deal with some devices that have issues with the user dictionary handling + +public class UserDictionaryLocalePicker extends Fragment { + public UserDictionaryLocalePicker() { + super(); + // TODO: implement + } + + public interface LocationChangedListener { + public void onLocaleSelected(Locale locale); + } +} diff --git a/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java new file mode 100644 index 000000000..36bc5ba49 --- /dev/null +++ b/java/src/com/android/inputmethod/latin/userdictionary/UserDictionarySettings.java @@ -0,0 +1,333 @@ +/** + * Copyright (C) 2013 Google Inc. + * + * 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.latin.userdictionary; + +import com.android.inputmethod.latin.R; + +import android.app.ListFragment; +import android.content.ContentResolver; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.os.Build; +import android.os.Bundle; +import android.provider.UserDictionary; +import android.text.TextUtils; +import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AlphabetIndexer; +import android.widget.ListAdapter; +import android.widget.ListView; +import android.widget.SectionIndexer; +import android.widget.SimpleCursorAdapter; +import android.widget.TextView; + +import java.util.Locale; + +// Caveat: This class is basically taken from +// packages/apps/Settings/src/com/android/settings/inputmethod/UserDictionarySettings.java +// in order to deal with some devices that have issues with the user dictionary handling + +public class UserDictionarySettings extends ListFragment { + + public static final boolean IS_SHORTCUT_API_SUPPORTED = + Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; + + private static final String[] QUERY_PROJECTION_SHORTCUT_UNSUPPORTED = + { UserDictionary.Words._ID, UserDictionary.Words.WORD}; + private static final String[] QUERY_PROJECTION_SHORTCUT_SUPPORTED = + { UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT}; + private static final String[] QUERY_PROJECTION = + IS_SHORTCUT_API_SUPPORTED ? + QUERY_PROJECTION_SHORTCUT_SUPPORTED : QUERY_PROJECTION_SHORTCUT_UNSUPPORTED; + + // The index of the shortcut in the above array. + private static final int INDEX_SHORTCUT = 2; + + private static final String[] ADAPTER_FROM_SHORTCUT_UNSUPPORTED = { + UserDictionary.Words.WORD, + }; + + private static final String[] ADAPTER_FROM_SHORTCUT_SUPPORTED = { + UserDictionary.Words.WORD, UserDictionary.Words.SHORTCUT + }; + + private static final String[] ADAPTER_FROM = IS_SHORTCUT_API_SUPPORTED ? + ADAPTER_FROM_SHORTCUT_SUPPORTED : ADAPTER_FROM_SHORTCUT_UNSUPPORTED; + + private static final int[] ADAPTER_TO_SHORTCUT_UNSUPPORTED = { + android.R.id.text1, + }; + + private static final int[] ADAPTER_TO_SHORTCUT_SUPPORTED = { + android.R.id.text1, android.R.id.text2 + }; + + private static final int[] ADAPTER_TO = IS_SHORTCUT_API_SUPPORTED ? + ADAPTER_TO_SHORTCUT_SUPPORTED : ADAPTER_TO_SHORTCUT_UNSUPPORTED; + + // Either the locale is empty (means the word is applicable to all locales) + // or the word equals our current locale + private static final String QUERY_SELECTION = + UserDictionary.Words.LOCALE + "=?"; + private static final String QUERY_SELECTION_ALL_LOCALES = + UserDictionary.Words.LOCALE + " is null"; + + private static final String DELETE_SELECTION_WITH_SHORTCUT = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.SHORTCUT + "=?"; + private static final String DELETE_SELECTION_WITHOUT_SHORTCUT = UserDictionary.Words.WORD + + "=? AND " + UserDictionary.Words.SHORTCUT + " is null OR " + + UserDictionary.Words.SHORTCUT + "=''"; + private static final String DELETE_SELECTION_SHORTCUT_UNSUPPORTED = + UserDictionary.Words.WORD + "=?"; + + private static final int OPTIONS_MENU_ADD = Menu.FIRST; + + private Cursor mCursor; + + protected String mLocale; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + + @Override + public View onCreateView( + LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate( + R.layout.user_dictionary_preference_list_fragment, container, false); + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + final Intent intent = getActivity().getIntent(); + final String localeFromIntent = + null == intent ? null : intent.getStringExtra("locale"); + + final Bundle arguments = getArguments(); + final String localeFromArguments = + null == arguments ? null : arguments.getString("locale"); + + final String locale; + if (null != localeFromArguments) { + locale = localeFromArguments; + } else if (null != localeFromIntent) { + locale = localeFromIntent; + } else { + locale = null; + } + + mLocale = locale; + mCursor = createCursor(locale); + TextView emptyView = (TextView) getView().findViewById(android.R.id.empty); + emptyView.setText(R.string.user_dict_settings_empty_text); + + final ListView listView = getListView(); + listView.setAdapter(createAdapter()); + listView.setFastScrollEnabled(true); + listView.setEmptyView(emptyView); + + setHasOptionsMenu(true); + + } + + @SuppressWarnings("deprecation") + private Cursor createCursor(final String locale) { + // Locale can be any of: + // - The string representation of a locale, as returned by Locale#toString() + // - The empty string. This means we want a cursor returning words valid for all locales. + // - null. This means we want a cursor for the current locale, whatever this is. + // Note that this contrasts with the data inside the database, where NULL means "all + // locales" and there should never be an empty string. The confusion is called by the + // historical use of null for "all locales". + // TODO: it should be easy to make this more readable by making the special values + // human-readable, like "all_locales" and "current_locales" strings, provided they + // can be guaranteed not to match locales that may exist. + if ("".equals(locale)) { + // Case-insensitive sort + return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, + QUERY_SELECTION_ALL_LOCALES, null, + "UPPER(" + UserDictionary.Words.WORD + ")"); + } else { + final String queryLocale = null != locale ? locale : Locale.getDefault().toString(); + return getActivity().managedQuery(UserDictionary.Words.CONTENT_URI, QUERY_PROJECTION, + QUERY_SELECTION, new String[] { queryLocale }, + "UPPER(" + UserDictionary.Words.WORD + ")"); + } + } + + private ListAdapter createAdapter() { + return new MyAdapter(getActivity(), R.layout.user_dictionary_item, mCursor, + ADAPTER_FROM, ADAPTER_TO, this); + } + + @Override + public void onListItemClick(ListView l, View v, int position, long id) { + final String word = getWord(position); + final String shortcut = getShortcut(position); + if (word != null) { + showAddOrEditDialog(word, shortcut); + } + } + + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + if (!UserDictionarySettings.IS_SHORTCUT_API_SUPPORTED) { + final Locale systemLocale = getResources().getConfiguration().locale; + if (!TextUtils.isEmpty(mLocale) && !mLocale.equals(systemLocale.toString())) { + // Hide the add button for ICS because it doesn't support specifying a locale + // for an entry. This new "locale"-aware API has been added in conjunction + // with the shortcut API. + return; + } + } + MenuItem actionItem = + menu.add(0, OPTIONS_MENU_ADD, 0, R.string.user_dict_settings_add_menu_title) + .setIcon(R.drawable.ic_menu_add); + actionItem.setShowAsAction( + MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT); + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == OPTIONS_MENU_ADD) { + showAddOrEditDialog(null, null); + return true; + } + return false; + } + + /** + * Add or edit a word. If editingWord is null, it's an add; otherwise, it's an edit. + * @param editingWord the word to edit, or null if it's an add. + * @param editingShortcut the shortcut for this entry, or null if none. + */ + private void showAddOrEditDialog(final String editingWord, final String editingShortcut) { + final Bundle args = new Bundle(); + args.putInt(UserDictionaryAddWordContents.EXTRA_MODE, null == editingWord + ? UserDictionaryAddWordContents.MODE_INSERT + : UserDictionaryAddWordContents.MODE_EDIT); + args.putString(UserDictionaryAddWordContents.EXTRA_WORD, editingWord); + args.putString(UserDictionaryAddWordContents.EXTRA_SHORTCUT, editingShortcut); + args.putString(UserDictionaryAddWordContents.EXTRA_LOCALE, mLocale); + android.preference.PreferenceActivity pa = + (android.preference.PreferenceActivity)getActivity(); + pa.startPreferencePanel(UserDictionaryAddWordFragment.class.getName(), + args, R.string.user_dict_settings_add_dialog_title, null, null, 0); + } + + private String getWord(final int position) { + if (null == mCursor) return null; + mCursor.moveToPosition(position); + // Handle a possible race-condition + if (mCursor.isAfterLast()) return null; + + return mCursor.getString( + mCursor.getColumnIndexOrThrow(UserDictionary.Words.WORD)); + } + + private String getShortcut(final int position) { + if (!IS_SHORTCUT_API_SUPPORTED) return null; + if (null == mCursor) return null; + mCursor.moveToPosition(position); + // Handle a possible race-condition + if (mCursor.isAfterLast()) return null; + + return mCursor.getString( + mCursor.getColumnIndexOrThrow(UserDictionary.Words.SHORTCUT)); + } + + public static void deleteWord(final String word, final String shortcut, + final ContentResolver resolver) { + if (!IS_SHORTCUT_API_SUPPORTED) { + resolver.delete(UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_SHORTCUT_UNSUPPORTED, + new String[] { word }); + } else if (TextUtils.isEmpty(shortcut)) { + resolver.delete( + UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITHOUT_SHORTCUT, + new String[] { word }); + } else { + resolver.delete( + UserDictionary.Words.CONTENT_URI, DELETE_SELECTION_WITH_SHORTCUT, + new String[] { word, shortcut }); + } + } + + private static class MyAdapter extends SimpleCursorAdapter implements SectionIndexer { + + private AlphabetIndexer mIndexer; + + private ViewBinder mViewBinder = new ViewBinder() { + + @Override + public boolean setViewValue(View v, Cursor c, int columnIndex) { + if (!IS_SHORTCUT_API_SUPPORTED) { + // just let SimpleCursorAdapter set the view values + return false; + } + if (columnIndex == INDEX_SHORTCUT) { + final String shortcut = c.getString(INDEX_SHORTCUT); + if (TextUtils.isEmpty(shortcut)) { + v.setVisibility(View.GONE); + } else { + ((TextView)v).setText(shortcut); + v.setVisibility(View.VISIBLE); + } + v.invalidate(); + return true; + } + + return false; + } + }; + + @SuppressWarnings("deprecation") + public MyAdapter(Context context, int layout, Cursor c, String[] from, int[] to, + UserDictionarySettings settings) { + super(context, layout, c, from, to); + + if (null != c) { + final String alphabet = context.getString(R.string.user_dict_fast_scroll_alphabet); + final int wordColIndex = c.getColumnIndexOrThrow(UserDictionary.Words.WORD); + mIndexer = new AlphabetIndexer(c, wordColIndex, alphabet); + } + setViewBinder(mViewBinder); + } + + @Override + public int getPositionForSection(int section) { + return null == mIndexer ? 0 : mIndexer.getPositionForSection(section); + } + + @Override + public int getSectionForPosition(int position) { + return null == mIndexer ? 0 : mIndexer.getSectionForPosition(position); + } + + @Override + public Object[] getSections() { + return null == mIndexer ? null : mIndexer.getSections(); + } + } +} diff --git a/java/src/com/android/inputmethod/research/FeedbackFragment.java b/java/src/com/android/inputmethod/research/FeedbackFragment.java index 39f9c87a0..a0738292e 100644 --- a/java/src/com/android/inputmethod/research/FeedbackFragment.java +++ b/java/src/com/android/inputmethod/research/FeedbackFragment.java @@ -65,12 +65,10 @@ public class FeedbackFragment extends Fragment implements OnClickListener { mCancelButton.setOnClickListener(this); if (savedInstanceState != null) { - Log.d(TAG, "restoring from savedInstanceState"); restoreState(savedInstanceState); } else { final Bundle bundle = getActivity().getIntent().getExtras(); if (bundle != null) { - Log.d(TAG, "restoring from getArguments()"); restoreState(bundle); } } @@ -81,10 +79,7 @@ public class FeedbackFragment extends Fragment implements OnClickListener { public void onClick(final View view) { final ResearchLogger researchLogger = ResearchLogger.getInstance(); if (view == mIncludingUserRecordingCheckBox) { - if (hasUserRecording()) { - // Remove the recording - setHasUserRecording(false); - } else { + if (mIncludingUserRecordingCheckBox.isChecked()) { final Bundle bundle = new Bundle(); onSaveInstanceState(bundle); @@ -103,9 +98,9 @@ public class FeedbackFragment extends Fragment implements OnClickListener { R.string.research_feedback_empty_feedback_error_message, Toast.LENGTH_LONG).show(); } else { - final boolean isIncludingAccountName = isIncludingAccountName(); - researchLogger.sendFeedback(feedbackContents, - false /* isIncludingHistory */, isIncludingAccountName, hasUserRecording()); + final boolean isIncludingAccountName = mIncludingAccountNameCheckBox.isChecked(); + researchLogger.sendFeedback(feedbackContents, false /* isIncludingHistory */, + isIncludingAccountName, mIncludingUserRecordingCheckBox.isChecked()); getActivity().finish(); researchLogger.setFeedbackDialogBundle(null); researchLogger.onLeavingSendFeedbackDialog(); @@ -125,29 +120,13 @@ public class FeedbackFragment extends Fragment implements OnClickListener { final String savedFeedbackString = mEditText.getText().toString(); bundle.putString(KEY_FEEDBACK_STRING, savedFeedbackString); - bundle.putBoolean(KEY_INCLUDE_ACCOUNT_NAME, isIncludingAccountName()); - bundle.putBoolean(KEY_HAS_USER_RECORDING, hasUserRecording()); + bundle.putBoolean(KEY_INCLUDE_ACCOUNT_NAME, mIncludingAccountNameCheckBox.isChecked()); + bundle.putBoolean(KEY_HAS_USER_RECORDING, mIncludingUserRecordingCheckBox.isChecked()); } - public void restoreState(final Bundle bundle) { + private void restoreState(final Bundle bundle) { mEditText.setText(bundle.getString(KEY_FEEDBACK_STRING)); - setIsIncludingAccountName(bundle.getBoolean(KEY_INCLUDE_ACCOUNT_NAME)); - setHasUserRecording(bundle.getBoolean(KEY_HAS_USER_RECORDING)); - } - - private boolean hasUserRecording() { - return mIncludingUserRecordingCheckBox.isChecked(); - } - - private void setHasUserRecording(final boolean hasRecording) { - mIncludingUserRecordingCheckBox.setChecked(hasRecording); - } - - private boolean isIncludingAccountName() { - return mIncludingAccountNameCheckBox.isChecked(); - } - - private void setIsIncludingAccountName(final boolean isIncludingAccountName) { - mIncludingAccountNameCheckBox.setChecked(isIncludingAccountName); + mIncludingAccountNameCheckBox.setChecked(bundle.getBoolean(KEY_INCLUDE_ACCOUNT_NAME)); + mIncludingUserRecordingCheckBox.setChecked(bundle.getBoolean(KEY_HAS_USER_RECORDING)); } } diff --git a/java/src/com/android/inputmethod/research/FixedLogBuffer.java b/java/src/com/android/inputmethod/research/FixedLogBuffer.java index 78dc59562..4249af544 100644 --- a/java/src/com/android/inputmethod/research/FixedLogBuffer.java +++ b/java/src/com/android/inputmethod/research/FixedLogBuffer.java @@ -51,38 +51,35 @@ public class FixedLogBuffer extends LogBuffer { mNumActualWords = 0; } - protected int getNumActualWords() { - return mNumActualWords; - } - /** * Adds a new LogUnit to the front of the LIFO queue, evicting existing LogUnit's * (oldest first) if word capacity is reached. */ @Override public void shiftIn(final LogUnit newLogUnit) { - if (!newLogUnit.hasWord()) { - // This LogUnit isn't a word, so it doesn't count toward the word-limit. + if (!newLogUnit.hasOneOrMoreWords()) { + // This LogUnit doesn't contain any word, so it doesn't count toward the word-limit. super.shiftIn(newLogUnit); return; } + final int numWordsIncoming = newLogUnit.getNumWords(); if (mNumActualWords >= mWordCapacity) { // Give subclass a chance to handle the buffer full condition by shifting out logUnits. onBufferFull(); // If still full, evict. if (mNumActualWords >= mWordCapacity) { - shiftOutWords(1); + shiftOutWords(numWordsIncoming); } } super.shiftIn(newLogUnit); - mNumActualWords++; // Must be a word, or we wouldn't be here. + mNumActualWords += numWordsIncoming; } @Override public LogUnit unshiftIn() { final LogUnit logUnit = super.unshiftIn(); - if (logUnit != null && logUnit.hasWord()) { - mNumActualWords--; + if (logUnit != null && logUnit.hasOneOrMoreWords()) { + mNumActualWords -= logUnit.getNumWords(); } return logUnit; } @@ -113,18 +110,30 @@ public class FixedLogBuffer extends LogBuffer { @Override public LogUnit shiftOut() { final LogUnit logUnit = super.shiftOut(); - if (logUnit != null && logUnit.hasWord()) { - mNumActualWords--; + if (logUnit != null && logUnit.hasOneOrMoreWords()) { + mNumActualWords -= logUnit.getNumWords(); } return logUnit; } - protected void shiftOutWords(final int numWords) { - final int targetNumWords = mNumActualWords - numWords; - final LinkedList<LogUnit> logUnits = getLogUnits(); - while (mNumActualWords > targetNumWords && !logUnits.isEmpty()) { - shiftOut(); + /** + * Remove LogUnits from the front of the LogBuffer until {@code numWords} have been removed. + * + * If there are less than {@code numWords} word-containing {@link LogUnit}s, shifts out + * all {@code LogUnit}s in the buffer. + * + * @param numWords the minimum number of word-containing {@link LogUnit}s to shift out + * @return the number of actual {@code LogUnit}s shifted out + */ + protected int shiftOutWords(final int numWords) { + int numWordContainingLogUnitsShiftedOut = 0; + for (LogUnit logUnit = shiftOut(); logUnit != null + && numWordContainingLogUnitsShiftedOut < numWords; logUnit = shiftOut()) { + if (logUnit.hasOneOrMoreWords()) { + numWordContainingLogUnitsShiftedOut += logUnit.getNumWords(); + } } + return numWordContainingLogUnitsShiftedOut; } public void shiftOutAll() { @@ -136,27 +145,31 @@ public class FixedLogBuffer extends LogBuffer { } /** - * Returns a list of {@link LogUnit}s at the front of the buffer that have associated words. No - * more than {@code n} LogUnits will have words associated with them. If there are not enough - * LogUnits in the buffer to meet the word requirement, returns the all LogUnits. + * Returns a list of {@link LogUnit}s at the front of the buffer that have words associated with + * them. + * + * There will be no more than {@code n} words in the returned list. So if 2 words are + * requested, and the first LogUnit has 3 words, it is not returned. If 2 words are requested, + * and the first LogUnit has only 1 word, and the next LogUnit 2 words, only the first LogUnit + * is returned. If the first LogUnit has no words associated with it, and the second LogUnit + * has three words, then only the first LogUnit (which has no associated words) is returned. If + * there are not enough LogUnits in the buffer to meet the word requirement, then all LogUnits + * will be returned. * * @param n The maximum number of {@link LogUnit}s with words to return. * @return The list of the {@link LogUnit}s containing the first n words */ public ArrayList<LogUnit> peekAtFirstNWords(int n) { final LinkedList<LogUnit> logUnits = getLogUnits(); - final int length = logUnits.size(); // Allocate space for n*2 logUnits. There will be at least n, one for each word, and // there may be additional for punctuation, between-word commands, etc. This should be // enough that reallocation won't be necessary. - final ArrayList<LogUnit> list = new ArrayList<LogUnit>(n * 2); - for (int i = 0; i < length && n > 0; i++) { - final LogUnit logUnit = logUnits.get(i); - list.add(logUnit); - if (logUnit.hasWord()) { - n--; - } + final ArrayList<LogUnit> resultList = new ArrayList<LogUnit>(n * 2); + for (final LogUnit logUnit : logUnits) { + n -= logUnit.getNumWords(); + if (n < 0) break; + resultList.add(logUnit); } - return list; + return resultList; } } diff --git a/java/src/com/android/inputmethod/research/LogUnit.java b/java/src/com/android/inputmethod/research/LogUnit.java index 1c01675bd..4d60bda53 100644 --- a/java/src/com/android/inputmethod/research/LogUnit.java +++ b/java/src/com/android/inputmethod/research/LogUnit.java @@ -25,10 +25,10 @@ import com.android.inputmethod.latin.SuggestedWords; import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo; import com.android.inputmethod.latin.define.ProductionFlag; -import java.io.IOException; -import java.io.StringWriter; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; +import java.util.regex.Pattern; /** * A group of log statements related to each other. @@ -49,27 +49,45 @@ public class LogUnit { private static final boolean DEBUG = false && ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS_DEBUG; + private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+"); + private static final String[] EMPTY_STRING_ARRAY = new String[0]; + private final ArrayList<LogStatement> mLogStatementList; private final ArrayList<Object[]> mValuesList; // Assume that mTimeList is sorted in increasing order. Do not insert null values into // mTimeList. private final ArrayList<Long> mTimeList; - // Word that this LogUnit generates. Should be null if the LogUnit does not generate a genuine - // word (i.e. separators alone do not count as a word). Should never be empty. - private String mWord; + // Words that this LogUnit generates. Should be null if the data in the LogUnit does not + // generate a genuine word (i.e. separators alone do not count as a word). Should never be + // empty. Note that if the user types spaces explicitly, then normally mWords should contain + // only a single word; it will only contain space-separate multiple words if the user does not + // enter a space, and the system enters one automatically. + private String mWords; + private String[] mWordArray = EMPTY_STRING_ARRAY; private boolean mMayContainDigit; private boolean mIsPartOfMegaword; private boolean mContainsCorrection; - // mCorrectionType indicates whether the word was corrected at all, and if so, whether it was - // to a different word or just a "typo" correction. It is considered a "typo" if the final - // word was listed in the suggestions available the first time the word was gestured or - // tapped. + // mCorrectionType indicates whether the word was corrected at all, and if so, the nature of the + // correction. private int mCorrectionType; + // LogUnits start in this state. If a word is entered without being corrected, it will have + // this CorrectiontType. public static final int CORRECTIONTYPE_NO_CORRECTION = 0; + // The LogUnit was corrected manually by the user in an unspecified way. public static final int CORRECTIONTYPE_CORRECTION = 1; + // The LogUnit was corrected manually by the user to a word not in the list of suggestions of + // the first word typed here. (Note: this is a heuristic value, it may be incorrect, for + // example, if the user repositions the cursor). public static final int CORRECTIONTYPE_DIFFERENT_WORD = 2; + // The LogUnit was corrected manually by the user to a word that was in the list of suggestions + // of the first word typed here. (Again, a heuristic). It is probably a typo correction. public static final int CORRECTIONTYPE_TYPO = 3; + // TODO: Rather than just tracking the current state, keep a historical record of the LogUnit's + // state and statistics. This should include how many times it has been corrected, whether + // other LogUnit edits were done between edits to this LogUnit, etc. Also track when a LogUnit + // previously contained a word, but was corrected to empty (because it was deleted, and there is + // no known replacement). private SuggestedWords mSuggestedWords; @@ -166,7 +184,7 @@ public class LogUnit { final LogStatement logStatement; if (canIncludePrivateData) { LOGSTATEMENT_LOG_UNIT_BEGIN_WITH_PRIVATE_DATA.outputToLocked(jsonWriter, - SystemClock.uptimeMillis(), getWord(), getCorrectionType()); + SystemClock.uptimeMillis(), getWordsAsString(), getCorrectionType()); } else { LOGSTATEMENT_LOG_UNIT_BEGIN_WITHOUT_PRIVATE_DATA.outputToLocked(jsonWriter, SystemClock.uptimeMillis()); @@ -181,22 +199,22 @@ public class LogUnit { } /** - * Mark the current logUnit as containing data to generate {@code word}. + * Mark the current logUnit as containing data to generate {@code newWords}. * * If {@code setWord()} was previously called for this LogUnit, then the method will try to * determine what kind of correction it is, and update its internal state of the correctionType * accordingly. * - * @param word The word this LogUnit generates. Caller should not pass null or the empty + * @param newWords The words this LogUnit generates. Caller should not pass null or the empty * string. */ - public void setWord(final String word) { - if (hasWord()) { + public void setWords(final String newWords) { + if (hasOneOrMoreWords()) { // The word was already set once, and it is now being changed. See if the new word // is close to the old word. If so, then the change is probably a typo correction. // If not, the user may have decided to enter a different word, so flag it. if (mSuggestedWords != null) { - if (isInSuggestedWords(word, mSuggestedWords)) { + if (isInSuggestedWords(newWords, mSuggestedWords)) { mCorrectionType = CORRECTIONTYPE_TYPO; } else { mCorrectionType = CORRECTIONTYPE_DIFFERENT_WORD; @@ -206,38 +224,71 @@ public class LogUnit { // Mark it as a generic correction. mCorrectionType = CORRECTIONTYPE_CORRECTION; } + } else { + mCorrectionType = CORRECTIONTYPE_NO_CORRECTION; + } + mWords = newWords; + + // Update mWordArray + mWordArray = (TextUtils.isEmpty(mWords)) ? EMPTY_STRING_ARRAY + : WHITESPACE_PATTERN.split(mWords); + if (mWordArray.length > 0 && TextUtils.isEmpty(mWordArray[0])) { + // Empty string at beginning of array. Must have been whitespace at the start of the + // word. Remove the empty string. + mWordArray = Arrays.copyOfRange(mWordArray, 1, mWordArray.length); } - mWord = word; } - public String getWord() { - return mWord; + public String getWordsAsString() { + return mWords; + } + + /** + * Retuns the words generated by the data in this LogUnit. + * + * The first word may be an empty string, if the data in the LogUnit started by generating + * whitespace. + * + * @return the array of words. an empty list of there are no words associated with this LogUnit. + */ + public String[] getWordsAsStringArray() { + return mWordArray; + } + + public boolean hasOneOrMoreWords() { + return mWordArray.length >= 1; } - public boolean hasWord() { - return mWord != null && !TextUtils.isEmpty(mWord.trim()); + public int getNumWords() { + return mWordArray.length; } + // TODO: Refactor to eliminate getter/setters public void setMayContainDigit() { mMayContainDigit = true; } + // TODO: Refactor to eliminate getter/setters public boolean mayContainDigit() { return mMayContainDigit; } + // TODO: Refactor to eliminate getter/setters public void setContainsCorrection() { mContainsCorrection = true; } + // TODO: Refactor to eliminate getter/setters public boolean containsCorrection() { return mContainsCorrection; } + // TODO: Refactor to eliminate getter/setters public void setCorrectionType(final int correctionType) { mCorrectionType = correctionType; } + // TODO: Refactor to eliminate getter/setters public int getCorrectionType() { return mCorrectionType; } @@ -267,7 +318,7 @@ public class LogUnit { new ArrayList<Object[]>(laterValues), new ArrayList<Long>(laterTimes), true /* isPartOfMegaword */); - newLogUnit.mWord = null; + newLogUnit.mWords = null; newLogUnit.mMayContainDigit = mMayContainDigit; newLogUnit.mContainsCorrection = mContainsCorrection; @@ -287,9 +338,9 @@ public class LogUnit { mLogStatementList.addAll(logUnit.mLogStatementList); mValuesList.addAll(logUnit.mValuesList); mTimeList.addAll(logUnit.mTimeList); - mWord = null; - if (logUnit.mWord != null) { - setWord(logUnit.mWord); + mWords = null; + if (logUnit.mWords != null) { + setWords(logUnit.mWords); } mMayContainDigit = mMayContainDigit || logUnit.mMayContainDigit; mContainsCorrection = mContainsCorrection || logUnit.mContainsCorrection; diff --git a/java/src/com/android/inputmethod/research/MainLogBuffer.java b/java/src/com/android/inputmethod/research/MainLogBuffer.java index 3303d2bdb..42ef5d3b6 100644 --- a/java/src/com/android/inputmethod/research/MainLogBuffer.java +++ b/java/src/com/android/inputmethod/research/MainLogBuffer.java @@ -25,7 +25,6 @@ import com.android.inputmethod.latin.define.ProductionFlag; import java.util.ArrayList; import java.util.LinkedList; -import java.util.Random; /** * MainLogBuffer is a FixedLogBuffer that tracks the state of LogUnits to make privacy guarantees. @@ -100,10 +99,6 @@ public abstract class MainLogBuffer extends FixedLogBuffer { return mSuggest.getMainDictionary(); } - public void resetWordCounter() { - mNumWordsUntilSafeToSample = mNumWordsBetweenNGrams; - } - public void setIsStopping() { mIsStopping = true; } @@ -131,10 +126,7 @@ public abstract class MainLogBuffer extends FixedLogBuffer { final int length = logUnits.size(); for (int i = 0; i < length; i++) { final LogUnit logUnit = logUnits.get(i); - final String word = logUnit.getWord(); - if (word != null) { - numWordsInLogUnitList++; - } + numWordsInLogUnitList += logUnit.getNumWords(); } return numWordsInLogUnitList >= minNGramSize; } @@ -158,29 +150,31 @@ public abstract class MainLogBuffer extends FixedLogBuffer { // the complete buffer contents in detail. int numWordsInLogUnitList = 0; final int length = logUnits.size(); - for (int i = 0; i < length; i++) { - final LogUnit logUnit = logUnits.get(i); - if (!logUnit.hasWord()) { + for (final LogUnit logUnit : logUnits) { + if (!logUnit.hasOneOrMoreWords()) { // Digits outside words are a privacy threat. if (logUnit.mayContainDigit()) { return false; } } else { - numWordsInLogUnitList++; - final String word = logUnit.getWord(); - // Words not in the dictionary are a privacy threat. - if (ResearchLogger.hasLetters(word) && !(dictionary.isValidWord(word))) { - if (DEBUG) { - Log.d(TAG, "NOT SAFE!: hasLetters: " + ResearchLogger.hasLetters(word) - + ", isValid: " + (dictionary.isValidWord(word))); + numWordsInLogUnitList += logUnit.getNumWords(); + final String[] words = logUnit.getWordsAsStringArray(); + for (final String word : words) { + // Words not in the dictionary are a privacy threat. + if (ResearchLogger.hasLetters(word) && !(dictionary.isValidWord(word))) { + if (DEBUG) { + Log.d(TAG, "\"" + word + "\" NOT SAFE!: hasLetters: " + + ResearchLogger.hasLetters(word) + + ", isValid: " + (dictionary.isValidWord(word))); + } + return false; } - return false; } } } - // Finally, only return true if the minNGramSize is met. - return numWordsInLogUnitList >= minNGramSize; + // Finally, only return true if the ngram is the right size. + return numWordsInLogUnitList == minNGramSize; } public void shiftAndPublishAll() { @@ -201,13 +195,16 @@ public abstract class MainLogBuffer extends FixedLogBuffer { // Good n-gram at the front of the buffer. Publish it, disclosing details. publish(logUnits, true /* canIncludePrivateData */); shiftOutWords(N_GRAM_SIZE); - resetWordCounter(); + mNumWordsUntilSafeToSample = mNumWordsBetweenNGrams; } else { - // No good n-gram at front, and buffer is full. Shift out the first word (or if there - // is none, the existing logUnits). - logUnits = peekAtFirstNWords(1); + // No good n-gram at front, and buffer is full. Shift out up through the first logUnit + // with associated words (or if there is none, all the existing logUnits). + logUnits.clear(); + for (LogUnit logUnit = shiftOut(); logUnit != null && !logUnit.hasOneOrMoreWords(); + logUnit = shiftOut()) { + logUnits.add(logUnit); + } publish(logUnits, false /* canIncludePrivateData */); - shiftOutWords(1); } } @@ -224,13 +221,13 @@ public abstract class MainLogBuffer extends FixedLogBuffer { final boolean canIncludePrivateData); @Override - protected void shiftOutWords(final int numWords) { - final int oldNumActualWords = getNumActualWords(); - super.shiftOutWords(numWords); - final int numWordsShifted = oldNumActualWords - getNumActualWords(); - mNumWordsUntilSafeToSample -= numWordsShifted; + protected int shiftOutWords(final int numWords) { + final int numWordContainingLogUnitsShiftedOut = super.shiftOutWords(numWords); + mNumWordsUntilSafeToSample = Math.max(0, mNumWordsUntilSafeToSample + - numWordContainingLogUnitsShiftedOut); if (DEBUG) { Log.d(TAG, "wordsUntilSafeToSample now at " + mNumWordsUntilSafeToSample); } + return numWordContainingLogUnitsShiftedOut; } } diff --git a/java/src/com/android/inputmethod/research/ResearchLog.java b/java/src/com/android/inputmethod/research/ResearchLog.java index 35a491f2c..18bf7ba54 100644 --- a/java/src/com/android/inputmethod/research/ResearchLog.java +++ b/java/src/com/android/inputmethod/research/ResearchLog.java @@ -108,10 +108,14 @@ public class ResearchLog { @Override public Object call() throws Exception { try { - if (mHasWrittenData) { - mJsonWriter.endArray(); - mHasWrittenData = false; + // TODO: This is necessary to avoid an exception. Better would be to not even + // open the JsonWriter if the file is not even opened unless there is valid data + // to write. + if (!mHasWrittenData) { + mJsonWriter.beginArray(); } + mJsonWriter.endArray(); + mHasWrittenData = false; mJsonWriter.flush(); mJsonWriter.close(); if (DEBUG) { @@ -159,6 +163,12 @@ public class ResearchLog { public Object call() throws Exception { try { if (mHasWrittenData) { + // TODO: This is necessary to avoid an exception. Better would be to not + // even open the JsonWriter if the file is not even opened unless there is + // valid data to write. + if (!mHasWrittenData) { + mJsonWriter.beginArray(); + } mJsonWriter.endArray(); mJsonWriter.close(); mHasWrittenData = false; diff --git a/java/src/com/android/inputmethod/research/ResearchLogDirectory.java b/java/src/com/android/inputmethod/research/ResearchLogDirectory.java index 291dea5d0..d156068d6 100644 --- a/java/src/com/android/inputmethod/research/ResearchLogDirectory.java +++ b/java/src/com/android/inputmethod/research/ResearchLogDirectory.java @@ -97,15 +97,17 @@ public class ResearchLogDirectory { } } - public File getLogFilePath(final long time) { - return new File(mFilesDir, getUniqueFilename(LOG_FILENAME_PREFIX, time)); + public File getLogFilePath(final long time, final long nanoTime) { + return new File(mFilesDir, getUniqueFilename(LOG_FILENAME_PREFIX, time, nanoTime)); } - public File getUserRecordingFilePath(final long time) { - return new File(mFilesDir, getUniqueFilename(USER_RECORDING_FILENAME_PREFIX, time)); + public File getUserRecordingFilePath(final long time, final long nanoTime) { + return new File(mFilesDir, getUniqueFilename(USER_RECORDING_FILENAME_PREFIX, time, + nanoTime)); } - private static String getUniqueFilename(final String prefix, final long time) { - return prefix + "-" + time + FILENAME_SUFFIX; + private static String getUniqueFilename(final String prefix, final long time, + final long nanoTime) { + return prefix + "-" + time + "-" + nanoTime + FILENAME_SUFFIX; } } diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java index 7a23ddb05..1f6845c8b 100644 --- a/java/src/com/android/inputmethod/research/ResearchLogger.java +++ b/java/src/com/android/inputmethod/research/ResearchLogger.java @@ -389,7 +389,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } if (mMainLogBuffer == null) { mMainResearchLog = new ResearchLog(mResearchLogDirectory.getLogFilePath( - System.currentTimeMillis()), mLatinIME); + System.currentTimeMillis(), System.nanoTime()), mLatinIME); final int numWordsToIgnore = new Random().nextInt(NUMBER_OF_WORDS_BETWEEN_SAMPLES + 1); mMainLogBuffer = new MainLogBuffer(NUMBER_OF_WORDS_BETWEEN_SAMPLES, numWordsToIgnore, mSuggest) { @@ -397,13 +397,14 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang protected void publish(final ArrayList<LogUnit> logUnits, boolean canIncludePrivateData) { canIncludePrivateData |= IS_LOGGING_EVERYTHING; - final int length = logUnits.size(); - for (int i = 0; i < length; i++) { - final LogUnit logUnit = logUnits.get(i); - final String word = logUnit.getWord(); - if (word != null && word.length() > 0 && hasLetters(word)) { - Log.d(TAG, "onPublish: " + word + ", hc: " - + logUnit.containsCorrection()); + for (final LogUnit logUnit : logUnits) { + if (DEBUG) { + final String wordsString = logUnit.getWordsAsString(); + Log.d(TAG, "onPublish: '" + wordsString + + "', hc: " + logUnit.containsCorrection() + + ", cipd: " + canIncludePrivateData); + } + for (final String word : logUnit.getWordsAsStringArray()) { final Dictionary dictionary = getDictionary(); mStatistics.recordWordEntered( dictionary != null && dictionary.isValidWord(word), @@ -420,7 +421,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang private void resetFeedbackLogging() { mFeedbackLog = new ResearchLog(mResearchLogDirectory.getLogFilePath( - System.currentTimeMillis()), mLatinIME); + System.currentTimeMillis(), System.nanoTime()), mLatinIME); mFeedbackLogBuffer = new FixedLogBuffer(FEEDBACK_WORD_BUFFER_SIZE); } @@ -545,7 +546,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang mUserRecordingLog.blockingAbort(RESEARCHLOG_ABORT_TIMEOUT_IN_MS); } mUserRecordingFile = mResearchLogDirectory.getUserRecordingFilePath( - System.currentTimeMillis()); + System.currentTimeMillis(), System.nanoTime()); mUserRecordingLog = new ResearchLog(mUserRecordingFile, mLatinIME); mUserRecordingLogBuffer = new LogBuffer(); resetRecordingTimer(); @@ -813,7 +814,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang // enabled. The dot is actually a zero-width, zero-height rectangle, placed at the // lower-right corner of the canvas, painted with a non-zero border width. paint.setStrokeWidth(3); - canvas.drawRect(width, height, width, height, paint); + canvas.drawRect(width - 1, height - 1, width, height, paint); } paint.setColor(savedColor); paint.setStyle(savedStyle); @@ -852,8 +853,8 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang /* package for test */ void commitCurrentLogUnit() { if (DEBUG) { - Log.d(TAG, "commitCurrentLogUnit" + (mCurrentLogUnit.hasWord() ? - ": " + mCurrentLogUnit.getWord() : "")); + Log.d(TAG, "commitCurrentLogUnit" + (mCurrentLogUnit.hasOneOrMoreWords() ? + ": " + mCurrentLogUnit.getWordsAsString() : "")); } if (!mCurrentLogUnit.isEmpty()) { if (mMainLogBuffer != null) { @@ -893,8 +894,8 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang // Check that expected word matches. if (oldLogUnit != null) { - final String oldLogUnitWord = oldLogUnit.getWord(); - if (!oldLogUnitWord.equals(expectedWord)) { + final String oldLogUnitWords = oldLogUnit.getWordsAsString(); + if (oldLogUnitWords != null && !oldLogUnitWords.equals(expectedWord)) { return; } } @@ -916,7 +917,8 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang enqueueEvent(LOGSTATEMENT_UNCOMMIT_CURRENT_LOGUNIT); if (DEBUG) { Log.d(TAG, "uncommitCurrentLogUnit (dump=" + dumpCurrentLogUnit + ") back to " - + (mCurrentLogUnit.hasWord() ? ": '" + mCurrentLogUnit.getWord() + "'" : "")); + + (mCurrentLogUnit.hasOneOrMoreWords() ? ": '" + + mCurrentLogUnit.getWordsAsString() + "'" : "")); } } @@ -950,8 +952,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } for (LogUnit logUnit : logUnits) { if (DEBUG) { - Log.d(TAG, "publishLogBuffer: " + (logUnit.hasWord() ? logUnit.getWord() - : "<wordless>") + ", correction?: " + logUnit.containsCorrection()); + Log.d(TAG, "publishLogBuffer: " + (logUnit.hasOneOrMoreWords() + ? logUnit.getWordsAsString() : "<wordless>") + + ", correction?: " + logUnit.containsCorrection()); } researchLog.publish(logUnit, canIncludePrivateData); } @@ -986,7 +989,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang return; } if (word.length() > 0 && hasLetters(word)) { - mCurrentLogUnit.setWord(word); + mCurrentLogUnit.setWords(word); } final LogUnit newLogUnit = mCurrentLogUnit.splitByTime(maxTime); enqueueCommitText(word, isBatchMode); @@ -1107,7 +1110,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang packageInfo = mLatinIME.getPackageManager().getPackageInfo(mLatinIME.getPackageName(), 0); final String versionName = packageInfo.versionName; - return !(developerBuildRegex.matcher(versionName).find()); + return developerBuildRegex.matcher(versionName).find(); } catch (final NameNotFoundException e) { Log.e(TAG, "Could not determine package name", e); return false; @@ -1478,7 +1481,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang } if (originallyTypedWord.length() > 0 && hasLetters(originallyTypedWord)) { if (logUnit != null) { - logUnit.setWord(originallyTypedWord); + logUnit.setWords(originallyTypedWord); } } researchLogger.enqueueEvent(logUnit != null ? logUnit : researchLogger.mCurrentLogUnit, @@ -1616,7 +1619,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang * Log a call to LatinIME.commitCurrentAutoCorrection(). * * SystemResponse: The IME has committed an auto-correction. An auto-correction changes the raw - * text input to another word that the user more likely desired to type. + * text input to another word (or words) that the user more likely desired to type. */ private static final LogStatement LOGSTATEMENT_LATINIME_COMMITCURRENTAUTOCORRECTION = new LogStatement("LatinIMECommitCurrentAutoCorrection", true, true, "typedWord", @@ -1826,6 +1829,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang public static void latinIME_onEndBatchInput(final CharSequence enteredText, final int enteredWordPos, final SuggestedWords suggestedWords) { final ResearchLogger researchLogger = getInstance(); + if (!TextUtils.isEmpty(enteredText) && hasLetters(enteredText.toString())) { + researchLogger.mCurrentLogUnit.setWords(enteredText.toString()); + } researchLogger.enqueueEvent(LOGSTATEMENT_LATINIME_ONENDBATCHINPUT, enteredText, enteredWordPos); researchLogger.mCurrentLogUnit.initializeSuggestions(suggestedWords); diff --git a/java/src/com/android/inputmethod/research/UploaderService.java b/java/src/com/android/inputmethod/research/UploaderService.java index 6a9717b7c..d2db34927 100644 --- a/java/src/com/android/inputmethod/research/UploaderService.java +++ b/java/src/com/android/inputmethod/research/UploaderService.java @@ -22,6 +22,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import android.os.SystemClock; import com.android.inputmethod.latin.define.ProductionFlag; @@ -79,28 +80,14 @@ public final class UploaderService extends IntentService { */ public static void cancelAndRescheduleUploadingService(final Context context, final boolean needsRescheduling) { - final PendingIntent pendingIntent = getPendingIntentForService(context); + final Intent intent = new Intent(context, UploaderService.class); + final PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0); final AlarmManager alarmManager = (AlarmManager) context.getSystemService( Context.ALARM_SERVICE); - cancelAnyScheduledServiceAlarm(alarmManager, pendingIntent); + alarmManager.cancel(pendingIntent); if (needsRescheduling) { - scheduleServiceAlarm(alarmManager, pendingIntent); + alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + + UploaderService.RUN_INTERVAL, pendingIntent); } } - - private static PendingIntent getPendingIntentForService(final Context context) { - final Intent intent = new Intent(context, UploaderService.class); - return PendingIntent.getService(context, 0, intent, 0); - } - - private static void cancelAnyScheduledServiceAlarm(final AlarmManager alarmManager, - final PendingIntent pendingIntent) { - alarmManager.cancel(pendingIntent); - } - - private static void scheduleServiceAlarm(final AlarmManager alarmManager, - final PendingIntent pendingIntent) { - alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, UploaderService.RUN_INTERVAL, - pendingIntent); - } } |