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;
35     private Fragment mRotaryGrid;
36     private Fragment mDirectManipulation;
37     private Fragment mSysUiDirectManipulation;
38     private Fragment mNotificationFragment;
39     private Fragment mScrollFragment;
40     private Fragment mWebViewFragment;
41     private Fragment mCustomFocusAreasFragment;
42     private Fragment mPopupWindowFragment;
43     private Fragment mSurfaceViewFragment;
44 
45     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)46     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
47             @Nullable Bundle savedInstanceState) {
48         View view = inflater.inflate(R.layout.rotary_menu, container, false);
49 
50         Button cardButton = view.findViewById(R.id.cards);
51         cardButton.setOnClickListener(v -> {
52             selectTab(v);
53             showRotaryCards();
54         });
55 
56         Button gridButton = view.findViewById(R.id.grid);
57         gridButton.setOnClickListener(v -> {
58             selectTab(v);
59             showGridExample();
60         });
61 
62         Button directManipulationButton = view.findViewById(R.id.direct_manipulation);
63         directManipulationButton.setOnClickListener(v -> {
64             selectTab(v);
65             showDirectManipulationExamples();
66         });
67 
68         Button sysUiDirectManipulationButton = view.findViewById(R.id.sys_ui_direct_manipulation);
69         sysUiDirectManipulationButton.setOnClickListener(v -> {
70             selectTab(v);
71             showSysUiDirectManipulationExamples();
72         });
73 
74         Button notificationButton = view.findViewById(R.id.notification);
75         notificationButton.setOnClickListener(v -> {
76             selectTab(v);
77             showNotificationExample();
78         });
79 
80         Button scrollButton = view.findViewById(R.id.scroll);
81         scrollButton.setOnClickListener(v -> {
82             selectTab(v);
83             showScrollFragment();
84         });
85 
86         Button webViewButton = view.findViewById(R.id.web_view);
87         webViewButton.setOnClickListener(v -> {
88             selectTab(v);
89             showWebViewFragment();
90         });
91 
92         Button customFocusAreasButton = view.findViewById(R.id.custom_focus_areas);
93         customFocusAreasButton.setOnClickListener(v -> {
94             selectTab(v);
95             showCustomFocusAreasFragment();
96         });
97 
98         Button popupWindowButton = view.findViewById(R.id.popup_window);
99         popupWindowButton.setOnClickListener(v -> {
100             selectTab(v);
101             showPopupWindowFragment();
102         });
103 
104         Button surfaceViewButton = view.findViewById(R.id.surface_view);
105         surfaceViewButton.setOnClickListener(v -> {
106             selectTab(v);
107             showSurfaceViewFragment();
108         });
109 
110         return view;
111     }
112 
selectTab(View view)113     private void selectTab(View view) {
114         ViewGroup container = (ViewGroup) view.getParent();
115         for (int i = 0; i < container.getChildCount(); i++) {
116             container.getChildAt(i).setSelected(false);
117         }
118         view.setSelected(true);
119     }
120 
showRotaryCards()121     private void showRotaryCards() {
122         if (mRotaryCards == null) {
123             mRotaryCards = new RotaryCards();
124         }
125         showFragment(mRotaryCards);
126     }
127 
showGridExample()128     private void showGridExample() {
129         if (mRotaryGrid == null) {
130             mRotaryGrid = new RotaryGrid();
131         }
132         showFragment(mRotaryGrid);
133     }
134 
135     // TODO(agathaman): refactor this and the showRotaryCards above into a
136     //  showFragment(Fragment fragment, boolean hasFocus); method.
showDirectManipulationExamples()137     private void showDirectManipulationExamples() {
138         if (mDirectManipulation == null) {
139             mDirectManipulation = new RotaryDirectManipulationWidgets();
140         }
141         showFragment(mDirectManipulation);
142     }
143 
showSysUiDirectManipulationExamples()144     private void showSysUiDirectManipulationExamples() {
145         if (mSysUiDirectManipulation == null) {
146             mSysUiDirectManipulation = new RotarySysUiDirectManipulationWidgets();
147         }
148         showFragment(mSysUiDirectManipulation);
149     }
150 
showNotificationExample()151     private void showNotificationExample() {
152         if (mNotificationFragment == null) {
153             mNotificationFragment = new HeadsUpNotificationFragment();
154         }
155         showFragment(mNotificationFragment);
156     }
157 
showScrollFragment()158     private void showScrollFragment() {
159         if (mScrollFragment == null) {
160             mScrollFragment = new ScrollFragment();
161         }
162         showFragment(mScrollFragment);
163     }
164 
showWebViewFragment()165     private void showWebViewFragment() {
166         if (mWebViewFragment == null) {
167             mWebViewFragment = new WebViewFragment();
168         }
169         showFragment(mWebViewFragment);
170     }
171 
showCustomFocusAreasFragment()172     private void showCustomFocusAreasFragment() {
173         if (mCustomFocusAreasFragment == null) {
174             mCustomFocusAreasFragment = new CustomFocusAreasFragment();
175         }
176         showFragment(mCustomFocusAreasFragment);
177     }
178 
showPopupWindowFragment()179     private void showPopupWindowFragment() {
180         if (mPopupWindowFragment == null) {
181             mPopupWindowFragment = new PopupWindowFragment();
182         }
183         showFragment(mPopupWindowFragment);
184     }
185 
showSurfaceViewFragment()186     private void showSurfaceViewFragment() {
187         if (mSurfaceViewFragment == null) {
188             mSurfaceViewFragment = new SurfaceViewFragment();
189         }
190         showFragment(mSurfaceViewFragment);
191     }
192 
showFragment(Fragment fragment)193     private void showFragment(Fragment fragment) {
194         getFragmentManager().beginTransaction()
195                 .replace(R.id.rotary_content, fragment)
196                 .commit();
197     }
198 }
199