package com.knacki.mimou.preference; import android.content.Context; import android.content.SharedPreferences; import com.knacki.mimou.activity.MainActivity; import java.util.logging.Logger; /** * Created by thibal on 1/4/18. */ public abstract class CredentialHolder extends PreferenceHolder { public static final String TOKEN_LOCAL = "_local"; private static final String PREF_SECURITY_TOKEN = "securityToken"; private final static String USE_SMS = "smsApp"; public static void setLocalUse(Context c) { getSharedPreferences(c).edit().putString(PREF_SECURITY_TOKEN, TOKEN_LOCAL).commit(); } public static void setSecurityToken(Context c, String token) { getSharedPreferences(c).edit().putString(PREF_SECURITY_TOKEN, token).commit(); } public static boolean hasCredential(Context c) { return getSharedPreferences(c).contains(PREF_SECURITY_TOKEN); } public static boolean isLocal(Context c) { SharedPreferences p = getSharedPreferences(c); return p.contains(PREF_SECURITY_TOKEN) && p.getString(PREF_SECURITY_TOKEN, "").equals(TOKEN_LOCAL); } public static boolean isLocal(String token) { return token.equals(TOKEN_LOCAL); } public static String getToken(Context c) { return getSharedPreferences(c).getString(PREF_SECURITY_TOKEN, null); } public static Boolean hasSMSPermission(Context c) { SharedPreferences p = getSharedPreferences(c); if (p.contains(PREF_SECURITY_TOKEN) && p.getString(PREF_SECURITY_TOKEN, "").equals(TOKEN_LOCAL)) return true; if (p.contains(USE_SMS)) { return p.getBoolean(USE_SMS, false); } return null; } public static Boolean setSMSPermission(Context c, boolean granted) { SharedPreferences p = getSharedPreferences(c); if (!p.contains(PREF_SECURITY_TOKEN) || !p.getString(PREF_SECURITY_TOKEN, "").equals(TOKEN_LOCAL)) { p.edit().putBoolean(USE_SMS, granted).commit(); } return null; } static void reset(SharedPreferences.Editor p) { p.remove(PREF_SECURITY_TOKEN); p.remove(USE_SMS); } }