PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/src/at/fhhgb/mc/swip/ui/TriggerEditActivity.java

https://bitbucket.org/K-RAD/swip
Java | 825 lines | 615 code | 90 blank | 120 comment | 172 complexity | f1b71c40f2b00b7de687d9dd1f3a8d36 MD5 | raw file
  1. package at.fhhgb.mc.swip.ui;
  2. import android.annotation.TargetApi;
  3. import android.app.AlertDialog;
  4. import android.content.Context;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.content.SharedPreferences;
  8. import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
  9. import android.content.res.Configuration;
  10. import android.os.Build;
  11. import android.os.Bundle;
  12. import android.preference.ListPreference;
  13. import android.preference.Preference;
  14. import android.preference.PreferenceActivity;
  15. import android.preference.PreferenceCategory;
  16. import android.preference.PreferenceFragment;
  17. import android.preference.PreferenceManager;
  18. import android.util.Log;
  19. import android.view.Menu;
  20. import android.view.MenuItem;
  21. import android.support.v4.app.NavUtils;
  22. import at.fhhgb.mc.swip.R;
  23. import at.fhhgb.mc.swip.trigger.LocationTrigger;
  24. import at.fhhgb.mc.swip.trigger.SimpleGeofence;
  25. import at.fhhgb.mc.swip.trigger.Trigger;
  26. import at.fhhgb.mc.swip.trigger.XmlCreatorTrigger;
  27. import java.io.File;
  28. import java.io.FileNotFoundException;
  29. import java.io.FileOutputStream;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Set;
  34. import javax.xml.parsers.ParserConfigurationException;
  35. import javax.xml.transform.TransformerException;
  36. import com.google.android.gms.location.Geofence;
  37. /**
  38. * Activity used to edit the different settings of a trigger.
  39. *
  40. * @author Florian Schrofner & Dominik Koeltringer
  41. *
  42. */
  43. public class TriggerEditActivity extends PreferenceActivity implements
  44. OnSharedPreferenceChangeListener {
  45. /**
  46. * Determines whether to always show the simplified settings UI, where
  47. * settings are presented in a single list. When false, settings are shown
  48. * as a master/detail two-pane view on tablets. When true, a single pane is
  49. * shown on tablets.
  50. */
  51. private static final boolean ALWAYS_SIMPLE_PREFS = false;
  52. // Indicates if there are unsaved changes in the preferences.
  53. private boolean preferencesChanged = false;
  54. private CharSequence[] profileArray;
  55. // saves the previous trigger name for the case the trigger gets renamed (so
  56. // the previous file of this trigger can be deleted)
  57. private String previousName;
  58. /**
  59. * Sets up the actionbar.
  60. *
  61. * @see android.preference.PreferenceActivity#onCreate(android.os.Bundle)
  62. */
  63. @Override
  64. protected void onCreate(Bundle savedInstanceState) {
  65. super.onCreate(savedInstanceState);
  66. setupActionBar();
  67. }
  68. @Override
  69. protected boolean isValidFragment(String fragmentName) {
  70. if (fragmentName
  71. .equals("at.fhhgb.mc.swip.TriggerEditActivity$GeneralPreferenceFragment")) {
  72. Log.i("TriggerEditActivity", "valid fragment started");
  73. return true;
  74. } else {
  75. Log.i("TriggerEditActivity", "invalid fragment started");
  76. return false;
  77. }
  78. }
  79. /**
  80. * Set up the {@link android.app.ActionBar}, if the API is available.
  81. */
  82. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  83. private void setupActionBar() {
  84. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
  85. // Show the Up button in the action bar.
  86. getActionBar().setDisplayHomeAsUpEnabled(true);
  87. }
  88. }
  89. /**
  90. * Adds the buttons to the actionbar (apply, cancel and write to tag).
  91. *
  92. * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
  93. */
  94. @Override
  95. public boolean onCreateOptionsMenu(Menu menu) {
  96. // Inflate the menu; this adds items to the action bar if it is present.
  97. getMenuInflater().inflate(R.menu.trigger_edit, menu);
  98. return true;
  99. }
  100. /**
  101. * If one of the items on the actionbar is pressed.
  102. *
  103. * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
  104. */
  105. @Override
  106. public boolean onOptionsItemSelected(MenuItem item) {
  107. SharedPreferences pref = PreferenceManager
  108. .getDefaultSharedPreferences(this);
  109. // Show dialogs if the user wants to save and something is wrong
  110. if (item.getItemId() == R.id.save_trigger) {
  111. if (pref.getString("name_trigger",
  112. getResources().getString(R.string.pref_default_name))
  113. .equals(getResources()
  114. .getString(R.string.pref_default_name))
  115. || pref.getString(
  116. "name_trigger",
  117. getResources()
  118. .getString(R.string.pref_default_name))
  119. .equals("")
  120. || pref.getString(
  121. "profile",
  122. getResources().getString(
  123. R.string.pref_profile_default)).equals(
  124. getResources().getString(
  125. R.string.pref_profile_default))
  126. || (pref.getInt("battery_start_level", -1) >= pref.getInt(
  127. "battery_end_level", -1) && pref.getInt(
  128. "battery_end_level", -1) != -1)) {
  129. AlertDialog.Builder dialog = new AlertDialog.Builder(this,
  130. AlertDialog.THEME_DEVICE_DEFAULT_DARK);
  131. dialog.setIcon(R.drawable.alerts_and_states_warning);
  132. if (pref.getInt("battery_start_level", -1) > pref.getInt(
  133. "battery_end_level", -1)
  134. && pref.getInt("battery_end_level", -1) != -1) {
  135. // Battery low-level is smaller than battery high-level
  136. dialog.setTitle(getResources().getString(
  137. R.string.alert_battery_title));
  138. dialog.setMessage(getResources().getString(
  139. R.string.alert_battery_text));
  140. } else if (pref.getInt("battery_start_level", -1) == pref
  141. .getInt("battery_end_level", -1)
  142. && pref.getInt("battery_end_level", -1) != -1) {
  143. // Battery low-level == battery high-level
  144. dialog.setTitle(getResources().getString(
  145. R.string.alert_battery_title));
  146. dialog.setMessage(getResources().getString(
  147. R.string.alert_battery_exact_text));
  148. } else if (pref.getString("name_trigger",
  149. getResources().getString(R.string.pref_default_name))
  150. .equals(getResources().getString(
  151. R.string.pref_default_name))) {
  152. // The name is the default name
  153. dialog.setTitle(getResources().getString(
  154. R.string.alert_name_title));
  155. dialog.setMessage(getResources().getString(
  156. R.string.alert_name_text));
  157. } else if (pref.getString("name_trigger",
  158. getResources().getString(R.string.pref_default_name))
  159. .equals("")) {
  160. // the name is empty
  161. dialog.setTitle(getResources().getString(
  162. R.string.alert_name_title));
  163. dialog.setMessage(getResources().getString(
  164. R.string.alert_name_text));
  165. } else if (pref
  166. .getString(
  167. "profile",
  168. getResources().getString(
  169. R.string.pref_profile_default)).equals(
  170. getResources().getString(
  171. R.string.pref_profile_default))) {
  172. // no profile is set
  173. dialog.setTitle(getResources().getString(
  174. R.string.alert_profile_title));
  175. dialog.setMessage(getResources().getString(
  176. R.string.alert_profile_text));
  177. }
  178. dialog.setNegativeButton(
  179. getResources().getString(R.string.alert_button),
  180. new DialogInterface.OnClickListener() {
  181. @Override
  182. public void onClick(DialogInterface dialog,
  183. int which) {
  184. dialog.dismiss();
  185. }
  186. });
  187. dialog.show();
  188. } else {
  189. this.saveTrigger();
  190. this.finish();
  191. }
  192. } else if (item.getItemId() == R.id.cancel_trigger) {
  193. this.finish();
  194. } else if (item.getItemId() == android.R.id.home) {
  195. NavUtils.navigateUpFromSameTask(this);
  196. return true;
  197. }
  198. return super.onOptionsItemSelected(item);
  199. }
  200. /**
  201. * Implemented a Dialog, if the user presses back and there were changes
  202. * made.
  203. *
  204. * @see android.app.Activity#onBackPressed()
  205. */
  206. @Override
  207. public void onBackPressed() {
  208. if (preferencesChanged) {
  209. AlertDialog.Builder dialog = new AlertDialog.Builder(this,
  210. AlertDialog.THEME_DEVICE_DEFAULT_DARK);
  211. dialog.setTitle(getResources().getString(
  212. R.string.alert_discard_title));
  213. dialog.setMessage(getResources().getString(
  214. R.string.alert_discard_text));
  215. dialog.setIcon(R.drawable.alerts_and_states_warning);
  216. dialog.setPositiveButton(
  217. getResources().getString(R.string.alert_discard_yes),
  218. new DialogInterface.OnClickListener() {
  219. @Override
  220. public void onClick(DialogInterface dialog, int which) {
  221. finish();
  222. }
  223. });
  224. dialog.setNegativeButton(
  225. getResources().getString(R.string.alert_discard_no),
  226. new DialogInterface.OnClickListener() {
  227. @Override
  228. public void onClick(DialogInterface dialog, int which) {
  229. dialog.dismiss();
  230. }
  231. });
  232. dialog.show();
  233. } else {
  234. finish();
  235. }
  236. }
  237. /**
  238. * @see android.app.Activity#onPostCreate(android.os.Bundle)
  239. */
  240. @Override
  241. protected void onPostCreate(Bundle savedInstanceState) {
  242. super.onPostCreate(savedInstanceState);
  243. setupSimplePreferencesScreen();
  244. }
  245. /**
  246. * Shows the simplified settings UI if the device configuration if the
  247. * device configuration dictates that a simplified, single-pane UI should be
  248. * shown.
  249. */
  250. private void setupSimplePreferencesScreen() {
  251. if (!isSimplePreferences(this)) {
  252. return;
  253. }
  254. SharedPreferences pref = PreferenceManager
  255. .getDefaultSharedPreferences(this);
  256. previousName = pref.getString("name_trigger", "default name");
  257. pref.registerOnSharedPreferenceChangeListener(this);
  258. // In the simplified UI, fragments are not used at all and we instead
  259. // use the older PreferenceActivity APIs.
  260. // Add 'general' preferences.
  261. addPreferencesFromResource(R.xml.pref_trigger_general);
  262. refreshProfileArray();
  263. ListPreference lp = (ListPreference) findPreference("profile");
  264. lp.setEntries(profileArray);
  265. lp.setEntryValues(profileArray);
  266. PreferenceCategory fakeHeader = new PreferenceCategory(this);
  267. // Add 'Location preferences, and a corresponding header.
  268. fakeHeader.setTitle(R.string.pref_header_location);
  269. getPreferenceScreen().addPreference(fakeHeader);
  270. addPreferencesFromResource(R.xml.pref_trigger_location);
  271. // Add 'Time' preferences, and a corresponding header.
  272. fakeHeader = new PreferenceCategory(this);
  273. fakeHeader.setTitle(R.string.pref_header_time);
  274. getPreferenceScreen().addPreference(fakeHeader);
  275. addPreferencesFromResource(R.xml.pref_trigger_time);
  276. // Add 'Battery' preferences, and a corresponding header.
  277. fakeHeader = new PreferenceCategory(this);
  278. fakeHeader.setTitle(R.string.pref_header_battery);
  279. getPreferenceScreen().addPreference(fakeHeader);
  280. addPreferencesFromResource(R.xml.pref_trigger_battery);
  281. // Add 'Headphone' preferences, and a corresponding header.
  282. fakeHeader = new PreferenceCategory(this);
  283. fakeHeader.setTitle(R.string.pref_header_headphone);
  284. getPreferenceScreen().addPreference(fakeHeader);
  285. addPreferencesFromResource(R.xml.pref_trigger_headphone);
  286. // Bind the summaries of EditText/List/Dialog preferences to
  287. // their values. When their values change, their summaries are updated
  288. // to reflect the new value, per the Android Design guidelines.
  289. bindPreferenceSummaryToValue(findPreference("name_trigger"));
  290. bindPreferenceSummaryToValue(findPreference("priority"));
  291. bindPreferenceSummaryToValue(findPreference("start_time"));
  292. bindPreferenceSummaryToValue(findPreference("end_time"));
  293. // bindPreferenceSummaryToValue(findPreference("weekdays"));
  294. bindPreferenceSummaryToValue(findPreference("profile"));
  295. bindPreferenceSummaryToValue(findPreference("battery_state"));
  296. bindPreferenceSummaryToValue(findPreference("headphone"));
  297. // binds the summary to the location-preference
  298. if (pref.getInt("geofence_radius", 50) > 0) {
  299. findPreference("location").setSummary(
  300. getString(R.string.pref_location_lat) + ": "
  301. + pref.getFloat("geofence_lat", 0F) + "\u00B0, "
  302. + getString(R.string.pref_location_lng) + ": "
  303. + pref.getFloat("geofence_lng", 0F) + "\u00B0, "
  304. + getString(R.string.pref_location_radius) + ": "
  305. + pref.getInt("geofence_radius", 50) + "m");
  306. } else {
  307. findPreference("location").setSummary(R.string.ignored);
  308. }
  309. if (pref.getString("start_time", getString(R.string.ignored)).equals(
  310. getString(R.string.ignored))) {
  311. findPreference("end_time").setEnabled(false);
  312. pref.edit().putString("end_time", getString(R.string.ignored))
  313. .commit();
  314. }
  315. if (pref.getInt("battery_start_level", -1) == -1) {
  316. findPreference("battery_end_level").setEnabled(false);
  317. pref.edit().putInt("battery_end_level", -1).commit();
  318. }
  319. //Binds the summary of the weekends
  320. int size = pref.getStringSet("weekdays", null).size();
  321. if (pref.getStringSet("weekdays", null).isEmpty()) {
  322. findPreference("weekdays").setSummary(R.string.pref_weekday_none);
  323. } else if (size == 7) {
  324. findPreference("weekdays").setSummary(R.string.pref_weekday_all);
  325. } else if (size == 5 && pref.getStringSet("weekdays", null).contains("1") &&
  326. pref.getStringSet("weekdays", null).contains("2") &&
  327. pref.getStringSet("weekdays", null).contains("3") &&
  328. pref.getStringSet("weekdays", null).contains("4") &&
  329. pref.getStringSet("weekdays", null).contains("5")) {
  330. findPreference("weekdays").setSummary(R.string.pref_weekday_workdays);
  331. } else if (size == 2 && pref.getStringSet("weekdays", null).contains("6") &&
  332. pref.getStringSet("weekdays", null).contains("7")) {
  333. findPreference("weekdays").setSummary(R.string.pref_weekday_weekend);
  334. } else {
  335. StringBuilder summary = new StringBuilder();
  336. int i = 1;
  337. if ((pref.getStringSet("weekdays", null).contains("1"))) {
  338. summary.append(getResources().getString(R.string.pref_mon));
  339. if (i < size - 1) {
  340. summary.append(", ");
  341. i++;
  342. } else if (i == size - 1) {
  343. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  344. i++;
  345. }
  346. }
  347. if ((pref.getStringSet("weekdays", null).contains("2"))) {
  348. summary.append(getResources().getString(R.string.pref_tue));
  349. if (i < size - 1) {
  350. summary.append(", ");
  351. i++;
  352. } else if (i == size - 1) {
  353. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  354. i++;
  355. }
  356. }
  357. if ((pref.getStringSet("weekdays", null).contains("3"))) {
  358. summary.append(getResources().getString(R.string.pref_wed));
  359. if (i < size - 1) {
  360. summary.append(", ");
  361. i++;
  362. } else if (i == size - 1) {
  363. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  364. i++;
  365. }
  366. }
  367. if ((pref.getStringSet("weekdays", null).contains("4"))) {
  368. summary.append(getResources().getString(R.string.pref_thur));
  369. if (i < size - 1) {
  370. summary.append(", ");
  371. i++;
  372. } else if (i == size - 1) {
  373. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  374. i++;
  375. }
  376. }
  377. if ((pref.getStringSet("weekdays", null).contains("5"))) {
  378. summary.append(getResources().getString(R.string.pref_fri));
  379. if (i < size - 1) {
  380. summary.append(", ");
  381. i++;
  382. } else if (i == size - 1) {
  383. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  384. i++;
  385. }
  386. }
  387. if ((pref.getStringSet("weekdays", null).contains("6"))) {
  388. summary.append(getResources().getString(R.string.pref_sat));
  389. if (i == size - 1) {
  390. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  391. i++;
  392. }
  393. }
  394. if ((pref.getStringSet("weekdays", null).contains("7"))) {
  395. summary.append(getResources().getString(R.string.pref_sun));
  396. }
  397. findPreference("weekdays").setSummary(summary.toString());
  398. }
  399. }
  400. /** {@inheritDoc} */
  401. @Override
  402. public boolean onIsMultiPane() {
  403. return isXLargeTablet(this) && !isSimplePreferences(this);
  404. }
  405. /**
  406. * Saves the current settings of the activity to a profile object and lets
  407. * it be written by the XmlCreator.
  408. */
  409. public void saveTrigger() {
  410. SharedPreferences pref = PreferenceManager
  411. .getDefaultSharedPreferences(this);
  412. LocationTrigger locTrig = new LocationTrigger(this);
  413. String name = pref.getString("name_trigger",
  414. getResources().getString(R.string.pref_default_name));
  415. Trigger trigger = new Trigger(name);
  416. trigger.setPriority(Integer.parseInt(pref.getString("priority", "0")));
  417. if (pref.getString("start_time", getString(R.string.ignored)).equals(
  418. getString(R.string.ignored))) {
  419. trigger.setStartHours(-1);
  420. trigger.setStartMinutes(-1);
  421. } else {
  422. trigger.setStartHours(Integer.parseInt(pref.getString("start_time",
  423. "00:00").split(":")[0]));
  424. trigger.setStartMinutes(Integer.parseInt(pref.getString(
  425. "start_time", "00:00").split(":")[1]));
  426. }
  427. if (pref.getString("end_time", getString(R.string.ignored)).equals(
  428. getString(R.string.ignored))) {
  429. trigger.setEndHours(-1);
  430. trigger.setEndMinutes(-1);
  431. } else {
  432. trigger.setEndHours(Integer.parseInt(pref.getString("end_time",
  433. "00:00").split(":")[0]));
  434. trigger.setEndMinutes(Integer.parseInt(pref.getString("end_time",
  435. "00:00").split(":")[1]));
  436. }
  437. trigger.setWeekdays(pref.getStringSet("weekdays", null));
  438. trigger.setProfileName(pref.getString("profile", getResources()
  439. .getString(R.string.pref_profile_default)));
  440. trigger.setBatteryStartLevel(pref.getInt("battery_start_level", -1));
  441. trigger.setBatteryEndLevel(pref.getInt("battery_end_level", -1));
  442. if (pref.getString("battery_state", "ignored").equals("charging")) {
  443. trigger.setBatteryState(Trigger.listen_state.listen_on);
  444. } else if (pref.getString("battery_state", "ignored").equals(
  445. "discharging")) {
  446. trigger.setBatteryState(Trigger.listen_state.listen_off);
  447. } else {
  448. trigger.setBatteryState(Trigger.listen_state.ignore);
  449. }
  450. if (pref.getString("headphone", "ignored").equals("plugged_in")) {
  451. trigger.setHeadphones(Trigger.listen_state.listen_on);
  452. } else if (pref.getString("headphone", "ignored").equals("unplugged")) {
  453. trigger.setHeadphones(Trigger.listen_state.listen_off);
  454. } else {
  455. trigger.setHeadphones(Trigger.listen_state.ignore);
  456. }
  457. // unregisters the old geofences from the system
  458. locTrig.unregisterGeofence(name);
  459. locTrig.unregisterGeofence(name + "_exit");
  460. // deletes the list of currently triggered geofences from the service
  461. Intent intent = new Intent();
  462. intent.setAction("at.fhhgb.mc.swip.trigger.clearGeofences");
  463. sendBroadcast(intent);
  464. if (pref.getInt("geofence_radius", 50) > 0) {
  465. // geofence that registers if you enter the area
  466. SimpleGeofence simple = new SimpleGeofence(name, pref.getFloat(
  467. "geofence_lat", 0F), pref.getFloat("geofence_lng", 0F),
  468. pref.getInt("geofence_radius", 0), Geofence.NEVER_EXPIRE,
  469. Geofence.GEOFENCE_TRANSITION_ENTER);
  470. locTrig.registerGeofence(simple);
  471. // geofence that registers if you leave the area
  472. simple = new SimpleGeofence(name + "_exit", pref.getFloat(
  473. "geofence_lat", 0F), pref.getFloat("geofence_lng", 0F),
  474. pref.getInt("geofence_radius", 0), Geofence.NEVER_EXPIRE,
  475. Geofence.GEOFENCE_TRANSITION_EXIT);
  476. locTrig.registerGeofence(simple);
  477. // sets the geofence of the trigger to the enter event
  478. trigger.setGeofence(name);
  479. } else {
  480. locTrig.unregisterGeofence(name);
  481. locTrig.unregisterGeofence(name + "_exit");
  482. trigger.setGeofence(null);
  483. }
  484. // Creates the xml
  485. XmlCreatorTrigger creator = new XmlCreatorTrigger();
  486. try {
  487. FileOutputStream output = openFileOutput(trigger.getName()
  488. + "_trigger.xml", Context.MODE_PRIVATE);
  489. output.write(creator.create(trigger).getBytes());
  490. output.close();
  491. } catch (FileNotFoundException e1) {
  492. e1.printStackTrace();
  493. } catch (IOException e) {
  494. e.printStackTrace();
  495. } catch (ParserConfigurationException e) {
  496. e.printStackTrace();
  497. } catch (TransformerException e) {
  498. e.printStackTrace();
  499. }
  500. if (!(name.equals(previousName))) {
  501. File file = new File(String.valueOf(getFilesDir()) + "/"
  502. + previousName + "_trigger.xml");
  503. file.delete();
  504. }
  505. intent = new Intent();
  506. intent.setAction("at.fhhgb.mc.swip.trigger.refresh");
  507. sendBroadcast(intent);
  508. }
  509. /**
  510. * Helper method to determine if the device has an extra-large screen. For
  511. * example, 10" tablets are extra-large.
  512. */
  513. private static boolean isXLargeTablet(Context context) {
  514. return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
  515. }
  516. /**
  517. * Determines whether the simplified settings UI should be shown. This is
  518. * true if this is forced via {@link #ALWAYS_SIMPLE_PREFS}, or the device
  519. * doesn't have newer APIs like {@link PreferenceFragment}, or the device
  520. * doesn't have an extra-large screen. In these cases, a single-pane
  521. * "simplified" settings UI should be shown.
  522. */
  523. private static boolean isSimplePreferences(Context context) {
  524. return ALWAYS_SIMPLE_PREFS
  525. || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
  526. || !isXLargeTablet(context);
  527. }
  528. /** {@inheritDoc} */
  529. @Override
  530. @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  531. public void onBuildHeaders(List<Header> target) {
  532. if (!isSimplePreferences(this)) {
  533. loadHeadersFromResource(R.xml.pref_headers, target);
  534. }
  535. }
  536. /**
  537. * A preference value change listener that updates the preference's summary
  538. * to reflect its new value.
  539. */
  540. private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
  541. @Override
  542. public boolean onPreferenceChange(Preference preference, Object value) {
  543. String stringValue = value.toString();
  544. if (preference instanceof ListPreference) {
  545. // For list preferences, look up the correct display value in
  546. // the preference's 'entries' list.
  547. ListPreference listPreference = (ListPreference) preference;
  548. int index = listPreference.findIndexOfValue(stringValue);
  549. // Set the summary to reflect the new value.
  550. preference
  551. .setSummary(index >= 0 ? listPreference.getEntries()[index]
  552. : null);
  553. } else {
  554. // For all other preferences, set the summary to the value's
  555. // simple string representation.
  556. preference.setSummary(stringValue);
  557. }
  558. return true;
  559. }
  560. };
  561. /**
  562. * Binds a preference's summary to its value. More specifically, when the
  563. * preference's value is changed, its summary (line of text below the
  564. * preference title) is updated to reflect the value. The summary is also
  565. * immediately updated upon calling this method. The exact display format is
  566. * dependent on the type of preference.
  567. *
  568. * @see #sBindPreferenceSummaryToValueListener
  569. */
  570. private static void bindPreferenceSummaryToValue(Preference preference) {
  571. // Set the listener to watch for value changes.
  572. preference
  573. .setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
  574. // Trigger the listener immediately with the preference's
  575. // current value.
  576. sBindPreferenceSummaryToValueListener.onPreferenceChange(
  577. preference,
  578. PreferenceManager.getDefaultSharedPreferences(
  579. preference.getContext()).getString(preference.getKey(),
  580. ""));
  581. }
  582. @Override
  583. public void onSharedPreferenceChanged(SharedPreferences _pref, String key) {
  584. // dis- and enables the endtime if the start time is changed
  585. if (key.equals("start_time")
  586. && _pref.getString("start_time", getString(R.string.ignored))
  587. .equals(getString(R.string.ignored))) {
  588. _pref.edit().putString("end_time", getString(R.string.ignored))
  589. .commit();
  590. findPreference("end_time").setEnabled(false);
  591. findPreference("end_time").setSummary(getString(R.string.ignored));
  592. } else if (key.equals("start_time")
  593. && !_pref.getString("start_time", getString(R.string.ignored))
  594. .equals(getString(R.string.ignored))) {
  595. findPreference("end_time").setEnabled(true);
  596. }
  597. // dis- and enables the end battery level if the start battery level is
  598. // changed
  599. if (key.equals("battery_start_level")
  600. && _pref.getInt("battery_start_level", -1) == -1) {
  601. _pref.edit().putInt("battery_end_level", -1).commit();
  602. findPreference("battery_end_level").setEnabled(false);
  603. findPreference("battery_end_level").setSummary(
  604. getString(R.string.ignored));
  605. } else if (key.equals("battery_start_level")
  606. && _pref.getInt("battery_start_level", -1) != -1) {
  607. findPreference("battery_end_level").setEnabled(true);
  608. }
  609. // Binds the summary of the location
  610. if (key.equals("geofence_lat") || key.equals("geofence_lng")
  611. || key.equals("geofence_radius")) {
  612. SharedPreferences pref = PreferenceManager
  613. .getDefaultSharedPreferences(this);
  614. if (pref.getInt("geofence_radius", 50) > 0) {
  615. findPreference("location").setSummary(
  616. getString(R.string.pref_location_lat) + ": "
  617. + pref.getFloat("geofence_lat", 0F) + "\u00B0, "
  618. + getString(R.string.pref_location_lng) + ": "
  619. + pref.getFloat("geofence_lng", 0F) + "\u00B0, "
  620. + getString(R.string.pref_location_radius)
  621. + ": " + pref.getInt("geofence_radius", 50)
  622. + "m");
  623. } else {
  624. findPreference("location").setSummary(R.string.ignored);
  625. }
  626. }
  627. // Binds the summary of the weekday
  628. if (key.equals("weekdays")) {
  629. SharedPreferences pref = PreferenceManager
  630. .getDefaultSharedPreferences(this);
  631. int size = pref.getStringSet("weekdays", null).size();
  632. if (pref.getStringSet("weekdays", null).isEmpty()) {
  633. findPreference("weekdays").setSummary(R.string.pref_weekday_none);
  634. } else if (size == 7) {
  635. findPreference("weekdays").setSummary(R.string.pref_weekday_all);
  636. } else if (size == 5 && pref.getStringSet("weekdays", null).contains("1") &&
  637. pref.getStringSet("weekdays", null).contains("2") &&
  638. pref.getStringSet("weekdays", null).contains("3") &&
  639. pref.getStringSet("weekdays", null).contains("4") &&
  640. pref.getStringSet("weekdays", null).contains("5")) {
  641. findPreference("weekdays").setSummary(R.string.pref_weekday_workdays);
  642. } else if (size == 2 && pref.getStringSet("weekdays", null).contains("6") &&
  643. pref.getStringSet("weekdays", null).contains("7")) {
  644. findPreference("weekdays").setSummary(R.string.pref_weekday_weekend);
  645. } else {
  646. StringBuilder summary = new StringBuilder();
  647. int i = 1;
  648. if ((pref.getStringSet("weekdays", null).contains("1"))) {
  649. summary.append(getResources().getString(R.string.pref_mon));
  650. if (i < size - 1) {
  651. summary.append(", ");
  652. i++;
  653. } else if (i == size - 1) {
  654. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  655. i++;
  656. }
  657. }
  658. if ((pref.getStringSet("weekdays", null).contains("2"))) {
  659. summary.append(getResources().getString(R.string.pref_tue));
  660. if (i < size - 1) {
  661. summary.append(", ");
  662. i++;
  663. } else if (i == size - 1) {
  664. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  665. i++;
  666. }
  667. }
  668. if ((pref.getStringSet("weekdays", null).contains("3"))) {
  669. summary.append(getResources().getString(R.string.pref_wed));
  670. if (i < size - 1) {
  671. summary.append(", ");
  672. i++;
  673. } else if (i == size - 1) {
  674. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  675. i++;
  676. }
  677. }
  678. if ((pref.getStringSet("weekdays", null).contains("4"))) {
  679. summary.append(getResources().getString(R.string.pref_thur));
  680. if (i < size - 1) {
  681. summary.append(", ");
  682. i++;
  683. } else if (i == size - 1) {
  684. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  685. i++;
  686. }
  687. }
  688. if ((pref.getStringSet("weekdays", null).contains("5"))) {
  689. summary.append(getResources().getString(R.string.pref_fri));
  690. if (i < size - 1) {
  691. summary.append(", ");
  692. i++;
  693. } else if (i == size - 1) {
  694. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  695. i++;
  696. }
  697. }
  698. if ((pref.getStringSet("weekdays", null).contains("6"))) {
  699. summary.append(getResources().getString(R.string.pref_sat));
  700. if (i == size - 1) {
  701. summary.append(" " + getResources().getString(R.string.pref_and) + " ");
  702. i++;
  703. }
  704. }
  705. if ((pref.getStringSet("weekdays", null).contains("7"))) {
  706. summary.append(getResources().getString(R.string.pref_sun));
  707. }
  708. findPreference("weekdays").setSummary(summary.toString());
  709. }
  710. }
  711. preferencesChanged = true;
  712. }
  713. /**
  714. * Refreshes the profile array.
  715. */
  716. private void refreshProfileArray() {
  717. List<String> profileList = new ArrayList<String>();
  718. String[] fileList = getFilesDir().list();
  719. StringBuffer sb = new StringBuffer();
  720. for (String file : fileList) {
  721. if (file.contains("_profile")) {
  722. sb.append(file);
  723. sb.delete(sb.length() - 12, sb.length());
  724. profileList.add(sb.toString());
  725. sb.delete(0, sb.length());
  726. }
  727. }
  728. profileArray = new CharSequence[profileList.size() + 1];
  729. profileArray[0] = getResources().getString(
  730. R.string.pref_profile_default);
  731. for (int i = 0; i < profileArray.length - 1; i++) {
  732. profileArray[i + 1] = profileList.get(i);
  733. }
  734. }
  735. }