|
@ -22,6 +22,9 @@ public class LoginDataManager { |
|
|
private static final String KEY_USER_ID = "user_id"; |
|
|
private static final String KEY_USER_ID = "user_id"; |
|
|
private static final String KEY_IS_LOGGED_IN = "is_logged_in"; |
|
|
private static final String KEY_IS_LOGGED_IN = "is_logged_in"; |
|
|
private static final String KEY_TOKEN_SAVE_TIME = "token_save_time"; // token保存时间,用于判断有效性 |
|
|
private static final String KEY_TOKEN_SAVE_TIME = "token_save_time"; // token保存时间,用于判断有效性 |
|
|
|
|
|
// 新增用户名和密码的键 |
|
|
|
|
|
private static final String KEY_USERNAME = "username"; |
|
|
|
|
|
private static final String KEY_PASSWORD = "password"; |
|
|
private SharedPreferences preferences; |
|
|
private SharedPreferences preferences; |
|
|
private Gson gson; |
|
|
private Gson gson; |
|
|
|
|
|
|
|
@ -31,6 +34,9 @@ public class LoginDataManager { |
|
|
// 内存缓存 |
|
|
// 内存缓存 |
|
|
private PadApiService.PadLoginResponse currentLoginData; |
|
|
private PadApiService.PadLoginResponse currentLoginData; |
|
|
private boolean isLoggedIn = false; |
|
|
private boolean isLoggedIn = false; |
|
|
|
|
|
// 新增用户名和密码的内存缓存 |
|
|
|
|
|
private String cachedUsername = ""; |
|
|
|
|
|
private String cachedPassword = ""; |
|
|
|
|
|
|
|
|
private LoginDataManager(Context context) { |
|
|
private LoginDataManager(Context context) { |
|
|
preferences = context.getApplicationContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
|
|
preferences = context.getApplicationContext().getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); |
|
@ -81,6 +87,36 @@ public class LoginDataManager { |
|
|
logLoginInfo(); |
|
|
logLoginInfo(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 新增保存用户名和密码的方法 |
|
|
|
|
|
public void saveCredentials(String username, String password) { |
|
|
|
|
|
SharedPreferences.Editor editor = preferences.edit(); |
|
|
|
|
|
editor.putString(KEY_USERNAME, username); |
|
|
|
|
|
editor.putString(KEY_PASSWORD, password); |
|
|
|
|
|
editor.apply(); |
|
|
|
|
|
|
|
|
|
|
|
// 更新内存缓存 |
|
|
|
|
|
this.cachedUsername = username; |
|
|
|
|
|
this.cachedPassword = password; |
|
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "用户名和密码保存成功"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 新增获取用户名的方法 |
|
|
|
|
|
public String getUsername() { |
|
|
|
|
|
if (!cachedUsername.isEmpty()) { |
|
|
|
|
|
return cachedUsername; |
|
|
|
|
|
} |
|
|
|
|
|
return preferences.getString(KEY_USERNAME, ""); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 新增获取密码的方法 |
|
|
|
|
|
public String getPassword() { |
|
|
|
|
|
if (!cachedPassword.isEmpty()) { |
|
|
|
|
|
return cachedPassword; |
|
|
|
|
|
} |
|
|
|
|
|
return preferences.getString(KEY_PASSWORD, ""); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 获取当前登录数据 |
|
|
* 获取当前登录数据 |
|
|
* @return 登录数据,如果未登录返回null |
|
|
* @return 登录数据,如果未登录返回null |
|
@ -204,6 +240,8 @@ public class LoginDataManager { |
|
|
// 清除内存缓存 |
|
|
// 清除内存缓存 |
|
|
this.currentLoginData = null; |
|
|
this.currentLoginData = null; |
|
|
this.isLoggedIn = false; |
|
|
this.isLoggedIn = false; |
|
|
|
|
|
this.cachedUsername = ""; |
|
|
|
|
|
this.cachedPassword = ""; |
|
|
|
|
|
|
|
|
// 清除持久化存储 |
|
|
// 清除持久化存储 |
|
|
SharedPreferences.Editor editor = preferences.edit(); |
|
|
SharedPreferences.Editor editor = preferences.edit(); |
|
@ -211,6 +249,8 @@ public class LoginDataManager { |
|
|
editor.remove(KEY_AUTH_TOKEN); |
|
|
editor.remove(KEY_AUTH_TOKEN); |
|
|
editor.remove(KEY_USER_ID); |
|
|
editor.remove(KEY_USER_ID); |
|
|
editor.remove(KEY_TOKEN_SAVE_TIME); |
|
|
editor.remove(KEY_TOKEN_SAVE_TIME); |
|
|
|
|
|
editor.remove(KEY_USERNAME); |
|
|
|
|
|
editor.remove(KEY_PASSWORD); |
|
|
editor.putBoolean(KEY_IS_LOGGED_IN, false); |
|
|
editor.putBoolean(KEY_IS_LOGGED_IN, false); |
|
|
editor.apply(); |
|
|
editor.apply(); |
|
|
|
|
|
|
|
@ -232,6 +272,10 @@ public class LoginDataManager { |
|
|
} else { |
|
|
} else { |
|
|
Log.d(TAG, "本地无有效登录数据"); |
|
|
Log.d(TAG, "本地无有效登录数据"); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 加载用户名和密码到内存缓存 |
|
|
|
|
|
cachedUsername = preferences.getString(KEY_USERNAME, ""); |
|
|
|
|
|
cachedPassword = preferences.getString(KEY_PASSWORD, ""); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
Log.e(TAG, "加载登录数据失败: " + e.getMessage()); |
|
|
Log.e(TAG, "加载登录数据失败: " + e.getMessage()); |
|
|
// 清除可能损坏的数据 |
|
|
// 清除可能损坏的数据 |
|
|