Home | History | Annotate | Download | only in policy
      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.policy;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.text.format.DateFormat;
     24 import android.util.AttributeSet;
     25 import android.view.View;
     26 import android.view.ViewParent;
     27 import android.widget.TextView;
     28 
     29 import com.android.systemui.R;
     30 
     31 import java.util.Date;
     32 
     33 public class DateView extends TextView {
     34     private static final String TAG = "DateView";
     35 
     36     private boolean mAttachedToWindow;
     37     private boolean mWindowVisible;
     38     private boolean mUpdating;
     39 
     40     private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
     41         @Override
     42         public void onReceive(Context context, Intent intent) {
     43             final String action = intent.getAction();
     44             if (Intent.ACTION_TIME_TICK.equals(action)
     45                     || Intent.ACTION_TIME_CHANGED.equals(action)
     46                     || Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
     47                 updateClock();
     48             }
     49         }
     50     };
     51 
     52     public DateView(Context context, AttributeSet attrs) {
     53         super(context, attrs);
     54     }
     55 
     56     @Override
     57     protected void onAttachedToWindow() {
     58         super.onAttachedToWindow();
     59         mAttachedToWindow = true;
     60         setUpdates();
     61     }
     62 
     63     @Override
     64     protected void onDetachedFromWindow() {
     65         super.onDetachedFromWindow();
     66         mAttachedToWindow = false;
     67         setUpdates();
     68     }
     69 
     70     @Override
     71     protected void onWindowVisibilityChanged(int visibility) {
     72         super.onWindowVisibilityChanged(visibility);
     73         mWindowVisible = visibility == VISIBLE;
     74         setUpdates();
     75     }
     76 
     77     @Override
     78     protected void onVisibilityChanged(View changedView, int visibility) {
     79         super.onVisibilityChanged(changedView, visibility);
     80         setUpdates();
     81     }
     82 
     83     @Override
     84     protected int getSuggestedMinimumWidth() {
     85         // makes the large background bitmap not force us to full width
     86         return 0;
     87     }
     88 
     89     protected void updateClock() {
     90         final String dateFormat = getContext().getString(R.string.abbrev_wday_month_day_no_year);
     91         setText(DateFormat.format(dateFormat, new Date()));
     92     }
     93 
     94     private boolean isVisible() {
     95         View v = this;
     96         while (true) {
     97             if (v.getVisibility() != VISIBLE) {
     98                 return false;
     99             }
    100             final ViewParent parent = v.getParent();
    101             if (parent instanceof View) {
    102                 v = (View)parent;
    103             } else {
    104                 return true;
    105             }
    106         }
    107     }
    108 
    109     private void setUpdates() {
    110         boolean update = mAttachedToWindow && mWindowVisible && isVisible();
    111         if (update != mUpdating) {
    112             mUpdating = update;
    113             if (update) {
    114                 // Register for Intent broadcasts for the clock and battery
    115                 IntentFilter filter = new IntentFilter();
    116                 filter.addAction(Intent.ACTION_TIME_TICK);
    117                 filter.addAction(Intent.ACTION_TIME_CHANGED);
    118                 filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
    119                 mContext.registerReceiver(mIntentReceiver, filter, null, null);
    120                 updateClock();
    121             } else {
    122                 mContext.unregisterReceiver(mIntentReceiver);
    123             }
    124         }
    125     }
    126 }
    127