Android Open Source - RateMyApp Shared Preferences Manager






From Project

Back to project page RateMyApp.

License

The source code is released under:

MIT License

If you think the Android project RateMyApp 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.mariosangiorgio.ratemyapp;
//  ww w  .ja  v a 2  s .c  om
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPreferencesManager implements PreferencesManager{
    private final static String SHARED_PREFERENCES_ID = "RateMyApp";
    private final static String ALERT_ENABLED = "alertEnabled";
    private final static String LAUNCH_COUNTER = "launchCounter";
    private final static String FIRST_LAUNCH_TIMESTAMP = "firstLaunchTimestamp";
    private final static int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;

    private final SharedPreferences sharedPreferences;

    private SharedPreferencesManager(Context context) {
        sharedPreferences = context.getSharedPreferences(SHARED_PREFERENCES_ID, 0);
        if (!sharedPreferences.contains(FIRST_LAUNCH_TIMESTAMP)) {
            resetFirstLaunchTimestamp();
        }
    }

    public static SharedPreferencesManager buildFromContext(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("context should not be null");
        }
        return new SharedPreferencesManager(context);
    }

    public boolean alertEnabled() {
        return sharedPreferences.getBoolean(ALERT_ENABLED, true);
    }

    public int launchCounter() {
        return sharedPreferences.getInt(LAUNCH_COUNTER, 0);
    }

    public long firstLaunchTimestamp() {
        return sharedPreferences.getLong(FIRST_LAUNCH_TIMESTAMP, 0);
    }

    public void incrementLaunchCounter() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(LAUNCH_COUNTER, launchCounter() + 1);
        editor.commit();
    }

    public int daysFromFirstLaunch() {
        return (int) (System.currentTimeMillis() - firstLaunchTimestamp()) / MILLIS_IN_DAY;
    }

    public void setAlertEnabled(boolean enable) {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putBoolean(ALERT_ENABLED, enable);
        editor.commit();
    }

    public void resetFirstLaunchTimestamp() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putLong(FIRST_LAUNCH_TIMESTAMP, System.currentTimeMillis());
        editor.commit();
    }

    @Override
    public void resetLaunchCount() {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putInt(LAUNCH_COUNTER, 0);
        editor.commit();
    }
}




Java Source Code List

com.mariosangiorgio.ratemyapp.ContextUtils.java
com.mariosangiorgio.ratemyapp.OptionalValue.java
com.mariosangiorgio.ratemyapp.PreferencesManager.java
com.mariosangiorgio.ratemyapp.RateMyAppBuilder.java
com.mariosangiorgio.ratemyapp.RateMyApp.java
com.mariosangiorgio.ratemyapp.SharedPreferencesManager.java
com.mariosangiorgio.ratemyapp.actions.Action.java
com.mariosangiorgio.ratemyapp.actions.OpenPlayStoreAction.java
com.mariosangiorgio.ratemyapp.actions.ShowDialogAction.java
com.mariosangiorgio.ratemyapp.dialogs.AbstractDialogFragment.java
com.mariosangiorgio.ratemyapp.dialogs.NumberOfStarsDialog.java
com.mariosangiorgio.ratemyapp.dialogs.SendEmailDialog.java
com.mariosangiorgio.ratemyapp.dialogs.WantToRateDialog.java
com.mariosangiorgio.ratemyapp.listeners.DialogClickListener.java
com.mariosangiorgio.ratemyapp.listeners.NumberOfStarsDialogListener.java
com.mariosangiorgio.ratemyapp.listeners.SentEmailDialogListener.java
com.mariosangiorgio.ratemyapp.listeners.WantToRateDialogListener.java
com.mariosangiorgio.ratemyapp.sample.MainActivity.java