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 package com.android.car.rotaryplayground;
17 
18 import android.os.Bundle;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22 import android.widget.Button;
23 
24 import androidx.annotation.Nullable;
25 import androidx.fragment.app.Fragment;
26 
27 /**
28  * Fragment for the menu.
29  *
30  * On focus of a menu item, the associated fragment will start in the R.id.rotary_content container.
31  */
32 public class RotaryMenu extends Fragment {
33 
34     private Fragment mRotaryCards = null;
35     private Fragment mRotaryGrid = null;
36     private Fragment mDirectManipulation = null;
37     private Fragment mSysUiDirectManipulation = null;
38     private Fragment mNotificationFragment = null;
39     private Fragment mScrollFragment = null;
40 
41     private Button mCardButton;
42     private Button mGridButton;
43     private Button mDirectManipulationButton;
44     private Button mSysUiDirectManipulationButton;
45     private Button mNotificationButton;
46     private Button mScrollButton;
47 
48     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)49     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
50             @Nullable Bundle savedInstanceState) {
51         View view = inflater.inflate(R.layout.rotary_menu, container, false);
52 
53         mCardButton = view.findViewById(R.id.cards);
54         mCardButton.setOnFocusChangeListener((v, hasFocus) -> showRotaryCards(hasFocus));
55         mCardButton.setOnClickListener(v -> showRotaryCards(/* hasFocus= */ true));
56 
57         mGridButton = view.findViewById(R.id.grid);
58         mGridButton.setOnFocusChangeListener((v, hasFocus) -> showGridExample(hasFocus));
59         mGridButton.setOnClickListener(v -> showGridExample(/* hasFocus= */ true));
60 
61         mDirectManipulationButton = view.findViewById(R.id.direct_manipulation);
62         mDirectManipulationButton.setOnFocusChangeListener(
63                 (v, hasFocus) -> showDirectManipulationExamples(hasFocus));
64         mDirectManipulationButton.setOnClickListener(
65                 (v -> showDirectManipulationExamples(/* hasFocus= */ true)));
66 
67         mSysUiDirectManipulationButton = view.findViewById(R.id.sys_ui_direct_manipulation);
68         mSysUiDirectManipulationButton.setOnFocusChangeListener(
69                 (v, hasFocus) -> showSysUiDirectManipulationExamples(hasFocus));
70         mSysUiDirectManipulationButton.setOnClickListener(
71                 (v -> showSysUiDirectManipulationExamples(/* hasFocus= */ true)));
72 
73         mNotificationButton = view.findViewById(R.id.notification);
74         mNotificationButton.setOnFocusChangeListener(
75                 (v, hasFocus) -> showNotificationExample(hasFocus));
76         mNotificationButton.setOnClickListener(v -> showNotificationExample(/* hasFocus= */ true));
77 
78         mScrollButton = view.findViewById(R.id.scroll);
79         mScrollButton.setOnFocusChangeListener((v, hasFocus) -> showScrollFragment(hasFocus));
80         mScrollButton.setOnClickListener(v -> showScrollFragment(/* hasFocus= */ true));
81 
82         return view;
83     }
84 
showRotaryCards(boolean hasFocus)85     private void showRotaryCards(boolean hasFocus) {
86         if (!hasFocus) {
87             return; // Do nothing if no focus.
88         }
89         if (mRotaryCards == null) {
90             mRotaryCards = new RotaryCards();
91         }
92         showFragment(mRotaryCards);
93     }
94 
showGridExample(boolean hasFocus)95     private void showGridExample(boolean hasFocus) {
96         if (!hasFocus) {
97             return; // do nothing if no focus.
98         }
99         if (mRotaryGrid == null) {
100             mRotaryGrid = new RotaryGrid();
101         }
102         showFragment(mRotaryGrid);
103     }
104 
105     // TODO(agathaman): refactor this and the showRotaryCards above into a
106     //  showFragment(Fragment fragment, boolean hasFocus); method.
showDirectManipulationExamples(boolean hasFocus)107     private void showDirectManipulationExamples(boolean hasFocus) {
108         if (!hasFocus) {
109             return; // Do nothing if no focus.
110         }
111         if (mDirectManipulation == null) {
112             mDirectManipulation = new RotaryDirectManipulationWidgets();
113         }
114         showFragment(mDirectManipulation);
115     }
116 
showSysUiDirectManipulationExamples(boolean hasFocus)117     private void showSysUiDirectManipulationExamples(boolean hasFocus) {
118         if (!hasFocus) {
119             return; // Do nothing if no focus.
120         }
121         if (mSysUiDirectManipulation == null) {
122             mSysUiDirectManipulation = new RotarySysUiDirectManipulationWidgets();
123         }
124         showFragment(mSysUiDirectManipulation);
125     }
126 
showNotificationExample(boolean hasFocus)127     private void showNotificationExample(boolean hasFocus) {
128         if (!hasFocus) {
129             return; // do nothing if no focus.
130         }
131         if (mNotificationFragment == null) {
132             mNotificationFragment = new HeadsUpNotificationFragment();
133         }
134         showFragment(mNotificationFragment);
135     }
136 
showScrollFragment(boolean hasFocus)137     private void showScrollFragment(boolean hasFocus) {
138         if (!hasFocus) {
139             return; // Do nothing if no focus.
140         }
141         if (mScrollFragment == null) {
142             mScrollFragment = new ScrollFragment();
143         }
144         showFragment(mScrollFragment);
145     }
146 
showFragment(Fragment fragment)147     private void showFragment(Fragment fragment) {
148         getFragmentManager().beginTransaction()
149                 .replace(R.id.rotary_content, fragment)
150                 .commit();
151     }
152 }
153