KeyguardStatusView.java revision 0c48689b088b6379ae2ffa0e3c884b1349e8b8af
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
17package com.android.keyguard;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.text.TextUtils;
22import android.text.format.DateFormat;
23import android.util.AttributeSet;
24import android.util.Log;
25import android.util.Slog;
26import android.view.View;
27import android.widget.GridLayout;
28import android.widget.TextClock;
29import android.widget.TextView;
30
31import com.android.internal.widget.LockPatternUtils;
32
33import java.util.Locale;
34
35public class KeyguardStatusView extends GridLayout {
36    private static final boolean DEBUG = KeyguardViewMediator.DEBUG;
37    private static final String TAG = "KeyguardStatusView";
38
39    private LockPatternUtils mLockPatternUtils;
40
41    private TextView mAlarmStatusView;
42    private TextClock mDateView;
43    private TextClock mClockView;
44
45    private KeyguardUpdateMonitorCallback mInfoCallback = new KeyguardUpdateMonitorCallback() {
46
47        @Override
48        public void onTimeChanged() {
49            refresh();
50        }
51
52        @Override
53        void onKeyguardVisibilityChanged(boolean showing) {
54            if (showing) {
55                if (DEBUG) Slog.v(TAG, "refresh statusview showing:" + showing);
56                refresh();
57            }
58        };
59
60        @Override
61        public void onScreenTurnedOn() {
62            setEnableMarquee(true);
63        };
64
65        @Override
66        public void onScreenTurnedOff(int why) {
67            setEnableMarquee(false);
68        };
69    };
70
71    public KeyguardStatusView(Context context) {
72        this(context, null, 0);
73    }
74
75    public KeyguardStatusView(Context context, AttributeSet attrs) {
76        this(context, attrs, 0);
77    }
78
79    public KeyguardStatusView(Context context, AttributeSet attrs, int defStyle) {
80        super(context, attrs, defStyle);
81    }
82
83    private void setEnableMarquee(boolean enabled) {
84        if (DEBUG) Log.v(TAG, (enabled ? "Enable" : "Disable") + " transport text marquee");
85        if (mAlarmStatusView != null) mAlarmStatusView.setSelected(enabled);
86    }
87
88    @Override
89    protected void onFinishInflate() {
90        super.onFinishInflate();
91        mAlarmStatusView = (TextView) findViewById(R.id.alarm_status);
92        mDateView = (TextClock) findViewById(R.id.date_view);
93        mClockView = (TextClock) findViewById(R.id.clock_view);
94        mLockPatternUtils = new LockPatternUtils(getContext());
95        final boolean screenOn = KeyguardUpdateMonitor.getInstance(mContext).isScreenOn();
96        setEnableMarquee(screenOn);
97        refresh();
98    }
99
100    protected void refresh() {
101        Resources res = mContext.getResources();
102        Locale locale = Locale.getDefault();
103        final String dateFormat = DateFormat.getBestDateTimePattern(locale,
104                res.getString(R.string.abbrev_wday_month_day_no_year));
105
106        mDateView.setFormat24Hour(dateFormat);
107        mDateView.setFormat12Hour(dateFormat);
108
109        // 12-hour clock.
110        // CLDR insists on adding an AM/PM indicator even though it wasn't in the skeleton
111        // format.  The following code removes the AM/PM indicator if we didn't want it.
112        final String clock12skel = res.getString(R.string.clock_12hr_format);
113        String clock12hr = DateFormat.getBestDateTimePattern(locale, clock12skel);
114        clock12hr = clock12skel.contains("a") ? clock12hr : clock12hr.replaceAll("a", "").trim();
115        mClockView.setFormat12Hour(clock12hr);
116
117        // 24-hour clock
118        final String clock24skel = res.getString(R.string.clock_24hr_format);
119        final String clock24hr = DateFormat.getBestDateTimePattern(locale, clock24skel);
120        mClockView.setFormat24Hour(clock24hr);
121
122        refreshAlarmStatus();
123    }
124
125    void refreshAlarmStatus() {
126        // Update Alarm status
127        String nextAlarm = mLockPatternUtils.getNextAlarm();
128        if (!TextUtils.isEmpty(nextAlarm)) {
129            mAlarmStatusView.setText(nextAlarm);
130            mAlarmStatusView.setVisibility(View.VISIBLE);
131        } else {
132            mAlarmStatusView.setVisibility(View.GONE);
133        }
134    }
135
136    @Override
137    protected void onAttachedToWindow() {
138        super.onAttachedToWindow();
139        KeyguardUpdateMonitor.getInstance(mContext).registerCallback(mInfoCallback);
140    }
141
142    @Override
143    protected void onDetachedFromWindow() {
144        super.onDetachedFromWindow();
145        KeyguardUpdateMonitor.getInstance(mContext).removeCallback(mInfoCallback);
146    }
147
148    public int getAppWidgetId() {
149        return LockPatternUtils.ID_DEFAULT_STATUS_WIDGET;
150    }
151
152}
153