SettingsActivity.java 10 KB

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