Home | History | Annotate | Download | only in statusbar
      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 
     17 package com.android.systemui.statusbar;
     18 
     19 import android.content.Context;
     20 import android.content.res.Configuration;
     21 import android.graphics.Canvas;
     22 import android.os.SystemClock;
     23 import android.util.AttributeSet;
     24 import android.view.MotionEvent;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.view.ViewParent;
     28 import android.widget.FrameLayout;
     29 
     30 import com.android.systemui.R;
     31 
     32 public class StatusBarView extends FrameLayout {
     33     private static final String TAG = "StatusBarView";
     34 
     35     static final int DIM_ANIM_TIME = 400;
     36 
     37     StatusBarService mService;
     38     boolean mTracking;
     39     int mStartX, mStartY;
     40     ViewGroup mNotificationIcons;
     41     ViewGroup mStatusIcons;
     42     View mDate;
     43     FixedSizeDrawable mBackground;
     44 
     45     public StatusBarView(Context context, AttributeSet attrs) {
     46         super(context, attrs);
     47     }
     48 
     49     @Override
     50     protected void onFinishInflate() {
     51         super.onFinishInflate();
     52         mNotificationIcons = (ViewGroup)findViewById(R.id.notificationIcons);
     53         mStatusIcons = (ViewGroup)findViewById(R.id.statusIcons);
     54         mDate = findViewById(R.id.date);
     55 
     56         mBackground = new FixedSizeDrawable(mDate.getBackground());
     57         mBackground.setFixedBounds(0, 0, 0, 0);
     58         mDate.setBackgroundDrawable(mBackground);
     59     }
     60 
     61     @Override
     62     protected void onAttachedToWindow() {
     63         super.onAttachedToWindow();
     64         mService.onBarViewAttached();
     65     }
     66 
     67     @Override
     68     protected void onSizeChanged(int w, int h, int oldw, int oldh) {
     69         super.onSizeChanged(w, h, oldw, oldh);
     70         mService.updateExpandedViewPos(StatusBarService.EXPANDED_LEAVE_ALONE);
     71     }
     72 
     73     @Override
     74     protected void onLayout(boolean changed, int l, int t, int r, int b) {
     75         super.onLayout(changed, l, t, r, b);
     76 
     77         // put the date date view quantized to the icons
     78         int oldDateRight = mDate.getRight();
     79         int newDateRight;
     80 
     81         newDateRight = getDateSize(mNotificationIcons, oldDateRight,
     82                 getViewOffset(mNotificationIcons));
     83         if (newDateRight < 0) {
     84             int offset = getViewOffset(mStatusIcons);
     85             if (oldDateRight < offset) {
     86                 newDateRight = oldDateRight;
     87             } else {
     88                 newDateRight = getDateSize(mStatusIcons, oldDateRight, offset);
     89                 if (newDateRight < 0) {
     90                     newDateRight = r;
     91                 }
     92             }
     93         }
     94         int max = r - getPaddingRight();
     95         if (newDateRight > max) {
     96             newDateRight = max;
     97         }
     98 
     99         mDate.layout(mDate.getLeft(), mDate.getTop(), newDateRight, mDate.getBottom());
    100         mBackground.setFixedBounds(-mDate.getLeft(), -mDate.getTop(), (r-l), (b-t));
    101     }
    102 
    103     /**
    104      * Gets the left position of v in this view.  Throws if v is not
    105      * a child of this.
    106      */
    107     private int getViewOffset(View v) {
    108         int offset = 0;
    109         while (v != this) {
    110             offset += v.getLeft();
    111             ViewParent p = v.getParent();
    112             if (v instanceof View) {
    113                 v = (View)p;
    114             } else {
    115                 throw new RuntimeException(v + " is not a child of " + this);
    116             }
    117         }
    118         return offset;
    119     }
    120 
    121     private int getDateSize(ViewGroup g, int w, int offset) {
    122         final int N = g.getChildCount();
    123         for (int i=0; i<N; i++) {
    124             View v = g.getChildAt(i);
    125             int l = v.getLeft() + offset;
    126             int r = v.getRight() + offset;
    127             if (w >= l && w <= r) {
    128                 return r;
    129             }
    130         }
    131         return -1;
    132     }
    133 
    134     /**
    135      * Ensure that, if there is no target under us to receive the touch,
    136      * that we process it ourself.  This makes sure that onInterceptTouchEvent()
    137      * is always called for the entire gesture.
    138      */
    139     @Override
    140     public boolean onTouchEvent(MotionEvent event) {
    141         if (event.getAction() != MotionEvent.ACTION_DOWN) {
    142             mService.interceptTouchEvent(event);
    143         }
    144         return true;
    145     }
    146 
    147     @Override
    148     public boolean onInterceptTouchEvent(MotionEvent event) {
    149         return mService.interceptTouchEvent(event)
    150                 ? true : super.onInterceptTouchEvent(event);
    151     }
    152 }
    153 
    154