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