@@ -3019,24 +3019,23 @@ private boolean enterTouchMode() {
30193019 // be when the window is first being added, and mFocused isn't
30203020 // set yet.
30213021 final View focused = mView .findFocus ();
3022- if (focused != null ) {
3023- if (focused .isFocusableInTouchMode ()) {
3024- return true ;
3025- }
3022+ if (focused != null && !focused .isFocusableInTouchMode ()) {
3023+
30263024 final ViewGroup ancestorToTakeFocus =
30273025 findAncestorToTakeFocusInTouchMode (focused );
30283026 if (ancestorToTakeFocus != null ) {
30293027 // there is an ancestor that wants focus after its descendants that
30303028 // is focusable in touch mode.. give it focus
30313029 return ancestorToTakeFocus .requestFocus ();
3030+ } else {
3031+ // nothing appropriate to have focus in touch mode, clear it out
3032+ mView .unFocus ();
3033+ mAttachInfo .mTreeObserver .dispatchOnGlobalFocusChange (focused , null );
3034+ mFocusedView = null ;
3035+ mOldFocusedView = null ;
3036+ return true ;
30323037 }
30333038 }
3034- // nothing appropriate to have focus in touch mode, clear it out
3035- mView .unFocus ();
3036- mAttachInfo .mTreeObserver .dispatchOnGlobalFocusChange (focused , null );
3037- mFocusedView = null ;
3038- mOldFocusedView = null ;
3039- return true ;
30403039 }
30413040 }
30423041 return false ;
@@ -3067,45 +3066,25 @@ private static ViewGroup findAncestorToTakeFocusInTouchMode(View focused) {
30673066
30683067 private boolean leaveTouchMode () {
30693068 if (mView != null ) {
3070- boolean inputFocusValid = false ;
30713069 if (mView .hasFocus ()) {
30723070 // i learned the hard way to not trust mFocusedView :)
30733071 mFocusedView = mView .findFocus ();
30743072 if (!(mFocusedView instanceof ViewGroup )) {
30753073 // some view has focus, let it keep it
3076- inputFocusValid = true ;
3077- } else if (((ViewGroup ) mFocusedView ).getDescendantFocusability () !=
3074+ return false ;
3075+ } else if (((ViewGroup )mFocusedView ).getDescendantFocusability () !=
30783076 ViewGroup .FOCUS_AFTER_DESCENDANTS ) {
30793077 // some view group has focus, and doesn't prefer its children
30803078 // over itself for focus, so let them keep it.
3081- inputFocusValid = true ;
3079+ return false ;
30823080 }
30833081 }
3084- // In accessibility mode we always have a view that has the
3085- // accessibility focus and input focus follows it, i.e. we
3086- // try to give input focus to the accessibility focused view.
3087- if (!AccessibilityManager .getInstance (mView .mContext ).isEnabled ()) {
3088- // If the current input focus is not valid, find the best view to give
3089- // focus to in this brave new non-touch-mode world.
3090- if (!inputFocusValid ) {
3091- final View focused = focusSearch (null , View .FOCUS_DOWN );
3092- if (focused != null ) {
3093- return focused .requestFocus (View .FOCUS_DOWN );
3094- }
3095- }
3096- } else {
3097- // If the current input focus is not valid clear it but do not
3098- // give it to another view since the accessibility focus is
3099- // leading now and the input one follows.
3100- if (!inputFocusValid ) {
3101- if (mFocusedView != null ) {
3102- mView .unFocus ();
3103- mAttachInfo .mTreeObserver .dispatchOnGlobalFocusChange (mFocusedView , null );
3104- mFocusedView = null ;
3105- mOldFocusedView = null ;
3106- return true ;
3107- }
3108- }
3082+
3083+ // find the best view to give focus to in this brave new non-touch-mode
3084+ // world
3085+ final View focused = focusSearch (null , View .FOCUS_DOWN );
3086+ if (focused != null ) {
3087+ return focused .requestFocus (View .FOCUS_DOWN );
31093088 }
31103089 }
31113090 return false ;
0 commit comments