1/*
2 * Copyright (C) 2013 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.phone;
18
19import android.content.Context;
20import android.os.ServiceManager;
21import android.util.SparseArray;
22import android.view.MotionEvent;
23import android.view.View;
24
25import com.android.internal.statusbar.IStatusBarService;
26import com.android.systemui.R;
27
28public final class NavigationBarTransitions extends BarTransitions {
29
30    private final NavigationBarView mView;
31    private final IStatusBarService mBarService;
32    private final LightBarTransitionsController mLightTransitionsController;
33
34    private boolean mLightsOut;
35
36    public NavigationBarTransitions(NavigationBarView view) {
37        super(view, R.drawable.nav_background);
38        mView = view;
39        mBarService = IStatusBarService.Stub.asInterface(
40                ServiceManager.getService(Context.STATUS_BAR_SERVICE));
41        mLightTransitionsController = new LightBarTransitionsController(view.getContext(),
42                this::applyDarkIntensity);
43    }
44
45    public void init() {
46        applyModeBackground(-1, getMode(), false /*animate*/);
47        applyMode(getMode(), false /*animate*/, true /*force*/);
48    }
49
50    public LightBarTransitionsController getLightTransitionsController() {
51        return mLightTransitionsController;
52    }
53
54    @Override
55    protected void onTransition(int oldMode, int newMode, boolean animate) {
56        super.onTransition(oldMode, newMode, animate);
57        applyMode(newMode, animate, false /*force*/);
58    }
59
60    private void applyMode(int mode, boolean animate, boolean force) {
61
62        // apply to lights out
63        applyLightsOut(isLightsOut(mode), animate, force);
64    }
65
66    private void applyLightsOut(boolean lightsOut, boolean animate, boolean force) {
67        if (!force && lightsOut == mLightsOut) return;
68
69        mLightsOut = lightsOut;
70
71        final View navButtons = mView.getCurrentView().findViewById(R.id.nav_buttons);
72
73        // ok, everyone, stop it right there
74        navButtons.animate().cancel();
75
76        final float navButtonsAlpha = lightsOut ? 0.5f : 1f;
77
78        if (!animate) {
79            navButtons.setAlpha(navButtonsAlpha);
80        } else {
81            final int duration = lightsOut ? LIGHTS_OUT_DURATION : LIGHTS_IN_DURATION;
82            navButtons.animate()
83                .alpha(navButtonsAlpha)
84                .setDuration(duration)
85                .start();
86        }
87    }
88
89
90    public void reapplyDarkIntensity() {
91        applyDarkIntensity(mLightTransitionsController.getCurrentDarkIntensity());
92    }
93
94    public void applyDarkIntensity(float darkIntensity) {
95        SparseArray<ButtonDispatcher> buttonDispatchers = mView.getButtonDispatchers();
96        for (int i = buttonDispatchers.size() - 1; i >= 0; i--) {
97            buttonDispatchers.valueAt(i).setDarkIntensity(darkIntensity);
98        }
99    }
100
101    private final View.OnTouchListener mLightsOutListener = new View.OnTouchListener() {
102        @Override
103        public boolean onTouch(View v, MotionEvent ev) {
104            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
105                // even though setting the systemUI visibility below will turn these views
106                // on, we need them to come up faster so that they can catch this motion
107                // event
108                applyLightsOut(false, false, false);
109
110                try {
111                    mBarService.setSystemUiVisibility(0, View.SYSTEM_UI_FLAG_LOW_PROFILE,
112                            "LightsOutListener");
113                } catch (android.os.RemoteException ex) {
114                }
115            }
116            return false;
117        }
118    };
119}
120