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 com.android.systemui.R;
20 import com.android.systemui.statusbar.ScrimView;
21 import com.android.systemui.statusbar.phone.PhoneStatusBar;
22 import com.android.systemui.statusbar.phone.StatusBarWindowView;
23 
24 import android.view.View;
25 import android.view.ViewPropertyAnimator;
26 import android.widget.FrameLayout;
27 
28 /**
29  * Controls showing and hiding of the brightness mirror.
30  */
31 public class BrightnessMirrorController {
32 
33     public long TRANSITION_DURATION_OUT = 150;
34     public long TRANSITION_DURATION_IN = 200;
35 
36     private final ScrimView mScrimBehind;
37     private final View mBrightnessMirror;
38     private final View mPanelHolder;
39     private final int[] mInt2Cache = new int[2];
40 
BrightnessMirrorController(StatusBarWindowView statusBarWindow)41     public BrightnessMirrorController(StatusBarWindowView statusBarWindow) {
42         mScrimBehind = (ScrimView) statusBarWindow.findViewById(R.id.scrim_behind);
43         mBrightnessMirror = statusBarWindow.findViewById(R.id.brightness_mirror);
44         mPanelHolder = statusBarWindow.findViewById(R.id.panel_holder);
45     }
46 
showMirror()47     public void showMirror() {
48         mBrightnessMirror.setVisibility(View.VISIBLE);
49         mScrimBehind.animateViewAlpha(0.0f, TRANSITION_DURATION_OUT, PhoneStatusBar.ALPHA_OUT);
50         outAnimation(mPanelHolder.animate())
51                 .withLayer();
52     }
53 
hideMirror()54     public void hideMirror() {
55         mScrimBehind.animateViewAlpha(1.0f, TRANSITION_DURATION_IN, PhoneStatusBar.ALPHA_IN);
56         inAnimation(mPanelHolder.animate())
57                 .withLayer()
58                 .withEndAction(new Runnable() {
59             @Override
60             public void run() {
61                 mBrightnessMirror.setVisibility(View.INVISIBLE);
62             }
63         });
64     }
65 
outAnimation(ViewPropertyAnimator a)66     private ViewPropertyAnimator outAnimation(ViewPropertyAnimator a) {
67         return a.alpha(0.0f)
68                 .setDuration(TRANSITION_DURATION_OUT)
69                 .setInterpolator(PhoneStatusBar.ALPHA_OUT);
70     }
inAnimation(ViewPropertyAnimator a)71     private ViewPropertyAnimator inAnimation(ViewPropertyAnimator a) {
72         return a.alpha(1.0f)
73                 .setDuration(TRANSITION_DURATION_IN)
74                 .setInterpolator(PhoneStatusBar.ALPHA_IN);
75     }
76 
77 
setLocation(View original)78     public void setLocation(View original) {
79         original.getLocationInWindow(mInt2Cache);
80 
81         // Original is slightly larger than the mirror, so make sure to use the center for the
82         // positioning.
83         int originalX = mInt2Cache[0] + original.getWidth()/2;
84         int originalY = mInt2Cache[1];
85         mBrightnessMirror.setTranslationX(0);
86         mBrightnessMirror.setTranslationY(0);
87         mBrightnessMirror.getLocationInWindow(mInt2Cache);
88         int mirrorX = mInt2Cache[0] + mBrightnessMirror.getWidth()/2;
89         int mirrorY = mInt2Cache[1];
90         mBrightnessMirror.setTranslationX(originalX - mirrorX);
91         mBrightnessMirror.setTranslationY(originalY - mirrorY);
92     }
93 
getMirror()94     public View getMirror() {
95         return mBrightnessMirror;
96     }
97 
updateResources()98     public void updateResources() {
99         FrameLayout.LayoutParams lp =
100                 (FrameLayout.LayoutParams) mBrightnessMirror.getLayoutParams();
101         lp.width = mBrightnessMirror.getResources().getDimensionPixelSize(
102                 R.dimen.notification_panel_width);
103         lp.gravity = mBrightnessMirror.getResources().getInteger(
104                 R.integer.notification_panel_layout_gravity);
105         mBrightnessMirror.setLayoutParams(lp);
106 
107         int padding = mBrightnessMirror.getResources().getDimensionPixelSize(
108                 R.dimen.notification_side_padding);
109         mBrightnessMirror.setPadding(padding, mBrightnessMirror.getPaddingTop(),
110                 padding, mBrightnessMirror.getPaddingBottom());
111     }
112 }
113