diff options
Diffstat (limited to 'java/src')
10 files changed, 226 insertions, 50 deletions
diff --git a/java/src/com/android/inputmethod/compat/IntentCompatUtils.java b/java/src/com/android/inputmethod/compat/IntentCompatUtils.java new file mode 100644 index 000000000..df2e22fe8 --- /dev/null +++ b/java/src/com/android/inputmethod/compat/IntentCompatUtils.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.compat; + +import android.content.Intent; + +public final class IntentCompatUtils { + // Note that Intent.ACTION_USER_INITIALIZE have been introduced in API level 17 + // (Build.VERSION_CODE.JELLY_BEAN_MR1). + public static final String ACTION_USER_INITIALIZE = + (String)CompatUtils.getFieldValue(null, null, + CompatUtils.getField(Intent.class, "ACTION_USER_INITIALIZE")); + + private IntentCompatUtils() { + // This utility class is not publicly instantiable. + } + + public static boolean has_ACTION_USER_INITIALIZE(final Intent intent) { + return ACTION_USER_INITIALIZE != null && intent != null + && ACTION_USER_INITIALIZE.equals(intent.getAction()); + } +} diff --git a/java/src/com/android/inputmethod/keyboard/KeyboardView.java b/java/src/com/android/inputmethod/keyboard/KeyboardView.java index 365c35b48..350dc69b2 100644 --- a/java/src/com/android/inputmethod/keyboard/KeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/KeyboardView.java @@ -635,15 +635,9 @@ public class KeyboardView extends View { invalidate(x, y, x + key.mWidth, y + key.mHeight); } - // TODO: Remove this method. - public void closing() { - mInvalidateAllKeys = true; - } - @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); - closing(); freeOffscreenBuffer(); } } diff --git a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java index 4d10f0e69..bc27f8553 100644 --- a/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MainKeyboardView.java @@ -1236,13 +1236,11 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack mDrawingHandler.cancelAllMessages(); } - @Override public void closing() { dismissAllKeyPreviews(); cancelAllMessages(); onDismissMoreKeysPanel(); mMoreKeysKeyboardCache.clear(); - super.closing(); } /** diff --git a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java index 9e75f8b8a..0d42ab2fe 100644 --- a/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java +++ b/java/src/com/android/inputmethod/keyboard/MoreKeysKeyboardView.java @@ -174,7 +174,6 @@ public class MoreKeysKeyboardView extends KeyboardView implements MoreKeysPanel @Override public boolean dismissMoreKeysPanel() { - super.closing(); if (mController == null) return false; return mController.onDismissMoreKeysPanel(); } diff --git a/java/src/com/android/inputmethod/latin/LatinIME.java b/java/src/com/android/inputmethod/latin/LatinIME.java index 2050c7663..79a05ceae 100644 --- a/java/src/com/android/inputmethod/latin/LatinIME.java +++ b/java/src/com/android/inputmethod/latin/LatinIME.java @@ -132,6 +132,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction private View mKeyPreviewBackingView; private View mSuggestionsContainer; private SuggestionStripView mSuggestionStripView; + // Never null + private SuggestedWords mSuggestedWords = SuggestedWords.EMPTY; @UsedForTesting Suggest mSuggest; private CompletionInfo[] mApplicationSpecifiedCompletions; private ApplicationInfo mTargetApplicationInfo; @@ -731,6 +733,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // otherwise it will clear the suggestion strip. setPunctuationSuggestions(); } + mSuggestedWords = SuggestedWords.EMPTY; mConnection.resetCachesUponCursorMove(editorInfo.initialSelStart); @@ -995,7 +998,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction false /* isPrediction */); // When in fullscreen mode, show completions generated by the application final boolean isAutoCorrection = false; - setSuggestionStrip(suggestedWords, isAutoCorrection); + setSuggestedWords(suggestedWords, isAutoCorrection); setAutoCorrectionIndicator(isAutoCorrection); setSuggestionStripShown(true); if (ProductionFlag.IS_EXPERIMENTAL) { @@ -1120,7 +1123,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction if (mSettings.getCurrent().mBigramPredictionEnabled) { clearSuggestionStrip(); } else { - setSuggestionStrip(mSettings.getCurrent().mSuggestPuncList, false); + setSuggestedWords(mSettings.getCurrent().mSuggestPuncList, false); } mConnection.resetCachesUponCursorMove(newCursorPosition); } @@ -1983,8 +1986,8 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // Outside LatinIME, only used by the test suite. @UsedForTesting boolean isShowingPunctuationList() { - if (mSuggestionStripView == null) return false; - return mSettings.getCurrent().mSuggestPuncList == mSuggestionStripView.getSuggestions(); + if (mSuggestedWords == null) return false; + return mSettings.getCurrent().mSuggestPuncList == mSuggestedWords; } private boolean isSuggestionsStripVisible() { @@ -2000,11 +2003,12 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } private void clearSuggestionStrip() { - setSuggestionStrip(SuggestedWords.EMPTY, false); + setSuggestedWords(SuggestedWords.EMPTY, false); setAutoCorrectionIndicator(false); } - private void setSuggestionStrip(final SuggestedWords words, final boolean isAutoCorrection) { + private void setSuggestedWords(final SuggestedWords words, final boolean isAutoCorrection) { + mSuggestedWords = words; if (mSuggestionStripView != null) { mSuggestionStripView.setSuggestions(words); mKeyboardSwitcher.onAutoCorrectionStateChanged(isAutoCorrection); @@ -2087,15 +2091,16 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } private SuggestedWords getOlderSuggestions(final String typedWord) { - SuggestedWords previousSuggestions = mSuggestionStripView.getSuggestions(); - if (previousSuggestions == mSettings.getCurrent().mSuggestPuncList) { - previousSuggestions = SuggestedWords.EMPTY; + SuggestedWords previousSuggestedWords = mSuggestedWords; + if (previousSuggestedWords == mSettings.getCurrent().mSuggestPuncList) { + previousSuggestedWords = SuggestedWords.EMPTY; } if (typedWord == null) { - return previousSuggestions; + return previousSuggestedWords; } final ArrayList<SuggestedWords.SuggestedWordInfo> typedWordAndPreviousSuggestions = - SuggestedWords.getTypedWordAndPreviousSuggestions(typedWord, previousSuggestions); + SuggestedWords.getTypedWordAndPreviousSuggestions(typedWord, + previousSuggestedWords); return new SuggestedWords(typedWordAndPreviousSuggestions, false /* typedWordValid */, false /* hasAutoCorrectionCandidate */, @@ -2117,7 +2122,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction } mWordComposer.setAutoCorrection(autoCorrection); final boolean isAutoCorrection = suggestedWords.willAutoCorrect(); - setSuggestionStrip(suggestedWords, isAutoCorrection); + setSuggestedWords(suggestedWords, isAutoCorrection); setAutoCorrectionIndicator(isAutoCorrection); setSuggestionStripShown(isSuggestionsStripVisible()); } @@ -2140,7 +2145,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction Stats.onAutoCorrection(typedWord, autoCorrection, separatorString, mWordComposer); } if (ProductionFlag.IS_EXPERIMENTAL) { - final SuggestedWords suggestedWords = mSuggestionStripView.getSuggestions(); + final SuggestedWords suggestedWords = mSuggestedWords; ResearchLogger.latinIme_commitCurrentAutoCorrection(typedWord, autoCorrection, separatorString, mWordComposer.isBatchMode(), suggestedWords); } @@ -2165,7 +2170,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction // interface @Override public void pickSuggestionManually(final int index, final String suggestion) { - final SuggestedWords suggestedWords = mSuggestionStripView.getSuggestions(); + final SuggestedWords suggestedWords = mSuggestedWords; // If this is a punctuation picked from the suggestion strip, pass it to onCodeInput if (suggestion.length() == 1 && isShowingPunctuationList()) { // Word separators are suggested before the user inputs something. @@ -2197,6 +2202,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction if (mSettings.getCurrent().isApplicationSpecifiedCompletionsOn() && mApplicationSpecifiedCompletions != null && index >= 0 && index < mApplicationSpecifiedCompletions.length) { + mSuggestedWords = SuggestedWords.EMPTY; if (mSuggestionStripView != null) { mSuggestionStripView.clear(); } @@ -2252,7 +2258,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction */ private void commitChosenWord(final String chosenWord, final int commitType, final String separatorString) { - final SuggestedWords suggestedWords = mSuggestionStripView.getSuggestions(); + final SuggestedWords suggestedWords = mSuggestedWords; mConnection.commitText(SuggestionSpanUtils.getTextWithSuggestionSpan( this, chosenWord, suggestedWords, mIsMainDictionaryAvailable), 1); // Add the word to the user history dictionary @@ -2269,7 +2275,7 @@ public final class LatinIME extends InputMethodService implements KeyboardAction if (mSettings.getCurrent().mBigramPredictionEnabled) { clearSuggestionStrip(); } else { - setSuggestionStrip(mSettings.getCurrent().mSuggestPuncList, false); + setSuggestedWords(mSettings.getCurrent().mSuggestPuncList, false); } setAutoCorrectionIndicator(false); setSuggestionStripShown(isSuggestionsStripVisible()); @@ -2566,6 +2572,12 @@ public final class LatinIME extends InputMethodService implements KeyboardAction dialog.show(); } + // TODO: can this be removed somehow without breaking the tests? + @UsedForTesting + /* package for test */ String getFirstSuggestedWord() { + return mSuggestedWords.size() > 0 ? mSuggestedWords.getWord(0) : null; + } + public void debugDumpStateAndCrashWithException(final String context) { final StringBuilder s = new StringBuilder(); s.append("Target application : ").append(mTargetApplicationInfo.name) diff --git a/java/src/com/android/inputmethod/latin/setup/LauncherIconVisibilityManager.java b/java/src/com/android/inputmethod/latin/setup/LauncherIconVisibilityManager.java new file mode 100644 index 000000000..ad34011ea --- /dev/null +++ b/java/src/com/android/inputmethod/latin/setup/LauncherIconVisibilityManager.java @@ -0,0 +1,125 @@ +/* + * 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.app.Activity; +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager; +import android.os.Process; +import android.util.Log; + +import com.android.inputmethod.compat.IntentCompatUtils; +import com.android.inputmethod.latin.RichInputMethodManager; + +/** + * This class detects the {@link Intent#ACTION_MY_PACKAGE_REPLACED} broadcast intent when this IME + * package has been replaced by a newer version of the same package. This class also detects + * {@link Intent#ACTION_BOOT_COMPLETED} and {@link Intent#ACTION_USER_INITIALIZE} broadcast intent. + * + * If this IME has already been installed in the system image and a new version of this IME has + * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver and it + * will hide the setup wizard's icon. + * + * If this IME has already been installed in the data partition and a new version of this IME has + * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver but it + * will not hide the setup wizard's icon, and the icon will appear on the launcher. + * + * If this IME hasn't been installed yet and has been newly installed, no + * {@link Intent#ACTION_MY_PACKAGE_REPLACED} will be sent and the setup wizard's icon will appear + * on the launcher. + * + * When the device has been booted, {@link Intent#ACTION_BOOT_COMPLETED} is received by this + * receiver and it checks whether the setup wizard's icon should be appeared or not on the launcher + * depending on which partition this IME is installed. + * + * When a multiuser account has been created, {@link Intent#ACTION_USER_INITIALIZE} is received + * by this receiver and it checks the whether the setup wizard's icon should be appeared or not on + * the launcher depending on which partition this IME is installed. + */ +public final class LauncherIconVisibilityManager extends BroadcastReceiver { + private static final String TAG = LauncherIconVisibilityManager.class.getSimpleName(); + + @Override + public void onReceive(final Context context, final Intent intent) { + if (shouldHandleThisIntent(intent, context)) { + if (isInSystemImage(context)) { + disableActivity(context, SetupActivity.class); + } else { + Log.i(TAG, "This package isn't in system image: " + context.getPackageName()); + } + } + + // The process that hosts this broadcast receiver is invoked and remains alive even after + // 1) the package has been re-installed, 2) the device has been booted, + // 3) a multiuser has been created. + // There is no good reason to keep the process alive if this IME isn't a current IME. + RichInputMethodManager.init(context); + if (!SetupActivity.isThisImeCurrent(context)) { + final int myPid = Process.myPid(); + Log.i(TAG, "Killing my process: pid=" + myPid); + Process.killProcess(myPid); + } + } + + private static boolean shouldHandleThisIntent(final Intent intent, final Context context) { + final String action = intent.getAction(); + if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) { + Log.i(TAG, "Package has been replaced: " + context.getPackageName()); + return true; + } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { + Log.i(TAG, "Boot has been completed"); + return true; + } else if (IntentCompatUtils.has_ACTION_USER_INITIALIZE(intent)) { + Log.i(TAG, "User initialize"); + return true; + } + return false; + } + + /** + * Disable an activity of the specified package. Disabling an activity will also hide its + * icon from the launcher. + * + * @param context package context of an activity to be disabled + * @param activityClass activity class to be disabled + */ + private static void disableActivity(final Context context, + final Class<? extends Activity> activityClass) { + final ComponentName activityComponent = new ComponentName(context, activityClass); + final PackageManager pm = context.getPackageManager(); + final int activityComponentState = pm.getComponentEnabledSetting(activityComponent); + if (activityComponentState == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) { + // This activity is already disabled. + Log.i(TAG, "Activity has already been disabled: " + activityComponent); + return; + } + // Disabling an activity will also hide its icon from the launcher. + pm.setComponentEnabledSetting(activityComponent, + PackageManager.COMPONENT_ENABLED_STATE_DISABLED, + PackageManager.DONT_KILL_APP); + Log.i(TAG, "Disable activity: " + activityComponent); + } + + private static boolean isInSystemImage(final Context context) { + final ApplicationInfo appInfo = context.getApplicationInfo(); + return (appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0; + } +} diff --git a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java index c30ecfb16..e009fbc39 100644 --- a/java/src/com/android/inputmethod/latin/setup/SetupActivity.java +++ b/java/src/com/android/inputmethod/latin/setup/SetupActivity.java @@ -17,6 +17,7 @@ package com.android.inputmethod.latin.setup; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.graphics.PorterDuff; @@ -43,7 +44,7 @@ public final class SetupActivity extends Activity { private SetupStepIndicatorView mStepIndicatorView; private final SetupStepGroup mSetupSteps = new SetupStepGroup(); private static final String STATE_STEP = "step"; - private int mStepNo; + private int mStepNumber; private static final int STEP_1 = 1; private static final int STEP_2 = 2; private static final int STEP_3 = 3; @@ -63,7 +64,7 @@ public final class SetupActivity extends Activity { final SetupActivity setupActivity = getOuterInstance(); switch (msg.what) { case MSG_POLLING_IME_SETTINGS: - if (setupActivity.isMyImeEnabled()) { + if (SetupActivity.isThisImeEnabled(setupActivity)) { setupActivity.invokeSetupWizardOfThisIme(); return; } @@ -92,12 +93,12 @@ public final class SetupActivity extends Activity { RichInputMethodManager.init(this); if (savedInstanceState == null) { - mStepNo = determineSetupStepNo(); + mStepNumber = determineSetupStepNumber(); } else { - mStepNo = savedInstanceState.getInt(STATE_STEP); + mStepNumber = savedInstanceState.getInt(STATE_STEP); } - if (mStepNo == STEP_3) { + if (mStepNumber == STEP_3) { // This IME already has been enabled and set as current IME. // TODO: Implement tutorial. invokeSettingsOfThisIme(); @@ -182,8 +183,16 @@ public final class SetupActivity extends Activity { startActivity(intent); } - private boolean isMyImeEnabled() { - final String packageName = getPackageName(); + /** + * Check if the IME specified by the context is enabled. + * Note that {@link RichInputMethodManager} must have been initialized before calling this + * method. + * + * @param context package context of the IME to be checked. + * @return true if this IME is enabled. + */ + public static boolean isThisImeEnabled(final Context context) { + final String packageName = context.getPackageName(); final InputMethodManager imm = RichInputMethodManager.getInstance().getInputMethodManager(); for (final InputMethodInfo imi : imm.getEnabledInputMethodList()) { if (packageName.equals(imi.getPackageName())) { @@ -193,20 +202,28 @@ public final class SetupActivity extends Activity { return false; } - private boolean isMyImeCurrent() { + /** + * Check if the IME specified by the context is the current IME. + * Note that {@link RichInputMethodManager} must have been initialized before calling this + * method. + * + * @param context package context of the IME to be checked. + * @return true if this IME is the current IME. + */ + public static boolean isThisImeCurrent(final Context context) { final InputMethodInfo myImi = RichInputMethodManager.getInstance().getInputMethodInfoOfThisIme(); final String currentImeId = Settings.Secure.getString( - getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); + context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD); return myImi.getId().equals(currentImeId); } - private int determineSetupStepNo() { + private int determineSetupStepNumber() { mHandler.cancelPollingImeSettings(); - if (!isMyImeEnabled()) { + if (!isThisImeEnabled(this)) { return STEP_1; } - if (!isMyImeCurrent()) { + if (!isThisImeCurrent(this)) { return STEP_2; } return STEP_3; @@ -215,25 +232,25 @@ public final class SetupActivity extends Activity { @Override protected void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); - outState.putInt(STATE_STEP, mStepNo); + outState.putInt(STATE_STEP, mStepNumber); } @Override protected void onRestoreInstanceState(final Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); - mStepNo = savedInstanceState.getInt(STATE_STEP); + mStepNumber = savedInstanceState.getInt(STATE_STEP); } @Override protected void onStart() { super.onStart(); - mStepNo = determineSetupStepNo(); + mStepNumber = determineSetupStepNumber(); } @Override protected void onRestart() { super.onRestart(); - mStepNo = determineSetupStepNo(); + mStepNumber = determineSetupStepNumber(); } @Override @@ -248,15 +265,15 @@ public final class SetupActivity extends Activity { if (!hasFocus) { return; } - mStepNo = determineSetupStepNo(); + mStepNumber = determineSetupStepNumber(); updateSetupStepView(); } private void updateSetupStepView() { final int layoutDirection = ViewCompatUtils.getLayoutDirection(mStepIndicatorView); mStepIndicatorView.setIndicatorPosition( - getIndicatorPosition(mStepNo, mSetupSteps.getTotalStep(), layoutDirection)); - mSetupSteps.enableStep(mStepNo); + getIndicatorPosition(mStepNumber, mSetupSteps.getTotalStep(), layoutDirection)); + mSetupSteps.enableStep(mStepNumber); } private static float getIndicatorPosition(final int step, final int totalStep, diff --git a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java index bc51d5d62..5a29eee4e 100644 --- a/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java +++ b/java/src/com/android/inputmethod/latin/suggestions/SuggestionStripView.java @@ -644,10 +644,6 @@ public final class SuggestionStripView extends RelativeLayout implements OnClick return false; } - public SuggestedWords getSuggestions() { - return mSuggestedWords; - } - public void clear() { mSuggestionsStrip.removeAllViews(); removeAllViews(); diff --git a/java/src/com/android/inputmethod/research/LogUnit.java b/java/src/com/android/inputmethod/research/LogUnit.java index c1258f496..839e2b7ba 100644 --- a/java/src/com/android/inputmethod/research/LogUnit.java +++ b/java/src/com/android/inputmethod/research/LogUnit.java @@ -16,7 +16,6 @@ package com.android.inputmethod.research; -import android.content.SharedPreferences; import android.os.SystemClock; import android.text.TextUtils; import android.util.JsonWriter; diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java index 0367a15a2..80e4af28f 100644 --- a/java/src/com/android/inputmethod/research/ResearchLogger.java +++ b/java/src/com/android/inputmethod/research/ResearchLogger.java @@ -206,7 +206,7 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang private Intent mUploadIntent; private Intent mUploadNowIntent; - private LogUnit mCurrentLogUnit = new LogUnit(); + /* package for test */ LogUnit mCurrentLogUnit = new LogUnit(); // Gestured or tapped words may be committed after the gesture of the next word has started. // To ensure that the gesture data of the next word is not associated with the previous word, |