1 /*
2  * Copyright (C) 2014 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.statusbar.policy;
18 
19 import android.annotation.NonNull;
20 import android.content.res.Resources;
21 import android.util.ArraySet;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.widget.FrameLayout;
25 
26 import com.android.systemui.R;
27 import com.android.systemui.statusbar.NotificationShadeDepthController;
28 import com.android.systemui.statusbar.phone.NotificationPanelViewController;
29 import com.android.systemui.statusbar.phone.NotificationShadeWindowView;
30 
31 import java.util.Objects;
32 import java.util.function.Consumer;
33 
34 /**
35  * Controls showing and hiding of the brightness mirror.
36  */
37 public class BrightnessMirrorController
38         implements CallbackController<BrightnessMirrorController.BrightnessMirrorListener> {
39 
40     private final NotificationShadeWindowView mStatusBarWindow;
41     private final Consumer<Boolean> mVisibilityCallback;
42     private final NotificationPanelViewController mNotificationPanel;
43     private final NotificationShadeDepthController mDepthController;
44     private final ArraySet<BrightnessMirrorListener> mBrightnessMirrorListeners = new ArraySet<>();
45     private final int[] mInt2Cache = new int[2];
46     private View mBrightnessMirror;
47 
BrightnessMirrorController(NotificationShadeWindowView statusBarWindow, NotificationPanelViewController notificationPanelViewController, NotificationShadeDepthController notificationShadeDepthController, @NonNull Consumer<Boolean> visibilityCallback)48     public BrightnessMirrorController(NotificationShadeWindowView statusBarWindow,
49             NotificationPanelViewController notificationPanelViewController,
50             NotificationShadeDepthController notificationShadeDepthController,
51             @NonNull Consumer<Boolean> visibilityCallback) {
52         mStatusBarWindow = statusBarWindow;
53         mBrightnessMirror = statusBarWindow.findViewById(R.id.brightness_mirror);
54         mNotificationPanel = notificationPanelViewController;
55         mDepthController = notificationShadeDepthController;
56         mNotificationPanel.setPanelAlphaEndAction(() -> {
57             mBrightnessMirror.setVisibility(View.INVISIBLE);
58         });
59         mVisibilityCallback = visibilityCallback;
60     }
61 
showMirror()62     public void showMirror() {
63         mBrightnessMirror.setVisibility(View.VISIBLE);
64         mVisibilityCallback.accept(true);
65         mNotificationPanel.setPanelAlpha(0, true /* animate */);
66         mDepthController.setBrightnessMirrorVisible(true);
67     }
68 
hideMirror()69     public void hideMirror() {
70         mVisibilityCallback.accept(false);
71         mNotificationPanel.setPanelAlpha(255, true /* animate */);
72         mDepthController.setBrightnessMirrorVisible(false);
73     }
74 
setLocation(View original)75     public void setLocation(View original) {
76         original.getLocationInWindow(mInt2Cache);
77 
78         // Original is slightly larger than the mirror, so make sure to use the center for the
79         // positioning.
80         int originalX = mInt2Cache[0] + original.getWidth() / 2;
81         int originalY = mInt2Cache[1] + original.getHeight() / 2;
82         mBrightnessMirror.setTranslationX(0);
83         mBrightnessMirror.setTranslationY(0);
84         mBrightnessMirror.getLocationInWindow(mInt2Cache);
85         int mirrorX = mInt2Cache[0] + mBrightnessMirror.getWidth() / 2;
86         int mirrorY = mInt2Cache[1] + mBrightnessMirror.getHeight() / 2;
87         mBrightnessMirror.setTranslationX(originalX - mirrorX);
88         mBrightnessMirror.setTranslationY(originalY - mirrorY);
89     }
90 
getMirror()91     public View getMirror() {
92         return mBrightnessMirror;
93     }
94 
updateResources()95     public void updateResources() {
96         FrameLayout.LayoutParams lp =
97                 (FrameLayout.LayoutParams) mBrightnessMirror.getLayoutParams();
98         Resources r = mBrightnessMirror.getResources();
99         lp.width = r.getDimensionPixelSize(R.dimen.qs_panel_width);
100         lp.height = r.getDimensionPixelSize(R.dimen.brightness_mirror_height);
101         lp.gravity = r.getInteger(R.integer.notification_panel_layout_gravity);
102         mBrightnessMirror.setLayoutParams(lp);
103     }
104 
onOverlayChanged()105     public void onOverlayChanged() {
106         reinflate();
107     }
108 
onDensityOrFontScaleChanged()109     public void onDensityOrFontScaleChanged() {
110         reinflate();
111     }
112 
reinflate()113     private void reinflate() {
114         int index = mStatusBarWindow.indexOfChild(mBrightnessMirror);
115         mStatusBarWindow.removeView(mBrightnessMirror);
116         mBrightnessMirror = LayoutInflater.from(mBrightnessMirror.getContext()).inflate(
117                 R.layout.brightness_mirror, mStatusBarWindow, false);
118         mStatusBarWindow.addView(mBrightnessMirror, index);
119 
120         for (int i = 0; i < mBrightnessMirrorListeners.size(); i++) {
121             mBrightnessMirrorListeners.valueAt(i).onBrightnessMirrorReinflated(mBrightnessMirror);
122         }
123     }
124 
125     @Override
addCallback(BrightnessMirrorListener listener)126     public void addCallback(BrightnessMirrorListener listener) {
127         Objects.requireNonNull(listener);
128         mBrightnessMirrorListeners.add(listener);
129     }
130 
131     @Override
removeCallback(BrightnessMirrorListener listener)132     public void removeCallback(BrightnessMirrorListener listener) {
133         mBrightnessMirrorListeners.remove(listener);
134     }
135 
onUiModeChanged()136     public void onUiModeChanged() {
137         reinflate();
138     }
139 
140     public interface BrightnessMirrorListener {
onBrightnessMirrorReinflated(View brightnessMirror)141         void onBrightnessMirrorReinflated(View brightnessMirror);
142     }
143 }
144