NavigationBarView.java revision 5c8da949804ed4b55dcebae63796714e0028b488
1/*
2 * Copyright (C) 2008 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.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.animation.ObjectAnimator;
22import android.content.Context;
23import android.os.ServiceManager;
24import android.util.AttributeSet;
25import android.view.Display;
26import android.view.KeyEvent;
27import android.view.MotionEvent;
28import android.view.View;
29import android.view.Surface;
30import android.view.WindowManager;
31import android.widget.LinearLayout;
32import android.content.res.Configuration;
33
34import com.android.internal.statusbar.IStatusBarService;
35
36import com.android.systemui.R;
37
38public class NavigationBarView extends LinearLayout {
39    final static boolean NAVBAR_ALWAYS_AT_RIGHT = true;
40
41    protected IStatusBarService mBarService;
42    final Display mDisplay;
43    View mCurrentView = null;
44    View[] mRotatedViews = new View[4];
45    View mBackground;
46    Animator mLastAnimator = null;
47
48    public View getRecentsButton() {
49        return mCurrentView.findViewById(R.id.recent_apps);
50    }
51
52    public View getMenuButton() {
53        return mCurrentView.findViewById(R.id.menu);
54    }
55
56    public NavigationBarView(Context context, AttributeSet attrs) {
57        super(context, attrs);
58        mDisplay = ((WindowManager)context.getSystemService(
59                Context.WINDOW_SERVICE)).getDefaultDisplay();
60        mBarService = IStatusBarService.Stub.asInterface(
61                ServiceManager.getService(Context.STATUS_BAR_SERVICE));
62
63        //setLayerType(View.LAYER_TYPE_HARDWARE, null);
64
65        setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
66            @Override
67            public void onSystemUiVisibilityChange(int visibility) {
68                boolean on = (visibility == View.STATUS_BAR_VISIBLE);
69                android.util.Log.d("NavigationBarView", "LIGHTS "
70                    + (on ? "ON" : "OUT"));
71                setLights(on);
72            }
73        });
74    }
75
76    private void setLights(final boolean on) {
77        float oldAlpha = mBackground.getAlpha();
78        android.util.Log.d("NavigationBarView", "animating alpha: " + oldAlpha + " -> "
79            + (on ? 1f : 0f));
80
81        if (mLastAnimator != null && mLastAnimator.isRunning()) mLastAnimator.cancel();
82
83        mLastAnimator = ObjectAnimator.ofFloat(mBackground, "alpha", oldAlpha, on ? 1f : 0f)
84            .setDuration(on ? 250 : 1500);
85        mLastAnimator.addListener(new AnimatorListenerAdapter() {
86            @Override
87            public void onAnimationEnd(Animator _a) {
88                mLastAnimator = null;
89            }
90        });
91        mLastAnimator.start();
92    }
93
94    public void onFinishInflate() {
95        mBackground = findViewById(R.id.background);
96
97        mRotatedViews[Surface.ROTATION_0] =
98        mRotatedViews[Surface.ROTATION_180] = findViewById(R.id.rot0);
99
100        mRotatedViews[Surface.ROTATION_90] = findViewById(R.id.rot90);
101
102        mRotatedViews[Surface.ROTATION_270] = NAVBAR_ALWAYS_AT_RIGHT
103                                                ? findViewById(R.id.rot90)
104                                                : findViewById(R.id.rot270);
105
106        mCurrentView = mRotatedViews[Surface.ROTATION_0];
107    }
108
109    @Override
110    public boolean onTouchEvent(MotionEvent ev) {
111        // immediately bring up the lights
112        setLights(true);
113        return false; // pass it on
114    }
115
116    public void reorient() {
117        final int rot = mDisplay.getRotation();
118        for (int i=0; i<4; i++) {
119            mRotatedViews[i].setVisibility(View.GONE);
120        }
121        mCurrentView = mRotatedViews[rot];
122        mCurrentView.setVisibility(View.VISIBLE);
123
124        android.util.Log.d("NavigationBarView", "reorient(): rot=" + mDisplay.getRotation());
125    }
126}
127