Merge "Fix NPE and don't call UI API from non-UI thread" into jb-mr1-dev

main
Tadashi G. Takaoka 2012-08-08 20:00:18 -07:00 committed by Android (Google) Code Review
commit 9abea49dd4
6 changed files with 45 additions and 43 deletions

View File

@ -59,6 +59,9 @@ public class KeyDetector {
} }
public Keyboard getKeyboard() { public Keyboard getKeyboard() {
if (mKeyboard == null) {
throw new IllegalStateException("keyboard isn't set");
}
return mKeyboard; return mKeyboard;
} }

View File

@ -108,9 +108,6 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
private int mDelayAfterPreview; private int mDelayAfterPreview;
private final PreviewPlacerView mPreviewPlacerView; private final PreviewPlacerView mPreviewPlacerView;
/** True if {@link KeyboardView} should handle gesture events. */
protected boolean mShouldHandleGesture;
// Drawing // Drawing
/** True if the entire keyboard needs to be dimmed. */ /** True if the entire keyboard needs to be dimmed. */
private boolean mNeedsToDimEntireKeyboard; private boolean mNeedsToDimEntireKeyboard;
@ -438,9 +435,8 @@ public class KeyboardView extends View implements PointerTracker.DrawingProxy {
return mShowKeyPreviewPopup; return mShowKeyPreviewPopup;
} }
public void setGestureHandlingMode(boolean shouldHandleGesture, public void setGesturePreviewMode(boolean drawsGesturePreviewTrail,
boolean drawsGesturePreviewTrail, boolean drawsGestureFloatingPreviewText) { boolean drawsGestureFloatingPreviewText) {
mShouldHandleGesture = shouldHandleGesture;
mPreviewPlacerView.setGesturePreviewMode( mPreviewPlacerView.setGesturePreviewMode(
drawsGesturePreviewTrail, drawsGestureFloatingPreviewText); drawsGesturePreviewTrail, drawsGestureFloatingPreviewText);
} }

View File

@ -482,7 +482,7 @@ public class MainKeyboardView extends KeyboardView implements PointerTracker.Key
super.setKeyboard(keyboard); super.setKeyboard(keyboard);
mKeyDetector.setKeyboard( mKeyDetector.setKeyboard(
keyboard, -getPaddingLeft(), -getPaddingTop() + mVerticalCorrection); keyboard, -getPaddingLeft(), -getPaddingTop() + mVerticalCorrection);
PointerTracker.setKeyDetector(mKeyDetector, mShouldHandleGesture); PointerTracker.setKeyDetector(mKeyDetector);
mTouchScreenRegulator.setKeyboard(keyboard); mTouchScreenRegulator.setKeyboard(keyboard);
mMoreKeysPanelCache.clear(); mMoreKeysPanelCache.clear();
@ -500,12 +500,13 @@ public class MainKeyboardView extends KeyboardView implements PointerTracker.Key
AccessibleKeyboardViewProxy.getInstance().setKeyboard(keyboard); AccessibleKeyboardViewProxy.getInstance().setKeyboard(keyboard);
} }
@Override // Note that this method is called from a non-UI thread.
public void setGestureHandlingMode(final boolean shouldHandleGesture, public void setMainDictionaryAvailability(boolean mainDictionaryAvailable) {
boolean drawsGesturePreviewTrail, boolean drawsGestureFloatingPreviewText) { PointerTracker.setMainDictionaryAvailability(mainDictionaryAvailable);
super.setGestureHandlingMode(shouldHandleGesture, drawsGesturePreviewTrail, }
drawsGestureFloatingPreviewText);
PointerTracker.setKeyDetector(mKeyDetector, shouldHandleGesture); public void setGestureHandlingEnabledByUser(boolean gestureHandlingEnabledByUser) {
PointerTracker.setGestureHandlingEnabledByUser(gestureHandlingEnabledByUser);
} }
/** /**

View File

@ -39,11 +39,7 @@ public class MoreKeysDetector extends KeyDetector {
Key nearestKey = null; Key nearestKey = null;
int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare; int nearestDist = (y < 0) ? mSlideAllowanceSquareTop : mSlideAllowanceSquare;
final Keyboard keyboard = getKeyboard(); for (final Key key : getKeyboard().mKeys) {
if (keyboard == null) {
throw new NullPointerException("Keyboard isn't set");
}
for (final Key key : keyboard.mKeys) {
final int dist = key.squaredDistanceToEdge(touchX, touchY); final int dist = key.squaredDistanceToEdge(touchX, touchY);
if (dist < nearestDist) { if (dist < nearestDist) {
nearestKey = key; nearestKey = key;

View File

@ -29,6 +29,7 @@ import com.android.inputmethod.keyboard.internal.GestureStroke;
import com.android.inputmethod.keyboard.internal.PointerTrackerQueue; import com.android.inputmethod.keyboard.internal.PointerTrackerQueue;
import com.android.inputmethod.latin.InputPointers; import com.android.inputmethod.latin.InputPointers;
import com.android.inputmethod.latin.LatinImeLogger; import com.android.inputmethod.latin.LatinImeLogger;
import com.android.inputmethod.latin.Utils;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import com.android.inputmethod.research.ResearchLogger; import com.android.inputmethod.research.ResearchLogger;
@ -43,6 +44,9 @@ public class PointerTracker implements PointerTrackerQueue.Element {
/** True if {@link PointerTracker}s should handle gesture events. */ /** True if {@link PointerTracker}s should handle gesture events. */
private static boolean sShouldHandleGesture = false; private static boolean sShouldHandleGesture = false;
private static boolean sMainDictionaryAvailable = false;
private static boolean sGestureHandlingEnabledByInputField = false;
private static boolean sGestureHandlingEnabledByUser = false;
private static final int MIN_GESTURE_RECOGNITION_TIME = 100; // msec private static final int MIN_GESTURE_RECOGNITION_TIME = 100; // msec
@ -198,7 +202,6 @@ public class PointerTracker implements PointerTrackerQueue.Element {
sNeedsPhantomSuddenMoveEventHack = needsPhantomSuddenMoveEventHack; sNeedsPhantomSuddenMoveEventHack = needsPhantomSuddenMoveEventHack;
setParameters(MainKeyboardView.PointerTrackerParams.DEFAULT); setParameters(MainKeyboardView.PointerTrackerParams.DEFAULT);
updateGestureHandlingMode(null, false /* shouldHandleGesture */);
} }
public static void setParameters(MainKeyboardView.PointerTrackerParams params) { public static void setParameters(MainKeyboardView.PointerTrackerParams params) {
@ -207,14 +210,22 @@ public class PointerTracker implements PointerTrackerQueue.Element {
params.mTouchNoiseThresholdDistance * params.mTouchNoiseThresholdDistance); params.mTouchNoiseThresholdDistance * params.mTouchNoiseThresholdDistance);
} }
private static void updateGestureHandlingMode(Keyboard keyboard, boolean shouldHandleGesture) { private static void updateGestureHandlingMode() {
if (!shouldHandleGesture sShouldHandleGesture = sMainDictionaryAvailable
|| AccessibilityUtils.getInstance().isTouchExplorationEnabled() && sGestureHandlingEnabledByInputField
|| (keyboard != null && keyboard.mId.passwordInput())) { && sGestureHandlingEnabledByUser
sShouldHandleGesture = false; && !AccessibilityUtils.getInstance().isTouchExplorationEnabled();
} else { }
sShouldHandleGesture = true;
} // Note that this method is called from a non-UI thread.
public static void setMainDictionaryAvailability(boolean mainDictionaryAvailable) {
sMainDictionaryAvailable = mainDictionaryAvailable;
updateGestureHandlingMode();
}
public static void setGestureHandlingEnabledByUser(boolean gestureHandlingEnabledByUser) {
sGestureHandlingEnabledByUser = gestureHandlingEnabledByUser;
updateGestureHandlingMode();
} }
public static PointerTracker getPointerTracker(final int id, KeyEventHandler handler) { public static PointerTracker getPointerTracker(final int id, KeyEventHandler handler) {
@ -241,7 +252,7 @@ public class PointerTracker implements PointerTrackerQueue.Element {
} }
} }
public static void setKeyDetector(KeyDetector keyDetector, boolean shouldHandleGesture) { public static void setKeyDetector(KeyDetector keyDetector) {
final int trackersSize = sTrackers.size(); final int trackersSize = sTrackers.size();
for (int i = 0; i < trackersSize; ++i) { for (int i = 0; i < trackersSize; ++i) {
final PointerTracker tracker = sTrackers.get(i); final PointerTracker tracker = sTrackers.get(i);
@ -250,7 +261,8 @@ public class PointerTracker implements PointerTrackerQueue.Element {
tracker.mKeyboardLayoutHasBeenChanged = true; tracker.mKeyboardLayoutHasBeenChanged = true;
} }
final Keyboard keyboard = keyDetector.getKeyboard(); final Keyboard keyboard = keyDetector.getKeyboard();
updateGestureHandlingMode(keyboard, shouldHandleGesture); sGestureHandlingEnabledByInputField = !keyboard.mId.passwordInput();
updateGestureHandlingMode();
} }
public static void dismissAllKeyPreviews() { public static void dismissAllKeyPreviews() {

View File

@ -433,10 +433,14 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
resetContactsDictionary(null == mSuggest ? null : mSuggest.getContactsDictionary()); resetContactsDictionary(null == mSuggest ? null : mSuggest.getContactsDictionary());
} }
// Note that this method is called from a non-UI thread.
@Override @Override
public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable) { public void onUpdateMainDictionaryAvailability(boolean isMainDictionaryAvailable) {
mIsMainDictionaryAvailable = isMainDictionaryAvailable; mIsMainDictionaryAvailable = isMainDictionaryAvailable;
updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability(); final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView();
if (mainKeyboardView != null) {
mainKeyboardView.setMainDictionaryAvailability(isMainDictionaryAvailable);
}
} }
private void initSuggest() { private void initSuggest() {
@ -701,7 +705,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
} }
switcher.loadKeyboard(editorInfo, mCurrentSettings); switcher.loadKeyboard(editorInfo, mCurrentSettings);
updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability();
} }
setSuggestionStripShownInternal( setSuggestionStripShownInternal(
isSuggestionsStripVisible(), /* needsInputViewShown */ false); isSuggestionsStripVisible(), /* needsInputViewShown */ false);
@ -721,6 +724,9 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mainKeyboardView.setKeyPreviewPopupEnabled(mCurrentSettings.mKeyPreviewPopupOn, mainKeyboardView.setKeyPreviewPopupEnabled(mCurrentSettings.mKeyPreviewPopupOn,
mCurrentSettings.mKeyPreviewPopupDismissDelay); mCurrentSettings.mKeyPreviewPopupDismissDelay);
mainKeyboardView.setGestureHandlingEnabledByUser(mCurrentSettings.mGestureInputEnabled);
mainKeyboardView.setGesturePreviewMode(mCurrentSettings.mGesturePreviewTrailEnabled,
mCurrentSettings.mGestureFloatingPreviewTextEnabled);
if (TRACE) Debug.startMethodTracing("/data/trace/latinime"); if (TRACE) Debug.startMethodTracing("/data/trace/latinime");
} }
@ -2103,7 +2109,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
if (mKeyboardSwitcher.getMainKeyboardView() != null) { if (mKeyboardSwitcher.getMainKeyboardView() != null) {
// Reload keyboard because the current language has been changed. // Reload keyboard because the current language has been changed.
mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(), mCurrentSettings); mKeyboardSwitcher.loadKeyboard(getCurrentInputEditorInfo(), mCurrentSettings);
updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability();
} }
// Since we just changed languages, we should re-evaluate suggestions with whatever word // Since we just changed languages, we should re-evaluate suggestions with whatever word
// we are currently composing. If we are not composing anything, we may want to display // we are currently composing. If we are not composing anything, we may want to display
@ -2111,17 +2116,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
mHandler.postUpdateSuggestionStrip(); mHandler.postUpdateSuggestionStrip();
} }
private void updateKeyboardViewGestureHandlingModeByMainDictionaryAvailability() {
final MainKeyboardView mainKeyboardView = mKeyboardSwitcher.getMainKeyboardView();
if (mainKeyboardView != null) {
final boolean shouldHandleGesture = mCurrentSettings.mGestureInputEnabled
&& mIsMainDictionaryAvailable;
mainKeyboardView.setGestureHandlingMode(shouldHandleGesture,
mCurrentSettings.mGesturePreviewTrailEnabled,
mCurrentSettings.mGestureFloatingPreviewTextEnabled);
}
}
// TODO: Remove this method from {@link LatinIME} and move {@link FeedbackManager} to // TODO: Remove this method from {@link LatinIME} and move {@link FeedbackManager} to
// {@link KeyboardSwitcher}. Called from KeyboardSwitcher // {@link KeyboardSwitcher}. Called from KeyboardSwitcher
public void hapticAndAudioFeedback(final int primaryCode) { public void hapticAndAudioFeedback(final int primaryCode) {