Merge "Make AccessibleKeyboardViewProxy not singleton"

main
Tadashi G. Takaoka 2014-05-01 03:29:30 +00:00 committed by Android (Google) Code Review
commit 3903e0d6e8
6 changed files with 27 additions and 86 deletions

View File

@ -68,7 +68,6 @@ public final class AccessibilityUtils {
// These only need to be initialized if the kill switch is off. // These only need to be initialized if the kill switch is off.
sInstance.initInternal(context); sInstance.initInternal(context);
KeyCodeDescriptionMapper.init(); KeyCodeDescriptionMapper.init();
AccessibleKeyboardViewProxy.init(context);
} }
public static AccessibilityUtils getInstance() { public static AccessibilityUtils getInstance() {

View File

@ -36,9 +36,7 @@ import com.android.inputmethod.keyboard.MainKeyboardView;
import com.android.inputmethod.latin.R; import com.android.inputmethod.latin.R;
import com.android.inputmethod.latin.utils.SubtypeLocaleUtils; import com.android.inputmethod.latin.utils.SubtypeLocaleUtils;
public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateCompat { public final class MainKeyboardAccessibilityDelegate extends AccessibilityDelegateCompat {
private static final AccessibleKeyboardViewProxy sInstance = new AccessibleKeyboardViewProxy();
/** Map of keyboard modes to resource IDs. */ /** Map of keyboard modes to resource IDs. */
private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray(); private static final SparseIntArray KEYBOARD_MODE_RES_IDS = new SparseIntArray();
@ -54,9 +52,9 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url); KEYBOARD_MODE_RES_IDS.put(KeyboardId.MODE_URL, R.string.keyboard_mode_url);
} }
private MainKeyboardView mView; private final MainKeyboardView mView;
private Keyboard mKeyboard; private Keyboard mKeyboard;
private AccessibilityEntityProvider mAccessibilityNodeProvider; private MainKeyboardAccessibilityNodeProvider mAccessibilityNodeProvider;
private Key mLastHoverKey = null; private Key mLastHoverKey = null;
@ -69,46 +67,14 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN; private int mLastKeyboardMode = KEYBOARD_IS_HIDDEN;
private static final int KEYBOARD_IS_HIDDEN = -1; private static final int KEYBOARD_IS_HIDDEN = -1;
public static void init(final Context context) { public MainKeyboardAccessibilityDelegate(final MainKeyboardView view) {
sInstance.initInternal(context); final Context context = view.getContext();
}
public static AccessibleKeyboardViewProxy getInstance() {
return sInstance;
}
private AccessibleKeyboardViewProxy() {
// Not publicly instantiable.
}
private void initInternal(final Context context) {
mEdgeSlop = context.getResources().getDimensionPixelSize( mEdgeSlop = context.getResources().getDimensionPixelSize(
R.dimen.config_accessibility_edge_slop); R.dimen.config_accessibility_edge_slop);
}
/**
* Sets the view wrapped by this proxy.
*
* @param view The view to wrap.
*/
public void setView(final MainKeyboardView view) {
if (view == null) {
// Ignore null views.
return;
}
mView = view; mView = view;
// Ensure that the view has an accessibility delegate. // Ensure that the view has an accessibility delegate.
ViewCompat.setAccessibilityDelegate(view, this); ViewCompat.setAccessibilityDelegate(view, this);
if (mAccessibilityNodeProvider == null) {
return;
}
mAccessibilityNodeProvider.setView(view);
// Since this class is constructed lazily, we might not get a subsequent
// call to setKeyboard() and therefore need to call it now.
setKeyboard(view.getKeyboard());
} }
/** /**
@ -156,9 +122,6 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
* Called when the keyboard is hidden and accessibility is enabled. * Called when the keyboard is hidden and accessibility is enabled.
*/ */
public void onHideWindow() { public void onHideWindow() {
if (mView == null) {
return;
}
announceKeyboardHidden(); announceKeyboardHidden();
mLastKeyboardMode = KEYBOARD_IS_HIDDEN; mLastKeyboardMode = KEYBOARD_IS_HIDDEN;
} }
@ -264,7 +227,7 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
} }
/** /**
* Proxy method for View.getAccessibilityNodeProvider(). This method is called in SDK * Delegate method for View.getAccessibilityNodeProvider(). This method is called in SDK
* version 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) and higher to obtain the virtual * version 15 (Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) and higher to obtain the virtual
* node hierarchy provider. * node hierarchy provider.
* *
@ -272,10 +235,7 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
* @return The accessibility node provider for the current keyboard. * @return The accessibility node provider for the current keyboard.
*/ */
@Override @Override
public AccessibilityEntityProvider getAccessibilityNodeProvider(final View host) { public MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider(final View host) {
if (mView == null) {
return null;
}
return getAccessibilityNodeProvider(); return getAccessibilityNodeProvider();
} }
@ -288,10 +248,6 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
* @return {@code true} if the event is handled * @return {@code true} if the event is handled
*/ */
public boolean dispatchHoverEvent(final MotionEvent event, final KeyDetector keyDetector) { public boolean dispatchHoverEvent(final MotionEvent event, final KeyDetector keyDetector) {
if (mView == null) {
return false;
}
final int x = (int) event.getX(); final int x = (int) event.getX();
final int y = (int) event.getY(); final int y = (int) event.getY();
final Key previousKey = mLastHoverKey; final Key previousKey = mLastHoverKey;
@ -325,14 +281,14 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
} }
/** /**
* @return A lazily-instantiated node provider for this view proxy. * @return A lazily-instantiated node provider for this view delegate.
*/ */
private AccessibilityEntityProvider getAccessibilityNodeProvider() { private MainKeyboardAccessibilityNodeProvider getAccessibilityNodeProvider() {
// Instantiate the provide only when requested. Since the system // Instantiate the provide only when requested. Since the system
// will call this method multiple times it is a good practice to // will call this method multiple times it is a good practice to
// cache the provider instance. // cache the provider instance.
if (mAccessibilityNodeProvider == null) { if (mAccessibilityNodeProvider == null) {
mAccessibilityNodeProvider = new AccessibilityEntityProvider(mView); mAccessibilityNodeProvider = new MainKeyboardAccessibilityNodeProvider(mView);
} }
return mAccessibilityNodeProvider; return mAccessibilityNodeProvider;
} }
@ -417,7 +373,7 @@ public final class AccessibleKeyboardViewProxy extends AccessibilityDelegateComp
if (key == null) { if (key == null) {
return false; return false;
} }
final AccessibilityEntityProvider provider = getAccessibilityNodeProvider(); final MainKeyboardAccessibilityNodeProvider provider = getAccessibilityNodeProvider();
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_HOVER_ENTER: case MotionEvent.ACTION_HOVER_ENTER:

View File

@ -47,8 +47,8 @@ import java.util.List;
* virtual views, thus conveying their logical structure. * virtual views, thus conveying their logical structure.
* </p> * </p>
*/ */
public final class AccessibilityEntityProvider extends AccessibilityNodeProviderCompat { public final class MainKeyboardAccessibilityNodeProvider extends AccessibilityNodeProviderCompat {
private static final String TAG = AccessibilityEntityProvider.class.getSimpleName(); private static final String TAG = MainKeyboardAccessibilityNodeProvider.class.getSimpleName();
private static final int UNDEFINED = Integer.MIN_VALUE; private static final int UNDEFINED = Integer.MIN_VALUE;
private final KeyCodeDescriptionMapper mKeyCodeDescriptionMapper; private final KeyCodeDescriptionMapper mKeyCodeDescriptionMapper;
@ -64,23 +64,14 @@ public final class AccessibilityEntityProvider extends AccessibilityNodeProvider
private int mAccessibilityFocusedView = UNDEFINED; private int mAccessibilityFocusedView = UNDEFINED;
/** The current keyboard view. */ /** The current keyboard view. */
private KeyboardView mKeyboardView; private final KeyboardView mKeyboardView;
/** The current keyboard. */ /** The current keyboard. */
private Keyboard mKeyboard; private Keyboard mKeyboard;
public AccessibilityEntityProvider(final KeyboardView keyboardView) { public MainKeyboardAccessibilityNodeProvider(final KeyboardView keyboardView) {
mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance(); mKeyCodeDescriptionMapper = KeyCodeDescriptionMapper.getInstance();
mAccessibilityUtils = AccessibilityUtils.getInstance(); mAccessibilityUtils = AccessibilityUtils.getInstance();
setView(keyboardView);
}
/**
* Sets the keyboard view represented by this node provider.
*
* @param keyboardView The keyboard view to represent.
*/
public void setView(final KeyboardView keyboardView) {
mKeyboardView = keyboardView; mKeyboardView = keyboardView;
updateParentLocation(); updateParentLocation();

View File

@ -26,7 +26,6 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.inputmethod.EditorInfo; import android.view.inputmethod.EditorInfo;
import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy;
import com.android.inputmethod.compat.InputMethodServiceCompatUtils; import com.android.inputmethod.compat.InputMethodServiceCompatUtils;
import com.android.inputmethod.keyboard.KeyboardLayoutSet.KeyboardLayoutSetException; import com.android.inputmethod.keyboard.KeyboardLayoutSet.KeyboardLayoutSetException;
import com.android.inputmethod.keyboard.internal.KeyboardState; import com.android.inputmethod.keyboard.internal.KeyboardState;
@ -148,6 +147,7 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
public void onHideWindow() { public void onHideWindow() {
mIsAutoCorrectionActive = false; mIsAutoCorrectionActive = false;
mKeyboardView.onHideWindow();
} }
private void setKeyboard(final Keyboard keyboard) { private void setKeyboard(final Keyboard keyboard) {
@ -353,11 +353,6 @@ public final class KeyboardSwitcher implements KeyboardState.SwitchActions {
mEmojiPalettesView.setHardwareAcceleratedDrawingEnabled( mEmojiPalettesView.setHardwareAcceleratedDrawingEnabled(
isHardwareAcceleratedDrawingEnabled); isHardwareAcceleratedDrawingEnabled);
mEmojiPalettesView.setKeyboardActionListener(mLatinIME); mEmojiPalettesView.setKeyboardActionListener(mLatinIME);
// This always needs to be set since the accessibility state can
// potentially change without the input view being re-created.
AccessibleKeyboardViewProxy.getInstance().setView(mKeyboardView);
return mCurrentInputView; return mCurrentInputView;
} }

View File

@ -39,7 +39,7 @@ import android.view.inputmethod.InputMethodSubtype;
import android.widget.TextView; import android.widget.TextView;
import com.android.inputmethod.accessibility.AccessibilityUtils; import com.android.inputmethod.accessibility.AccessibilityUtils;
import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy; import com.android.inputmethod.accessibility.MainKeyboardAccessibilityDelegate;
import com.android.inputmethod.annotations.ExternallyReferenced; import com.android.inputmethod.annotations.ExternallyReferenced;
import com.android.inputmethod.keyboard.internal.DrawingHandler; import com.android.inputmethod.keyboard.internal.DrawingHandler;
import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView; import com.android.inputmethod.keyboard.internal.DrawingPreviewPlacerView;
@ -179,6 +179,8 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
private final DrawingHandler mDrawingHandler = private final DrawingHandler mDrawingHandler =
new DrawingHandler(this); new DrawingHandler(this);
private final MainKeyboardAccessibilityDelegate mAccessibilityDelegate;
public MainKeyboardView(final Context context, final AttributeSet attrs) { public MainKeyboardView(final Context context, final AttributeSet attrs) {
this(context, attrs, R.attr.mainKeyboardViewStyle); this(context, attrs, R.attr.mainKeyboardViewStyle);
} }
@ -278,6 +280,8 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
mLanguageOnSpacebarHorizontalMargin = (int)getResources().getDimension( mLanguageOnSpacebarHorizontalMargin = (int)getResources().getDimension(
R.dimen.config_language_on_spacebar_horizontal_margin); R.dimen.config_language_on_spacebar_horizontal_margin);
mAccessibilityDelegate = new MainKeyboardAccessibilityDelegate(this);
} }
@Override @Override
@ -404,9 +408,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
ResearchLogger.mainKeyboardView_setKeyboard(keyboard, orientation); ResearchLogger.mainKeyboardView_setKeyboard(keyboard, orientation);
} }
// This always needs to be set since the accessibility state can mAccessibilityDelegate.setKeyboard(keyboard);
// potentially change without the keyboard being set again.
AccessibleKeyboardViewProxy.getInstance().setKeyboard(keyboard);
} }
/** /**
@ -769,6 +771,10 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
mMoreKeysKeyboardCache.clear(); mMoreKeysKeyboardCache.clear();
} }
public void onHideWindow() {
mAccessibilityDelegate.onHideWindow();
}
/** /**
* Receives hover events from the input framework. * Receives hover events from the input framework.
* *
@ -779,8 +785,7 @@ public final class MainKeyboardView extends KeyboardView implements PointerTrack
@Override @Override
public boolean dispatchHoverEvent(final MotionEvent event) { public boolean dispatchHoverEvent(final MotionEvent event) {
if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) { if (AccessibilityUtils.getInstance().isTouchExplorationEnabled()) {
return AccessibleKeyboardViewProxy.getInstance().dispatchHoverEvent( return mAccessibilityDelegate.dispatchHoverEvent(event, mKeyDetector);
event, mKeyDetector);
} }
// Reflection doesn't support calling superclass methods. // Reflection doesn't support calling superclass methods.

View File

@ -55,7 +55,6 @@ import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodSubtype; import android.view.inputmethod.InputMethodSubtype;
import com.android.inputmethod.accessibility.AccessibilityUtils; import com.android.inputmethod.accessibility.AccessibilityUtils;
import com.android.inputmethod.accessibility.AccessibleKeyboardViewProxy;
import com.android.inputmethod.annotations.UsedForTesting; import com.android.inputmethod.annotations.UsedForTesting;
import com.android.inputmethod.compat.InputMethodServiceCompatUtils; import com.android.inputmethod.compat.InputMethodServiceCompatUtils;
import com.android.inputmethod.dictionarypack.DictionaryPackConstants; import com.android.inputmethod.dictionarypack.DictionaryPackConstants;
@ -1002,10 +1001,6 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
LatinImeLogger.commit(); LatinImeLogger.commit();
mKeyboardSwitcher.onHideWindow(); mKeyboardSwitcher.onHideWindow();
if (AccessibilityUtils.getInstance().isAccessibilityEnabled()) {
AccessibleKeyboardViewProxy.getInstance().onHideWindow();
}
if (TRACE) Debug.stopMethodTracing(); if (TRACE) Debug.stopMethodTracing();
if (isShowingOptionDialog()) { if (isShowingOptionDialog()) {
mOptionsDialog.dismiss(); mOptionsDialog.dismiss();