aboutsummaryrefslogtreecommitdiff
path: root/RotaryPlayground/src/com/android/car/rotaryplayground
diff options
context:
space:
mode:
authorYabin Huang <yabinh@google.com>2021-04-09 15:52:49 -0700
committerYabin Huang <yabinh@google.com>2021-04-16 01:13:53 +0000
commitc1eaa136602fd93106c38912a79b5279a831741e (patch)
tree49889043e37b5badcd2ba8f388be671fa15fc980 /RotaryPlayground/src/com/android/car/rotaryplayground
parent20022aae9a24b50c391daf0cda2c7f6516d6c329 (diff)
downloadtests-c1eaa136602fd93106c38912a79b5279a831741e.tar.gz
Demo updating a view without losing focus
Add a FocusArea to the Cards tab to demonstrate how to update a view properly. Fixes: 185377477 Test: manual Change-Id: I66038ef4677ee904246bf1dd16503fca4b27c9c1
Diffstat (limited to 'RotaryPlayground/src/com/android/car/rotaryplayground')
-rw-r--r--RotaryPlayground/src/com/android/car/rotaryplayground/CustomButton.java65
-rw-r--r--RotaryPlayground/src/com/android/car/rotaryplayground/RotaryCards.java61
2 files changed, 126 insertions, 0 deletions
diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/CustomButton.java b/RotaryPlayground/src/com/android/car/rotaryplayground/CustomButton.java
new file mode 100644
index 0000000..924908f
--- /dev/null
+++ b/RotaryPlayground/src/com/android/car/rotaryplayground/CustomButton.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2021 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.car.rotaryplayground;
+
+import android.content.Context;
+import android.util.AttributeSet;
+import android.widget.Button;
+
+import androidx.annotation.Nullable;
+
+public class CustomButton extends Button {
+
+ private static final int[] STATE_ROTARY_ENABLED = {R.attr.state_rotary_enabled};
+
+ private boolean mRotaryEnabled = true;
+
+ public CustomButton(Context context) {
+ super(context);
+ }
+
+ public CustomButton(Context context, @Nullable AttributeSet attrs) {
+ super(context, attrs);
+ }
+
+ public CustomButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
+ super(context, attrs, defStyleAttr);
+ }
+
+ public CustomButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
+ int defStyleRes) {
+ super(context, attrs, defStyleAttr, defStyleRes);
+ }
+
+ public void setRotaryEnabled(boolean enabled) {
+ mRotaryEnabled = enabled;
+ refreshDrawableState();
+ }
+
+ public boolean isRotaryEnabled() {
+ return mRotaryEnabled;
+ }
+
+ @Override
+ protected int[] onCreateDrawableState(int extraSpace) {
+ final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
+ if (mRotaryEnabled) {
+ mergeDrawableStates(drawableState, STATE_ROTARY_ENABLED);
+ }
+ return drawableState;
+ }
+}
diff --git a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryCards.java b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryCards.java
index 28f34a1..730c7c5 100644
--- a/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryCards.java
+++ b/RotaryPlayground/src/com/android/car/rotaryplayground/RotaryCards.java
@@ -16,13 +16,18 @@
package com.android.car.rotaryplayground;
+import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
+import android.widget.Button;
+
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
+import com.android.car.ui.FocusArea;
+
/** Fragment to demo a layout with cards that are FocusArea containers. */
public class RotaryCards extends Fragment {
@@ -30,6 +35,62 @@ public class RotaryCards extends Fragment {
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.rotary_cards, container, false);
+
+ // This button will be removed and immediately added back after click. So the focus
+ // highlight will jump to another view after rotary click.
+ FocusArea focusArea5 = view.findViewById(R.id.focus_area5);
+ Button button5a = view.findViewById(R.id.button_5a);
+ button5a.setOnClickListener(v -> {
+ int index = focusArea5.indexOfChild(button5a);
+ focusArea5.removeView(button5a);
+ focusArea5.addView(button5a, index);
+ });
+
+ // The background of this button will be changed after click. The focus highlight will stay
+ // on this Button after click.
+ Button button5b = view.findViewById(R.id.button_5b);
+ boolean[] stopped = {false};
+ button5b.setOnClickListener(v -> {
+ stopped[0] = !stopped[0];
+ Drawable drawable = view.getContext().getDrawable(
+ stopped[0] ? R.drawable.ic_stop : R.drawable.ic_play_arrow);
+ button5b.setBackground(drawable);
+ });
+
+ // This button will be removed and immediately added back after click. It's not focusable
+ // but its container is focusable. The focus highlight will stay on the container after
+ // click.
+ ViewGroup button5cContainer = view.findViewById(R.id.button_5c_container);
+ Button button5c = view.findViewById(R.id.button_5c);
+ button5c.setFocusable(false);
+ button5cContainer.setOnClickListener(v -> {
+ button5cContainer.removeView(button5c);
+ button5cContainer.addView(button5c, 0);
+ });
+
+ // This button will be removed then added back and request focus explicitly after click.
+ // So the focus highlight will jump to another view then jump back after rotary click.
+ Button button5d = view.findViewById(R.id.button_5d);
+ button5d.setOnClickListener(v -> {
+ boolean needRestoreFocus = button5d.isFocused();
+ int index = focusArea5.indexOfChild(button5d);
+ focusArea5.removeView(button5d);
+ focusArea5.addView(button5d, index);
+ if (needRestoreFocus) {
+ button5d.requestFocus();
+ }
+ });
+
+ // This button will be disabled after click. So the focus highlight will jump to another
+ // view after rotary click.
+ Button button5e = view.findViewById(R.id.button_5e);
+ button5e.setOnClickListener(v -> button5e.setEnabled(!button5e.isEnabled()));
+
+ // This button will appear disabled but is not disabled after click. So the focus
+ // highlight will stay after rotary click.
+ CustomButton button5f = view.findViewById(R.id.button_5f);
+ button5f.setOnClickListener(v -> button5f.setRotaryEnabled(!button5f.isRotaryEnabled()));
+
return view;
}
}