@Override
 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
   System.out.println(sharedPreferences.getAll().toString());
   SettingsUtil.saveSettings(
       this,
       new Settings(
           sharedPreferences.getBoolean(SettingsUtil.ENABLE_UPDATES, true),
           sharedPreferences.getBoolean(SettingsUtil.KEY_BOUNDING_BOX, false),
           sharedPreferences.getBoolean(SettingsUtil.KEY_LOCK_GPS, false),
           sharedPreferences.getBoolean(SettingsUtil.DRIVING_MODE, false),
           Integer.valueOf(sharedPreferences.getString(SettingsUtil.SCAN_VALUE, "4")),
           Integer.valueOf(sharedPreferences.getString(SettingsUtil.SERVER_REFRESH_RATE, "1")),
           Integer.valueOf(sharedPreferences.getString(SettingsUtil.POKEMON_ICON_SCALE, "2")),
           Integer.valueOf(sharedPreferences.getString(SettingsUtil.MAP_REFRESH_RATE, "2")),
           sharedPreferences.getString(SettingsUtil.LAST_USERNAME, ""),
           sharedPreferences.getBoolean(SettingsUtil.SHOW_ONLY_LURED, true),
           sharedPreferences.getBoolean(SettingsUtil.SHOW_GYMS, true),
           sharedPreferences.getBoolean(SettingsUtil.SHOW_POKESTOPS, true),
           sharedPreferences.getBoolean(SettingsUtil.SHOW_LURED_POKEMON, true),
           sharedPreferences.getBoolean(SettingsUtil.KEY_OLD_MARKER, false),
           sharedPreferences.getBoolean(SettingsUtil.SHUFFLE_ICONS, false)));
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    preferences = PreferenceManager.getDefaultSharedPreferences(this);
    preferences.registerOnSharedPreferenceChangeListener(this);
    Settings settings = SettingsUtil.getSettings(this);

    preferences
        .edit()
        .putBoolean(SettingsUtil.ENABLE_UPDATES, settings.isUpdatesEnabled())
        .putBoolean(SettingsUtil.KEY_BOUNDING_BOX, settings.isBoundingBoxEnabled())
        .putString(SettingsUtil.SCAN_VALUE, String.valueOf(settings.getScanValue()))
        .putBoolean(SettingsUtil.SHOW_ONLY_LURED, settings.isShowOnlyLured())
        .putBoolean(SettingsUtil.SHOW_GYMS, settings.isGymsEnabled())
        .putBoolean(SettingsUtil.SHOW_POKESTOPS, settings.isPokestopsEnabled())
        .putBoolean(SettingsUtil.SHOW_LURED_POKEMON, settings.isShowLuredPokemon())
        .putBoolean(SettingsUtil.KEY_LOCK_GPS, settings.isLockGpsEnabled())
        .putBoolean(SettingsUtil.KEY_OLD_MARKER, settings.isUseOldMapMarker())
        .putString(SettingsUtil.SERVER_REFRESH_RATE, String.valueOf(settings.getServerRefresh()))
        .putString(SettingsUtil.MAP_REFRESH_RATE, String.valueOf(settings.getMapRefresh()))
        .putString(SettingsUtil.POKEMON_ICON_SCALE, String.valueOf(settings.getScale()))
        .putString(SettingsUtil.LAST_USERNAME, settings.getLastUsername())
        .putBoolean(SettingsUtil.SHUFFLE_ICONS, settings.isShuffleIcons())
        .commit();

    addPreferencesFromResource(R.xml.settings);

    realm = Realm.getDefaultInstance();

    scan_dialog = (Preference) getPreferenceManager().findPreference("scan_dialog");
    scan_dialog.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            SettingsUtil.searchRadiusDialog(SettingsActivity.this);
            return true;
          }
        });

    gym_filter = (Preference) getPreferenceManager().findPreference("gym_filter");
    gym_filter.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            GymFilters.showGymFiltersDialog(SettingsActivity.this);
            return true;
          }
        });

    expiration_filter = (Preference) getPreferenceManager().findPreference("expiration_filter");
    expiration_filter.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            ExpirationFilters.showExpirationFiltersDialog(SettingsActivity.this);
            return true;
          }
        });

    pokemon_blacklist = (Preference) getPreferenceManager().findPreference("pokemon_blacklist");
    pokemon_blacklist.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            Intent filterIntent = new Intent(SettingsActivity.this, BlacklistActivity.class);
            startActivity(filterIntent);
            return true;
          }
        });

    pokemon_whitelist = (Preference) getPreferenceManager().findPreference("pokemon_whitelist");
    pokemon_whitelist.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            Intent filterIntent = new Intent(SettingsActivity.this, WhitelistActivity.class);
            startActivity(filterIntent);
            return true;
          }
        });

    clear_gyms = (Preference) getPreferenceManager().findPreference("clear_gyms");
    clear_gyms.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            realm.executeTransaction(
                new Realm.Transaction() {
                  @Override
                  public void execute(Realm realm) {
                    if (realm.where(Gym.class).findAll().deleteAllFromRealm()) {
                      Toast.makeText(
                              SettingsActivity.this,
                              getString(R.string.gyms_cleared),
                              Toast.LENGTH_SHORT)
                          .show();
                    }
                  }
                });
            return true;
          }
        });

    clear_pokemon = (Preference) getPreferenceManager().findPreference("clear_pokemon");
    clear_pokemon.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            realm.executeTransaction(
                new Realm.Transaction() {
                  @Override
                  public void execute(Realm realm) {
                    if (realm.where(Pokemons.class).findAll().deleteAllFromRealm()) {
                      Toast.makeText(
                              SettingsActivity.this,
                              getString(R.string.pokemon_cleared),
                              Toast.LENGTH_SHORT)
                          .show();
                    }
                  }
                });
            return true;
          }
        });

    clear_pokestops = (Preference) getPreferenceManager().findPreference("clear_pokestops");
    clear_pokestops.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            realm.executeTransaction(
                new Realm.Transaction() {
                  @Override
                  public void execute(Realm realm) {
                    if (realm.where(PokeStop.class).findAll().deleteAllFromRealm()) {
                      Toast.makeText(
                              SettingsActivity.this,
                              getString(R.string.pokestops_cleared),
                              Toast.LENGTH_SHORT)
                          .show();
                    }
                  }
                });
            return true;
          }
        });

    logout = (Preference) getPreferenceManager().findPreference("logout");
    logout.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            logOut();
            return true;
          }
        });

    update = (Preference) getPreferenceManager().findPreference("update");
    update.setOnPreferenceClickListener(
        new Preference.OnPreferenceClickListener() {
          public boolean onPreferenceClick(Preference preference) {
            new AppUpdateLoader().start();
            return true;
          }
        });
  }