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