Support refined vibrate/silent behavior.

See change I14cf91b0 for explanation and required framework
support.

Bug: 2457183
Change-Id: I09ad7d697ff17c24fc41744ed70add214d132bd3
diff --git a/policy/com/android/internal/policy/impl/GlobalActions.java b/policy/com/android/internal/policy/impl/GlobalActions.java
index 8b6257f..b10bda2 100644
--- a/policy/com/android/internal/policy/impl/GlobalActions.java
+++ b/policy/com/android/internal/policy/impl/GlobalActions.java
@@ -117,7 +117,6 @@
      * @return A new dialog.
      */
     private AlertDialog createDialog() {
-
         mSilentModeToggle = new ToggleAction(
                 R.drawable.ic_lock_silent_mode,
                 R.drawable.ic_lock_silent_mode_off,
@@ -125,9 +124,23 @@
                 R.string.global_action_silent_mode_on_status,
                 R.string.global_action_silent_mode_off_status) {
 
+            void willCreate() {
+                // XXX: FIXME: switch to ic_lock_vibrate_mode when available
+                mEnabledIconResId = (Settings.System.getInt(mContext.getContentResolver(),
+                        Settings.System.VIBRATE_IN_SILENT, 1) == 1)
+                    ? R.drawable.ic_lock_silent_mode
+                    : R.drawable.ic_lock_silent_mode;
+            }
+
             void onToggle(boolean on) {
-                mAudioManager.setRingerMode(on ? AudioManager.RINGER_MODE_SILENT
-                        : AudioManager.RINGER_MODE_NORMAL);
+                if (on) {
+                    mAudioManager.setRingerMode((Settings.System.getInt(mContext.getContentResolver(),
+                        Settings.System.VIBRATE_IN_SILENT, 1) == 1)
+                        ? AudioManager.RINGER_MODE_VIBRATE
+                        : AudioManager.RINGER_MODE_SILENT);
+                } else {
+                    mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
+                }
             }
 
             public boolean showDuringKeyguard() {
@@ -412,11 +425,11 @@
         protected State mState = State.Off;
 
         // prefs
-        private final int mEnabledIconResId;
-        private final int mDisabledIconResid;
-        private final int mMessageResId;
-        private final int mEnabledStatusMessageResId;
-        private final int mDisabledStatusMessageResId;
+        protected int mEnabledIconResId;
+        protected int mDisabledIconResid;
+        protected int mMessageResId;
+        protected int mEnabledStatusMessageResId;
+        protected int mDisabledStatusMessageResId;
 
         /**
          * @param enabledIconResId The icon for when this action is on.
@@ -437,8 +450,18 @@
             mDisabledStatusMessageResId = disabledStatusMessageResId;
         }
 
+        /**
+         * Override to make changes to resource IDs just before creating the
+         * View.
+         */
+        void willCreate() {
+
+        }
+
         public View create(Context context, View convertView, ViewGroup parent,
                 LayoutInflater inflater) {
+            willCreate();
+
             View v = (convertView != null) ?
                     convertView :
                     inflater.inflate(R
diff --git a/policy/com/android/internal/policy/impl/LockScreen.java b/policy/com/android/internal/policy/impl/LockScreen.java
index 7724816..22a4336 100644
--- a/policy/com/android/internal/policy/impl/LockScreen.java
+++ b/policy/com/android/internal/policy/impl/LockScreen.java
@@ -37,6 +37,7 @@
 import android.util.Log;
 import android.media.AudioManager;
 import android.os.SystemProperties;
+import android.provider.Settings;
 
 import java.util.Date;
 import java.io.File;
@@ -229,17 +230,23 @@
     }
 
     private boolean isSilentMode() {
-        return mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT;
+        return mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
     }
 
     private void updateRightTabResources() {
+        boolean vibe = mSilentMode 
+            && (mAudioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
+
         mSelector.setRightTabResources(
-                mSilentMode ? R.drawable.ic_jog_dial_sound_off : R.drawable.ic_jog_dial_sound_on,
-                mSilentMode ? R.drawable.jog_tab_target_yellow : R.drawable.jog_tab_target_gray,
+                mSilentMode ? ( vibe ? R.drawable.ic_jog_dial_vibrate_on
+                                     : R.drawable.ic_jog_dial_sound_off ) 
+                            : R.drawable.ic_jog_dial_sound_on,
+                mSilentMode ? R.drawable.jog_tab_target_yellow 
+                            : R.drawable.jog_tab_target_gray,
                 mSilentMode ? R.drawable.jog_tab_bar_right_sound_on
-                        : R.drawable.jog_tab_bar_right_sound_off,
+                            : R.drawable.jog_tab_bar_right_sound_off,
                 mSilentMode ? R.drawable.jog_tab_right_sound_on
-                        : R.drawable.jog_tab_right_sound_off);
+                            : R.drawable.jog_tab_right_sound_off);
     }
 
     private void resetStatusInfo(KeyguardUpdateMonitor updateMonitor) {
@@ -275,8 +282,17 @@
         } else if (whichHandle == SlidingTab.OnTriggerListener.RIGHT_HANDLE) {
             // toggle silent mode
             mSilentMode = !mSilentMode;
-            mAudioManager.setRingerMode(mSilentMode ? AudioManager.RINGER_MODE_SILENT
-                        : AudioManager.RINGER_MODE_NORMAL);
+            if (mSilentMode) {
+                final boolean vibe = (Settings.System.getInt(
+                    getContext().getContentResolver(),
+                    Settings.System.VIBRATE_IN_SILENT, 1) == 1);
+
+                mAudioManager.setRingerMode(vibe
+                    ? AudioManager.RINGER_MODE_VIBRATE 
+                    : AudioManager.RINGER_MODE_SILENT);
+            } else {
+                mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
+            }
 
             updateRightTabResources();
 
@@ -625,7 +641,7 @@
 
     /** {@inheritDoc} */
     public void onRingerModeChanged(int state) {
-        boolean silent = AudioManager.RINGER_MODE_SILENT == state;
+        boolean silent = AudioManager.RINGER_MODE_NORMAL != state;
         if (silent != mSilentMode) {
             mSilentMode = silent;
             updateRightTabResources();