blob: b6f5ae0a545fdbe75b660f181a13c3cd34a6b60c [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 Spurlockcd686b52013-06-05 10:13:46 -040027import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028
Wink Savillea639b312012-07-10 12:37:54 -070029import com.android.internal.telephony.IccCardConstants;
Wink Saville04e71b32009-04-02 11:00:54 -070030import com.android.internal.telephony.TelephonyIntents;
31import com.android.internal.telephony.cdma.TtyIntent;
Joe Onorato798ac4c2010-05-27 16:39:00 -040032import com.android.systemui.R;
33
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034/**
35 * This class contains all of the policy about which icons are installed in the status
Joe Onorato798ac4c2010-05-27 16:39:00 -040036 * bar at boot time. It goes through the normal API for icons, even though it probably
37 * strictly doesn't need to.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038 */
Joe Onoratofd52b182010-11-10 18:00:52 -080039public class PhoneStatusBarPolicy {
40 private static final String TAG = "PhoneStatusBarPolicy";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042 // message codes for the handler
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043 private static final int EVENT_BATTERY_CLOSE = 4;
44
Daniel Sandler2c195f72010-05-21 15:42:06 -040045 private static final int AM_PM_STYLE_NORMAL = 0;
46 private static final int AM_PM_STYLE_SMALL = 1;
47 private static final int AM_PM_STYLE_GONE = 2;
48
49 private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
50
Robert Greenwaltd7085fc2010-09-08 15:24:47 -070051 private static final int INET_CONDITION_THRESHOLD = 50;
52
Daniel Sandlerb1e8dab2011-09-26 20:06:19 -040053 private static final boolean SHOW_SYNC_ICON = false;
54
Dianne Hackborn4840e142009-03-24 22:40:29 -070055 private final Context mContext;
Joe Onorato798ac4c2010-05-27 16:39:00 -040056 private final StatusBarManager mService;
Joe Onorato10523b4d2010-10-25 10:42:46 -070057 private final Handler mHandler = new Handler();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 // Assume it's all good unless we hear otherwise. We don't always seem
60 // to get broadcasts that it *is* there.
Wink Savillea639b312012-07-10 12:37:54 -070061 IccCardConstants.State mSimState = IccCardConstants.State.READY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
63 // ringer volume
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 private boolean mVolumeVisible;
Wink Saville04e71b32009-04-02 11:00:54 -070065
John Spurlockba2c6a02014-05-19 09:44:06 -040066 // zen mode
67 private boolean mZen;
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 // bluetooth device status
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -040070 private boolean mBluetoothEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 private int mLastWifiSignalLevel = -1;
73 private boolean mIsWifiConnected = false;
Robert Greenwalt1e9aac22010-09-15 17:36:33 -070074
75 // state of inet connection - 0 not connected, 100 connected
76 private int mInetCondition = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78 // sync state
79 // If sync is active the SyncActive icon is displayed. If sync is not active but
80 // sync is failing the SyncFailing icon is displayed. Otherwise neither are displayed.
Wink Savillee9b06d72009-05-18 21:47:50 -070081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
83 @Override
84 public void onReceive(Context context, Intent intent) {
85 String action = intent.getAction();
Daniel Sandler2b697352011-07-22 16:23:09 -040086 if (action.equals(Intent.ACTION_ALARM_CHANGED)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 updateAlarm(intent);
88 }
89 else if (action.equals(Intent.ACTION_SYNC_STATE_CHANGED)) {
90 updateSyncState(intent);
91 }
Nick Pellyde893f52009-09-08 13:15:33 -070092 else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED) ||
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -070093 action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 updateBluetooth(intent);
95 }
Eric Laurentbffc3d12012-05-07 17:43:49 -070096 else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 updateVolume();
98 }
99 else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
100 updateSimState(intent);
101 }
Wink Saville04e71b32009-04-02 11:00:54 -0700102 else if (action.equals(TtyIntent.TTY_ENABLED_CHANGE_ACTION)) {
103 updateTTY(intent);
104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 }
106 };
107
Joe Onoratofd52b182010-11-10 18:00:52 -0800108 public PhoneStatusBarPolicy(Context context) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 mContext = context;
Joe Onorato798ac4c2010-05-27 16:39:00 -0400110 mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400112 // listen for broadcasts
113 IntentFilter filter = new IntentFilter();
114 filter.addAction(Intent.ACTION_ALARM_CHANGED);
115 filter.addAction(Intent.ACTION_SYNC_STATE_CHANGED);
116 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400117 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
118 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
119 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
120 filter.addAction(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
121 mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
122
Wink Saville04e71b32009-04-02 11:00:54 -0700123 // TTY status
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700124 mService.setIcon("tty", R.drawable.stat_sys_tty_mode, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400125 mService.setIconVisibility("tty", false);
Wink Saville04e71b32009-04-02 11:00:54 -0700126
Wink Savillee9b06d72009-05-18 21:47:50 -0700127 // Cdma Roaming Indicator, ERI
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700128 mService.setIcon("cdma_eri", R.drawable.stat_sys_roaming_cdma_0, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400129 mService.setIconVisibility("cdma_eri", false);
Wink Savillee9b06d72009-05-18 21:47:50 -0700130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 // bluetooth status
Nick Pellyf242b7b2009-10-08 00:12:45 +0200132 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400133 int bluetoothIcon = R.drawable.stat_sys_data_bluetooth;
Nick Pellybd022f42009-08-14 18:33:38 -0700134 if (adapter != null) {
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400135 mBluetoothEnabled = (adapter.getState() == BluetoothAdapter.STATE_ON);
136 if (adapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED) {
137 bluetoothIcon = R.drawable.stat_sys_data_bluetooth_connected;
138 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 }
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400140 mService.setIcon("bluetooth", bluetoothIcon, 0, null);
Joe Onorato0cbda992010-05-02 16:28:15 -0700141 mService.setIconVisibility("bluetooth", mBluetoothEnabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 // Alarm clock
Justin Hofdfbbf62011-08-26 08:51:27 -0700144 mService.setIcon("alarm_clock", R.drawable.stat_sys_alarm, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400145 mService.setIconVisibility("alarm_clock", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
147 // Sync state
Justin Ho9f176582011-09-20 18:16:48 -0700148 mService.setIcon("sync_active", R.drawable.stat_sys_sync, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400149 mService.setIconVisibility("sync_active", false);
John Spurlock3c875662013-08-31 15:07:25 -0400150 // "sync_failing" is obsolete: b/1297963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151
152 // volume
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700153 mService.setIcon("volume", R.drawable.stat_sys_ringer_silent, 0, null);
Joe Onorato798ac4c2010-05-27 16:39:00 -0400154 mService.setIconVisibility("volume", false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 updateVolume();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 }
157
John Spurlockba2c6a02014-05-19 09:44:06 -0400158 public void setZenMode(boolean zen) {
159 mZen = zen;
160 updateVolume();
161 }
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 private final void updateAlarm(Intent intent) {
164 boolean alarmSet = intent.getBooleanExtra("alarmSet", false);
Joe Onorato0cbda992010-05-02 16:28:15 -0700165 mService.setIconVisibility("alarm_clock", alarmSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
167
168 private final void updateSyncState(Intent intent) {
Daniel Sandlerb1e8dab2011-09-26 20:06:19 -0400169 if (!SHOW_SYNC_ICON) return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 boolean isActive = intent.getBooleanExtra("active", false);
Joe Onorato0cbda992010-05-02 16:28:15 -0700171 mService.setIconVisibility("sync_active", isActive);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 }
173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 private final void updateSimState(Intent intent) {
Wink Savillea639b312012-07-10 12:37:54 -0700175 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
176 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
177 mSimState = IccCardConstants.State.ABSENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 }
Rakesh Pallerlaf099a5f2013-02-06 11:44:23 +0530179 else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
180 mSimState = IccCardConstants.State.CARD_IO_ERROR;
181 }
Wink Savillea639b312012-07-10 12:37:54 -0700182 else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
183 mSimState = IccCardConstants.State.READY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
Wink Savillea639b312012-07-10 12:37:54 -0700185 else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
186 final String lockedReason =
187 intent.getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
188 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
189 mSimState = IccCardConstants.State.PIN_REQUIRED;
johnwang347c5a22009-08-24 22:11:11 -0700190 }
Wink Savillea639b312012-07-10 12:37:54 -0700191 else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
192 mSimState = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 }
194 else {
Wink Savillea639b312012-07-10 12:37:54 -0700195 mSimState = IccCardConstants.State.NETWORK_LOCKED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 }
197 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700198 mSimState = IccCardConstants.State.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
201
202 private final void updateVolume() {
203 AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
204 final int ringerMode = audioManager.getRingerMode();
205 final boolean visible = ringerMode == AudioManager.RINGER_MODE_SILENT ||
John Spurlockba2c6a02014-05-19 09:44:06 -0400206 ringerMode == AudioManager.RINGER_MODE_VIBRATE ||
207 mZen;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700208
209 final int iconId;
210 String contentDescription = null;
John Spurlockba2c6a02014-05-19 09:44:06 -0400211 if (mZen) {
212 iconId = R.drawable.stat_sys_ringer_zen;
213 contentDescription = mContext.getString(R.string.zen_mode_title);
214 } else if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700215 iconId = R.drawable.stat_sys_ringer_vibrate;
216 contentDescription = mContext.getString(R.string.accessibility_ringer_vibrate);
217 } else {
218 iconId = R.drawable.stat_sys_ringer_silent;
219 contentDescription = mContext.getString(R.string.accessibility_ringer_silent);
220 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221
222 if (visible) {
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700223 mService.setIcon("volume", iconId, 0, contentDescription);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 }
225 if (visible != mVolumeVisible) {
Joe Onorato0cbda992010-05-02 16:28:15 -0700226 mService.setIconVisibility("volume", visible);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 mVolumeVisible = visible;
228 }
229 }
230
231 private final void updateBluetooth(Intent intent) {
Joe Onorato798ac4c2010-05-27 16:39:00 -0400232 int iconId = R.drawable.stat_sys_data_bluetooth;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700233 String contentDescription = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 String action = intent.getAction();
Nick Pellyde893f52009-09-08 13:15:33 -0700235 if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
Nick Pellyb24e11b2009-09-08 17:40:43 -0700236 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
Nick Pellyde893f52009-09-08 13:15:33 -0700237 mBluetoothEnabled = state == BluetoothAdapter.STATE_ON;
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -0700238 } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
Jaikumar Ganesh0b63a6a2011-07-11 14:34:34 -0700239 int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -0700240 BluetoothAdapter.STATE_DISCONNECTED);
241 if (state == BluetoothAdapter.STATE_CONNECTED) {
242 iconId = R.drawable.stat_sys_data_bluetooth_connected;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700243 contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);
244 } else {
245 contentDescription = mContext.getString(
246 R.string.accessibility_bluetooth_disconnected);
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800247 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 } else {
249 return;
250 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700251
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700252 mService.setIcon("bluetooth", iconId, 0, contentDescription);
Joe Onorato0cbda992010-05-02 16:28:15 -0700253 mService.setIconVisibility("bluetooth", mBluetoothEnabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 }
255
Wink Savillee9b06d72009-05-18 21:47:50 -0700256 private final void updateTTY(Intent intent) {
Wink Saville04e71b32009-04-02 11:00:54 -0700257 final String action = intent.getAction();
258 final boolean enabled = intent.getBooleanExtra(TtyIntent.TTY_ENABLED, false);
259
John Spurlockcd686b52013-06-05 10:13:46 -0400260 if (false) Log.v(TAG, "updateTTY: enabled: " + enabled);
Wink Saville04e71b32009-04-02 11:00:54 -0700261
262 if (enabled) {
263 // TTY is on
John Spurlockcd686b52013-06-05 10:13:46 -0400264 if (false) Log.v(TAG, "updateTTY: set TTY on");
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700265 mService.setIcon("tty", R.drawable.stat_sys_tty_mode, 0,
266 mContext.getString(R.string.accessibility_tty_enabled));
Joe Onorato0cbda992010-05-02 16:28:15 -0700267 mService.setIconVisibility("tty", true);
Wink Saville04e71b32009-04-02 11:00:54 -0700268 } else {
269 // TTY is off
John Spurlockcd686b52013-06-05 10:13:46 -0400270 if (false) Log.v(TAG, "updateTTY: set TTY off");
Joe Onorato0cbda992010-05-02 16:28:15 -0700271 mService.setIconVisibility("tty", false);
Wink Saville04e71b32009-04-02 11:00:54 -0700272 }
273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274}