Advertisement
Lauda

Untitled

Sep 4th, 2018
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. package rs.iotegral.courtsessionnotifier.activities;
  2.  
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.preference.ListPreference;
  6. import android.preference.Preference;
  7. import android.preference.PreferenceActivity;
  8. import android.preference.PreferenceFragment;
  9. import android.support.annotation.Nullable;
  10. import android.util.Log;
  11.  
  12. import java.util.Objects;
  13.  
  14. import rs.iotegral.courtsessionnotifier.R;
  15. import rs.iotegral.courtsessionnotifier.sync.CourtNewsSyncAdapter;
  16.  
  17. /**
  18.  * Created by Lauda on 9/4/2018 20:54.
  19.  */
  20. public class SettingsActivity extends PreferenceActivity {
  21.     private static final String LOG_TAG = SettingsActivity.class.getSimpleName();
  22.  
  23.     public static class SettingsFragment extends PreferenceFragment {
  24.         @Override
  25.         public void onCreate(final Bundle savedInstanceState)
  26.         {
  27.             super.onCreate(savedInstanceState);
  28.             addPreferencesFromResource(R.xml.preference_general);
  29.  
  30.             Preference syncFrequency = findPreference("court_news_sync_frequency");
  31.             Preference enableNotifications = findPreference(getResources().getString(R.string.court_news_notification_value));
  32.  
  33.             enableNotifications.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
  34.                 @Override
  35.                 public boolean onPreferenceClick(Preference preference) {
  36.                     return false;
  37.                 }
  38.             });
  39.  
  40.             syncFrequency.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
  41.                 @Override
  42.                 public boolean onPreferenceChange(Preference preference, Object newValue) {
  43.                     String value = newValue.toString();
  44.  
  45.                     if (preference instanceof ListPreference) {
  46.                         // For list pref find the correct display value in pref entries list
  47.                         ListPreference listPref = (ListPreference) preference;
  48.                         int index = listPref.findIndexOfValue(value);
  49.  
  50.                         if (index >= 0) {
  51.                             preference.setSummary(listPref.getEntries()[index]);
  52.                             final int sync = Integer.parseInt(newValue.toString());
  53.                             CourtNewsSyncAdapter.configurePeriodicSync(getActivity().getApplicationContext(), sync, (sync < 0));
  54.                             Log.d(LOG_TAG, "Sync interval change triggered! Syncing now!");
  55.                         }
  56.                     }
  57.                     else {
  58.                         preference.setSummary(value);
  59.                     }
  60.                     return true;
  61.                 }
  62.             });
  63.         }
  64.     }
  65.  
  66.     @Override
  67.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  68.         super.onCreate(savedInstanceState);
  69.         getFragmentManager().beginTransaction().replace(android.R.id.content, new SettingsFragment()).commit();
  70.     }
  71.    
  72.     @Nullable
  73.     @Override
  74.     public Intent getParentActivityIntent() {
  75.         return Objects.requireNonNull(super.getParentActivityIntent()).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement