1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.tv.quicksettings; 18 19 import android.app.AlertDialog; 20 import android.content.DialogInterface; 21 import android.content.Intent; 22 import android.content.SharedPreferences; 23 import android.content.res.Resources; 24 import android.os.Bundle; 25 import android.support.v17.preference.LeanbackPreferenceFragment; 26 import android.support.v7.preference.ListPreference; 27 import android.support.v7.preference.Preference; 28 29 import java.util.Arrays; 30 import java.util.List; 31 32 public class QuickSettingsPreferenceFragment extends LeanbackPreferenceFragment implements 33 SharedPreferences.OnSharedPreferenceChangeListener, Preference.OnPreferenceClickListener { 34 35 private ListPreference mPresetPref; 36 private Preference mBacklightPref; 37 private Preference mContrastPref; 38 private Preference mBrightnessPref; 39 private Preference mSharpnessPref; 40 private Preference mColorPref; 41 private Preference mTintPref; 42 43 private PresetSettingsListener mPresetSettingsListener; 44 45 @Override onCreate(Bundle savedInstanceState)46 public void onCreate(Bundle savedInstanceState) { 47 super.onCreate(savedInstanceState); 48 49 mPresetSettingsListener = new PresetSettingsListener(getActivity()); 50 } 51 52 @Override onCreatePreferences(Bundle savedInstanceState, String key)53 public void onCreatePreferences(Bundle savedInstanceState, String key) { 54 setPreferencesFromResource(R.xml.quick_settings, key); 55 56 mPresetPref = (ListPreference) findPreference("preset"); 57 mPresetPref.setOnPreferenceClickListener(this); 58 59 mBacklightPref = findPreference("backlight"); 60 mBacklightPref.setOnPreferenceClickListener(this); 61 62 mContrastPref = findPreference("contrast"); 63 mContrastPref.setOnPreferenceClickListener(this); 64 65 mBrightnessPref = findPreference("brightness"); 66 mBrightnessPref.setOnPreferenceClickListener(this); 67 68 mSharpnessPref = findPreference("sharpness"); 69 mSharpnessPref.setOnPreferenceClickListener(this); 70 71 mColorPref = findPreference("color"); 72 mColorPref.setOnPreferenceClickListener(this); 73 74 mTintPref = findPreference("tint"); 75 mTintPref.setOnPreferenceClickListener(this); 76 77 final Preference resetPreference = findPreference("reset"); 78 resetPreference.setOnPreferenceClickListener(this); 79 } 80 81 @Override onResume()82 public void onResume() { 83 super.onResume(); 84 85 final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); 86 sharedPreferences.registerOnSharedPreferenceChangeListener(this); 87 sharedPreferences.registerOnSharedPreferenceChangeListener(mPresetSettingsListener); 88 89 updateDescriptions(sharedPreferences); 90 } 91 92 93 @Override onPause()94 public void onPause() { 95 super.onPause(); 96 97 final SharedPreferences sharedPreferences = getPreferenceManager().getSharedPreferences(); 98 sharedPreferences.unregisterOnSharedPreferenceChangeListener(this); 99 sharedPreferences.unregisterOnSharedPreferenceChangeListener(mPresetSettingsListener); 100 } 101 updateDescriptions(SharedPreferences sharedPreferences)102 private void updateDescriptions(SharedPreferences sharedPreferences) { 103 final Resources res = getResources(); 104 mPresetPref.setSummary(mPresetPref.getEntry()); 105 mBacklightPref.setSummary(String.format("%d", sharedPreferences.getInt("backlight", 106 res.getInteger(R.integer.standard_setting_backlight)))); 107 mContrastPref.setSummary(String.format("%d", sharedPreferences.getInt("contrast", 108 res.getInteger(R.integer.standard_setting_contrast)))); 109 mBrightnessPref.setSummary(String.format("%d", sharedPreferences.getInt("brightness", 110 res.getInteger(R.integer.standard_setting_brightness)))); 111 mSharpnessPref.setSummary(String.format("%d", sharedPreferences.getInt("sharpness", 112 res.getInteger(R.integer.standard_setting_sharpness)))); 113 mColorPref.setSummary(String.format("%d", sharedPreferences.getInt("color", 114 res.getInteger(R.integer.standard_setting_color)))); 115 mTintPref.setSummary(String.format("%d", sharedPreferences.getInt("tint", 116 res.getInteger(R.integer.standard_setting_tint)))); 117 } 118 119 @Override onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)120 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 121 switch (key) { 122 case "preset": 123 case "backlight": 124 case "contrast": 125 case "brightness": 126 case "sharpness": 127 case "color": 128 case "tint": 129 updateDescriptions(sharedPreferences); 130 } 131 } 132 launchSettingsDialog(int initialPos)133 private void launchSettingsDialog(int initialPos) { 134 final Intent intent = new Intent(getActivity(), SettingsDialog.class); 135 intent.putExtra(SettingsDialog.EXTRA_START_POS, initialPos); 136 startActivity(intent); 137 } 138 139 @Override onPreferenceClick(Preference preference)140 public boolean onPreferenceClick(Preference preference) { 141 final String key = preference.getKey(); 142 final List<String> keys = 143 Arrays.asList(getResources().getStringArray(R.array.setting_keys)); 144 final int pos = keys.indexOf(key); 145 switch (key) { 146 case "preset": 147 case "backlight": 148 case "contrast": 149 case "brightness": 150 case "sharpness": 151 case "color": 152 case "tint": 153 launchSettingsDialog(pos); 154 return true; 155 case "reset": 156 new AlertDialog.Builder(getActivity()).setPositiveButton( 157 android.R.string.ok, new DialogInterface.OnClickListener() { 158 @Override 159 public void onClick(DialogInterface dialog, int id) { 160 // User clicked OK button 161 getPreferenceManager().getSharedPreferences().edit() 162 .putString("preset", "standard") 163 .commit(); 164 } 165 }).setNegativeButton(android.R.string.cancel, 166 new DialogInterface.OnClickListener() { 167 @Override 168 public void onClick(DialogInterface dialog, int id) { 169 // User cancelled the dialog - do nothing 170 } 171 }).setTitle(R.string.reset_dialog_message).create().show(); 172 return true; 173 } 174 return false; 175 } 176 177 @Override onDisplayPreferenceDialog(Preference preference)178 public void onDisplayPreferenceDialog(Preference preference) { 179 // Do nothing 180 } 181 } 182