package nodomain.freeyourgadget.gadgetbridge.womp;
|
|
import android.content.Context;
|
import android.content.SharedPreferences;
|
|
import org.json.JSONObject;
|
|
/**
|
* 分拣员本地登录态:保存手机号密码 7 天,不依赖服务端 session。
|
*/
|
public final class WompSession {
|
private WompSession() {}
|
|
public static SharedPreferences prefs(Context context) {
|
return context.getSharedPreferences(WompConfig.PREFS, Context.MODE_PRIVATE);
|
}
|
|
public static boolean isLoggedIn(Context context) {
|
SharedPreferences p = prefs(context);
|
String sorterId = p.getString("sorterId", null);
|
long loginAt = p.getLong("loginAt", 0);
|
return sorterId != null && sorterId.length() > 0
|
&& (System.currentTimeMillis() - loginAt) < WompConfig.LOGIN_KEEP_MS;
|
}
|
|
public static void saveCredentials(Context context, String phone, String password) {
|
prefs(context).edit()
|
.putString("phone", phone == null ? "" : phone)
|
.putString("password", password == null ? "" : password)
|
.apply();
|
}
|
|
public static void saveLogin(Context context, String phone, String password, JSONObject sorter) {
|
prefs(context).edit()
|
.putString("phone", phone)
|
.putString("password", password)
|
.putString("sorterId", sorter.optString("sorterId"))
|
.putString("businessName", sorter.optString("businessName"))
|
.putString("sorterJson", sorter.toString())
|
.putLong("loginAt", System.currentTimeMillis())
|
.apply();
|
}
|
|
public static void clear(Context context) {
|
SharedPreferences p = prefs(context);
|
String phone = p.getString("phone", "");
|
String password = p.getString("password", "");
|
boolean checkedIn = p.getBoolean("checkedIn", false);
|
String checkInSorterId = p.getString("checkInSorterId", "");
|
String checkInWorkDate = p.getString("checkInWorkDate", "");
|
p.edit().clear()
|
.putString("phone", phone)
|
.putString("password", password)
|
.putBoolean("checkedIn", checkedIn)
|
.putString("checkInSorterId", checkInSorterId)
|
.putString("checkInWorkDate", checkInWorkDate)
|
.apply();
|
}
|
|
public static void setCheckedIn(Context context, boolean checkedIn) {
|
SharedPreferences.Editor editor = prefs(context).edit()
|
.putBoolean("checkedIn", checkedIn);
|
if (checkedIn) {
|
editor.putString("checkInSorterId", getSorterId(context))
|
.putString("checkInWorkDate", today());
|
}
|
editor.apply();
|
WompKeepAliveService.onAttendanceChanged(context);
|
}
|
|
public static boolean isCheckedIn(Context context) {
|
SharedPreferences p = prefs(context);
|
if (!p.getBoolean("checkedIn", false)) {
|
return false;
|
}
|
String sorterId = p.getString("sorterId", "");
|
String owner = p.getString("checkInSorterId", "");
|
String workDate = p.getString("checkInWorkDate", "");
|
return sorterId.length() > 0 && sorterId.equals(owner) && today().equals(workDate);
|
}
|
|
public static String today() {
|
return new java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.CHINA)
|
.format(new java.util.Date());
|
}
|
|
public static String getSorterId(Context context) {
|
return prefs(context).getString("sorterId", "");
|
}
|
|
public static String getBusinessName(Context context) {
|
return prefs(context).getString("businessName", "");
|
}
|
|
public static String getPhone(Context context) {
|
return prefs(context).getString("phone", "");
|
}
|
|
public static String getPassword(Context context) {
|
return prefs(context).getString("password", "");
|
}
|
|
public static String getSorterJson(Context context) {
|
String json = prefs(context).getString("sorterJson", "{}");
|
return json == null ? "{}" : json;
|
}
|
}
|