From 7ee994d728acf2b3b3297a67001f2d21c9094109 Mon Sep 17 00:00:00 2001 From: Amith Yamasani Date: Thu, 24 May 2012 13:53:26 -0700 Subject: Show power menu on tablets. Bug: 6524432 Show power menu on all devices by default. Specific devices will be disabled in overlays. Handle airplane mode changes differently when the telephony states are not reliable. Use simple toggle for silent mode when there's no vibrator. Change-Id: Ic5ef521eee19cd300d909250203ff204f3a1ae1e --- .../internal/policy/impl/GlobalActions.java | 107 +++++++++++++++++---- 1 file changed, 90 insertions(+), 17 deletions(-) (limited to 'policy') diff --git a/policy/src/com/android/internal/policy/impl/GlobalActions.java b/policy/src/com/android/internal/policy/impl/GlobalActions.java index aa73de4..fc187ce 100644 --- a/policy/src/com/android/internal/policy/impl/GlobalActions.java +++ b/policy/src/com/android/internal/policy/impl/GlobalActions.java @@ -28,7 +28,9 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.UserInfo; +import android.database.ContentObserver; import android.media.AudioManager; +import android.net.ConnectivityManager; import android.os.Handler; import android.os.IBinder; import android.os.Message; @@ -73,7 +75,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac private ArrayList mItems; private AlertDialog mDialog; - private SilentModeAction mSilentModeAction; + private Action mSilentModeAction; private ToggleAction mAirplaneModeOn; private MyAdapter mAdapter; @@ -82,6 +84,8 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac private boolean mDeviceProvisioned = false; private ToggleAction.State mAirplaneState = ToggleAction.State.Off; private boolean mIsWaitingForEcmExit = false; + private boolean mHasTelephony; + private boolean mHasVibrator; private IWindowManager mIWindowManager; @@ -104,6 +108,14 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE); + ConnectivityManager cm = (ConnectivityManager) + context.getSystemService(Context.CONNECTIVITY_SERVICE); + mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE); + mContext.getContentResolver().registerContentObserver( + Settings.System.getUriFor(Settings.System.AIRPLANE_MODE_ON), true, + mAirplaneModeObserver); + Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); + mHasVibrator = vibrator != null && vibrator.hasVibrator(); } /** @@ -130,13 +142,18 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac mDialog.show(); mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND); } + /** * Create the global actions dialog. * @return A new dialog. */ private AlertDialog createDialog() { - mSilentModeAction = new SilentModeAction(mContext, mAudioManager, mHandler); - + // Simple toggle style if there's no vibrator, otherwise use a tri-state + if (!mHasVibrator) { + mSilentModeAction = new SilentModeToggleAction(); + } else { + mSilentModeAction = new SilentModeTriStateAction(mContext, mAudioManager, mHandler); + } mAirplaneModeOn = new ToggleAction( R.drawable.ic_lock_airplane_mode, R.drawable.ic_lock_airplane_mode_off, @@ -145,7 +162,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac R.string.global_actions_airplane_mode_off_status) { void onToggle(boolean on) { - if (Boolean.parseBoolean( + if (mHasTelephony && Boolean.parseBoolean( SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { mIsWaitingForEcmExit = true; // Launch ECM exit dialog @@ -160,6 +177,8 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac @Override protected void changeStateFromPress(boolean buttonOn) { + if (!mHasTelephony) return; + // In ECM mode airplane state cannot be changed if (!(Boolean.parseBoolean( SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) { @@ -176,6 +195,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac return false; } }; + onAirplaneModeChanged(); mItems = new ArrayList(); @@ -247,6 +267,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac mItems.add(switchToUser); } } + mAdapter = new MyAdapter(); final AlertDialog.Builder ab = new AlertDialog.Builder(mContext); @@ -273,8 +294,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac } private void prepareDialog() { - final boolean silentModeOn = - mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; + refreshSilentMode(); mAirplaneModeOn.updateState(mAirplaneState); mAdapter.notifyDataSetChanged(); if (mKeyguardShowing) { @@ -288,6 +308,15 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac } } + private void refreshSilentMode() { + if (!mHasVibrator) { + final boolean silentModeOn = + mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL; + ((ToggleAction)mSilentModeAction).updateState( + silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off); + } + } + /** {@inheritDoc} */ public void onDismiss(DialogInterface dialog) { if (SHOW_SILENT_TOGGLE) { @@ -297,7 +326,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac /** {@inheritDoc} */ public void onClick(DialogInterface dialog, int which) { - if (!(mAdapter.getItem(which) instanceof SilentModeAction)) { + if (!(mAdapter.getItem(which) instanceof SilentModeTriStateAction)) { dialog.dismiss(); } mAdapter.getItem(which).onPress(); @@ -495,12 +524,12 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac */ public ToggleAction(int enabledIconResId, int disabledIconResid, - int essage, + int message, int enabledStatusMessageResId, int disabledStatusMessageResId) { mEnabledIconResId = enabledIconResId; mDisabledIconResid = disabledIconResid; - mMessageResId = essage; + mMessageResId = message; mEnabledStatusMessageResId = enabledStatusMessageResId; mDisabledStatusMessageResId = disabledStatusMessageResId; } @@ -583,21 +612,44 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac } } - private static class SilentModeAction implements Action, View.OnClickListener { + private class SilentModeToggleAction extends ToggleAction { + public SilentModeToggleAction() { + super(R.drawable.ic_audio_vol_mute, + R.drawable.ic_audio_vol, + R.string.global_action_toggle_silent_mode, + R.string.global_action_silent_mode_on_status, + R.string.global_action_silent_mode_off_status); + } + + void onToggle(boolean on) { + if (on) { + mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT); + } else { + mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); + } + } + + public boolean showDuringKeyguard() { + return true; + } + + public boolean showBeforeProvisioning() { + return false; + } + } + + private static class SilentModeTriStateAction implements Action, View.OnClickListener { private final int[] ITEM_IDS = { R.id.option1, R.id.option2, R.id.option3 }; private final AudioManager mAudioManager; private final Handler mHandler; - private final boolean mHasVibrator; private final Context mContext; - SilentModeAction(Context context, AudioManager audioManager, Handler handler) { + SilentModeTriStateAction(Context context, AudioManager audioManager, Handler handler) { mAudioManager = audioManager; mHandler = handler; mContext = context; - Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE); - mHasVibrator = vibrator != null && vibrator.hasVibrator(); } private int ringerModeToIndex(int ringerMode) { @@ -621,9 +673,6 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac // Set up click handler itemView.setTag(i); itemView.setOnClickListener(this); - if (itemView.getId() == R.id.option2 && !mHasVibrator) { - itemView.setVisibility(View.GONE); - } } return v; } @@ -683,6 +732,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac PhoneStateListener mPhoneStateListener = new PhoneStateListener() { @Override public void onServiceStateChanged(ServiceState serviceState) { + if (!mHasTelephony) return; final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF; mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off; mAirplaneModeOn.updateState(mAirplaneState); @@ -699,6 +749,13 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac } }; + private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) { + @Override + public void onChange(boolean selfChange) { + onAirplaneModeChanged(); + } + }; + private static final int MESSAGE_DISMISS = 0; private static final int MESSAGE_REFRESH = 1; private static final int MESSAGE_SHOW = 2; @@ -713,6 +770,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac } break; case MESSAGE_REFRESH: + refreshSilentMode(); mAdapter.notifyDataSetChanged(); break; case MESSAGE_SHOW: @@ -722,6 +780,18 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac } }; + private void onAirplaneModeChanged() { + // Let the service state callbacks handle the state. + if (mHasTelephony) return; + + boolean airplaneModeOn = Settings.System.getInt( + mContext.getContentResolver(), + Settings.System.AIRPLANE_MODE_ON, + 0) == 1; + mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off; + mAirplaneModeOn.updateState(mAirplaneState); + } + /** * Change the airplane mode system setting */ @@ -734,6 +804,9 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); intent.putExtra("state", on); mContext.sendBroadcast(intent); + if (!mHasTelephony) { + mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off; + } } private IWindowManager getWindowManager() { -- cgit v1.1