Make Theme a DropDownPreference

Also make DropDownPreference support xml lists defined the same
way as ListPreferences.

Bug: 21300431
Change-Id: I1698e8ddcd6134034c3367a6afe36f9740e9efec
This commit is contained in:
Jason Monk
2015-06-01 15:12:22 -04:00
parent b777c6e205
commit 3d95908e02
4 changed files with 36 additions and 17 deletions

View File

@@ -113,4 +113,9 @@
<!-- Confirm device credentials screen --> <!-- Confirm device credentials screen -->
<attr name="confirmDeviceCredentialsSideMargin" format="dimension" /> <attr name="confirmDeviceCredentialsSideMargin" format="dimension" />
<attr name="confirmDeviceCredentialsTopMargin" format="dimension" /> <attr name="confirmDeviceCredentialsTopMargin" format="dimension" />
<declare-styleable name="DropDownPreference">
<attr name="android:entries" />
<attr name="android:entryValues" />
</declare-styleable>
</resources> </resources>

View File

@@ -65,11 +65,10 @@
android:persistent="false" android:persistent="false"
android:title="@string/system_ui_settings" /> android:title="@string/system_ui_settings" />
<ListPreference <com.android.settings.DropDownPreference
android:key="night_mode" android:key="night_mode"
android:title="@string/night_mode_title" android:title="@string/night_mode_title"
android:summary="@string/night_mode_summary" android:summary="@string/night_mode_summary"
android:persistent="false"
android:entries="@array/night_mode_entries" android:entries="@array/night_mode_entries"
android:entryValues="@array/night_mode_values" /> android:entryValues="@array/night_mode_values" />

View File

@@ -265,7 +265,7 @@ public class DevelopmentSettings extends SettingsPreferenceFragment
private SwitchPreference mShowAllANRs; private SwitchPreference mShowAllANRs;
private ListPreference mNightModePreference; private DropDownPreference mNightModePreference;
private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>(); private final ArrayList<Preference> mAllPrefs = new ArrayList<Preference>();
@@ -427,12 +427,26 @@ public class DevelopmentSettings extends SettingsPreferenceFragment
removePreferenceForProduction(hdcpChecking); removePreferenceForProduction(hdcpChecking);
} }
mNightModePreference = (ListPreference) findPreference(KEY_NIGHT_MODE); mNightModePreference = (DropDownPreference) findPreference(KEY_NIGHT_MODE);
final UiModeManager uiManager = (UiModeManager) getSystemService( final UiModeManager uiManager = (UiModeManager) getSystemService(
Context.UI_MODE_SERVICE); Context.UI_MODE_SERVICE);
final int currentNightMode = uiManager.getNightMode(); final int currentNightMode = uiManager.getNightMode();
mNightModePreference.setValue(String.valueOf(currentNightMode)); mNightModePreference.setSelectedValue(String.valueOf(currentNightMode));
mNightModePreference.setOnPreferenceChangeListener(this); mNightModePreference.setCallback(new DropDownPreference.Callback() {
@Override
public boolean onItemSelected(int pos, Object newValue) {
try {
final int value = Integer.parseInt((String) newValue);
final UiModeManager uiManager = (UiModeManager) getSystemService(
Context.UI_MODE_SERVICE);
uiManager.setNightMode(value);
return true;
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist night mode setting", e);
return false;
}
}
});
} }
private ListPreference addListPreference(String prefKey) { private ListPreference addListPreference(String prefKey) {
@@ -1808,16 +1822,6 @@ public class DevelopmentSettings extends SettingsPreferenceFragment
} else if (preference == mTunerUiPref) { } else if (preference == mTunerUiPref) {
writeTweakUi(newValue); writeTweakUi(newValue);
return true; return true;
} else if (preference == mNightModePreference) {
try {
final int value = Integer.parseInt((String) newValue);
final UiModeManager uiManager = (UiModeManager) getSystemService(
Context.UI_MODE_SERVICE);
uiManager.setNightMode(value);
} catch (NumberFormatException e) {
Log.e(TAG, "could not persist night mode setting", e);
}
return true;
} }
return false; return false;
} }

View File

@@ -17,14 +17,15 @@
package com.android.settings; package com.android.settings;
import android.content.Context; import android.content.Context;
import android.content.res.TypedArray;
import android.preference.Preference; import android.preference.Preference;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Spinner; import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import java.util.ArrayList; import java.util.ArrayList;
@@ -69,6 +70,16 @@ public class DropDownPreference extends Preference {
return true; return true;
} }
}); });
// Support XML specification like ListPreferences do.
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DropDownPreference);
CharSequence[] entries = a.getTextArray(R.styleable.DropDownPreference_android_entries);
CharSequence[] values = a.getTextArray(R.styleable.DropDownPreference_android_entryValues);
if (entries != null && values != null) {
for (int i= 0; i < entries.length; i++) {
addItem(entries[i].toString(), values[i]);
}
}
} }
public void setDropDownWidth(int dimenResId) { public void setDropDownWidth(int dimenResId) {