blob: e8a000c1f2d38c602b7dce0b3ec994570fb0ca3d [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
Adrian Roosc42a1e12014-07-07 23:35:53 +020019import android.app.AlarmManager;
Joe Onorato798ac4c2010-05-27 16:39:00 -040020import android.app.StatusBarManager;
Nick Pellybd022f42009-08-14 18:33:38 -070021import android.bluetooth.BluetoothAdapter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.BroadcastReceiver;
23import android.content.Context;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Intent;
25import android.content.IntentFilter;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.os.Handler;
Adrian Roosc42a1e12014-07-07 23:35:53 +020028import android.os.UserHandle;
John Spurlockae641c92014-06-30 18:11:40 -040029import android.provider.Settings.Global;
Tyler Gunnef9f6f92014-09-12 22:16:17 -070030import android.telecom.TelecomManager;
John Spurlockcd686b52013-06-05 10:13:46 -040031import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032
Wink Savillea639b312012-07-10 12:37:54 -070033import com.android.internal.telephony.IccCardConstants;
Wink Saville04e71b32009-04-02 11:00:54 -070034import com.android.internal.telephony.TelephonyIntents;
Joe Onorato798ac4c2010-05-27 16:39:00 -040035import com.android.systemui.R;
John Spurlock96fe98d2014-08-27 09:36:02 -040036import com.android.systemui.statusbar.policy.CastController;
37import com.android.systemui.statusbar.policy.CastController.CastDevice;
Jason Monk952d5d82014-10-27 15:28:22 -040038import com.android.systemui.statusbar.policy.HotspotController;
Joe Onorato798ac4c2010-05-27 16:39:00 -040039
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040/**
41 * This class contains all of the policy about which icons are installed in the status
Joe Onorato798ac4c2010-05-27 16:39:00 -040042 * bar at boot time. It goes through the normal API for icons, even though it probably
43 * strictly doesn't need to.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 */
Joe Onoratofd52b182010-11-10 18:00:52 -080045public class PhoneStatusBarPolicy {
46 private static final String TAG = "PhoneStatusBarPolicy";
John Spurlocka37e9c82014-07-08 09:40:31 -040047 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Robert Greenwaltd7085fc2010-09-08 15:24:47 -070048
John Spurlock96fe98d2014-08-27 09:36:02 -040049 private static final String SLOT_CAST = "cast";
Jason Monk952d5d82014-10-27 15:28:22 -040050 private static final String SLOT_HOTSPOT = "hotspot";
John Spurlocka37e9c82014-07-08 09:40:31 -040051 private static final String SLOT_BLUETOOTH = "bluetooth";
52 private static final String SLOT_TTY = "tty";
53 private static final String SLOT_ZEN = "zen";
54 private static final String SLOT_VOLUME = "volume";
John Spurlocka37e9c82014-07-08 09:40:31 -040055 private static final String SLOT_ALARM_CLOCK = "alarm_clock";
56
Dianne Hackborn4840e142009-03-24 22:40:29 -070057 private final Context mContext;
Joe Onorato798ac4c2010-05-27 16:39:00 -040058 private final StatusBarManager mService;
Joe Onorato10523b4d2010-10-25 10:42:46 -070059 private final Handler mHandler = new Handler();
John Spurlock96fe98d2014-08-27 09:36:02 -040060 private final CastController mCast;
Jason Monk952d5d82014-10-27 15:28:22 -040061 private final HotspotController mHotspot;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 // Assume it's all good unless we hear otherwise. We don't always seem
64 // to get broadcasts that it *is* there.
Wink Savillea639b312012-07-10 12:37:54 -070065 IccCardConstants.State mSimState = IccCardConstants.State.READY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066
John Spurlocka37e9c82014-07-08 09:40:31 -040067 private boolean mZenVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 private boolean mVolumeVisible;
Wink Saville04e71b32009-04-02 11:00:54 -070069
John Spurlockae641c92014-06-30 18:11:40 -040070 private int mZen;
John Spurlockba2c6a02014-05-19 09:44:06 -040071
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -040072 private boolean mBluetoothEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
Wink Savillee9b06d72009-05-18 21:47:50 -070074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
76 @Override
77 public void onReceive(Context context, Intent intent) {
78 String action = intent.getAction();
Adrian Roosc42a1e12014-07-07 23:35:53 +020079 if (action.equals(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED)) {
80 updateAlarm();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 }
Nick Pellyde893f52009-09-08 13:15:33 -070082 else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED) ||
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -070083 action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
Jorim Jaggi2d75f9f2014-10-08 17:10:58 -070084 updateBluetooth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 }
John Spurlockbcc10872014-11-28 15:29:21 -050086 else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION) ||
87 action.equals(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION)) {
John Spurlocka37e9c82014-07-08 09:40:31 -040088 updateVolumeZen();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 }
90 else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
91 updateSimState(intent);
92 }
Tyler Gunnef9f6f92014-09-12 22:16:17 -070093 else if (action.equals(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED)) {
Wink Saville04e71b32009-04-02 11:00:54 -070094 updateTTY(intent);
95 }
Adrian Roosc42a1e12014-07-07 23:35:53 +020096 else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
97 updateAlarm();
98 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 }
100 };
101
Jason Monk952d5d82014-10-27 15:28:22 -0400102 public PhoneStatusBarPolicy(Context context, CastController cast, HotspotController hotspot) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 mContext = context;
John Spurlock96fe98d2014-08-27 09:36:02 -0400104 mCast = cast;
Jason Monk952d5d82014-10-27 15:28:22 -0400105 mHotspot = hotspot;
Joe Onorato798ac4c2010-05-27 16:39:00 -0400106 mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400108 // listen for broadcasts
109 IntentFilter filter = new IntentFilter();
Adrian Roosc42a1e12014-07-07 23:35:53 +0200110 filter.addAction(AlarmManager.ACTION_NEXT_ALARM_CLOCK_CHANGED);
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400111 filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
John Spurlockbcc10872014-11-28 15:29:21 -0500112 filter.addAction(AudioManager.INTERNAL_RINGER_MODE_CHANGED_ACTION);
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400113 filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
114 filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
115 filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700116 filter.addAction(TelecomManager.ACTION_CURRENT_TTY_MODE_CHANGED);
Adrian Roosc42a1e12014-07-07 23:35:53 +0200117 filter.addAction(Intent.ACTION_USER_SWITCHED);
Daniel Sandlere3b7a2d2011-10-27 16:16:15 -0400118 mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
119
Wink Saville04e71b32009-04-02 11:00:54 -0700120 // TTY status
John Spurlocka37e9c82014-07-08 09:40:31 -0400121 mService.setIcon(SLOT_TTY, R.drawable.stat_sys_tty_mode, 0, null);
122 mService.setIconVisibility(SLOT_TTY, false);
Wink Saville04e71b32009-04-02 11:00:54 -0700123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 // bluetooth status
Jorim Jaggi2d75f9f2014-10-08 17:10:58 -0700125 updateBluetooth();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 // Alarm clock
John Spurlocka37e9c82014-07-08 09:40:31 -0400128 mService.setIcon(SLOT_ALARM_CLOCK, R.drawable.stat_sys_alarm, 0, null);
129 mService.setIconVisibility(SLOT_ALARM_CLOCK, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130
John Spurlocka37e9c82014-07-08 09:40:31 -0400131 // zen
132 mService.setIcon(SLOT_ZEN, R.drawable.stat_sys_zen_important, 0, null);
133 mService.setIconVisibility(SLOT_ZEN, false);
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 // volume
John Spurlockad494bc2014-07-19 15:56:19 -0400136 mService.setIcon(SLOT_VOLUME, R.drawable.stat_sys_ringer_vibrate, 0, null);
John Spurlocka37e9c82014-07-08 09:40:31 -0400137 mService.setIconVisibility(SLOT_VOLUME, false);
138 updateVolumeZen();
John Spurlock96fe98d2014-08-27 09:36:02 -0400139
140 // cast
141 mService.setIcon(SLOT_CAST, R.drawable.stat_sys_cast, 0, null);
142 mService.setIconVisibility(SLOT_CAST, false);
143 mCast.addCallback(mCastCallback);
Jason Monk952d5d82014-10-27 15:28:22 -0400144
145 // hotspot
146 mService.setIcon(SLOT_HOTSPOT, R.drawable.stat_sys_hotspot, 0, null);
147 mService.setIconVisibility(SLOT_HOTSPOT, mHotspot.isHotspotEnabled());
148 mHotspot.addCallback(mHotspotCallback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 }
150
John Spurlockae641c92014-06-30 18:11:40 -0400151 public void setZenMode(int zen) {
John Spurlockba2c6a02014-05-19 09:44:06 -0400152 mZen = zen;
John Spurlocka37e9c82014-07-08 09:40:31 -0400153 updateVolumeZen();
John Spurlockba2c6a02014-05-19 09:44:06 -0400154 }
155
Adrian Roosc42a1e12014-07-07 23:35:53 +0200156 private void updateAlarm() {
157 AlarmManager alarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
John Spurlock5b13e922015-01-07 11:04:58 -0500158 boolean alarmSet = alarmManager.getNextAlarmClock(UserHandle.USER_CURRENT) != null;
John Spurlocka37e9c82014-07-08 09:40:31 -0400159 mService.setIconVisibility(SLOT_ALARM_CLOCK, alarmSet);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 }
161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 private final void updateSimState(Intent intent) {
Wink Savillea639b312012-07-10 12:37:54 -0700163 String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
164 if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
165 mSimState = IccCardConstants.State.ABSENT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 }
Rakesh Pallerlaf099a5f2013-02-06 11:44:23 +0530167 else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
168 mSimState = IccCardConstants.State.CARD_IO_ERROR;
169 }
Wink Savillea639b312012-07-10 12:37:54 -0700170 else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
171 mSimState = IccCardConstants.State.READY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 }
Wink Savillea639b312012-07-10 12:37:54 -0700173 else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
174 final String lockedReason =
175 intent.getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
176 if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
177 mSimState = IccCardConstants.State.PIN_REQUIRED;
johnwang347c5a22009-08-24 22:11:11 -0700178 }
Wink Savillea639b312012-07-10 12:37:54 -0700179 else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
180 mSimState = IccCardConstants.State.PUK_REQUIRED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 }
182 else {
Wink Savillea639b312012-07-10 12:37:54 -0700183 mSimState = IccCardConstants.State.NETWORK_LOCKED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 }
185 } else {
Wink Savillea639b312012-07-10 12:37:54 -0700186 mSimState = IccCardConstants.State.UNKNOWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 }
189
John Spurlockbcc10872014-11-28 15:29:21 -0500190 private final void updateVolumeZen() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
John Spurlocka37e9c82014-07-08 09:40:31 -0400192
193 boolean zenVisible = false;
194 int zenIconId = 0;
195 String zenDescription = null;
196
197 boolean volumeVisible = false;
198 int volumeIconId = 0;
199 String volumeDescription = null;
200
201 if (mZen == Global.ZEN_MODE_NO_INTERRUPTIONS) {
202 zenVisible = true;
203 zenIconId = R.drawable.stat_sys_zen_none;
204 zenDescription = mContext.getString(R.string.zen_no_interruptions);
John Spurlockae641c92014-06-30 18:11:40 -0400205 } else if (mZen == Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS) {
John Spurlocka37e9c82014-07-08 09:40:31 -0400206 zenVisible = true;
207 zenIconId = R.drawable.stat_sys_zen_important;
208 zenDescription = mContext.getString(R.string.zen_important_interruptions);
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210
John Spurlocka37e9c82014-07-08 09:40:31 -0400211 if (mZen != Global.ZEN_MODE_NO_INTERRUPTIONS &&
John Spurlock661f2cf2014-11-17 10:29:10 -0500212 audioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_VIBRATE) {
John Spurlocka37e9c82014-07-08 09:40:31 -0400213 volumeVisible = true;
214 volumeIconId = R.drawable.stat_sys_ringer_vibrate;
215 volumeDescription = mContext.getString(R.string.accessibility_ringer_vibrate);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 }
John Spurlocka37e9c82014-07-08 09:40:31 -0400217
218 if (zenVisible) {
219 mService.setIcon(SLOT_ZEN, zenIconId, 0, zenDescription);
220 }
221 if (zenVisible != mZenVisible) {
222 mService.setIconVisibility(SLOT_ZEN, zenVisible);
223 mZenVisible = zenVisible;
224 }
225
226 if (volumeVisible) {
227 mService.setIcon(SLOT_VOLUME, volumeIconId, 0, volumeDescription);
228 }
229 if (volumeVisible != mVolumeVisible) {
230 mService.setIconVisibility(SLOT_VOLUME, volumeVisible);
231 mVolumeVisible = volumeVisible;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 }
233 }
234
Jorim Jaggi2d75f9f2014-10-08 17:10:58 -0700235 private final void updateBluetooth() {
236 BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
Joe Onorato798ac4c2010-05-27 16:39:00 -0400237 int iconId = R.drawable.stat_sys_data_bluetooth;
Jorim Jaggi2d75f9f2014-10-08 17:10:58 -0700238 String contentDescription =
239 mContext.getString(R.string.accessibility_bluetooth_disconnected);
240 if (adapter != null) {
241 mBluetoothEnabled = (adapter.getState() == BluetoothAdapter.STATE_ON);
242 if (adapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED) {
Jaikumar Ganeshd9bb6882010-09-27 17:05:24 -0700243 iconId = R.drawable.stat_sys_data_bluetooth_connected;
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700244 contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);
Jaikumar Ganesh084c6652009-12-07 10:58:18 -0800245 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 } else {
Jorim Jaggi2d75f9f2014-10-08 17:10:58 -0700247 mBluetoothEnabled = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800248 }
Wink Savillee9b06d72009-05-18 21:47:50 -0700249
John Spurlocka37e9c82014-07-08 09:40:31 -0400250 mService.setIcon(SLOT_BLUETOOTH, iconId, 0, contentDescription);
251 mService.setIconVisibility(SLOT_BLUETOOTH, mBluetoothEnabled);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 }
253
Wink Savillee9b06d72009-05-18 21:47:50 -0700254 private final void updateTTY(Intent intent) {
Tyler Gunnef9f6f92014-09-12 22:16:17 -0700255 int currentTtyMode = intent.getIntExtra(TelecomManager.EXTRA_CURRENT_TTY_MODE,
256 TelecomManager.TTY_MODE_OFF);
257 boolean enabled = currentTtyMode != TelecomManager.TTY_MODE_OFF;
Wink Saville04e71b32009-04-02 11:00:54 -0700258
John Spurlocka37e9c82014-07-08 09:40:31 -0400259 if (DEBUG) Log.v(TAG, "updateTTY: enabled: " + enabled);
Wink Saville04e71b32009-04-02 11:00:54 -0700260
261 if (enabled) {
262 // TTY is on
John Spurlocka37e9c82014-07-08 09:40:31 -0400263 if (DEBUG) Log.v(TAG, "updateTTY: set TTY on");
264 mService.setIcon(SLOT_TTY, R.drawable.stat_sys_tty_mode, 0,
Svetoslav Ganov6179ea32011-06-28 01:12:41 -0700265 mContext.getString(R.string.accessibility_tty_enabled));
John Spurlocka37e9c82014-07-08 09:40:31 -0400266 mService.setIconVisibility(SLOT_TTY, true);
Wink Saville04e71b32009-04-02 11:00:54 -0700267 } else {
268 // TTY is off
John Spurlocka37e9c82014-07-08 09:40:31 -0400269 if (DEBUG) Log.v(TAG, "updateTTY: set TTY off");
270 mService.setIconVisibility(SLOT_TTY, false);
Wink Saville04e71b32009-04-02 11:00:54 -0700271 }
272 }
John Spurlock96fe98d2014-08-27 09:36:02 -0400273
274 private void updateCast() {
275 boolean isCasting = false;
276 for (CastDevice device : mCast.getCastDevices()) {
277 if (device.state == CastDevice.STATE_CONNECTING
278 || device.state == CastDevice.STATE_CONNECTED) {
279 isCasting = true;
280 break;
281 }
282 }
283 if (DEBUG) Log.v(TAG, "updateCast: isCasting: " + isCasting);
284 if (isCasting) {
285 mService.setIcon(SLOT_CAST, R.drawable.stat_sys_cast, 0,
286 mContext.getString(R.string.accessibility_casting));
287 }
288 mService.setIconVisibility(SLOT_CAST, isCasting);
289 }
290
Jason Monk952d5d82014-10-27 15:28:22 -0400291 private final HotspotController.Callback mHotspotCallback = new HotspotController.Callback() {
292 @Override
293 public void onHotspotChanged(boolean enabled) {
294 mService.setIconVisibility(SLOT_HOTSPOT, enabled);
295 }
296 };
297
John Spurlock96fe98d2014-08-27 09:36:02 -0400298 private final CastController.Callback mCastCallback = new CastController.Callback() {
299 @Override
300 public void onCastDevicesChanged() {
301 updateCast();
302 }
303 };
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304}