• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.systemui.car.hvac;
18 
19 import android.app.UiModeManager;
20 import android.content.Context;
21 import android.content.res.Configuration;
22 import android.content.res.Resources;
23 import android.view.KeyEvent;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.view.WindowInsets;
28 
29 import com.android.systemui.R;
30 import com.android.systemui.car.CarDeviceProvisionedController;
31 import com.android.systemui.car.window.OverlayPanelViewController;
32 import com.android.systemui.car.window.OverlayViewGlobalStateController;
33 import com.android.systemui.dagger.SysUISingleton;
34 import com.android.systemui.dagger.qualifiers.Main;
35 import com.android.systemui.statusbar.policy.ConfigurationController;
36 import com.android.wm.shell.animation.FlingAnimationUtils;
37 
38 import javax.inject.Inject;
39 
40 @SysUISingleton
41 public class HvacPanelOverlayViewController extends OverlayPanelViewController implements
42         ConfigurationController.ConfigurationListener {
43 
44     private final Context mContext;
45     private final Resources mResources;
46     private final HvacController mHvacController;
47     private final UiModeManager mUiModeManager;
48 
49     private boolean mIsUiModeNight;
50 
51     private HvacPanelView mHvacPanelView;
52 
53     @Inject
HvacPanelOverlayViewController(Context context, @Main Resources resources, HvacController hvacController, OverlayViewGlobalStateController overlayViewGlobalStateController, FlingAnimationUtils.Builder flingAnimationUtilsBuilder, CarDeviceProvisionedController carDeviceProvisionedController, ConfigurationController configurationController, UiModeManager uiModeManager)54     public HvacPanelOverlayViewController(Context context,
55             @Main Resources resources,
56             HvacController hvacController,
57             OverlayViewGlobalStateController overlayViewGlobalStateController,
58             FlingAnimationUtils.Builder flingAnimationUtilsBuilder,
59             CarDeviceProvisionedController carDeviceProvisionedController,
60             ConfigurationController configurationController,
61             UiModeManager uiModeManager) {
62         super(context, resources, R.id.hvac_panel_stub, overlayViewGlobalStateController,
63                 flingAnimationUtilsBuilder, carDeviceProvisionedController);
64         mContext = context;
65         mResources = resources;
66         mHvacController = hvacController;
67         mUiModeManager = uiModeManager;
68         configurationController.addCallback(this);
69     }
70 
71     @Override
onFinishInflate()72     protected void onFinishInflate() {
73         super.onFinishInflate();
74 
75         View closeButton = getLayout().findViewById(R.id.hvac_panel_close_button);
76         if (closeButton != null) {
77             closeButton.setOnClickListener(v -> toggle());
78         }
79 
80         mHvacPanelView = getLayout().findViewById(R.id.hvac_panel);
81         mHvacController.registerHvacViews(mHvacPanelView);
82 
83         mHvacPanelView.setKeyEventHandler((event) -> {
84             if (event.getKeyCode() != KeyEvent.KEYCODE_BACK) {
85                 return false;
86             }
87 
88             if (event.getAction() == KeyEvent.ACTION_UP && isPanelExpanded()) {
89                 toggle();
90             }
91             return true;
92         });
93     }
94 
95     @Override
getInsetTypesToFit()96     protected int getInsetTypesToFit() {
97         return WindowInsets.Type.systemBars();
98     }
99 
100     @Override
shouldShowStatusBarInsets()101     protected boolean shouldShowStatusBarInsets() {
102         return true;
103     }
104 
105     @Override
shouldShowNavigationBarInsets()106     protected boolean shouldShowNavigationBarInsets() {
107         return true;
108     }
109 
110     @Override
shouldAnimateCollapsePanel()111     protected boolean shouldAnimateCollapsePanel() {
112         return true;
113     }
114 
115     @Override
shouldAnimateExpandPanel()116     protected boolean shouldAnimateExpandPanel() {
117         return true;
118     }
119 
120     @Override
shouldAllowClosingScroll()121     protected boolean shouldAllowClosingScroll() {
122         return true;
123     }
124 
125     @Override
getHandleBarViewId()126     protected Integer getHandleBarViewId() {
127         return R.id.handle_bar;
128     }
129 
130     @Override
getFocusAreaViewId()131     protected int getFocusAreaViewId() {
132         return R.id.hvac_panel_container;
133     }
134 
135     @Override
getSettleClosePercentage()136     protected int getSettleClosePercentage() {
137         return mResources.getInteger(R.integer.hvac_panel_settle_close_percentage);
138     }
139 
140     @Override
onAnimateCollapsePanel()141     protected void onAnimateCollapsePanel() {
142         // no-op.
143     }
144 
145     @Override
onAnimateExpandPanel()146     protected void onAnimateExpandPanel() {
147         // no-op.
148     }
149 
150     @Override
onCollapseAnimationEnd()151     protected void onCollapseAnimationEnd() {
152         // no-op.
153     }
154 
155     @Override
onExpandAnimationEnd()156     protected void onExpandAnimationEnd() {
157         // no-op.
158     }
159 
160     @Override
onOpenScrollStart()161     protected void onOpenScrollStart() {
162         // no-op.
163     }
164 
165     @Override
onConfigChanged(Configuration newConfig)166     public void onConfigChanged(Configuration newConfig) {
167         boolean isConfigNightMode = (newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
168                 == Configuration.UI_MODE_NIGHT_YES;
169         // Only refresh UI on Night mode changes
170         if (isConfigNightMode != mIsUiModeNight) {
171             mIsUiModeNight = isConfigNightMode;
172             mUiModeManager.setNightModeActivated(mIsUiModeNight);
173 
174             if (getLayout() == null) return;
175             mHvacPanelView = getLayout().findViewById(R.id.hvac_panel);
176             if (mHvacPanelView == null) return;
177             ViewGroup hvacViewGroupParent = (ViewGroup) mHvacPanelView.getParent();
178 
179             // cache properties of {@link HvacPanelView}
180             int inflatedId = mHvacPanelView.getId();
181             ViewGroup.LayoutParams layoutParams = mHvacPanelView.getLayoutParams();
182             HvacPanelView.KeyEventHandler hvacKeyEventHandler = mHvacPanelView
183                     .getKeyEventHandler();
184             int indexOfView = hvacViewGroupParent.indexOfChild(mHvacPanelView);
185 
186             // remove {@link HvacPanelView} from its parent and reinflate it
187             hvacViewGroupParent.removeView(mHvacPanelView);
188             HvacPanelView newHvacPanelView = (HvacPanelView) LayoutInflater.from(mContext).inflate(
189                     R.layout.hvac_panel, /* root= */ hvacViewGroupParent,
190                     /* attachToRoot= */ false);
191             hvacViewGroupParent.addView(newHvacPanelView, indexOfView);
192             mHvacPanelView = newHvacPanelView;
193 
194             // reset {@link HvacPanelView} cached properties
195             mHvacPanelView.setId(inflatedId);
196             mHvacPanelView.setLayoutParams(layoutParams);
197             mHvacController.registerHvacViews(mHvacPanelView);
198             mHvacPanelView.setKeyEventHandler(hvacKeyEventHandler);
199         }
200     }
201 }
202