diff options
| author | Meticulus <theonejohnnyd@gmail.com> | 2016-10-24 15:13:24 +0200 |
|---|---|---|
| committer | LorDClockaN <lordclockan@gmail.com> | 2016-10-24 15:37:47 +0200 |
| commit | 07501c80d4ec8f370782c1f50a59275d6f6fcea9 (patch) | |
| tree | f3c2eda099f68f6cbb1bb03d3106e92afbd8eb10 | |
| parent | 85e5dce1f2a2de33efbb499087cd7c647854d9e8 (diff) | |
Live Volume Steps [2/2]n7.0
-Volume steps changes are instant, no need to reboot!
LiveVolumeSteps: transitioned volume steps volume normalization.
-This calculates an equivalent volume ratio for the new
volume steps.
-example: Volume steps are set at 60. Volume is set at 5. That is
very low volume. Then volume steps are switched to 5. Now you
are instantly in a state were your volume steps are 5 and your
volume is 5. That's full volume! This fixes that issue.
-So it basically keeps the volume from jumping around while your
changing volume steps. It may be a little lower but should be
very close.
**** Bringup for Marshmallow by DirtyUnicorns ****
-- set ListPreference values onCreate and onPreferenceChange
-- Use separate methods for updating preferences and
commiting change to settings provider and AudioService
-- Support for resetting to default values
**** AICP'fied
Change-Id: I06403c703767baedf6d44170e8a0baed91d8d743
| -rw-r--r-- | res/values/aicp_arrays.xml | 10 | ||||
| -rw-r--r-- | res/values/aicp_strings.xml | 17 | ||||
| -rw-r--r-- | res/xml/sound_settings.xml | 7 | ||||
| -rw-r--r-- | res/xml/volume_steps_settings.xml | 60 | ||||
| -rw-r--r-- | src/com/android/settings/aicp/VolumeSteps.java | 194 |
5 files changed, 287 insertions, 1 deletions
diff --git a/res/values/aicp_arrays.xml b/res/values/aicp_arrays.xml index 6dcd6d0e193..5ff96df561f 100644 --- a/res/values/aicp_arrays.xml +++ b/res/values/aicp_arrays.xml @@ -144,4 +144,14 @@ <item>1</item> </string-array> + <!-- Volume step options. --> + <string-array name="volume_steps_entries" translatable="false"> + <item>@string/volume_steps_60</item> + <item>@string/volume_steps_45</item> + <item>@string/volume_steps_30</item> + <item>@string/volume_steps_15</item> + <item>@string/volume_steps_7</item> + <item>@string/volume_steps_5</item> + </string-array> + </resources> diff --git a/res/values/aicp_strings.xml b/res/values/aicp_strings.xml index a43ef8c99f3..2a746b44071 100644 --- a/res/values/aicp_strings.xml +++ b/res/values/aicp_strings.xml @@ -243,4 +243,21 @@ <string name="link_volume_option_title">Link ringtone & notification volumes</string> + <!-- Volume Steps --> + <string name="volume_steps_title">Volume steps</string> + <string name="volume_steps_alarm_title">Volume steps: Alarm</string> + <string name="volume_steps_dtmf_title">Volume steps: DTMF</string> + <string name="volume_steps_music_title">Volume steps: Music</string> + <string name="volume_steps_notification_title">Volume steps: Notification</string> + <string name="volume_steps_ring_title">Volume steps: Ringer</string> + <string name="volume_steps_system_title">Volume steps: System</string> + <string name="volume_steps_voice_call_title">Volume steps: Voice Call</string> + <string name="volume_steps_reset">Reset</string> + <string name="volume_steps_60">60</string> + <string name="volume_steps_45">45</string> + <string name="volume_steps_30">30</string> + <string name="volume_steps_15">15</string> + <string name="volume_steps_7">7</string> + <string name="volume_steps_5">5</string> + </resources> diff --git a/res/xml/sound_settings.xml b/res/xml/sound_settings.xml index abbdf84fa8c..43295d9422f 100644 --- a/res/xml/sound_settings.xml +++ b/res/xml/sound_settings.xml @@ -20,6 +20,12 @@ android:key="sound_settings" settings:keywords="@string/keywords_sounds"> + <!-- Volume Steps --> + <PreferenceScreen + android:key="volume_steps" + android:title="@string/volume_steps_title" + android:fragment="com.android.settings.aicp.VolumeSteps" /> + <!-- Media volume --> <com.android.settings.notification.VolumeSeekBarPreference android:key="media_volume" @@ -54,7 +60,6 @@ android:key="vibrate_when_ringing" android:title="@string/vibrate_when_ringing_title" /> - <!-- Interruptions --> <com.android.settingslib.RestrictedPreference android:key="zen_mode" diff --git a/res/xml/volume_steps_settings.xml b/res/xml/volume_steps_settings.xml new file mode 100644 index 00000000000..143bb9e375f --- /dev/null +++ b/res/xml/volume_steps_settings.xml @@ -0,0 +1,60 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- Copyright (C) 2008 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. +--> + +<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" + android:title="@string/volume_steps_title" + xmlns:settings="http://schemas.android.com/apk/res/com.android.settings"> + + <ListPreference + android:key="volume_steps_alarm" + android:title="@string/volume_steps_alarm_title" + android:entries="@array/volume_steps_entries" + android:entryValues="@array/volume_steps_entries" /> + + <ListPreference + android:key="volume_steps_dtmf" + android:title="@string/volume_steps_dtmf_title" + android:entries="@array/volume_steps_entries" + android:entryValues="@array/volume_steps_entries" /> + + <ListPreference + android:key="volume_steps_music" + android:title="@string/volume_steps_music_title" + android:entries="@array/volume_steps_entries" + android:entryValues="@array/volume_steps_entries" /> + + <ListPreference + android:key="volume_steps_notification" + android:title="@string/volume_steps_notification_title" + android:entries="@array/volume_steps_entries" + android:entryValues="@array/volume_steps_entries" /> + + <ListPreference + android:key="volume_steps_ring" + android:title="@string/volume_steps_ring_title" + android:entries="@array/volume_steps_entries" + android:entryValues="@array/volume_steps_entries" /> + + <ListPreference + android:key="volume_steps_system" + android:title="@string/volume_steps_system_title" + android:entries="@array/volume_steps_entries" + android:entryValues="@array/volume_steps_entries" /> + + <ListPreference + android:key="volume_steps_voice_call" + android:title="@string/volume_steps_voice_call_title" + android:entries="@array/volume_steps_entries" + android:entryValues="@array/volume_steps_entries" /> + +</PreferenceScreen>
\ No newline at end of file diff --git a/src/com/android/settings/aicp/VolumeSteps.java b/src/com/android/settings/aicp/VolumeSteps.java new file mode 100644 index 00000000000..9a655cf02c4 --- /dev/null +++ b/src/com/android/settings/aicp/VolumeSteps.java @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2015 The DU 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.aicp; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import android.content.Context; +import android.content.SharedPreferences; +import android.media.AudioManager; +import android.os.Bundle; +import android.support.v7.preference.ListPreference; +import android.support.v7.preference.Preference; +import android.support.v7.preference.PreferenceScreen; +import android.provider.Settings; +import android.telephony.TelephonyManager; +import android.text.TextUtils; +import android.util.Log; + +import com.android.internal.logging.MetricsLogger; +import com.android.settings.R; +import com.android.settings.SettingsPreferenceFragment; + +public class VolumeSteps extends SettingsPreferenceFragment implements + Preference.OnPreferenceChangeListener { + private static final String TAG = "VolumeSteps"; + private static final String VOLUME_STEP_DEFAULTS = "volume_step_defaults"; + private static final String FIRST_RUN_KEY = "first_run"; + + // base map of all preference keys and the associated stream + private static final Map<String, Integer> volume_map = new HashMap<String, Integer>(); + static { + volume_map.put("volume_steps_alarm", new Integer(AudioManager.STREAM_ALARM)); + volume_map.put("volume_steps_dtmf", new Integer(AudioManager.STREAM_DTMF)); + volume_map.put("volume_steps_music", new Integer(AudioManager.STREAM_MUSIC)); + volume_map.put("volume_steps_notification", new Integer(AudioManager.STREAM_NOTIFICATION)); + volume_map.put("volume_steps_ring", new Integer(AudioManager.STREAM_RING)); + volume_map.put("volume_steps_system", new Integer(AudioManager.STREAM_SYSTEM)); + volume_map.put("volume_steps_voice_call", new Integer(AudioManager.STREAM_VOICE_CALL)); + } + + // entries to remove on non-telephony devices + private static final Set<String> telephony_set = new HashSet<String>(); + static { + telephony_set.add("volume_steps_dtmf"); + telephony_set.add("volume_steps_ring"); + telephony_set.add("volume_steps_voice_call"); + } + + // set of available pref keys after device configuration filter + private Set<String> mAvailableKeys = new HashSet<String>(); + private AudioManager mAudioManager; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.volume_steps_settings); + mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); + + final PreferenceScreen prefScreen = getPreferenceScreen(); + mAvailableKeys = volume_map.keySet(); + + // remove invalid audio stream prefs + boolean isPhone = TelephonyManager.getDefault().getCurrentPhoneType() != TelephonyManager.PHONE_TYPE_NONE; + + if (!isPhone) { + // remove telephony keys from available set + mAvailableKeys.removeAll(telephony_set); + for (String key : telephony_set) { + Preference toRemove = prefScreen.findPreference(key); + if (toRemove != null) { + prefScreen.removePreference(toRemove); + } + } + } + + // check prefs for initial run of this fragment + final boolean firstRun = checkForFirstRun(); + + // entries array isn't translatable ugh + final String defEntry = getString(R.string.volume_steps_reset); + + // initialize prefs: set defaults if first run, set listeners and update values + for (String key : mAvailableKeys) { + Preference pref = prefScreen.findPreference(key); + if (pref == null || !(pref instanceof ListPreference)) { + continue; + } + final ListPreference listPref = (ListPreference) pref; + int steps = mAudioManager.getStreamMaxVolume(volume_map.get(key)); + if (firstRun) { + saveDefaultSteps(listPref, steps); + } + final int defSteps = getDefaultSteps(listPref); + CharSequence[] entries = new CharSequence[listPref.getEntries().length + 1]; + CharSequence[] values = new CharSequence[listPref.getEntryValues().length + 1]; + + for (int i = 0; i < entries.length; i++) { + if (i == 0) { + entries[i] = defEntry; + values[i] = String.valueOf(defSteps); + continue; + } + entries[i] = listPref.getEntries()[i - 1]; + values[i] = listPref.getEntryValues()[i - 1]; + } + listPref.setEntries(entries); + listPref.setEntryValues(values); + updateVolumeStepPrefs(listPref, steps); + listPref.setOnPreferenceChangeListener(this); + } + } + + @Override + public boolean onPreferenceChange(Preference preference, Object objValue) { + if (preference.hasKey() && mAvailableKeys.contains(preference.getKey())) { + commitVolumeSteps(preference, Integer.parseInt(objValue.toString())); + } + return true; + } + + private SharedPreferences getDefaultStepsPrefs() { + return getActivity().getSharedPreferences(VOLUME_STEP_DEFAULTS, + Context.MODE_PRIVATE); + } + + // test for initial run of this fragment + private boolean checkForFirstRun() { + String isFirstRun = getDefaultStepsPrefs().getString(FIRST_RUN_KEY, null); + if (isFirstRun == null) { + getDefaultStepsPrefs().edit().putString(FIRST_RUN_KEY, "first_run_initialized").commit(); + return true; + } + return false; + } + + @Override + protected int getMetricsCategory() { + return MetricsLogger.AICPEXTRAS; + } + + + private int getDefaultSteps(Preference pref) { + if (pref == null || !(pref instanceof ListPreference)) { + // unlikely + return -1; + } + String key = pref.getKey(); + String value = getDefaultStepsPrefs().getString(key, null); + if (value == null) { + // unlikely + return -1; + } + return Integer.parseInt(value); + } + + // on the initial run, let's store true device defaults in sharedPrefs + private void saveDefaultSteps(Preference volPref, int defaultSteps) { + String key = volPref.getKey(); + getDefaultStepsPrefs().edit().putString(key, String.valueOf(defaultSteps)).commit(); + } + + private void updateVolumeStepPrefs(Preference pref, int steps) { + if (pref == null || !(pref instanceof ListPreference)) { + return; + } + final ListPreference listPref = (ListPreference) pref; + listPref.setSummary(String.valueOf(steps)); + listPref.setValue(String.valueOf(steps)); + } + + private void commitVolumeSteps(Preference pref, int steps) { + Settings.System.putInt(getContentResolver(), pref.getKey(), steps); + mAudioManager.setStreamMaxVolume(volume_map.get(pref.getKey()), steps); + updateVolumeStepPrefs(pref, steps); + Log.i(TAG, "Volume steps:" + pref.getKey() + "" + String.valueOf(steps)); + } +} |
