1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.car.rotaryplayground; 18 19 import android.graphics.drawable.Drawable; 20 import android.os.Bundle; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.Button; 25 26 import androidx.annotation.Nullable; 27 import androidx.fragment.app.Fragment; 28 29 import com.android.car.ui.FocusArea; 30 31 /** Fragment to demo a layout with cards that are FocusArea containers. */ 32 public class RotaryCards extends Fragment { 33 34 @Override onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)35 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 36 @Nullable Bundle savedInstanceState) { 37 View view = inflater.inflate(R.layout.rotary_cards, container, false); 38 39 // This button will be removed and immediately added back after click. So the focus 40 // highlight will jump to another view after rotary click. 41 FocusArea focusArea5 = view.findViewById(R.id.focus_area5); 42 Button button5a = view.findViewById(R.id.button_5a); 43 button5a.setOnClickListener(v -> { 44 int index = focusArea5.indexOfChild(button5a); 45 focusArea5.removeView(button5a); 46 focusArea5.addView(button5a, index); 47 }); 48 49 // The background of this button will be changed after click. The focus highlight will stay 50 // on this Button after click. 51 Button button5b = view.findViewById(R.id.button_5b); 52 boolean[] stopped = {false}; 53 button5b.setOnClickListener(v -> { 54 stopped[0] = !stopped[0]; 55 Drawable drawable = view.getContext().getDrawable( 56 stopped[0] ? R.drawable.ic_stop : R.drawable.ic_play_arrow); 57 button5b.setBackground(drawable); 58 }); 59 60 // This button will be removed and immediately added back after click. It's not focusable 61 // but its container is focusable. The focus highlight will stay on the container after 62 // click. 63 ViewGroup button5cContainer = view.findViewById(R.id.button_5c_container); 64 Button button5c = view.findViewById(R.id.button_5c); 65 button5c.setFocusable(false); 66 button5cContainer.setOnClickListener(v -> { 67 button5cContainer.removeView(button5c); 68 button5cContainer.addView(button5c, 0); 69 }); 70 71 // This button will be removed then added back and request focus explicitly after click. 72 // So the focus highlight will jump to another view then jump back after rotary click. 73 Button button5d = view.findViewById(R.id.button_5d); 74 button5d.setOnClickListener(v -> { 75 boolean needRestoreFocus = button5d.isFocused(); 76 int index = focusArea5.indexOfChild(button5d); 77 focusArea5.removeView(button5d); 78 focusArea5.addView(button5d, index); 79 if (needRestoreFocus) { 80 button5d.requestFocus(); 81 } 82 }); 83 84 // This button will be disabled after click. So the focus highlight will jump to another 85 // view after rotary click. 86 Button button5e = view.findViewById(R.id.button_5e); 87 button5e.setOnClickListener(v -> button5e.setEnabled(!button5e.isEnabled())); 88 89 // This button will appear disabled but is not disabled after click. So the focus 90 // highlight will stay after rotary click. 91 CustomButton button5f = view.findViewById(R.id.button_5f); 92 button5f.setOnClickListener(v -> button5f.setRotaryEnabled(!button5f.isRotaryEnabled())); 93 94 return view; 95 } 96 } 97