PhoneStatusBarView.java revision 0c8275b392344c24bb621b2a8af360f48a6459f9
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.content.Context;
20import android.content.res.Configuration;
21import android.graphics.Canvas;
22import android.graphics.Rect;
23import android.os.SystemClock;
24import android.util.AttributeSet;
25import android.view.KeyEvent;
26import android.view.MotionEvent;
27import android.view.View;
28import android.view.ViewGroup;
29import android.view.ViewParent;
30import android.view.accessibility.AccessibilityEvent;
31import android.widget.FrameLayout;
32
33import com.android.systemui.R;
34import com.android.systemui.statusbar.BaseStatusBar;
35import com.android.systemui.statusbar.policy.FixedSizeDrawable;
36
37public class PhoneStatusBarView extends FrameLayout {
38    private static final String TAG = "PhoneStatusBarView";
39
40    static final int DIM_ANIM_TIME = 400;
41
42    PhoneStatusBar mService;
43    boolean mTracking;
44    int mStartX, mStartY;
45    ViewGroup mNotificationIcons;
46    ViewGroup mStatusIcons;
47
48    boolean mNightMode = false;
49    int mStartAlpha = 0, mEndAlpha = 0;
50    long mEndTime = 0;
51
52    Rect mButtonBounds = new Rect();
53    boolean mCapturingEvents = true;
54
55    public PhoneStatusBarView(Context context, AttributeSet attrs) {
56        super(context, attrs);
57    }
58
59    @Override
60    protected void onFinishInflate() {
61        super.onFinishInflate();
62        mNotificationIcons = (ViewGroup)findViewById(R.id.notificationIcons);
63        mStatusIcons = (ViewGroup)findViewById(R.id.statusIcons);
64    }
65
66    @Override
67    protected void onAttachedToWindow() {
68        super.onAttachedToWindow();
69        //mService.onBarViewAttached();
70    }
71
72    @Override
73    protected void onConfigurationChanged(Configuration newConfig) {
74        super.onConfigurationChanged(newConfig);
75        mService.updateDisplaySize();
76        boolean nightMode = (newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
77                == Configuration.UI_MODE_NIGHT_YES;
78        if (mNightMode != nightMode) {
79            mNightMode = nightMode;
80            mStartAlpha = getCurAlpha();
81            mEndAlpha = mNightMode ? 0x80 : 0x00;
82            mEndTime = SystemClock.uptimeMillis() + DIM_ANIM_TIME;
83            invalidate();
84        }
85    }
86
87    int getCurAlpha() {
88        long time = SystemClock.uptimeMillis();
89        if (time > mEndTime) {
90            return mEndAlpha;
91        }
92        return mEndAlpha
93                - (int)(((mEndAlpha-mStartAlpha) * (mEndTime-time) / DIM_ANIM_TIME));
94    }
95
96    @Override
97    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
98        super.onSizeChanged(w, h, oldw, oldh);
99        mService.updateExpandedViewPos(BaseStatusBar.EXPANDED_LEAVE_ALONE);
100    }
101
102    @Override
103    protected void onLayout(boolean changed, int l, int t, int r, int b) {
104        super.onLayout(changed, l, t, r, b);
105    }
106
107    @Override
108    protected void dispatchDraw(Canvas canvas) {
109        super.dispatchDraw(canvas);
110        int alpha = getCurAlpha();
111        if (alpha != 0) {
112            canvas.drawARGB(alpha, 0, 0, 0);
113        }
114        if (alpha != mEndAlpha) {
115            invalidate();
116        }
117    }
118
119    /**
120     * Gets the left position of v in this view.  Throws if v is not
121     * a child of this.
122     */
123    private int getViewOffset(View v) {
124        int offset = 0;
125        while (v != this) {
126            offset += v.getLeft();
127            ViewParent p = v.getParent();
128            if (v instanceof View) {
129                v = (View)p;
130            } else {
131                throw new RuntimeException(v + " is not a child of " + this);
132            }
133        }
134        return offset;
135    }
136
137    /**
138     * Ensure that, if there is no target under us to receive the touch,
139     * that we process it ourself.  This makes sure that onInterceptTouchEvent()
140     * is always called for the entire gesture.
141     */
142    @Override
143    public boolean onTouchEvent(MotionEvent event) {
144        if (!mCapturingEvents) {
145            return false;
146        }
147        if (event.getAction() != MotionEvent.ACTION_DOWN) {
148            mService.interceptTouchEvent(event);
149        }
150        return true;
151    }
152
153    @Override
154    public boolean onInterceptTouchEvent(MotionEvent event) {
155        if (event.getAction() == MotionEvent.ACTION_DOWN) {
156            if (mButtonBounds.contains((int)event.getX(), (int)event.getY())) {
157                mCapturingEvents = false;
158                return false;
159            }
160        }
161        mCapturingEvents = true;
162        return mService.interceptTouchEvent(event)
163                ? true : super.onInterceptTouchEvent(event);
164    }
165
166    @Override
167    public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) {
168        if (super.onRequestSendAccessibilityEvent(child, event)) {
169            // The status bar is very small so augment the view that the user is touching
170            // with the content of the status bar a whole. This way an accessibility service
171            // may announce the current item as well as the entire content if appropriate.
172            AccessibilityEvent record = AccessibilityEvent.obtain();
173            onInitializeAccessibilityEvent(record);
174            dispatchPopulateAccessibilityEvent(record);
175            event.appendRecord(record);
176            return true;
177        }
178        return false;
179    }
180}
181