Android Open Source - MrParkingNavigator Preferences






From Project

Back to project page MrParkingNavigator.

License

The source code is released under:

MIT License

If you think the Android project MrParkingNavigator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ru.mail.parking;
/*from   w  ww.  ja  va 2s . c om*/
import android.content.Context;
import android.content.SharedPreferences;

import ru.mail.parking.floors.Place;
import ru.mail.parking.utils.Utils;
import ru.mail.parking.widget.MainWidgetProvider;
import ru.mail.parking.widget.SmartUpdate;

import static ru.mail.parking.App.app;

public class Preferences implements SharedPreferences.OnSharedPreferenceChangeListener {
  public static final String NAME = "settings";

  private final SharedPreferences mPrefs;

  public enum Keys {
    last_fetch,
    last_update,
    last_places,
    last_data,
    click_action,
    doubletap_action,
    time_format,
    update_policy,
    stored_place
  }

  public enum ClickAction {
    navigator,
    update,
    details,
    settings;

    private static ClickAction sDefault;
    private static ClickAction sDefaultDoubletap;

    static ClickAction getDefault(boolean doubletap) {
      if (sDefault == null) {
        sDefault = valueOf(app().getString(R.string.prefs_config_click_action_default));
        sDefaultDoubletap = valueOf(app().getString(R.string.prefs_config_doubletap_action_default));
      }

      return (doubletap ? sDefaultDoubletap : sDefault);
    }
  }

  public enum TimeFormat {
    none {
      @Override
      public String getFormatString() {
        return "";
      }
    },
    time {
      @Override
      public String getFormatString() {
        return app().getString(R.string.time_format);
      }
    },
    full {
      @Override
      public String getFormatString() {
        return time.getFormatString() + ' ' + app().getString(R.string.time_format_date);
      }
    },
    smartwatch {
      @Override
      public String getFormatString() {
        return time.getFormatString() + '\n' + app().getString(R.string.time_format_date);
      }
    };

    private static TimeFormat sDefault;

    public static TimeFormat getDefault() {
      if (sDefault == null)
        sDefault = valueOf(app().getString(R.string.prefs_config_time_format_default));

      return sDefault;
    }

    public abstract String getFormatString();
  }


  public Preferences() {
    mPrefs = app().getSharedPreferences(NAME, Context.MODE_PRIVATE);
    mPrefs.registerOnSharedPreferenceChangeListener(this);
  }

  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    if (Keys.time_format.name().equals(key))
      MainWidgetProvider.updateAll();

    if (Keys.update_policy.name().equals(key))
      SmartUpdate.restart();
  }

  public String getLastFetch() {
    return Utils.formatDate(mPrefs.getLong(Keys.last_fetch.name(), 0), TimeFormat.full, false);
  }

  public String getLastRefresh() {
    return Utils.formatDate(mPrefs.getLong(Keys.last_update.name(), 0), getTimeFormat(), true);
  }

  public String getSwLastRefresh() {
    return Utils.formatDate(mPrefs.getLong(Keys.last_update.name(), 0), TimeFormat.smartwatch, true);
  }

  public int getLastPlaces() {
    return mPrefs.getInt(Keys.last_places.name(), -1);
  }

  public String getLastData() {
    return mPrefs.getString(Keys.last_data.name(), app().getString(R.string.unknown));
  }

  public Place getStoredPlace() {
    int p = mPrefs.getInt(Keys.stored_place.name(), Place.INVALID);
    return App.floors().getPlace(p);
  }

  public ClickAction getClickAction() {
    String v = mPrefs.getString(Keys.click_action.name(), ClickAction.getDefault(false).name());
    try {
      return ClickAction.valueOf(v);
    } catch (IllegalArgumentException e) {
      return ClickAction.getDefault(false);
    }
  }

  public ClickAction getDoubletapAction() {
    String v = mPrefs.getString(Keys.doubletap_action.name(), ClickAction.getDefault(true).name());
    try {
      return ClickAction.valueOf(v);
    } catch (IllegalArgumentException e) {
      return ClickAction.getDefault(true);
    }
  }

  public SmartUpdate.Policy getUpdatePolicy() {
    String v = mPrefs.getString(Keys.update_policy.name(), SmartUpdate.Policy.getDefault().name());
    try {
      return SmartUpdate.Policy.valueOf(v);
    } catch (IllegalArgumentException e) {
      return SmartUpdate.Policy.getDefault();
    }
  }

  public TimeFormat getTimeFormat() {
    String v = mPrefs.getString(Keys.time_format.name(), TimeFormat.getDefault().name());
    try {
      return TimeFormat.valueOf(v);
    } catch (IllegalArgumentException e) {
      return TimeFormat.getDefault();
    }
  }

  public void setLastInfo(int places, long lastUpdate, String data) {
    mPrefs.edit()
          .putInt(Keys.last_places.name(), places)
          .putLong(Keys.last_update.name(), lastUpdate)
          .putLong(Keys.last_fetch.name(), System.currentTimeMillis())
          .putString(Keys.last_data.name(), data)
          .commit();
  }

  public void setStoredPlace(int place) {
    SharedPreferences.Editor e = mPrefs.edit();
    if (place == Place.INVALID) e.remove(Keys.stored_place.name());
                           else e.putInt(Keys.stored_place.name(), place);
    e.commit();
  }

  public void registerListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    mPrefs.registerOnSharedPreferenceChangeListener(listener);
  }

  public void unregisterListener(SharedPreferences.OnSharedPreferenceChangeListener listener) {
    mPrefs.unregisterOnSharedPreferenceChangeListener(listener);
  }
}




Java Source Code List

com.sonyericsson.extras.liveware.aef.control.Control.java
com.sonyericsson.extras.liveware.aef.control.package-info.java
com.sonyericsson.extras.liveware.aef.notification.Notification.java
com.sonyericsson.extras.liveware.aef.notification.package-info.java
com.sonyericsson.extras.liveware.aef.registration.Registration.java
com.sonyericsson.extras.liveware.aef.registration.package-info.java
com.sonyericsson.extras.liveware.aef.sensor.Sensor.java
com.sonyericsson.extras.liveware.aef.sensor.package-info.java
com.sonyericsson.extras.liveware.extension.util.Dbg.java
com.sonyericsson.extras.liveware.extension.util.ExtensionService.java
com.sonyericsson.extras.liveware.extension.util.ExtensionUtils.java
com.sonyericsson.extras.liveware.extension.util.control.ControlExtension.java
com.sonyericsson.extras.liveware.extension.util.control.ControlListItem.java
com.sonyericsson.extras.liveware.extension.util.control.ControlObjectClickEvent.java
com.sonyericsson.extras.liveware.extension.util.control.ControlTouchEvent.java
com.sonyericsson.extras.liveware.extension.util.control.ControlViewGroup.java
com.sonyericsson.extras.liveware.extension.util.control.ControlView.java
com.sonyericsson.extras.liveware.extension.util.notification.DelayedContentObserver.java
com.sonyericsson.extras.liveware.extension.util.notification.NotificationUtil.java
com.sonyericsson.extras.liveware.extension.util.registration.DeviceInfoHelper.java
com.sonyericsson.extras.liveware.extension.util.registration.DeviceInfo.java
com.sonyericsson.extras.liveware.extension.util.registration.DisplayInfo.java
com.sonyericsson.extras.liveware.extension.util.registration.HostApplicationInfo.java
com.sonyericsson.extras.liveware.extension.util.registration.IRegisterCallback.java
com.sonyericsson.extras.liveware.extension.util.registration.InputInfo.java
com.sonyericsson.extras.liveware.extension.util.registration.KeyPadInfo.java
com.sonyericsson.extras.liveware.extension.util.registration.RegisterExtensionTask.java
com.sonyericsson.extras.liveware.extension.util.registration.RegistrationAdapter.java
com.sonyericsson.extras.liveware.extension.util.registration.RegistrationInformation.java
com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorEventListener.java
com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorEvent.java
com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorException.java
com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorManager.java
com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensorType.java
com.sonyericsson.extras.liveware.extension.util.sensor.AccessorySensor.java
ru.mail.parking.App.java
ru.mail.parking.Preferences.java
ru.mail.parking.floors.FloorNavigator.java
ru.mail.parking.floors.Floor.java
ru.mail.parking.floors.Place.java
ru.mail.parking.sw2.SwControlFlow.java
ru.mail.parking.sw2.screens.SwBaseScreen.java
ru.mail.parking.sw2.screens.SwEnterPlaceScreen.java
ru.mail.parking.sw2.screens.SwFreePlacesScreen.java
ru.mail.parking.sw2.screens.SwNavigatorScreen.java
ru.mail.parking.sw2.screens.SwPlaceScreen.java
ru.mail.parking.sw2.system.SwReceiver.java
ru.mail.parking.sw2.system.SwRegInfo.java
ru.mail.parking.sw2.system.SwService.java
ru.mail.parking.ui.AboutActivity.java
ru.mail.parking.ui.DataDetailsActivity.java
ru.mail.parking.ui.EnterPlaceActivity.java
ru.mail.parking.ui.MultiscrollView.java
ru.mail.parking.ui.NavigatorActivity.java
ru.mail.parking.ui.NavigatorFloorView.java
ru.mail.parking.ui.SettingsActivity.java
ru.mail.parking.utils.NetworkAwaiter.java
ru.mail.parking.utils.NetworkStateReceiver.java
ru.mail.parking.utils.Utils.java
ru.mail.parking.widget.MainReceiver.java
ru.mail.parking.widget.MainWidgetProvider.java
ru.mail.parking.widget.SmartUpdate.java
ru.mail.parking.widget.UpdateService.java