Home | History | Annotate | Download | only in keyguard
      1 /*
      2  * Copyright (C) 2012 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.internal.policy.impl.keyguard;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.graphics.Typeface;
     22 import android.text.TextUtils;
     23 import android.text.format.DateFormat;
     24 import android.util.AttributeSet;
     25 import android.util.Slog;
     26 import android.view.View;
     27 import android.widget.GridLayout;
     28 import android.widget.TextView;
     29 
     30 import com.android.internal.R;
     31 import com.android.internal.widget.LockPatternUtils;
     32 
     33 import java.util.Date;
     34 
     35 public class KeyguardStatusView extends GridLayout {
     36     private static final boolean DEBUG = KeyguardViewMediator.DEBUG;
     37     private static final String TAG = "KeyguardStatusView";
     38 
     39     public static final int LOCK_ICON = 0; // R.drawable.ic_lock_idle_lock;
     40     public static final int ALARM_ICON = com.android.internal.R.drawable.ic_lock_idle_alarm;
     41     public static final int CHARGING_ICON = 0; //R.drawable.ic_lock_idle_charging;
     42     public static final int BATTERY_LOW_ICON = 0; //R.drawable.ic_lock_idle_low_battery;
     43 
     44     private CharSequence mDateFormatString;
     45     private LockPatternUtils mLockPatternUtils;
     46 
     47     private TextView mDateView;
     48     private TextView mAlarmStatusView;
     49     private ClockView mClockView;
     50 
     51     private KeyguardUpdateMonitorCallback mInfoCallback = new KeyguardUpdateMonitorCallback() {
     52 
     53         @Override
     54         public void onTimeChanged() {
     55             refresh();
     56         }
     57 
     58         @Override
     59         void onKeyguardVisibilityChanged(boolean showing) {
     60             if (showing) {
     61                 if (DEBUG) Slog.v(TAG, "refresh statusview showing:" + showing);
     62                 refresh();
     63             }
     64         };
     65     };
     66 
     67     public KeyguardStatusView(Context context) {
     68         this(context, null, 0);
     69     }
     70 
     71     public KeyguardStatusView(Context context, AttributeSet attrs) {
     72         this(context, attrs, 0);
     73     }
     74 
     75     public KeyguardStatusView(Context context, AttributeSet attrs, int defStyle) {
     76         super(context, attrs, defStyle);
     77     }
     78 
     79     @Override
     80     protected void onFinishInflate() {
     81         super.onFinishInflate();
     82         Resources res = getContext().getResources();
     83         mDateFormatString =
     84                 res.getText(com.android.internal.R.string.abbrev_wday_month_day_no_year);
     85         mDateView = (TextView) findViewById(R.id.date);
     86         mAlarmStatusView = (TextView) findViewById(R.id.alarm_status);
     87         mClockView = (ClockView) findViewById(R.id.clock_view);
     88         mLockPatternUtils = new LockPatternUtils(getContext());
     89 
     90         // Use custom font in mDateView
     91         mDateView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
     92 
     93         // Required to get Marquee to work.
     94         final View marqueeViews[] = { mDateView, mAlarmStatusView };
     95         for (int i = 0; i < marqueeViews.length; i++) {
     96             View v = marqueeViews[i];
     97             if (v == null) {
     98                 throw new RuntimeException("Can't find widget at index " + i);
     99             }
    100             v.setSelected(true);
    101         }
    102         refresh();
    103     }
    104 
    105     protected void refresh() {
    106         mClockView.updateTime();
    107         refreshDate();
    108         refreshAlarmStatus(); // might as well
    109     }
    110 
    111     void refreshAlarmStatus() {
    112         // Update Alarm status
    113         String nextAlarm = mLockPatternUtils.getNextAlarm();
    114         if (!TextUtils.isEmpty(nextAlarm)) {
    115             maybeSetUpperCaseText(mAlarmStatusView, nextAlarm);
    116             mAlarmStatusView.setCompoundDrawablesWithIntrinsicBounds(ALARM_ICON, 0, 0, 0);
    117             mAlarmStatusView.setVisibility(View.VISIBLE);
    118         } else {
    119             mAlarmStatusView.setVisibility(View.GONE);
    120         }
    121     }
    122 
    123     void refreshDate() {
    124         maybeSetUpperCaseText(mDateView, DateFormat.format(mDateFormatString, new Date()));
    125     }
    126 
    127     @Override
    128     protected void onAttachedToWindow() {
    129         super.onAttachedToWindow();
    130         KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mInfoCallback);
    131     }
    132 
    133     @Override
    134     protected void onDetachedFromWindow() {
    135         super.onDetachedFromWindow();
    136         KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mInfoCallback);
    137     }
    138 
    139     public int getAppWidgetId() {
    140         return LockPatternUtils.ID_DEFAULT_STATUS_WIDGET;
    141     }
    142 
    143     private void maybeSetUpperCaseText(TextView textView, CharSequence text) {
    144         if (KeyguardViewManager.USE_UPPER_CASE
    145                 && textView.getId() != R.id.owner_info) { // currently only required for date view
    146             textView.setText(text != null ? text.toString().toUpperCase() : null);
    147         } else {
    148             textView.setText(text);
    149         }
    150     }
    151 }
    152