Last active
December 20, 2015 01:19
-
-
Save KamilLelonek/6048442 to your computer and use it in GitHub Desktop.
PreferencesActivity with PreferenceScreen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package pwr.rss.reader | |
import scala.collection.mutable.HashSet | |
import com.actionbarsherlock.app.SherlockPreferenceActivity | |
import com.actionbarsherlock.view.MenuItem | |
import android.content.SharedPreferences | |
import android.content.SharedPreferences.OnSharedPreferenceChangeListener | |
import android.os.Bundle | |
import android.preference.PreferenceManager | |
import PreferencesActivity._ | |
object PreferencesActivity { | |
final lazy val KEY_AUTO_REFRESH = "preference_auto_refresh" | |
final lazy val KEY_REFRESH_PERIOD = "preference_refresh_period" | |
final lazy val KEY_NOTIFICATIONS = "preferencescreen_notifications" | |
final lazy val KEY_NOTIFICATIONS_LED = "preference_notifications_led" | |
final lazy val KEY_NOTIFICATIONS_SOUND = "preference_notifications_sound" | |
final lazy val KEY_NOTIFICATIONS_VIBRATIONS = "preference_notifications_vibrations" | |
final lazy val KEY_AUTO_MARK_AS_READ = "preference_auto_mark_as_read" | |
final lazy val KEY_CHECKED_RADIO_BUTTON = "preference_checked_radio" | |
final lazy val KEY_SHOW_SELECTED_ONLY = "preference_show_selected_only" | |
final lazy val KEY_KEEP_READ_FEEDS = "preference_keep_read_feeds" | |
private final val DEFAULT_REFRESH_PERIOD = "10" | |
} | |
class PreferencesActivity extends SherlockPreferenceActivity with OnSharedPreferenceChangeListener { | |
private lazy val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this) | |
private lazy val notificationsSet = new HashSet[String] | |
private lazy val textViewRefreshPeriod = findPreference(KEY_REFRESH_PERIOD) | |
private lazy val preferenceNotifications = findPreference(KEY_NOTIFICATIONS) | |
override def onCreate(savedInstanceState: Bundle) = { | |
super.onCreate(savedInstanceState) | |
addPreferencesFromResource(R.xml.preferences) | |
getSupportActionBar.setDisplayHomeAsUpEnabled(true) | |
} | |
override def onResume = { | |
super.onResume | |
sharedPreferences | |
.registerOnSharedPreferenceChangeListener(this) | |
configureRefreshPeriod | |
configureNotificationsType | |
} | |
override def onPause = { | |
super.onPause | |
sharedPreferences | |
.unregisterOnSharedPreferenceChangeListener(this) | |
} | |
private def configureRefreshPeriod = textViewRefreshPeriod.setSummary(getNotificationPeriod) | |
private def getNotificationPeriod = { | |
val refreshPeriodValue = sharedPreferences.getString(KEY_REFRESH_PERIOD, DEFAULT_REFRESH_PERIOD) | |
val summaryRefreshPeriod_general = getString(R.string.preference_period_summary) | |
val summaryRefreshPeriod_concrete = String.format(summaryRefreshPeriod_general, refreshPeriodValue.toString) | |
summaryRefreshPeriod_concrete | |
} | |
def configureNotificationsType = { | |
configureNotifications(KEY_NOTIFICATIONS_LED) | |
configureNotifications(KEY_NOTIFICATIONS_SOUND) | |
configureNotifications(KEY_NOTIFICATIONS_VIBRATIONS) | |
preferenceNotifications.setSummary(getNotificationsSummary) | |
} | |
private def configureNotifications(key: String) = { | |
val notificationPreference = findPreference(key) | |
val notificationEnabled = sharedPreferences.getBoolean(key, false) | |
val notificationType = notificationPreference.getTitle.toString | |
if (notificationEnabled) | |
notificationsSet += notificationType | |
else | |
notificationsSet -= notificationType | |
preferenceNotifications.setSummary(getNotificationsSummary) | |
onContentChanged | |
} | |
private def getNotificationsSummary = | |
if (!notificationsSet.isEmpty) | |
getString(R.string.preference_notifications_summary_checked) + | |
getStringValuesFromSet(notificationsSet) | |
else | |
getString(R.string.preference_notifications_summary_unchecked) | |
private def getStringValuesFromSet(notificationsSet: HashSet[String]) = | |
notificationsSet.mkString(", ") | |
override def onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) = | |
key match { | |
case `KEY_REFRESH_PERIOD` => configureRefreshPeriod | |
case `KEY_NOTIFICATIONS_LED` | |
| `KEY_NOTIFICATIONS_SOUND` | |
| `KEY_NOTIFICATIONS_VIBRATIONS` => configureNotifications(key) | |
case _ => | |
} | |
override def onMenuItemSelected(featureId: Int, item: MenuItem) = { | |
item.getItemId match { | |
case android.R.id.home => finish | |
case _ => | |
} | |
true | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > | |
<PreferenceCategory android:title="@string/preference_refreshing" > | |
<CheckBoxPreference | |
android:defaultValue="true" | |
android:key="preference_auto_refresh" | |
android:summary="@string/preference_auto_refresh_summary" | |
android:title="@string/preference_auto_refresh" /> | |
<EditTextPreference | |
android:defaultValue="10" | |
android:dependency="preference_auto_refresh" | |
android:inputType="number" | |
android:key="preference_refresh_period" | |
android:title="@string/preference_period" /> | |
</PreferenceCategory> | |
<PreferenceCategory android:title="@string/preference_notifications" > | |
<PreferenceScreen | |
android:key="preferencescreen_notifications" | |
android:title="@string/preference_notifications" > | |
<CheckBoxPreference | |
android:defaultValue="true" | |
android:key="preference_notifications_led" | |
android:summary="@string/preference_notifications_led_summary" | |
android:title="@string/preference_notifications_led" /> | |
<CheckBoxPreference | |
android:defaultValue="true" | |
android:key="preference_notifications_sound" | |
android:summary="@string/preference_notifications_sound_summary" | |
android:title="@string/preference_notifications_sound" /> | |
<CheckBoxPreference | |
android:defaultValue="true" | |
android:key="preference_notifications_vibrations" | |
android:summary="@string/preference_notifications_vibration_summary" | |
android:title="@string/preference_notifications_vibration" /> | |
</PreferenceScreen> | |
</PreferenceCategory> | |
<PreferenceCategory android:title="@string/preference_extras" > | |
<CheckBoxPreference | |
android:defaultValue="true" | |
android:key="preference_auto_mark_as_read" | |
android:summary="@string/preference_auto_mark_as_read_summary" | |
android:title="@string/preference_auto_mark_as_read" /> | |
<CheckBoxPreference | |
android:defaultValue="true" | |
android:key="preference_keep_read_feeds" | |
android:summary="@string/preference_keep_read_feeds_summary" | |
android:title="@string/preference_keep_read_feeds" /> | |
</PreferenceCategory> | |
</PreferenceScreen> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment