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