refactor(prefs): 用 MMKV 替换 SPUtils/SharedPreferences

新增 KVStore 统一轻量配置读写,删除 SPUtils,并在 MMKV 初始化后一次性迁移旧 share_data 配置。
This commit is contained in:
tongtong committed 2026-11-12 17:46:25 +08:00
1 parent 9ff1abfb3c
commit 024def0017
17 files changed
+171 -188

No files matched your search

@@ -12,6 +12,7 @@ import com.arialyy.aria.core.Aria;
import com.fuying.sn.BuildConfig;
import com.fuying.sn.core.common.sync.DeviceSyncGateways;
import com.fuying.sn.core.common.system.AppEnvironment;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.core.data.repository.DeviceRepository;
import com.fuying.sn.platform.app.RunningAppManager;
import com.fuying.sn.platform.time.TimeControlManager;
@@ -100,6 +101,8 @@ public class BaseApplication extends Application {
private void utilsInint() {
String rootDir = MMKV.initialize(this);
Log.e(TAG, "mmkv root: " + rootDir);
// MMKV 初始化完成后把旧版 SharedPreferences 的配置一次性迁移过来
KVStore.migrateLegacy(this);
PushManager.init(this);
aliyunPushInit();
@@ -22,7 +22,7 @@ import io.reactivex.rxjava3.disposables.Disposable;
* - 手动创建 Thread / Executor / AsyncTask 执行异步任务(必须使用 RxJava + {@link #addDisposable(Disposable)})
* - 忘记将 Disposable add 到 {@link #mDisposables} 中
*
* 若需要 Context / Application(如 SharedPreferences、PackageManager 等),请继承
* 若需要 Context / Application(如 PackageManager 等),请继承
* {@link BaseAndroidViewModel},通过 {@code getAppContext()} 获取 Application Context。
*/
public abstract class BaseViewModel extends ViewModel implements IMvvmUiState {
@@ -21,6 +21,7 @@ import android.util.Log;
import android.view.WindowManager;
import com.fuying.sn.core.common.system.ReceiverCompat;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.google.gson.JsonObject;
import java.io.BufferedReader;
@@ -251,7 +252,7 @@ public class HardwareInfoUtils {
Log.e(TAG, "getHardware: storage = " + storage);
double use_space = getUse_space(context);
Log.e(TAG, "getHardware: use_space = " + use_space);
long wifi_time = (long) com.fuying.sn.core.data.local.prefs.SPUtils.get(context, "wifi_last_connect_time", 0L) / 1000;
long wifi_time = KVStore.getLong("wifi_last_connect_time", 0L) / 1000;
Log.e(TAG, "getHardware: wifi_time" + wifi_time);
int CPU = getNumCores();
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
@@ -4,7 +4,7 @@ import android.content.Context;
import com.fuying.sn.BuildConfig;
import com.fuying.sn.core.common.config.CommonConfig;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
/**
* 应用运行环境。
@@ -48,6 +48,6 @@ public final class AppEnvironment {
* @return 调试模式返回 true
*/
public static boolean isDebugMode() {
return BuildConfig.DEBUG || (boolean) SPUtils.get(getContext(), CommonConfig.EnableDebug, false);
return BuildConfig.DEBUG || KVStore.getBoolean(CommonConfig.EnableDebug, false);
}
}
@@ -0,0 +1,117 @@
package com.fuying.sn.core.data.local.prefs;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.fuying.sn.core.common.config.CommonConfig;
import com.tencent.mmkv.MMKV;
import java.util.Map;
/**
* 本地键值存储统一入口,底层使用 MMKV。
* <p>
* 业务层统一通过本类读写轻量配置,不再直接使用 SharedPreferences / SPUtils。
*/
public final class KVStore {
private static final String TAG = "KVStore";
/**
* 旧版 SharedPreferences 文件名,仅用于一次性迁移
*/
private static final String LEGACY_SP_NAME = "share_data";
/**
* 迁移完成标记,避免每次启动重复读取旧 SP
*/
private static final String KEY_LEGACY_MIGRATED = "kv_legacy_sp_migrated";
private KVStore() {
}
private static MMKV store() {
return MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
}
public static int getInt(String key, int defaultValue) {
return store().decodeInt(key, defaultValue);
}
public static void putInt(String key, int value) {
store().encode(key, value);
}
public static String getString(String key, String defaultValue) {
return store().decodeString(key, defaultValue);
}
public static void putString(String key, String value) {
store().encode(key, value);
}
public static boolean getBoolean(String key, boolean defaultValue) {
return store().decodeBool(key, defaultValue);
}
public static void putBoolean(String key, boolean value) {
store().encode(key, value);
}
public static long getLong(String key, long defaultValue) {
return store().decodeLong(key, defaultValue);
}
public static void putLong(String key, long value) {
store().encode(key, value);
}
/**
* @return MMKV 中的全部键值,仅用于诊断日志
*/
public static Map<String, ?> getAll() {
return store().getAll();
}
/**
* 一次性把旧版 SharedPreferences("share_data") 的数据迁移到 MMKV。
* 迁移完成后写入标记,后续不再读取 SharedPreferences。
*/
public static void migrateLegacy(Context context) {
if (context == null || store().decodeBool(KEY_LEGACY_MIGRATED, false)) {
return;
}
try {
SharedPreferences legacy = context.getSharedPreferences(LEGACY_SP_NAME, Context.MODE_PRIVATE);
Map<String, ?> values = legacy.getAll();
if (values != null) {
for (Map.Entry<String, ?> entry : values.entrySet()) {
encodeObject(entry.getKey(), entry.getValue());
}
Log.d(TAG, "migrateLegacy: 已迁移旧配置 " + values.size() + " 项");
}
store().encode(KEY_LEGACY_MIGRATED, true);
} catch (Throwable e) {
Log.e(TAG, "migrateLegacy: " + e.getMessage());
}
}
private static void encodeObject(String key, Object value) {
if (key == null || value == null) {
return;
}
if (value instanceof String) {
store().encode(key, (String) value);
} else if (value instanceof Integer) {
store().encode(key, (Integer) value);
} else if (value instanceof Boolean) {
store().encode(key, (Boolean) value);
} else if (value instanceof Float) {
store().encode(key, (Float) value);
} else if (value instanceof Long) {
store().encode(key, (Long) value);
} else {
store().encode(key, String.valueOf(value));
}
}
}
@@ -1,139 +0,0 @@
package com.fuying.sn.core.data.local.prefs;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
/**
* 作者 mjsheng
* 日期 2018/10/9 18:41
* 邮箱 501802639@qq.com
* 来自:
*/
public class SPUtils {
private static final String TAG = "SPUtils";
/**
* 保存在手机里面的文件名
*/
public static final String FILE_NAME = "share_data";
/**
* 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
*/
public static boolean put(Context context, String key, Object object) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
return SharedPreferencesCompat.apply(editor);
}
/**
* 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
*/
public static Object get(Context context, String key, Object defaultObject) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
if (defaultObject instanceof String) {
return sp.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return sp.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return sp.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return sp.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return sp.getLong(key, (Long) defaultObject);
}
return null;
}
// /**
// * 清除所有数据
// */
// public static void clear(Context context) {
// SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
// Context.MODE_PRIVATE);
// SharedPreferences.Editor editor = sp.edit();
// editor.clear();
// SharedPreferencesCompat.apply(editor);
// reductFirstEnter(context);
// }
// //还原状态firstEnter信息
// private static void reductFirstEnter(Context context){
// put(context, CommonDatas.FLAG_FIRST_ENTER,CommonDatas.FLAG_FIRST_ENTER);
// }
/**
* 返回所有的键值对
*/
public static Map<String, ?> getAll(Context context) {
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
return sp.getAll();
}
/**
* 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类
*/
private static class SharedPreferencesCompat {
private static final Method sApplyMethod = findApplyMethod();
/**
* 反射查找apply的方法
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static Method findApplyMethod() {
try {
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
} catch (NoSuchMethodException e) {
Log.e(TAG, "findApplyMethod: " + e.getMessage());
}
return null;
}
/**
* 如果找到则使用apply执行,否则使用commit
*/
public static boolean apply(SharedPreferences.Editor editor) {
try {
if (sApplyMethod != null) {
Log.e(TAG, "apply: false");
sApplyMethod.invoke(editor);
return false;
}
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
Log.e(TAG, "apply: " + e.getMessage());
}
return editor.commit();
}
}
}
@@ -27,7 +27,7 @@ import com.fuying.sn.core.common.ui.BaseFragmentPagerAdapter;
import com.fuying.sn.core.common.ui.ToastUtil;
import com.fuying.sn.core.data.dto.common.BaseResponse;
import com.fuying.sn.core.data.dto.device.SnInfo;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.databinding.ActivityHomeBinding;
import com.fuying.sn.feature.home.UserDebugDialog;
import com.fuying.sn.feature.device.DeviceFragment;
@@ -197,7 +197,7 @@ public class HomeActivity extends BaseMvvmActivity<HomeViewModel, ActivityHomeBi
if (DeviceManager.isDebugMode()) {
return true;
} else {
return (boolean) SPUtils.get(this, CommonConfig.userDebugMode, false);
return KVStore.getBoolean(CommonConfig.userDebugMode, false);
}
}
@@ -212,7 +212,7 @@ public class HomeActivity extends BaseMvvmActivity<HomeViewModel, ActivityHomeBi
mDEBUGHits[mDEBUGHits.length - 1] = SystemClock.uptimeMillis();
if (mDEBUGHits[0] >= SystemClock.uptimeMillis() - DEBUGDURATION) {
mDEBUGHits = new long[DEBUGCOUNTS]; //重新初始化数组
if ((boolean) SPUtils.get(HomeActivity.this, CommonConfig.EnableDebug, false)) {
if (KVStore.getBoolean(CommonConfig.EnableDebug, false)) {
ToastUtil.show("已经是Debug模式");
} else {
showDebugDialog();
@@ -226,7 +226,7 @@ public class HomeActivity extends BaseMvvmActivity<HomeViewModel, ActivityHomeBi
@Override
public void onPositiveClick() {
if (DebugPasswordVerifier.verify(dialog.getEdittext())) {
SPUtils.put(HomeActivity.this, CommonConfig.EnableDebug, true);
KVStore.putBoolean(CommonConfig.EnableDebug, true);
AndroidSettingsWriter.putInt(HomeActivity.this, AndroidSettingsWriter.NAMESPACE_GLOBAL, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
AndroidSettingsWriter.putInt(HomeActivity.this, AndroidSettingsWriter.NAMESPACE_GLOBAL, Settings.Global.ADB_ENABLED, 1);
@@ -62,7 +62,7 @@ import io.reactivex.rxjava3.core.ObservableOnSubscribe;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.platform.app.ApkUtils;
@@ -359,7 +359,7 @@ public class JgyUtils {
}
public void cleanLauncherCache() {
int cleaned = (int) SPUtils.get(mContext, "fristcleanLauncherCache", 0);
int cleaned = KVStore.getInt("fristcleanLauncherCache", 0);
if (cleaned != 0) {
return;
}
@@ -367,7 +367,7 @@ public class JgyUtils {
// 清理最多会等待 60 秒,交给异步任务,避免安装广播的主线程被阻塞
ApkUtils.cleanApplicationUserDataAsync(mContext, "com.android.launcher3", succeeded -> {
if (succeeded) {
SPUtils.put(mContext, "fristcleanLauncherCache", 1);
KVStore.putInt("fristcleanLauncherCache", 1);
}
Log.e(TAG, "cleanLauncherCache: end " + succeeded);
});
@@ -669,9 +669,9 @@ public class JgyUtils {
public ArrayList<String> getForbidList() {
ArrayList<String> disableApp = new ArrayList<>();
int search_topic = (int) SPUtils.get(mContext, "search_topic", 0);
int search_topic = KVStore.getInt("search_topic", 0);
Log.e(TAG, "getForbidList: search_topic = " + search_topic);
Log.e(TAG, "getForbidList: " + GsonUtils.toJSONString(SPUtils.getAll(mContext)));
Log.e(TAG, "getForbidList: " + GsonUtils.toJSONString(KVStore.getAll()));
if (search_topic == 0) {
disableApp.add("拍照搜题");
@@ -715,7 +715,7 @@ public class JgyUtils {
}
public ArrayList<String> getHideList() {
String appString = (String) SPUtils.get(mContext, "Hide_APP_List", "");
String appString = KVStore.getString("Hide_APP_List", "");
Log.e(TAG, "Hide_APP_List: " + appString);
if (TextUtils.isEmpty(appString)) {
return new ArrayList<>();
@@ -47,7 +47,7 @@ import com.fuying.sn.runtime.sync.RemoteDiagnostics;
import com.fuying.sn.platform.app.ApkUtils;
import com.fuying.sn.platform.system.CmdUtil;
import com.fuying.sn.platform.system.JgyUtils;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.platform.system.SystemUtils;
import com.fuying.sn.platform.time.TimeUtils;
import com.fuying.sn.core.common.ui.ToastUtil;
@@ -930,7 +930,8 @@ public class PushManager {
Log.e(TAG, "searchTopic: " + jsonString);
JsonObject jsonObject = GsonUtils.getJsonObject(jsonString);
int search_topic = jsonObject.get("search_topic").getAsInt();
Log.e(TAG, "searchTopic: put = " + SPUtils.put(mContext, "search_topic", search_topic));
KVStore.putInt("search_topic", search_topic);
Log.e(TAG, "searchTopic: put = " + search_topic);
JgyUtils.getInstance().updateForbidList();
}
@@ -1559,7 +1560,7 @@ public class PushManager {
}
Intent intent = new Intent();
// intent.putExtra("name", name);
SPUtils.put(mContext, LOCK_STATE, type);
KVStore.putInt(LOCK_STATE, type);
if (1 == type) {
intent.setAction(ManagerService.ACTION_LOCK);
} else if (0 == type) {
@@ -13,7 +13,7 @@ import com.fuying.sn.platform.control.ControlManager;
import com.fuying.sn.platform.time.TimeManager;
import com.fuying.sn.platform.app.ApkUtils;
import com.fuying.sn.platform.system.JgyUtils;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import java.util.concurrent.TimeUnit;
@@ -101,7 +101,7 @@ public class ApkInstallReceiver extends BroadcastReceiver {
.observeOn(RxSchedulers.io())
.subscribe(packageName -> {
Log.e("sendAppInfo", "onNext: " + packageName);
int isLogined = (int) SPUtils.get(mContext, CommonConfig.isLogined, 2);
int isLogined = KVStore.getInt(CommonConfig.isLogined, 2);
if (isLogined != 2) {
// AppPolicySync.getInstance().getForceInstall();
// AppInventorySync.getInstance().getAppInside();
@@ -31,7 +31,7 @@ import com.fuying.sn.runtime.push.PushManager;
import com.fuying.sn.runtime.receiver.ApkInstallReceiver;
import com.fuying.sn.runtime.receiver.BootReceiver;
import com.fuying.sn.core.common.system.ReceiverCompat;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.google.gson.JsonObject;
import com.tencent.mmkv.MMKV;
import com.tuo.customview.VerificationCodeView;
@@ -96,7 +96,7 @@ public class ManagerService extends BaseService implements NetworkUtils.OnNetwor
@Override
public void onNext(Long aLong) {
Log.e("TimeObserver", "onNext: " + aLong);
int isLogined = (int) SPUtils.get(ManagerService.this, CommonConfig.isLogined, 2);
int isLogined = KVStore.getInt(CommonConfig.isLogined, 2);
if (isLogined == 2) {
}
@@ -157,7 +157,7 @@ public class ManagerService extends BaseService implements NetworkUtils.OnNetwor
} else {
// 服务端明确返回非锁定(未绑定/未登录等):直接解锁
hideFloatingWindow();
SPUtils.put(ManagerService.this, LOCK_STATE, 0);
KVStore.putInt(LOCK_STATE, 0);
}
}
@@ -179,10 +179,10 @@ public class ManagerService extends BaseService implements NetworkUtils.OnNetwor
private void setLockedState(int lockedState) {
if (lockedState == 1) {
showFloatingWindow("屏幕已锁定");
SPUtils.put(ManagerService.this, LOCK_STATE, 1);
KVStore.putInt(LOCK_STATE, 1);
} else {
hideFloatingWindow();
SPUtils.put(ManagerService.this, LOCK_STATE, 0);
KVStore.putInt(LOCK_STATE, 0);
}
}
@@ -197,7 +197,7 @@ public class ManagerService extends BaseService implements NetworkUtils.OnNetwor
if (pwdUnlocked) {
return 0;
}
return (int) SPUtils.get(ManagerService.this, LOCK_STATE, 0);
return KVStore.getInt(LOCK_STATE, 0);
}
private void showFloatingWindow(String name) {
@@ -47,7 +47,7 @@ import com.fuying.sn.runtime.sync.DeviceProfileSync;
import com.fuying.sn.platform.app.ApkUtils;
import com.fuying.sn.platform.system.JgyUtils;
import com.fuying.sn.core.common.system.ReceiverCompat;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.platform.system.AndroidSettingsWriter;
import com.fuying.sn.platform.system.SystemUtils;
import com.fuying.sn.platform.time.TimeUtils;
@@ -160,7 +160,7 @@ public class MainService extends BaseService implements ViewModelStoreOwner, Net
if (screenOnReconnect) {
// 使用记录补传仍按亮屏重连的原有条件执行。
AppUsageReporter.getInstance().reUploadAppUsageRecords();
SPUtils.put(this, CommonConfig.JGY_FIRST_BOOT, 1);
KVStore.putInt(CommonConfig.JGY_FIRST_BOOT, 1);
}
}
@@ -253,7 +253,7 @@ public class MainService extends BaseService implements ViewModelStoreOwner, Net
private static final String DEFAULT_SOURCE = "com.fuying.sn,com.fuying.appstore,com.fuying.fuxiaoying";
private void versionIsUpdate() {
int oldVersionCode = (int) SPUtils.get(this, CommonConfig.SN_VERSIONCODE, 0);
int oldVersionCode = KVStore.getInt(CommonConfig.SN_VERSIONCODE, 0);
if (oldVersionCode < BuildConfig.VERSION_CODE) {
Log.e(TAG, "onCreate: " + "upgrade successful");
try {
@@ -278,7 +278,7 @@ public class MainService extends BaseService implements ViewModelStoreOwner, Net
} else {
}
SPUtils.put(this, CommonConfig.SN_VERSIONCODE, BuildConfig.VERSION_CODE);
KVStore.putInt(CommonConfig.SN_VERSIONCODE, BuildConfig.VERSION_CODE);
}
@Override
@@ -26,7 +26,7 @@ import com.fuying.sn.runtime.sync.DeviceProfileSync;
import com.fuying.sn.runtime.sync.DeviceSettingsSync;
import com.fuying.sn.runtime.sync.RequestCallbacks;
import com.fuying.sn.runtime.sync.TimePolicySync;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.platform.system.SystemUtils;
import com.google.gson.JsonObject;
import com.tencent.mmkv.MMKV;
@@ -282,7 +282,7 @@ public class MainServiceModel extends BaseAndroidViewModel {
if (!SystemUtils.isServiceAlive(getApplication(), ManagerService.class.getName())) {
getApplication().startService(new Intent(getApplication(), ManagerService.class));
}
SPUtils.put(getApplication(), ManagerService.LOCK_STATE, is_screen_lock);
KVStore.putInt(ManagerService.LOCK_STATE, is_screen_lock);
Intent intent = new Intent();
// intent.putExtra("name", name);
if (1 == is_screen_lock) {
@@ -293,7 +293,7 @@ public class MainServiceModel extends BaseAndroidViewModel {
getApplication().sendBroadcast(intent);
} else {
// if (BuildConfig.DEBUG) {
// SPUtils.put(getApplication(), ManagerService.LOCK_STATE, 1);
// KVStore.putInt(ManagerService.LOCK_STATE, 1);
// Intent intent = new Intent();
// intent.setAction(ManagerService.ACTION_LOCK);
// getApplication().sendBroadcast(intent);
@@ -29,7 +29,7 @@ import com.fuying.sn.core.data.remote.UrlAddress;
import com.fuying.sn.runtime.sync.RequestCallbacks.CompleteCallback;
import com.fuying.sn.platform.app.ApkUtils;
import com.fuying.sn.platform.system.JgyUtils;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.platform.control.AppWhitelistUtils;
import com.google.gson.JsonObject;
import com.tencent.mmkv.MMKV;
@@ -249,7 +249,7 @@ public final class AppPolicySync {
}
}
String jsonString = GsonUtils.toJSONString(hideAPPList);
SPUtils.put(mContext, "Hide_APP_List", String.join(",", jsonString));
KVStore.putString("Hide_APP_List", String.join(",", jsonString));
JgyUtils.getInstance().updateHideList();
Log.e(TAG, "Hide_APP_List: " + jsonString);
}
@@ -15,7 +15,7 @@ import com.fuying.sn.core.data.local.db.time.AppUsageRecordEntity;
import com.fuying.sn.core.data.remote.BackgroundRequests;
import com.fuying.sn.runtime.sync.RequestCallbacks.CompleteCallback;
import com.fuying.sn.platform.app.ApkUtils;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import java.util.List;
@@ -54,7 +54,7 @@ public final class AppUsageReporter {
public void sendAppUsageRecord(String packageName, long openTime, long closeTime, CompleteCallback completeCallback) {
Log.e(TAG, "sendAppUsageRecord: packageName = " + packageName + ", openTime = " + openTime + ", closeTime = " + closeTime);
int isLogined = (int) SPUtils.get(mContext, CommonConfig.isLogined, 2);
int isLogined = KVStore.getInt(CommonConfig.isLogined, 2);
if (isLogined != 1) {
Log.e(TAG, "sendAppUsageRecord: 没有绑定");
// TODO: 2025/6/13 延迟获取不了数据
@@ -167,7 +167,7 @@ public final class AppUsageReporter {
@Deprecated
public void sendCloseApp(String packageName, CompleteCallback completeCallback) {
int isLogined = (int) SPUtils.get(mContext, CommonConfig.isLogined, 2);
int isLogined = KVStore.getInt(CommonConfig.isLogined, 2);
if (isLogined != 1) {
Log.e(TAG, "sendCloseApp: 没有绑定");
// TODO: 2025/6/13 延迟获取不了数据
@@ -20,7 +20,7 @@ import com.fuying.sn.core.data.remote.BackgroundRequests;
import com.fuying.sn.core.data.remote.UrlAddress;
import com.fuying.sn.platform.app.ApkUtils;
import com.fuying.sn.platform.system.JgyUtils;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.fuying.sn.core.common.device.HardwareInfoUtils;
import com.google.gson.JsonObject;
import com.tencent.mmkv.MMKV;
@@ -92,32 +92,32 @@ public final class DeviceProfileSync {
@Override
public void onNext(BaseResponse<SnInfo> userInfoBaseResponse) {
Log.e("getSnInfo", "onNext: " + userInfoBaseResponse);
SPUtils.put(mContext, CommonConfig.JGY_FIRST_CONNECT, 1);
KVStore.putInt(CommonConfig.JGY_FIRST_CONNECT, 1);
if (callback != null)
callback.onNext(userInfoBaseResponse);
int code = userInfoBaseResponse.code;
if (code == 200 && userInfoBaseResponse.data != null) {
SPUtils.put(mContext, CommonConfig.isLogined, 1);
KVStore.putInt(CommonConfig.isLogined, 1);
SnInfo snInfo = userInfoBaseResponse.data;
// 统一交由 TimeConfigRepository 落库 + 回调,保证数据源唯一
TimeConfigRepository.getInstance().applySnInfo(snInfo);
AndroidSettingsWriter.putString(mContext, AndroidSettingsWriter.NAMESPACE_SYSTEM, "UserInfo_username", snInfo.getSn_name());
AndroidSettingsWriter.putString(mContext, AndroidSettingsWriter.NAMESPACE_SYSTEM, "UserInfo_grade", snInfo.getGrade());
SPUtils.put(mContext, "sn_id", snInfo.getId());
KVStore.putInt("sn_id", snInfo.getId());
if (!TextUtils.isEmpty(snInfo.getSn_name())) {
SPUtils.put(mContext, "USERINFO_NAME", snInfo.getSn_name());
KVStore.putString("USERINFO_NAME", snInfo.getSn_name());
}
if (!TextUtils.isEmpty(snInfo.getSchool())) {
SPUtils.put(mContext, "USERINFO_SCHOOL", snInfo.getSchool());
KVStore.putString("USERINFO_SCHOOL", snInfo.getSchool());
}
if (!TextUtils.isEmpty(snInfo.getGrade())) {
SPUtils.put(mContext, "USERINFO_GRADE", snInfo.getGrade());
KVStore.putString("USERINFO_GRADE", snInfo.getGrade());
}
} else if (code == 300) {
SPUtils.put(mContext, CommonConfig.isLogined, 0);
KVStore.putInt(CommonConfig.isLogined, 0);
AndroidSettingsWriter.putInt(mContext, AndroidSettingsWriter.NAMESPACE_SYSTEM, CommonConfig.KEY_IS_CONTROL, 0);
} else if (code == 401) {
SPUtils.put(mContext, CommonConfig.isLogined, 2);
KVStore.putInt(CommonConfig.isLogined, 2);
AndroidSettingsWriter.putInt(mContext, AndroidSettingsWriter.NAMESPACE_SYSTEM, CommonConfig.KEY_IS_CONTROL, 0);
}
}
@@ -22,7 +22,7 @@ import com.fuying.sn.platform.control.ControlManager;
import com.fuying.sn.core.data.remote.BackgroundRequests;
import com.fuying.sn.core.data.remote.UrlAddress;
import com.fuying.sn.runtime.sync.RequestCallbacks.CompleteCallback;
import com.fuying.sn.core.data.local.prefs.SPUtils;
import com.fuying.sn.core.data.local.prefs.KVStore;
import com.google.gson.Gson;
import com.tencent.mmkv.MMKV;
@@ -155,7 +155,7 @@ public final class DeviceSettingsSync {
@Override
public void onNext(BaseResponse<SystemSettingsSet> systemSettingsBaseResponse) {
Log.e("getSystemSettings", "onNext: " + systemSettingsBaseResponse);
SPUtils.put(mContext, "is_first_connection", 0);
KVStore.putInt("is_first_connection", 0);
int code = systemSettingsBaseResponse.code;
if (code == 200) {
@@ -177,7 +177,7 @@ public final class DeviceSettingsSync {
@Override
public void onError(Throwable e) {
if ((int) SPUtils.get(mContext, "is_first_connection", 1) == 1) {
if (KVStore.getInt("is_first_connection", 1) == 1) {
ControlManager.getInstance().setDisableSetting();
}
Log.e("getSystemSettings", "onError: " + e.getMessage());