package com.jsbogang.womp.ble; import android.content.Context; import android.content.SharedPreferences; import com.jsbogang.womp.workbench.WompConfig; public final class DevicePrefs { private DevicePrefs() {} private static SharedPreferences p(Context c) { return c.getSharedPreferences(WompConfig.DEVICE_PREFS, Context.MODE_PRIVATE); } public static void bind(Context c, String mac, String name, String password, int watchDay) { p(c).edit() .putBoolean("bound", true) .putString("mac", mac == null ? "" : mac) .putString("name", name == null ? "" : name) .putString("password", password == null || password.isEmpty() ? "0000" : password) .putInt("watchDay", Math.max(watchDay, 1)) .apply(); } public static void unbind(Context c) { p(c).edit().clear().apply(); } public static boolean isBound(Context c) { return p(c).getBoolean("bound", false) && !getMac(c).isEmpty(); } public static String getMac(Context c) { return p(c).getString("mac", ""); } public static String getName(Context c) { return p(c).getString("name", ""); } public static String getPassword(Context c) { return p(c).getString("password", "0000"); } public static int getWatchDay(Context c) { return p(c).getInt("watchDay", 3); } public static void setWatchDay(Context c, int watchDay) { p(c).edit().putInt("watchDay", Math.max(watchDay, 1)).apply(); } public static void setLiveStep(Context c, int step) { String today = new java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.CHINA) .format(new java.util.Date()); p(c).edit().putInt("liveStep", Math.max(step, 0)).putString("liveStepDate", today).apply(); } public static int getLiveStep(Context c) { String today = new java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.CHINA) .format(new java.util.Date()); if (!today.equals(p(c).getString("liveStepDate", ""))) { return 0; } return p(c).getInt("liveStep", 0); } }