diff options
Diffstat (limited to 'RotaryPlayground/src/com/android/car/rotaryplayground/DirectManipulationState.java')
-rw-r--r-- | RotaryPlayground/src/com/android/car/rotaryplayground/DirectManipulationState.java | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/DirectManipulationState.java b/RotaryPlayground/src/com/android/car/rotaryplayground/DirectManipulationState.java index 05d236b..a802c71 100644 --- a/RotaryPlayground/src/com/android/car/rotaryplayground/DirectManipulationState.java +++ b/RotaryPlayground/src/com/android/car/rotaryplayground/DirectManipulationState.java @@ -16,7 +16,10 @@ package com.android.car.rotaryplayground; +import static android.view.ViewGroup.FOCUS_AFTER_DESCENDANTS; + import android.graphics.Color; +import android.graphics.drawable.Drawable; import android.view.View; import android.view.ViewGroup; @@ -34,19 +37,20 @@ import com.android.car.ui.utils.DirectManipulationHelper; */ public class DirectManipulationState { - /** Background color of a view when it's in direct manipulation mode. */ private static final int BACKGROUND_COLOR_IN_DIRECT_MANIPULATION_MODE = Color.BLUE; - /** Background color of a view when it's not in direct manipulation mode. */ - private static final int BACKGROUND_COLOR_NOT_IN_DIRECT_MANIPULATION_MODE = Color.TRANSPARENT; + /** Indicates that the descendant focusability has not been set. */ + private static final int UNKNOWN_DESCENDANT_FOCUSABILITY = -1; /** The view that is in direct manipulation mode, or null if none. */ - @Nullable private View mViewInDirectManipulationMode; - - private void setStartingView(@Nullable View view) { - mViewInDirectManipulationMode = view; - } + @Nullable + private View mViewInDirectManipulationMode; + /** The original background of the view in direct manipulation mode. */ + @Nullable + private Drawable mOriginalBackground; + /** The original descendant focusability value of the view in direct manipulation mode. */ + private int mOriginalDescendantFocusability = UNKNOWN_DESCENDANT_FOCUSABILITY; /** * Returns true if Direct Manipulation mode is active, false otherwise. @@ -62,19 +66,18 @@ public class DirectManipulationState { * We generally want to give some kind of visual indication that this change has happened. In * this example we change the background color of {@code view}. * - * @param view - the {@link View} from which we entered into Direct Manipulation mode. + * @param view the {@link View} from which we entered into Direct Manipulation mode */ public void enable(@NonNull View view) { - /* - * A more robust approach would be to fetch the current background color from - * the view object and store it back onto the View itself using the {@link - * View#setTag(int, java.lang.Object)} API. This could then be fetched back - * and used to restore the background color without needing to keep a constant - * reference to the color here which could fall out of sync with the xml files. - */ + mViewInDirectManipulationMode = view; + mOriginalBackground = view.getBackground(); + if (mViewInDirectManipulationMode instanceof ViewGroup) { + ViewGroup viewGroup = (ViewGroup) mViewInDirectManipulationMode; + mOriginalDescendantFocusability = viewGroup.getDescendantFocusability(); + viewGroup.setDescendantFocusability(FOCUS_AFTER_DESCENDANTS); + } view.setBackgroundColor(BACKGROUND_COLOR_IN_DIRECT_MANIPULATION_MODE); DirectManipulationHelper.enableDirectManipulationMode(view, /* enable= */ true); - setStartingView(view); } /** @@ -82,17 +85,18 @@ public class DirectManipulationState { * from which we entered into Direct Manipulation mode. */ public void disable() { - mViewInDirectManipulationMode.setBackgroundColor( - BACKGROUND_COLOR_NOT_IN_DIRECT_MANIPULATION_MODE); + mViewInDirectManipulationMode.setBackground(mOriginalBackground); DirectManipulationHelper.enableDirectManipulationMode( mViewInDirectManipulationMode, /* enable= */ false); - // For ViewGroup objects, restore descendant focusability to FOCUS_BLOCK_DESCENDANTS so - // during non-Direct Manipulation mode, aka, general rotary navigation, we don't go - // through the individual inner UI elements. - if (mViewInDirectManipulationMode instanceof ViewGroup) { + // For ViewGroup objects, restore descendant focusability to the previous value. + if (mViewInDirectManipulationMode instanceof ViewGroup + && mOriginalDescendantFocusability != UNKNOWN_DESCENDANT_FOCUSABILITY) { ViewGroup viewGroup = (ViewGroup) mViewInDirectManipulationMode; viewGroup.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS); } - setStartingView(null); + + mViewInDirectManipulationMode = null; + mOriginalBackground = null; + mOriginalDescendantFocusability = UNKNOWN_DESCENDANT_FOCUSABILITY; } } |