| 1 |
package ${packageName}; |
| 2 |
|
| 3 |
<#assign includeSimple=(minApiLevel?? && minApiLevel lt 10)> |
| 4 |
|
| 5 |
import android.annotation.TargetApi; |
| 6 |
import android.content.Context; |
| 7 |
import android.content.Intent; |
| 8 |
import android.content.res.Configuration; |
| 9 |
import android.media.Ringtone; |
| 10 |
import android.media.RingtoneManager; |
| 11 |
import android.net.Uri; |
| 12 |
import android.os.Build; |
| 13 |
import android.os.Bundle; |
| 14 |
import android.preference.ListPreference; |
| 15 |
import android.preference.Preference; |
| 16 |
import android.preference.PreferenceActivity; |
| 17 |
<#if includeSimple> |
| 18 |
import android.preference.PreferenceCategory; |
| 19 |
</#if> |
| 20 |
import ${PreferenceActionBarClassFqcn}; |
| 21 |
import android.preference.PreferenceFragment; |
| 22 |
import android.preference.PreferenceManager; |
| 23 |
import android.preference.RingtonePreference; |
| 24 |
import android.text.TextUtils; |
| 25 |
import android.view.MenuItem; |
| 26 |
<#if parentActivityClass != ""> |
| 27 |
import android.support.v4.app.NavUtils; |
| 28 |
</#if> |
| 29 |
<#if applicationPackage??> |
| 30 |
import ${applicationPackage}.R; |
| 31 |
</#if> |
| 32 |
import java.util.List; |
| 33 |
|
| 34 |
/** |
| 35 |
* A {@link PreferenceActivity} that presents a set of application settings. On |
| 36 |
* handset devices, settings are presented as a single list. On tablets, |
| 37 |
* settings are split by category, with category headers shown to the left of |
| 38 |
* the list of settings. |
| 39 |
* <p> |
| 40 |
* See <a href="http://developer.android.com/design/patterns/settings.html"> |
| 41 |
* Android Design: Settings</a> for design guidelines and the <a |
| 42 |
* href="http://developer.android.com/guide/topics/ui/settings.html">Settings |
| 43 |
* API Guide</a> for more information on developing a Settings UI. |
| 44 |
*/ |
| 45 |
public class ${activityClass} extends ${preferenceSuperClass} { |
| 46 |
<#if includeSimple> |
| 47 |
/** |
| 48 |
* Determines whether to always show the simplified settings UI, where |
| 49 |
* settings are presented in a single list. When false, settings are shown |
| 50 |
* as a master/detail two-pane view on tablets. When true, a single pane is |
| 51 |
* shown on tablets. |
| 52 |
*/ |
| 53 |
private static final boolean ALWAYS_SIMPLE_PREFS = false; |
| 54 |
|
| 55 |
</#if> |
| 56 |
@Override |
| 57 |
protected void onCreate(Bundle savedInstanceState) { |
| 58 |
super.onCreate(savedInstanceState); |
| 59 |
setupActionBar(); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Set up the {@link android.app.ActionBar}, if the API is available. |
| 64 |
*/ |
| 65 |
private void setupActionBar() { |
| 66 |
ActionBar actionBar = get${PreferenceSupport}ActionBar(); |
| 67 |
if (actionBar != null) { |
| 68 |
// Show the Up button in the action bar. |
| 69 |
actionBar.setDisplayHomeAsUpEnabled(true); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
<#if parentActivityClass != ""> |
| 74 |
@Override |
| 75 |
public boolean onMenuItemSelected(int featureId, MenuItem item) { |
| 76 |
int id = item.getItemId(); |
| 77 |
if (id == android.R.id.home) { |
| 78 |
if (!super.onMenuItemSelected(featureId, item)) { |
| 79 |
NavUtils.navigateUpFromSameTask(this); |
| 80 |
} |
| 81 |
return true; |
| 82 |
} |
| 83 |
return super.onMenuItemSelected(featureId, item); |
| 84 |
} |
| 85 |
|
| 86 |
</#if> |
| 87 |
<#if includeSimple> |
| 88 |
@Override |
| 89 |
protected void onPostCreate(Bundle savedInstanceState) { |
| 90 |
super.onPostCreate(savedInstanceState); |
| 91 |
|
| 92 |
setupSimplePreferencesScreen(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Shows the simplified settings UI if the device configuration if the |
| 97 |
* device configuration dictates that a simplified, single-pane UI should be |
| 98 |
* shown. |
| 99 |
*/ |
| 100 |
private void setupSimplePreferencesScreen() { |
| 101 |
if (!isSimplePreferences(this)) { |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
// In the simplified UI, fragments are not used at all and we instead |
| 106 |
// use the older PreferenceActivity APIs. |
| 107 |
|
| 108 |
// Add 'general' preferences. |
| 109 |
addPreferencesFromResource(R.xml.pref_general); |
| 110 |
|
| 111 |
// Add 'notifications' preferences, and a corresponding header. |
| 112 |
PreferenceCategory fakeHeader = new PreferenceCategory(this); |
| 113 |
fakeHeader.setTitle(R.string.pref_header_notifications); |
| 114 |
getPreferenceScreen().addPreference(fakeHeader); |
| 115 |
addPreferencesFromResource(R.xml.pref_notification); |
| 116 |
|
| 117 |
// Add 'data and sync' preferences, and a corresponding header. |
| 118 |
fakeHeader = new PreferenceCategory(this); |
| 119 |
fakeHeader.setTitle(R.string.pref_header_data_sync); |
| 120 |
getPreferenceScreen().addPreference(fakeHeader); |
| 121 |
addPreferencesFromResource(R.xml.pref_data_sync); |
| 122 |
|
| 123 |
// Bind the summaries of EditText/List/Dialog/Ringtone preferences to |
| 124 |
// their values. When their values change, their summaries are updated |
| 125 |
// to reflect the new value, per the Android Design guidelines. |
| 126 |
bindPreferenceSummaryToValue(findPreference("example_text")); |
| 127 |
bindPreferenceSummaryToValue(findPreference("example_list")); |
| 128 |
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone")); |
| 129 |
bindPreferenceSummaryToValue(findPreference("sync_frequency")); |
| 130 |
} |
| 131 |
</#if> |
| 132 |
|
| 133 |
/** {@inheritDoc} */ |
| 134 |
@Override |
| 135 |
public boolean onIsMultiPane() { |
| 136 |
return isXLargeTablet(this)<#if includeSimple> && !isSimplePreferences(this)</#if>; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Helper method to determine if the device has an extra-large screen. For |
| 141 |
* example, 10" tablets are extra-large. |
| 142 |
*/ |
| 143 |
private static boolean isXLargeTablet(Context context) { |
| 144 |
return (context.getResources().getConfiguration().screenLayout |
| 145 |
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE; |
| 146 |
} |
| 147 |
|
| 148 |
<#if includeSimple> |
| 149 |
/** |
| 150 |
* Determines whether the simplified settings UI should be shown. This is |
| 151 |
* true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device |
| 152 |
* doesn't have newer APIs like {@link PreferenceFragment}, or the device |
| 153 |
* doesn't have an extra-large screen. In these cases, a single-pane |
| 154 |
* "simplified" settings UI should be shown. |
| 155 |
*/ |
| 156 |
private static boolean isSimplePreferences(Context context) { |
| 157 |
return ALWAYS_SIMPLE_PREFS |
| 158 |
|| Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB |
| 159 |
|| !isXLargeTablet(context); |
| 160 |
} |
| 161 |
|
| 162 |
</#if> |
| 163 |
/** {@inheritDoc} */ |
| 164 |
@Override |
| 165 |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 166 |
public void onBuildHeaders(List<Header> target) { |
| 167 |
<#if includeSimple> |
| 168 |
if (!isSimplePreferences(this)) { |
| 169 |
loadHeadersFromResource(R.xml.pref_headers, target); |
| 170 |
} |
| 171 |
<#else> |
| 172 |
loadHeadersFromResource(R.xml.pref_headers, target); |
| 173 |
</#if> |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* A preference value change listener that updates the preference's summary |
| 178 |
* to reflect its new value. |
| 179 |
*/ |
| 180 |
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { |
| 181 |
@Override |
| 182 |
public boolean onPreferenceChange(Preference preference, Object value) { |
| 183 |
String stringValue = value.toString(); |
| 184 |
|
| 185 |
if (preference instanceof ListPreference) { |
| 186 |
// For list preferences, look up the correct display value in |
| 187 |
// the preference's 'entries' list. |
| 188 |
ListPreference listPreference = (ListPreference) preference; |
| 189 |
int index = listPreference.findIndexOfValue(stringValue); |
| 190 |
|
| 191 |
// Set the summary to reflect the new value. |
| 192 |
preference.setSummary( |
| 193 |
index >= 0 |
| 194 |
? listPreference.getEntries()[index] |
| 195 |
: null); |
| 196 |
|
| 197 |
} else if (preference instanceof RingtonePreference) { |
| 198 |
// For ringtone preferences, look up the correct display value |
| 199 |
// using RingtoneManager. |
| 200 |
if (TextUtils.isEmpty(stringValue)) { |
| 201 |
// Empty values correspond to 'silent' (no ringtone). |
| 202 |
preference.setSummary(R.string.pref_ringtone_silent); |
| 203 |
|
| 204 |
} else { |
| 205 |
Ringtone ringtone = RingtoneManager.getRingtone( |
| 206 |
preference.getContext(), Uri.parse(stringValue)); |
| 207 |
|
| 208 |
if (ringtone == null) { |
| 209 |
// Clear the summary if there was a lookup error. |
| 210 |
preference.setSummary(null); |
| 211 |
} else { |
| 212 |
// Set the summary to reflect the new ringtone display |
| 213 |
// name. |
| 214 |
String name = ringtone.getTitle(preference.getContext()); |
| 215 |
preference.setSummary(name); |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
} else { |
| 220 |
// For all other preferences, set the summary to the value's |
| 221 |
// simple string representation. |
| 222 |
preference.setSummary(stringValue); |
| 223 |
} |
| 224 |
return true; |
| 225 |
} |
| 226 |
}; |
| 227 |
|
| 228 |
/** |
| 229 |
* Binds a preference's summary to its value. More specifically, when the |
| 230 |
* preference's value is changed, its summary (line of text below the |
| 231 |
* preference title) is updated to reflect the value. The summary is also |
| 232 |
* immediately updated upon calling this method. The exact display format is |
| 233 |
* dependent on the type of preference. |
| 234 |
* |
| 235 |
* @see #sBindPreferenceSummaryToValueListener |
| 236 |
*/ |
| 237 |
private static void bindPreferenceSummaryToValue(Preference preference) { |
| 238 |
// Set the listener to watch for value changes. |
| 239 |
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener); |
| 240 |
|
| 241 |
// Trigger the listener immediately with the preference's |
| 242 |
// current value. |
| 243 |
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, |
| 244 |
PreferenceManager |
| 245 |
.getDefaultSharedPreferences(preference.getContext()) |
| 246 |
.getString(preference.getKey(), "")); |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* This method stops fragment injection in malicious applications. |
| 251 |
* Make sure to deny any unknown fragments here. |
| 252 |
*/ |
| 253 |
protected boolean isValidFragment(String fragmentName) { |
| 254 |
return PreferenceFragment.class.getName().equals(fragmentName) |
| 255 |
|| GeneralPreferenceFragment.class.getName().equals(fragmentName) |
| 256 |
|| DataSyncPreferenceFragment.class.getName().equals(fragmentName) |
| 257 |
|| NotificationPreferenceFragment.class.getName().equals(fragmentName); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* This fragment shows general preferences only. It is used when the |
| 262 |
* activity is showing a two-pane settings UI. |
| 263 |
*/ |
| 264 |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 265 |
public static class GeneralPreferenceFragment extends PreferenceFragment { |
| 266 |
@Override |
| 267 |
public void onCreate(Bundle savedInstanceState) { |
| 268 |
super.onCreate(savedInstanceState); |
| 269 |
addPreferencesFromResource(R.xml.pref_general); |
| 270 |
setHasOptionsMenu(true); |
| 271 |
|
| 272 |
// Bind the summaries of EditText/List/Dialog/Ringtone preferences |
| 273 |
// to their values. When their values change, their summaries are |
| 274 |
// updated to reflect the new value, per the Android Design |
| 275 |
// guidelines. |
| 276 |
bindPreferenceSummaryToValue(findPreference("example_text")); |
| 277 |
bindPreferenceSummaryToValue(findPreference("example_list")); |
| 278 |
} |
| 279 |
|
| 280 |
@Override |
| 281 |
public boolean onOptionsItemSelected(MenuItem item) { |
| 282 |
int id = item.getItemId(); |
| 283 |
if (id == android.R.id.home) { |
| 284 |
startActivity(new Intent(getActivity(), ${activityClass}.class)); |
| 285 |
return true; |
| 286 |
} |
| 287 |
return super.onOptionsItemSelected(item); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* This fragment shows notification preferences only. It is used when the |
| 293 |
* activity is showing a two-pane settings UI. |
| 294 |
*/ |
| 295 |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 296 |
public static class NotificationPreferenceFragment extends PreferenceFragment { |
| 297 |
@Override |
| 298 |
public void onCreate(Bundle savedInstanceState) { |
| 299 |
super.onCreate(savedInstanceState); |
| 300 |
addPreferencesFromResource(R.xml.pref_notification); |
| 301 |
setHasOptionsMenu(true); |
| 302 |
|
| 303 |
// Bind the summaries of EditText/List/Dialog/Ringtone preferences |
| 304 |
// to their values. When their values change, their summaries are |
| 305 |
// updated to reflect the new value, per the Android Design |
| 306 |
// guidelines. |
| 307 |
bindPreferenceSummaryToValue(findPreference("notifications_new_message_ringtone")); |
| 308 |
} |
| 309 |
|
| 310 |
@Override |
| 311 |
public boolean onOptionsItemSelected(MenuItem item) { |
| 312 |
int id = item.getItemId(); |
| 313 |
if (id == android.R.id.home) { |
| 314 |
startActivity(new Intent(getActivity(), ${activityClass}.class)); |
| 315 |
return true; |
| 316 |
} |
| 317 |
return super.onOptionsItemSelected(item); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* This fragment shows data and sync preferences only. It is used when the |
| 323 |
* activity is showing a two-pane settings UI. |
| 324 |
*/ |
| 325 |
@TargetApi(Build.VERSION_CODES.HONEYCOMB) |
| 326 |
public static class DataSyncPreferenceFragment extends PreferenceFragment { |
| 327 |
@Override |
| 328 |
public void onCreate(Bundle savedInstanceState) { |
| 329 |
super.onCreate(savedInstanceState); |
| 330 |
addPreferencesFromResource(R.xml.pref_data_sync); |
| 331 |
setHasOptionsMenu(true); |
| 332 |
|
| 333 |
// Bind the summaries of EditText/List/Dialog/Ringtone preferences |
| 334 |
// to their values. When their values change, their summaries are |
| 335 |
// updated to reflect the new value, per the Android Design |
| 336 |
// guidelines. |
| 337 |
bindPreferenceSummaryToValue(findPreference("sync_frequency")); |
| 338 |
} |
| 339 |
|
| 340 |
@Override |
| 341 |
public boolean onOptionsItemSelected(MenuItem item) { |
| 342 |
int id = item.getItemId(); |
| 343 |
if (id == android.R.id.home) { |
| 344 |
startActivity(new Intent(getActivity(), ${activityClass}.class)); |
| 345 |
return true; |
| 346 |
} |
| 347 |
return super.onOptionsItemSelected(item); |
| 348 |
} |
| 349 |
} |
| 350 |
} |