Android Open Source - playnomics-android Context Wrapper






From Project

Back to project page playnomics-android.

License

The source code is released under:

Apache License

If you think the Android project playnomics-android 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 com.playnomics.android.util;
/*  w  w w .  j  a  va2s . co m*/
import android.content.Context;
import android.content.SharedPreferences;

public class ContextWrapper {

  static final String CACHE_NAME = "com.playnomics.cache";
  static final String PUSH_ID_CACHE_KEY = "pushId";
  static final String LAST_EVENT_TIME_CACHE_KEY = "lastEventTime";
  static final String SESSION_START_TIME_CACHE_KEY = "sessionStartTime";
  static final String APP_VERSION_CACHE_KEY = "appVersion";
  static final String APP_VERSION_NAME_CACHE_KEY = "appVersionName";
  static final String SESSION_ID_KEY = "sessionId";
  static final String ANDROID_VERSION_CACHE_KEY = "androidVersion";

  static final int DEFAULT_CACHE_VALUE = -1;

  private SharedPreferences preferences;
  private Logger logger;
  private Util util;
  private Context context;

  public ContextWrapper(Context context, Logger logger, Util util) {
    this.logger = logger;
    this.util = util;
    this.context = context;
    preferences = context.getSharedPreferences(CACHE_NAME,
        Context.MODE_PRIVATE);
  }

  public Context getContext() {
    return context;
  }

  public EventTime getLastEventTime() {
    return getEventTimeValue(ContextWrapper.LAST_EVENT_TIME_CACHE_KEY);
  }

  public void setLastEventTime(EventTime time) {
    setEventTimeValue(ContextWrapper.LAST_EVENT_TIME_CACHE_KEY, time);
  }

  public EventTime getLastSessionStartTime() {
    return getEventTimeValue(ContextWrapper.SESSION_START_TIME_CACHE_KEY);
  }

  public void setLastSessionStartTime(EventTime time) {
    setEventTimeValue(ContextWrapper.SESSION_START_TIME_CACHE_KEY, time);
  }

  public LargeGeneratedId getPreviousSessionId() {
    Long sessionId = preferences.getLong(SESSION_ID_KEY,
        DEFAULT_CACHE_VALUE);
    if (sessionId < 0) {
      // session ID was never saved
      return null;
    }

    return new LargeGeneratedId(sessionId);
  }

  public void setPreviousSessionId(LargeGeneratedId sessionId) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putLong(SESSION_ID_KEY, sessionId.getId());
    editor.commit();
  }

  public String getPushRegistrationId() {
    return preferences.getString(ContextWrapper.PUSH_ID_CACHE_KEY, null);
  }

  public void setPushRegistrationId(String pushId) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(ContextWrapper.PUSH_ID_CACHE_KEY, pushId);
    editor.commit();
  }

  public int getApplicationVersion() {
    return preferences.getInt(ContextWrapper.APP_VERSION_CACHE_KEY,
        DEFAULT_CACHE_VALUE);
  }

  private void setApplicationVersion(int version) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putInt(ContextWrapper.APP_VERSION_CACHE_KEY, version);
    editor.commit();
  }

  public int getCurrentAppVersion() {
    return util.getApplicationVersionFromContext(context);
  }

  public String getApplicationVersionName() {
    return preferences.getString(ContextWrapper.APP_VERSION_NAME_CACHE_KEY,
        null);
  }

  private void setApplicationVersionName(String version) {
    if (version==null) return;
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(ContextWrapper.APP_VERSION_NAME_CACHE_KEY, version);
    editor.commit();
  }

  public String getCurrentAppVersionName() {
    return util.getApplicationVersionStringFromContext(context);
  }

  public String getCachedAndroidVersion() {
    return preferences.getString(ContextWrapper.ANDROID_VERSION_CACHE_KEY,
        null);
  }

  private void cacheAndroidVersion(String version) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(ContextWrapper.ANDROID_VERSION_CACHE_KEY, version);
    editor.commit();
  }

  public boolean isAppVersionChanged() {
    int cachedVersion = getApplicationVersion();
    int currentVersion = getCurrentAppVersion();
    String cachedVersionName = getApplicationVersionName();
    String currentVersionName = getCurrentAppVersionName();
    if ( (cachedVersion != currentVersion) ||
       (currentVersionName!=null && Util.safeStringCompare(currentVersionName, cachedVersionName)!=0) ) {
      setApplicationVersion(currentVersion);
      setApplicationVersionName(currentVersionName);
      // the push ID is no longer valid
      setPushRegistrationId(null);
      return true;
    }
    return false;
  }

  public boolean isAndroidVersionChanged() {
    String cachedVersion = getCachedAndroidVersion();
    String currentVersion = Util.getAndroidOSVersion();
    if (Util.safeStringCompare(currentVersion, cachedVersion)!=0) {
      cacheAndroidVersion(currentVersion);
      return true;
    }
    return false;
  }

  public boolean pushSettingsOutdated() {
    return Util.stringIsNullOrEmpty(getPushRegistrationId());
  }

  private EventTime getEventTimeValue(String key) {
    long lastEventTimeMilliseconds = preferences.getLong(key,
        DEFAULT_CACHE_VALUE);

    if (lastEventTimeMilliseconds >= 0) {
      return new EventTime(lastEventTimeMilliseconds);
    }
    return null;
  }

  private void setEventTimeValue(String key, EventTime value) {
    SharedPreferences.Editor editor = preferences.edit();
    editor.putLong(key, value.getTimeInMillis());
    editor.commit();
  }
}




Java Source Code List

com.playnomics.PlaynomicsMoreTestActivity.java
com.playnomics.PlaynomicsTestAppActivity.java
com.playnomics.RichDataFrameDelegate.java
com.playnomics.android.client.AssetClient.java
com.playnomics.android.client.EventQueue.java
com.playnomics.android.client.EventWorker.java
com.playnomics.android.client.HttpConnectionFactory.java
com.playnomics.android.client.IEventQueue.java
com.playnomics.android.client.IEventWorker.java
com.playnomics.android.client.IHttpConnectionFactory.java
com.playnomics.android.client.PlacementDataClient.java
com.playnomics.android.client.SegmentationClient.java
com.playnomics.android.events.AppPageEvent.java
com.playnomics.android.events.AppPauseEvent.java
com.playnomics.android.events.AppResumeEvent.java
com.playnomics.android.events.AppRunningEvent.java
com.playnomics.android.events.AppStartEvent.java
com.playnomics.android.events.CustomEvent.java
com.playnomics.android.events.ExplicitEvent.java
com.playnomics.android.events.ImplicitEvent.java
com.playnomics.android.events.PlaynomicsEvent.java
com.playnomics.android.events.TransactionEvent.java
com.playnomics.android.events.UserInfoEvent.java
com.playnomics.android.messaging.CloseButton.java
com.playnomics.android.messaging.HtmlAdFactory.java
com.playnomics.android.messaging.HtmlAd.java
com.playnomics.android.messaging.HtmlCloseButton.java
com.playnomics.android.messaging.MessagingManager.java
com.playnomics.android.messaging.NativeCloseButton.java
com.playnomics.android.messaging.Placement.java
com.playnomics.android.messaging.Position.java
com.playnomics.android.messaging.Target.java
com.playnomics.android.messaging.ui.IPlayViewFactory.java
com.playnomics.android.messaging.ui.PlayDialog.java
com.playnomics.android.messaging.ui.PlayViewFactory.java
com.playnomics.android.messaging.ui.PlayWebView.java
com.playnomics.android.messaging.ui.RenderTaskFactory.java
com.playnomics.android.push.GcmBroadcastReceiver.java
com.playnomics.android.push.GcmIntentService.java
com.playnomics.android.push.GcmManager.java
com.playnomics.android.sdk.IGoogleCloudMessageConfig.java
com.playnomics.android.sdk.IPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementDelegate.java
com.playnomics.android.sdk.IPlaynomicsPlacementRawDelegate.java
com.playnomics.android.sdk.IPlaynomicsSegmentationDelegate.java
com.playnomics.android.sdk.IPushConfig.java
com.playnomics.android.sdk.IPushNotificationDelegate.java
com.playnomics.android.sdk.Playnomics.java
com.playnomics.android.segments.UserSegmentIds.java
com.playnomics.android.session.ActivityObserver.java
com.playnomics.android.session.GameSessionInfo.java
com.playnomics.android.session.HeartBeatHandler.java
com.playnomics.android.session.HeartBeatProducer.java
com.playnomics.android.session.IActivityObserver.java
com.playnomics.android.session.ICallbackProcessor.java
com.playnomics.android.session.IHeartBeatProducer.java
com.playnomics.android.session.SessionStateMachine.java
com.playnomics.android.session.Session.java
com.playnomics.android.session.TouchEventHandler.java
com.playnomics.android.session.WindowCallbackProxy.java
com.playnomics.android.util.AndroidLogger.java
com.playnomics.android.util.AsyncTaskRunner.java
com.playnomics.android.util.CacheFile.java
com.playnomics.android.util.Config.java
com.playnomics.android.util.ContextWrapper.java
com.playnomics.android.util.EventTime.java
com.playnomics.android.util.IAsyncCall.java
com.playnomics.android.util.IConfig.java
com.playnomics.android.util.IRandomGenerator.java
com.playnomics.android.util.LargeGeneratedId.java
com.playnomics.android.util.LogWriter.java
com.playnomics.android.util.Logger.java
com.playnomics.android.util.Util.java