Refactor settings hierarchy and clump all volumes in one dialog.

Bug:2362077 Bug:2312836 Bug:2166486

Split Sound and Display into separate top-level settings.
All volume settings (including Alarm volume) now in one Volume dialog.
Remove some sub-texts to reduce clutter.
diff --git a/src/com/android/settings/DisplaySettings.java b/src/com/android/settings/DisplaySettings.java
new file mode 100644
index 0000000..28a5829
--- /dev/null
+++ b/src/com/android/settings/DisplaySettings.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.settings;
+
+import static android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
+
+import com.android.settings.bluetooth.DockEventReceiver;
+
+import android.content.BroadcastReceiver;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.media.AudioManager;
+import android.os.Bundle;
+import android.os.IMountService;
+import android.os.RemoteException;
+import android.os.ServiceManager;
+import android.preference.CheckBoxPreference;
+import android.preference.ListPreference;
+import android.preference.Preference;
+import android.preference.PreferenceActivity;
+import android.preference.PreferenceGroup;
+import android.preference.PreferenceScreen;
+import android.provider.Settings;
+import android.provider.Settings.SettingNotFoundException;
+import android.telephony.TelephonyManager;
+import android.util.Log;
+import android.view.IWindowManager;
+
+public class DisplaySettings extends PreferenceActivity implements
+        Preference.OnPreferenceChangeListener {
+    private static final String TAG = "DisplaySettings";
+
+    /** If there is no setting in the provider, use this. */
+    private static final int FALLBACK_SCREEN_TIMEOUT_VALUE = 30000;
+
+    private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
+    private static final String KEY_ANIMATIONS = "animations";
+    private static final String KEY_ACCELEROMETER = "accelerometer";
+
+    private ListPreference mAnimations;
+    private CheckBoxPreference mAccelerometer;
+    private float[] mAnimationScales;
+
+    private IWindowManager mWindowManager;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        ContentResolver resolver = getContentResolver();
+        mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
+
+        addPreferencesFromResource(R.xml.display_settings);
+
+        mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS);
+        mAnimations.setOnPreferenceChangeListener(this);
+        mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
+        mAccelerometer.setPersistent(false);
+
+        ListPreference screenTimeoutPreference =
+            (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
+        screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt(
+                resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE)));
+        screenTimeoutPreference.setOnPreferenceChangeListener(this);
+
+    }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+
+        updateState(true);
+    }
+
+    private void updateState(boolean force) {
+        int animations = 0;
+        try {
+            mAnimationScales = mWindowManager.getAnimationScales();
+        } catch (RemoteException e) {
+        }
+        if (mAnimationScales != null) {
+            if (mAnimationScales.length >= 1) {
+                animations = ((int)(mAnimationScales[0]+.5f)) % 10;
+            }
+            if (mAnimationScales.length >= 2) {
+                animations += (((int)(mAnimationScales[1]+.5f)) & 0x7) * 10;
+            }
+        }
+        int idx = 0;
+        int best = 0;
+        CharSequence[] aents = mAnimations.getEntryValues();
+        for (int i=0; i<aents.length; i++) {
+            int val = Integer.parseInt(aents[i].toString());
+            if (val <= animations && val > best) {
+                best = val;
+                idx = i;
+            }
+        }
+        mAnimations.setValueIndex(idx);
+        updateAnimationsSummary(mAnimations.getValue());
+        mAccelerometer.setChecked(Settings.System.getInt(
+                getContentResolver(),
+                Settings.System.ACCELEROMETER_ROTATION, 0) != 0);
+    }
+
+    private void updateAnimationsSummary(Object value) {
+        CharSequence[] summaries = getResources().getTextArray(R.array.animations_summaries);
+        CharSequence[] values = mAnimations.getEntryValues();
+        for (int i=0; i<values.length; i++) {
+            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
+            //        + mAnimations.getValue());
+            if (values[i].equals(value)) {
+                mAnimations.setSummary(summaries[i]);
+                break;
+            }
+        }
+    }
+
+    @Override
+    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
+        if (preference == mAccelerometer) {
+            Settings.System.putInt(getContentResolver(),
+                    Settings.System.ACCELEROMETER_ROTATION,
+                    mAccelerometer.isChecked() ? 1 : 0);
+        }
+        return true;
+    }
+
+    public boolean onPreferenceChange(Preference preference, Object objValue) {
+        final String key = preference.getKey();
+        if (KEY_ANIMATIONS.equals(key)) {
+            try {
+                int value = Integer.parseInt((String) objValue);
+                if (mAnimationScales.length >= 1) {
+                    mAnimationScales[0] = value%10;
+                }
+                if (mAnimationScales.length >= 2) {
+                    mAnimationScales[1] = (value/10)%10;
+                }
+                try {
+                    mWindowManager.setAnimationScales(mAnimationScales);
+                } catch (RemoteException e) {
+                }
+                updateAnimationsSummary(objValue);
+            } catch (NumberFormatException e) {
+                Log.e(TAG, "could not persist animation setting", e);
+            }
+
+        }
+        if (KEY_SCREEN_TIMEOUT.equals(key)) {
+            int value = Integer.parseInt((String) objValue);
+            try {
+                Settings.System.putInt(getContentResolver(),
+                        SCREEN_OFF_TIMEOUT, value);
+            } catch (NumberFormatException e) {
+                Log.e(TAG, "could not persist screen timeout setting", e);
+            }
+        }
+
+        return true;
+    }
+}
diff --git a/src/com/android/settings/RingerVolumePreference.java b/src/com/android/settings/RingerVolumePreference.java
index 0d01965..a546818 100644
--- a/src/com/android/settings/RingerVolumePreference.java
+++ b/src/com/android/settings/RingerVolumePreference.java
@@ -39,16 +39,28 @@
     private static final String TAG = "RingerVolumePreference";
 
     private CheckBox mNotificationsUseRingVolumeCheckbox;
-    private SeekBarVolumizer mNotificationSeekBarVolumizer;
+    private SeekBarVolumizer [] mSeekBarVolumizer;
+    private static final int[] SEEKBAR_ID = new int[] {
+        R.id.notification_volume_seekbar,
+        R.id.media_volume_seekbar,
+        R.id.alarm_volume_seekbar
+    };
+    private static final int[] SEEKBAR_TYPE = new int[] {
+        AudioManager.STREAM_NOTIFICATION,
+        AudioManager.STREAM_MUSIC,
+        AudioManager.STREAM_ALARM
+    };
+    //private SeekBarVolumizer mNotificationSeekBarVolumizer;
     private TextView mNotificationVolumeTitle;
-    
+
     public RingerVolumePreference(Context context, AttributeSet attrs) {
         super(context, attrs);
 
         // The always visible seekbar is for ring volume
         setStreamType(AudioManager.STREAM_RING);
-        
+
         setDialogLayoutResource(R.layout.preference_dialog_ringervolume);
+        mSeekBarVolumizer = new SeekBarVolumizer[SEEKBAR_ID.length];
     }
 
     @Override
@@ -61,13 +73,14 @@
         mNotificationsUseRingVolumeCheckbox.setChecked(Settings.System.getInt(
                 getContext().getContentResolver(),
                 Settings.System.NOTIFICATIONS_USE_RING_VOLUME, 1) == 1);
-        
-        final SeekBar seekBar = (SeekBar) view.findViewById(R.id.notification_volume_seekbar);
-        mNotificationSeekBarVolumizer = new SeekBarVolumizer(getContext(), seekBar,
-                AudioManager.STREAM_NOTIFICATION);
-        
+      
+        for (int i = 0; i < SEEKBAR_ID.length; i++) {
+            SeekBar seekBar = (SeekBar) view.findViewById(SEEKBAR_ID[i]);
+            mSeekBarVolumizer[i] = new SeekBarVolumizer(getContext(), seekBar,
+                SEEKBAR_TYPE[i]);
+        }
+
         mNotificationVolumeTitle = (TextView) view.findViewById(R.id.notification_volume_title);
-        
         setNotificationVolumeVisibility(!mNotificationsUseRingVolumeCheckbox.isChecked());
     }
 
@@ -75,10 +88,11 @@
     protected void onDialogClosed(boolean positiveResult) {
         super.onDialogClosed(positiveResult);
         
-        if (!positiveResult && mNotificationSeekBarVolumizer != null) {
-            mNotificationSeekBarVolumizer.revertVolume();
-        }
-        
+        if (!positiveResult) {
+            for (SeekBarVolumizer vol : mSeekBarVolumizer) {
+                if (vol != null) vol.revertVolume();
+            }
+        }        
         cleanup();
     }
 
@@ -107,29 +121,28 @@
     @Override
     protected void onSampleStarting(SeekBarVolumizer volumizer) {
         super.onSampleStarting(volumizer);
-        
-        if (mNotificationSeekBarVolumizer != null && volumizer != mNotificationSeekBarVolumizer) {
-            mNotificationSeekBarVolumizer.stopSample();
+        for (SeekBarVolumizer vol : mSeekBarVolumizer) {
+            if (vol != volumizer) vol.stopSample();
         }
     }
 
     private void setNotificationVolumeVisibility(boolean visible) {
-        if (mNotificationSeekBarVolumizer != null) {
-            mNotificationSeekBarVolumizer.getSeekBar().setVisibility(
-                    visible ? View.VISIBLE : View.GONE);
-            mNotificationVolumeTitle.setVisibility(visible ? View.VISIBLE : View.GONE);
-        }
+        mSeekBarVolumizer[0].getSeekBar().setVisibility(
+                visible ? View.VISIBLE : View.GONE);
+        mNotificationVolumeTitle.setVisibility(visible ? View.VISIBLE : View.GONE);
     }
-    
+
     private void cleanup() {
-        if (mNotificationSeekBarVolumizer != null) {
-            Dialog dialog = getDialog();
-            if (dialog != null && dialog.isShowing()) {
-                // Stopped while dialog was showing, revert changes
-                mNotificationSeekBarVolumizer.revertVolume();
+        for (int i = 0; i < SEEKBAR_ID.length; i++) {
+            if (mSeekBarVolumizer[i] != null) {
+                Dialog dialog = getDialog();
+                if (dialog != null && dialog.isShowing()) {
+                    // Stopped while dialog was showing, revert changes
+                    mSeekBarVolumizer[i].revertVolume();
+                }
+                mSeekBarVolumizer[i].stop();
+                mSeekBarVolumizer[i] = null;
             }
-            mNotificationSeekBarVolumizer.stop();
-            mNotificationSeekBarVolumizer = null;
         }
     }
 
@@ -142,8 +155,12 @@
         }
 
         final SavedState myState = new SavedState(superState);
-        if (mNotificationSeekBarVolumizer != null) {
-            mNotificationSeekBarVolumizer.onSaveInstanceState(myState.getVolumeStore());
+        VolumeStore[] volumeStore = myState.getVolumeStore(SEEKBAR_ID.length);
+        for (int i = 0; i < SEEKBAR_ID.length; i++) {
+            SeekBarVolumizer vol = mSeekBarVolumizer[i];
+            if (vol != null) {
+                vol.onSaveInstanceState(volumeStore[i]);
+            }
         }
         return myState;
     }
@@ -158,28 +175,44 @@
 
         SavedState myState = (SavedState) state;
         super.onRestoreInstanceState(myState.getSuperState());
-        if (mNotificationSeekBarVolumizer != null) {
-            mNotificationSeekBarVolumizer.onRestoreInstanceState(myState.getVolumeStore());
+        VolumeStore[] volumeStore = myState.getVolumeStore(SEEKBAR_ID.length);
+        for (int i = 0; i < SEEKBAR_ID.length; i++) {
+            SeekBarVolumizer vol = mSeekBarVolumizer[i];
+            if (vol != null) {
+                vol.onRestoreInstanceState(volumeStore[i]);
+            }
         }
     }
 
     private static class SavedState extends BaseSavedState {
-        VolumeStore mVolumeStore = new VolumeStore();
+        VolumeStore [] mVolumeStore;
 
         public SavedState(Parcel source) {
             super(source);
-            mVolumeStore.volume = source.readInt();
-            mVolumeStore.originalVolume = source.readInt();
+            mVolumeStore = new VolumeStore[SEEKBAR_ID.length];
+            for (int i = 0; i < SEEKBAR_ID.length; i++) {
+                mVolumeStore[i] = new VolumeStore();
+                mVolumeStore[i].volume = source.readInt();
+                mVolumeStore[i].originalVolume = source.readInt();
+            }
         }
 
         @Override
         public void writeToParcel(Parcel dest, int flags) {
             super.writeToParcel(dest, flags);
-            dest.writeInt(mVolumeStore.volume);
-            dest.writeInt(mVolumeStore.originalVolume);
+            for (int i = 0; i < SEEKBAR_ID.length; i++) {
+                dest.writeInt(mVolumeStore[i].volume);
+                dest.writeInt(mVolumeStore[i].originalVolume);
+            }
         }
 
-        VolumeStore getVolumeStore() {
+        VolumeStore[] getVolumeStore(int count) {
+            if (mVolumeStore == null || mVolumeStore.length != count) {
+                mVolumeStore = new VolumeStore[count];
+                for (int i = 0; i < count; i++) {
+                    mVolumeStore[i] = new VolumeStore();
+                }
+            }
             return mVolumeStore;
         }
 
diff --git a/src/com/android/settings/SoundAndDisplaySettings.java b/src/com/android/settings/SoundSettings.java
similarity index 72%
rename from src/com/android/settings/SoundAndDisplaySettings.java
rename to src/com/android/settings/SoundSettings.java
index 29eb878..9ac2fe1 100644
--- a/src/com/android/settings/SoundAndDisplaySettings.java
+++ b/src/com/android/settings/SoundSettings.java
@@ -40,7 +40,7 @@
 import android.util.Log;
 import android.view.IWindowManager;
 
-public class SoundAndDisplaySettings extends PreferenceActivity implements
+public class SoundSettings extends PreferenceActivity implements
         Preference.OnPreferenceChangeListener {
     private static final String TAG = "SoundAndDisplaysSettings";
 
@@ -50,12 +50,9 @@
 
     private static final String KEY_SILENT = "silent";
     private static final String KEY_VIBRATE = "vibrate";
-    private static final String KEY_SCREEN_TIMEOUT = "screen_timeout";
     private static final String KEY_DTMF_TONE = "dtmf_tone";
     private static final String KEY_SOUND_EFFECTS = "sound_effects";
     private static final String KEY_HAPTIC_FEEDBACK = "haptic_feedback";
-    private static final String KEY_ANIMATIONS = "animations";
-    private static final String KEY_ACCELEROMETER = "accelerometer";
     private static final String KEY_PLAY_MEDIA_NOTIFICATION_SOUNDS =
             "play_media_notification_sounds";
     private static final String KEY_EMERGENCY_TONE = "emergency_tone";
@@ -79,15 +76,10 @@
     private CheckBoxPreference mDtmfTone;
     private CheckBoxPreference mSoundEffects;
     private CheckBoxPreference mHapticFeedback;
-    private ListPreference mAnimations;
-    private CheckBoxPreference mAccelerometer;
     private CheckBoxPreference mNotificationPulse;
-    private float[] mAnimationScales;
 
     private AudioManager mAudioManager;
 
-    private IWindowManager mWindowManager;
-
     private BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -106,11 +98,10 @@
         int activePhoneType = TelephonyManager.getDefault().getPhoneType();
 
         mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
-        mWindowManager = IWindowManager.Stub.asInterface(ServiceManager.getService("window"));
 
         mMountService = IMountService.Stub.asInterface(ServiceManager.getService("mount"));
 
-        addPreferencesFromResource(R.xml.sound_and_display_settings);
+        addPreferencesFromResource(R.xml.sound_settings);
 
         if (TelephonyManager.PHONE_TYPE_CDMA != activePhoneType) {
             // device is not CDMA, do not display CDMA emergency_tone
@@ -133,16 +124,6 @@
         mHapticFeedback.setPersistent(false);
         mHapticFeedback.setChecked(Settings.System.getInt(resolver,
                 Settings.System.HAPTIC_FEEDBACK_ENABLED, 0) != 0);
-        mAnimations = (ListPreference) findPreference(KEY_ANIMATIONS);
-        mAnimations.setOnPreferenceChangeListener(this);
-        mAccelerometer = (CheckBoxPreference) findPreference(KEY_ACCELEROMETER);
-        mAccelerometer.setPersistent(false);
-
-        ListPreference screenTimeoutPreference =
-            (ListPreference) findPreference(KEY_SCREEN_TIMEOUT);
-        screenTimeoutPreference.setValue(String.valueOf(Settings.System.getInt(
-                resolver, SCREEN_OFF_TIMEOUT, FALLBACK_SCREEN_TIMEOUT_VALUE)));
-        screenTimeoutPreference.setOnPreferenceChangeListener(this);
 
         if (TelephonyManager.PHONE_TYPE_CDMA == activePhoneType) {
             ListPreference emergencyTonePreference =
@@ -219,47 +200,6 @@
                 R.string.silent_mode_incl_alarm_summary :
                 R.string.silent_mode_summary);
 
-        int animations = 0;
-        try {
-            mAnimationScales = mWindowManager.getAnimationScales();
-        } catch (RemoteException e) {
-        }
-        if (mAnimationScales != null) {
-            if (mAnimationScales.length >= 1) {
-                animations = ((int)(mAnimationScales[0]+.5f)) % 10;
-            }
-            if (mAnimationScales.length >= 2) {
-                animations += (((int)(mAnimationScales[1]+.5f)) & 0x7) * 10;
-            }
-        }
-        int idx = 0;
-        int best = 0;
-        CharSequence[] aents = mAnimations.getEntryValues();
-        for (int i=0; i<aents.length; i++) {
-            int val = Integer.parseInt(aents[i].toString());
-            if (val <= animations && val > best) {
-                best = val;
-                idx = i;
-            }
-        }
-        mAnimations.setValueIndex(idx);
-        updateAnimationsSummary(mAnimations.getValue());
-        mAccelerometer.setChecked(Settings.System.getInt(
-                getContentResolver(),
-                Settings.System.ACCELEROMETER_ROTATION, 0) != 0);
-    }
-
-    private void updateAnimationsSummary(Object value) {
-        CharSequence[] summaries = getResources().getTextArray(R.array.animations_summaries);
-        CharSequence[] values = mAnimations.getEntryValues();
-        for (int i=0; i<values.length; i++) {
-            //Log.i("foo", "Comparing entry "+ values[i] + " to current "
-            //        + mAnimations.getValue());
-            if (values[i].equals(value)) {
-                mAnimations.setSummary(summaries[i]);
-                break;
-            }
-        }
     }
 
     private void setRingerMode(boolean silent, boolean vibrate) {
@@ -301,10 +241,6 @@
             Settings.System.putInt(getContentResolver(), Settings.System.HAPTIC_FEEDBACK_ENABLED,
                     mHapticFeedback.isChecked() ? 1 : 0);
 
-        } else if (preference == mAccelerometer) {
-            Settings.System.putInt(getContentResolver(),
-                    Settings.System.ACCELEROMETER_ROTATION,
-                    mAccelerometer.isChecked() ? 1 : 0);
         } else if (preference == mNotificationPulse) {
             boolean value = mNotificationPulse.isChecked();
             Settings.System.putInt(getContentResolver(),
@@ -316,34 +252,7 @@
 
     public boolean onPreferenceChange(Preference preference, Object objValue) {
         final String key = preference.getKey();
-        if (KEY_ANIMATIONS.equals(key)) {
-            try {
-                int value = Integer.parseInt((String) objValue);
-                if (mAnimationScales.length >= 1) {
-                    mAnimationScales[0] = value%10;
-                }
-                if (mAnimationScales.length >= 2) {
-                    mAnimationScales[1] = (value/10)%10;
-                }
-                try {
-                    mWindowManager.setAnimationScales(mAnimationScales);
-                } catch (RemoteException e) {
-                }
-                updateAnimationsSummary(objValue);
-            } catch (NumberFormatException e) {
-                Log.e(TAG, "could not persist animation setting", e);
-            }
-
-        }
-        if (KEY_SCREEN_TIMEOUT.equals(key)) {
-            int value = Integer.parseInt((String) objValue);
-            try {
-                Settings.System.putInt(getContentResolver(),
-                        SCREEN_OFF_TIMEOUT, value);
-            } catch (NumberFormatException e) {
-                Log.e(TAG, "could not persist screen timeout setting", e);
-            }
-        } else if (KEY_EMERGENCY_TONE.equals(key)) {
+        if (KEY_EMERGENCY_TONE.equals(key)) {
             int value = Integer.parseInt((String) objValue);
             try {
                 Settings.System.putInt(getContentResolver(),
diff --git a/src/com/android/settings/fuelgauge/PowerUsageDetail.java b/src/com/android/settings/fuelgauge/PowerUsageDetail.java
index 0c44c40..a98bfed 100644
--- a/src/com/android/settings/fuelgauge/PowerUsageDetail.java
+++ b/src/com/android/settings/fuelgauge/PowerUsageDetail.java
@@ -271,7 +271,7 @@
                 }
                 break;
             case SCREEN:
-                addControl(R.string.sound_and_display_settings,
+                addControl(R.string.display_settings,
                         R.string.battery_sugg_display,
                         ACTION_DISPLAY_SETTINGS);
                 removeHeader = false;