blob: 186b61848ddd27ba31751bc46e4ff1696929fff7 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
Joe Onoratofd52b182010-11-10 18:00:52 -080017package com.android.systemui.statusbar.phone;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080018
Joe Onorato798ac4c2010-05-27 16:39:00 -040019import android.app.StatusBarManager;
Nick Pellybd022f42009-08-14 18:33:38 -070020import android.bluetooth.BluetoothAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.BroadcastReceiver;
22import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.content.Intent;
24import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.os.Handler;
John Spurlockae641c92014-06-30 18:11:40 -040027import android.provider.Settings.Global;
John Spurlockcd686b52013-06-05 10:13:46 -040028import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029
Wink Savillea639b312012-07-10 12:37:54 -070030import com.android.internal.telephony.IccCardConstants;
Wink Saville04e71b32009-04-02 11:00:54 -070031import com.android.internal.telephony.TelephonyIntents;
32import com.android.internal.telephony.cdma.TtyIntent;
Joe Onorato798ac4c2010-05-27 16:39:00 -040033import com.android.systemui.R;
34
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035/**
36 * This class contains all of the policy about which icons are installed in the status
Joe Onorato798ac4c2010-05-27 16:39:00 -040037 * bar at boot time. It goes through the normal API for icons, even though it probably
38 * strictly doesn't need to.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039 */
Joe Onoratofd52b182010-11-10 18:00:52 -080040public class PhoneStatusBarPolicy {
41 private static final String TAG = "PhoneStatusBarPolicy";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 // message codes for the handler
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 private static final int EVENT_BATTERY_CLOSE = 4;
45
Daniel Sandler2c195f72010-05-21 15:42:06 -040046 private static final int AM_PM_STYLE_NORMAL = 0;
47 private static final int AM_PM_STYLE_SMALL = 1;
48 private static final int AM_PM_STYLE_GONE = 2;
49
50 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
51
Robert Greenwaltd7085fc2010-09-08 15:24:47 -070052 private static final int INET_CONDITION_THRESHOLD = 50;
53
Daniel Sandlerb1e8dab2011-09-26 20:06:19 -040054 private static final boolean SHOW_SYNC_ICON = false;
55
Dianne Hackborn4840e142009-03-24 22:40:29 -070056 private final Context mContext;
Joe Onorato798ac4c2010-05-27 16:39:00 -040057 private final StatusBarManager mService;
Joe Onorato10523b4d2010-10-25 10:42:46 -070058 private final Handler mHandler = new Handler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 // Assume it's all good unless we hear otherwise. We don't always seem
61 // to get broadcasts that it *is* there.
Wink Savillea639b312012-07-10 12:37:54 -070062 IccCardConstants.State mSimState = IccCardConstants.State.READY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063
64 // ringer volume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 private boolean mVolumeVisible;
Wink Saville04e71b32009-04-02 11:00:54 -070066
John Spurlockba2c6a02014-05-19 09:44:06 -040067 // zen mode
John Spurlockae641c92014-06-30 18:11:40 -040068 private int mZen;
John Spurlockba2c6a02014-05-19 09:44:06 -040069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 // bluetooth device status
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -040071 private boolean mBluetoothEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 private int mLastWifiSignalLevel = -1;
74 private boolean mIsWifiConnected = false;
Robert Greenwalt1e9aac22010-09-15 17:36:33 -070075
76 // state of inet connection - 0 not connected, 100 connected
77 private int mInetCondition = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
79 // sync state
80 // If sync is active the SyncActive icon is displayed. If sync is not active but
81 // sync is failing the SyncFailing icon is displayed. Otherwise neither are displayed.
Wink Savillee9b06d72009-05-18 21:47:50 -070082
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
84 @Override
85 public void onReceive(Context context, Intent intent) {
86 String action = intent.getAction();
Daniel Sandler2b697352011-07-22 16:23:09 -040087 if (action.equals(Intent.ACTION_ALARM_CHANGED)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 updateAlarm(intent);
89 }
90 else if (action.equals(Intent.ACTION_SYNC_STATE_CHANGED)) {
91 updateSyncState(intent);
92 }
Nick Pellyde893f52009-09-08 13:15:33 -070093 else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED) ||
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -070094 action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 updateBluetooth(intent);
96 }
Eric Laurentbffc3d12012-05-07 17:43:49 -070097 else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 updateVolume();
99 }
100 else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
101 updateSimState(intent);
102 }
Wink Saville04e71b32009-04-02 11:00:54 -0700103 else if (action.equals(TtyIntent.TTY_ENABLED_CHANGE_ACTION)) {
104 updateTTY(intent);
105 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 }
107 };
108
Joe Onoratofd52b182010-11-10 18:00:52 -0800109 public PhoneStatusBarPolicy(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 mContext = context;
Joe Onorato798ac4c2010-05-27 16:39:00 -0400111 mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400113 // listen for broadcasts
114 IntentFilter filter = new IntentFilter();
115 filter.addAction(Intent.ACTION_ALARM_CHANGED);
116 filter.addAction(Intent.ACTION_SYNC_STATE_CHANGED);
117 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400118 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
119 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
120 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
121 filter.addAction(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
122 mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
123
Wink Saville04e71b32009-04-02 11:00:54 -0700124 // TTY status
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700125 mService.setIcon("tty", R.drawable.stat_sys_tty_mode, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400126 mService.setIconVisibility("tty", false);
Wink Saville04e71b32009-04-02 11:00:54 -0700127
Wink Savillee9b06d72009-05-18 21:47:50 -0700128 // Cdma Roaming Indicator, ERI
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700129 mService.setIcon("cdma_eri", R.drawable.stat_sys_roaming_cdma_0, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400130 mService.setIconVisibility("cdma_eri", false);
Wink Savillee9b06d72009-05-18 21:47:50 -0700131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 // bluetooth status
Nick Pellyf242b7b2009-10-08 00:12:45 +0200133 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400134 int bluetoothIcon = R.drawable.stat_sys_data_bluetooth;
Nick Pellybd022f42009-08-14 18:33:38 -0700135 if (adapter != null) {
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400136 mBluetoothEnabled = (adapter.getState() == BluetoothAdapter.STATE_ON);
137 if (adapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED) {
138 bluetoothIcon = R.drawable.stat_sys_data_bluetooth_connected;
139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 }
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400141 mService.setIcon("bluetooth", bluetoothIcon, 0, null);
Joe Onorato0cbda992010-05-02 16:28:15 -0700142 mService.setIconVisibility("bluetooth", mBluetoothEnabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 // Alarm clock
Justin Hofdfbbf62011-08-26 08:51:27 -0700145 mService.setIcon("alarm_clock", R.drawable.stat_sys_alarm, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400146 mService.setIconVisibility("alarm_clock", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147
148 // Sync state
Justin Ho9f176582011-09-20 18:16:48 -0700149 mService.setIcon("sync_active", R.drawable.stat_sys_sync, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400150 mService.setIconVisibility("sync_active", false);
John Spurlock3c875662013-08-31 15:07:25 -0400151 // "sync_failing" is obsolete: b/1297963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152
153 // volume
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700154 mService.setIcon("volume", R.drawable.stat_sys_ringer_silent, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400155 mService.setIconVisibility("volume", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 updateVolume();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158
John Spurlockae641c92014-06-30 18:11:40 -0400159 public void setZenMode(int zen) {
John Spurlockba2c6a02014-05-19 09:44:06 -0400160 mZen = zen;
161 updateVolume();
162 }
163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 private final void updateAlarm(Intent intent) {
165 boolean alarmSet = intent.getBooleanExtra("alarmSet", false);
Joe Onorato0cbda992010-05-02 16:28:15 -0700166 mService.setIconVisibility("alarm_clock", alarmSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 }
168
169 private final void updateSyncState(Intent intent) {
Daniel Sandlerb1e8dab2011-09-26 20:06:19 -0400170 if (!SHOW_SYNC_ICON) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 boolean isActive = intent.getBooleanExtra("active", false);
Joe Onorato0cbda992010-05-02 16:28:15 -0700172 mService.setIconVisibility("sync_active", isActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 }
174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 private final void updateSimState(Intent intent) {
Wink Savillea639b312012-07-10 12:37:54 -0700176 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
177 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
178 mSimState = IccCardConstants.State.ABSENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 }
Rakesh Pallerlaf099a5f2013-02-06 11:44:23 +0530180 else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
181 mSimState = IccCardConstants.State.CARD_IO_ERROR;
182 }
Wink Savillea639b312012-07-10 12:37:54 -0700183 else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
184 mSimState = IccCardConstants.State.READY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 }
Wink Savillea639b312012-07-10 12:37:54 -0700186 else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
187 final String lockedReason =
188 intent.getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
189 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
190 mSimState = IccCardConstants.State.PIN_REQUIRED;
johnwang347c5a22009-08-24 22:11:11 -0700191 }
Wink Savillea639b312012-07-10 12:37:54 -0700192 else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
193 mSimState = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 }
195 else {
Wink Savillea639b312012-07-10 12:37:54 -0700196 mSimState = IccCardConstants.State.NETWORK_LOCKED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 }
198 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700199 mSimState = IccCardConstants.State.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 }
202
203 private final void updateVolume() {
204 AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
205 final int ringerMode = audioManager.getRingerMode();
John Spurlockae641c92014-06-30 18:11:40 -0400206 int iconId = 0;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700207 String contentDescription = null;
John Spurlockae641c92014-06-30 18:11:40 -0400208 boolean visible = false;
209 if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
210 visible = true;
211 iconId = R.drawable.stat_sys_ringer_silent;
212 contentDescription = mContext.getString(R.string.accessibility_ringer_silent);
213 } else if (mZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
214 visible = true;
215 iconId = R.drawable.stat_sys_zen_important;
216 contentDescription = mContext.getString(R.string.zen_important_interruptions);
John Spurlockba2c6a02014-05-19 09:44:06 -0400217 } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
John Spurlockae641c92014-06-30 18:11:40 -0400218 visible = true;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700219 iconId = R.drawable.stat_sys_ringer_vibrate;
220 contentDescription = mContext.getString(R.string.accessibility_ringer_vibrate);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222
223 if (visible) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700224 mService.setIcon("volume", iconId, 0, contentDescription);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 }
226 if (visible != mVolumeVisible) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700227 mService.setIconVisibility("volume", visible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 mVolumeVisible = visible;
229 }
230 }
231
232 private final void updateBluetooth(Intent intent) {
Joe Onorato798ac4c2010-05-27 16:39:00 -0400233 int iconId = R.drawable.stat_sys_data_bluetooth;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700234 String contentDescription = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 String action = intent.getAction();
Nick Pellyde893f52009-09-08 13:15:33 -0700236 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
Nick Pellyb24e11b2009-09-08 17:40:43 -0700237 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
Nick Pellyde893f52009-09-08 13:15:33 -0700238 mBluetoothEnabled = state == BluetoothAdapter.STATE_ON;
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -0700239 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
Jaikumar Ganesh0b63a6a2011-07-11 14:34:34 -0700240 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -0700241 BluetoothAdapter.STATE_DISCONNECTED);
242 if (state == BluetoothAdapter.STATE_CONNECTED) {
243 iconId = R.drawable.stat_sys_data_bluetooth_connected;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700244 contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);
245 } else {
246 contentDescription = mContext.getString(
247 R.string.accessibility_bluetooth_disconnected);
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800248 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 } else {
250 return;
251 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700252
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700253 mService.setIcon("bluetooth", iconId, 0, contentDescription);
Joe Onorato0cbda992010-05-02 16:28:15 -0700254 mService.setIconVisibility("bluetooth", mBluetoothEnabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 }
256
Wink Savillee9b06d72009-05-18 21:47:50 -0700257 private final void updateTTY(Intent intent) {
Wink Saville04e71b32009-04-02 11:00:54 -0700258 final String action = intent.getAction();
259 final boolean enabled = intent.getBooleanExtra(TtyIntent.TTY_ENABLED, false);
260
John Spurlockcd686b52013-06-05 10:13:46 -0400261 if (false) Log.v(TAG, "updateTTY: enabled: " + enabled);
Wink Saville04e71b32009-04-02 11:00:54 -0700262
263 if (enabled) {
264 // TTY is on
John Spurlockcd686b52013-06-05 10:13:46 -0400265 if (false) Log.v(TAG, "updateTTY: set TTY on");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700266 mService.setIcon("tty", R.drawable.stat_sys_tty_mode, 0,
267 mContext.getString(R.string.accessibility_tty_enabled));
Joe Onorato0cbda992010-05-02 16:28:15 -0700268 mService.setIconVisibility("tty", true);
Wink Saville04e71b32009-04-02 11:00:54 -0700269 } else {
270 // TTY is off
John Spurlockcd686b52013-06-05 10:13:46 -0400271 if (false) Log.v(TAG, "updateTTY: set TTY off");
Joe Onorato0cbda992010-05-02 16:28:15 -0700272 mService.setIconVisibility("tty", false);
Wink Saville04e71b32009-04-02 11:00:54 -0700273 }
274 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275}