/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarPolicy.java

https://github.com/aizuzi/platform_frameworks_base · Java · 262 lines · 187 code · 36 blank · 39 comment · 50 complexity · d010de4de2afadb3f0623e4a50c7c972 MD5 · raw file

  1. /*
  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. package com.android.systemui.statusbar.phone;
  17. import android.app.StatusBarManager;
  18. import android.bluetooth.BluetoothAdapter;
  19. import android.content.BroadcastReceiver;
  20. import android.content.Context;
  21. import android.content.Intent;
  22. import android.content.IntentFilter;
  23. import android.media.AudioManager;
  24. import android.os.Handler;
  25. import android.util.Log;
  26. import com.android.internal.telephony.IccCardConstants;
  27. import com.android.internal.telephony.TelephonyIntents;
  28. import com.android.internal.telephony.cdma.TtyIntent;
  29. import com.android.systemui.R;
  30. /**
  31. * This class contains all of the policy about which icons are installed in the status
  32. * bar at boot time. It goes through the normal API for icons, even though it probably
  33. * strictly doesn't need to.
  34. */
  35. public class PhoneStatusBarPolicy {
  36. private static final String TAG = "PhoneStatusBarPolicy";
  37. // message codes for the handler
  38. private static final int EVENT_BATTERY_CLOSE = 4;
  39. private static final int AM_PM_STYLE_NORMAL = 0;
  40. private static final int AM_PM_STYLE_SMALL = 1;
  41. private static final int AM_PM_STYLE_GONE = 2;
  42. private static final int AM_PM_STYLE = AM_PM_STYLE_GONE;
  43. private static final int INET_CONDITION_THRESHOLD = 50;
  44. private static final boolean SHOW_SYNC_ICON = false;
  45. private final Context mContext;
  46. private final StatusBarManager mService;
  47. private final Handler mHandler = new Handler();
  48. // Assume it's all good unless we hear otherwise. We don't always seem
  49. // to get broadcasts that it *is* there.
  50. IccCardConstants.State mSimState = IccCardConstants.State.READY;
  51. // ringer volume
  52. private boolean mVolumeVisible;
  53. // bluetooth device status
  54. private boolean mBluetoothEnabled = false;
  55. private int mLastWifiSignalLevel = -1;
  56. private boolean mIsWifiConnected = false;
  57. // state of inet connection - 0 not connected, 100 connected
  58. private int mInetCondition = 0;
  59. // sync state
  60. // If sync is active the SyncActive icon is displayed. If sync is not active but
  61. // sync is failing the SyncFailing icon is displayed. Otherwise neither are displayed.
  62. private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
  63. @Override
  64. public void onReceive(Context context, Intent intent) {
  65. String action = intent.getAction();
  66. if (action.equals(Intent.ACTION_ALARM_CHANGED)) {
  67. updateAlarm(intent);
  68. }
  69. else if (action.equals(Intent.ACTION_SYNC_STATE_CHANGED)) {
  70. updateSyncState(intent);
  71. }
  72. else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED) ||
  73. action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
  74. updateBluetooth(intent);
  75. }
  76. else if (action.equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
  77. updateVolume();
  78. }
  79. else if (action.equals(TelephonyIntents.ACTION_SIM_STATE_CHANGED)) {
  80. updateSimState(intent);
  81. }
  82. else if (action.equals(TtyIntent.TTY_ENABLED_CHANGE_ACTION)) {
  83. updateTTY(intent);
  84. }
  85. }
  86. };
  87. public PhoneStatusBarPolicy(Context context) {
  88. mContext = context;
  89. mService = (StatusBarManager)context.getSystemService(Context.STATUS_BAR_SERVICE);
  90. // listen for broadcasts
  91. IntentFilter filter = new IntentFilter();
  92. filter.addAction(Intent.ACTION_ALARM_CHANGED);
  93. filter.addAction(Intent.ACTION_SYNC_STATE_CHANGED);
  94. filter.addAction(AudioManager.RINGER_MODE_CHANGED_ACTION);
  95. filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
  96. filter.addAction(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED);
  97. filter.addAction(TelephonyIntents.ACTION_SIM_STATE_CHANGED);
  98. filter.addAction(TtyIntent.TTY_ENABLED_CHANGE_ACTION);
  99. mContext.registerReceiver(mIntentReceiver, filter, null, mHandler);
  100. // TTY status
  101. mService.setIcon("tty", R.drawable.stat_sys_tty_mode, 0, null);
  102. mService.setIconVisibility("tty", false);
  103. // Cdma Roaming Indicator, ERI
  104. mService.setIcon("cdma_eri", R.drawable.stat_sys_roaming_cdma_0, 0, null);
  105. mService.setIconVisibility("cdma_eri", false);
  106. // bluetooth status
  107. BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
  108. int bluetoothIcon = R.drawable.stat_sys_data_bluetooth;
  109. if (adapter != null) {
  110. mBluetoothEnabled = (adapter.getState() == BluetoothAdapter.STATE_ON);
  111. if (adapter.getConnectionState() == BluetoothAdapter.STATE_CONNECTED) {
  112. bluetoothIcon = R.drawable.stat_sys_data_bluetooth_connected;
  113. }
  114. }
  115. mService.setIcon("bluetooth", bluetoothIcon, 0, null);
  116. mService.setIconVisibility("bluetooth", mBluetoothEnabled);
  117. // Alarm clock
  118. mService.setIcon("alarm_clock", R.drawable.stat_sys_alarm, 0, null);
  119. mService.setIconVisibility("alarm_clock", false);
  120. // Sync state
  121. mService.setIcon("sync_active", R.drawable.stat_sys_sync, 0, null);
  122. mService.setIconVisibility("sync_active", false);
  123. // "sync_failing" is obsolete: b/1297963
  124. // volume
  125. mService.setIcon("volume", R.drawable.stat_sys_ringer_silent, 0, null);
  126. mService.setIconVisibility("volume", false);
  127. updateVolume();
  128. }
  129. private final void updateAlarm(Intent intent) {
  130. boolean alarmSet = intent.getBooleanExtra("alarmSet", false);
  131. mService.setIconVisibility("alarm_clock", alarmSet);
  132. }
  133. private final void updateSyncState(Intent intent) {
  134. if (!SHOW_SYNC_ICON) return;
  135. boolean isActive = intent.getBooleanExtra("active", false);
  136. mService.setIconVisibility("sync_active", isActive);
  137. }
  138. private final void updateSimState(Intent intent) {
  139. String stateExtra = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
  140. if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(stateExtra)) {
  141. mSimState = IccCardConstants.State.ABSENT;
  142. }
  143. else if (IccCardConstants.INTENT_VALUE_ICC_CARD_IO_ERROR.equals(stateExtra)) {
  144. mSimState = IccCardConstants.State.CARD_IO_ERROR;
  145. }
  146. else if (IccCardConstants.INTENT_VALUE_ICC_READY.equals(stateExtra)) {
  147. mSimState = IccCardConstants.State.READY;
  148. }
  149. else if (IccCardConstants.INTENT_VALUE_ICC_LOCKED.equals(stateExtra)) {
  150. final String lockedReason =
  151. intent.getStringExtra(IccCardConstants.INTENT_KEY_LOCKED_REASON);
  152. if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PIN.equals(lockedReason)) {
  153. mSimState = IccCardConstants.State.PIN_REQUIRED;
  154. }
  155. else if (IccCardConstants.INTENT_VALUE_LOCKED_ON_PUK.equals(lockedReason)) {
  156. mSimState = IccCardConstants.State.PUK_REQUIRED;
  157. }
  158. else {
  159. mSimState = IccCardConstants.State.NETWORK_LOCKED;
  160. }
  161. } else {
  162. mSimState = IccCardConstants.State.UNKNOWN;
  163. }
  164. }
  165. private final void updateVolume() {
  166. AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
  167. final int ringerMode = audioManager.getRingerMode();
  168. final boolean visible = ringerMode == AudioManager.RINGER_MODE_SILENT ||
  169. ringerMode == AudioManager.RINGER_MODE_VIBRATE;
  170. final int iconId;
  171. String contentDescription = null;
  172. if (ringerMode == AudioManager.RINGER_MODE_VIBRATE) {
  173. iconId = R.drawable.stat_sys_ringer_vibrate;
  174. contentDescription = mContext.getString(R.string.accessibility_ringer_vibrate);
  175. } else {
  176. iconId = R.drawable.stat_sys_ringer_silent;
  177. contentDescription = mContext.getString(R.string.accessibility_ringer_silent);
  178. }
  179. if (visible) {
  180. mService.setIcon("volume", iconId, 0, contentDescription);
  181. }
  182. if (visible != mVolumeVisible) {
  183. mService.setIconVisibility("volume", visible);
  184. mVolumeVisible = visible;
  185. }
  186. }
  187. private final void updateBluetooth(Intent intent) {
  188. int iconId = R.drawable.stat_sys_data_bluetooth;
  189. String contentDescription = null;
  190. String action = intent.getAction();
  191. if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
  192. int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
  193. mBluetoothEnabled = state == BluetoothAdapter.STATE_ON;
  194. } else if (action.equals(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) {
  195. int state = intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE,
  196. BluetoothAdapter.STATE_DISCONNECTED);
  197. if (state == BluetoothAdapter.STATE_CONNECTED) {
  198. iconId = R.drawable.stat_sys_data_bluetooth_connected;
  199. contentDescription = mContext.getString(R.string.accessibility_bluetooth_connected);
  200. } else {
  201. contentDescription = mContext.getString(
  202. R.string.accessibility_bluetooth_disconnected);
  203. }
  204. } else {
  205. return;
  206. }
  207. mService.setIcon("bluetooth", iconId, 0, contentDescription);
  208. mService.setIconVisibility("bluetooth", mBluetoothEnabled);
  209. }
  210. private final void updateTTY(Intent intent) {
  211. final String action = intent.getAction();
  212. final boolean enabled = intent.getBooleanExtra(TtyIntent.TTY_ENABLED, false);
  213. if (false) Log.v(TAG, "updateTTY: enabled: " + enabled);
  214. if (enabled) {
  215. // TTY is on
  216. if (false) Log.v(TAG, "updateTTY: set TTY on");
  217. mService.setIcon("tty", R.drawable.stat_sys_tty_mode, 0,
  218. mContext.getString(R.string.accessibility_tty_enabled));
  219. mService.setIconVisibility("tty", true);
  220. } else {
  221. // TTY is off
  222. if (false) Log.v(TAG, "updateTTY: set TTY off");
  223. mService.setIconVisibility("tty", false);
  224. }
  225. }
  226. }