Files
CubeAoleyunSN/app/src/main/java/com/aoleyun/sn/manager/ConnectManager.java
Fanhuitong 51a125ce3f version:huaruian 1.1.1
fix:修复自动升级后不下载桌面和其他更新
update:增加bugly
2023-08-31 17:13:08 +08:00

152 lines
5.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.aoleyun.sn.manager;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import com.aoleyun.sn.comm.CommonConfig;
import com.tencent.mmkv.MMKV;
public class ConnectManager {
private static final String TAG = ConnectManager.class.getSimpleName();
public static final long ONE_MILLISECOND = 1000;
public static final long ONE_MINUTES_TIME = 60 * ONE_MILLISECOND;
public static final long FIFTEEN_MINUTES_TIME = ONE_MINUTES_TIME * 15;
public static final long HALF_HOUR_TIME = FIFTEEN_MINUTES_TIME * 2;
public static final long ONE_HOUR_TIME = HALF_HOUR_TIME * 2;
public static final long SIX_HOUR_TIME = ONE_HOUR_TIME * 6;
public static final long HALF_DAY_TIME = SIX_HOUR_TIME * 2;
public static final long ONE_DAY_TIME = HALF_DAY_TIME * 2;
public static final long TWO_DAY_TIME = ONE_DAY_TIME * 2;
public static final long FOUR_DAY_TIME = TWO_DAY_TIME * 2;
public static final long A_WEEK_TIME = ONE_DAY_TIME * 7;
/*重启后连接成功的时间*/
public static final String REBOOT_LAST_ONNECT_TIME = "reboot_last_connect_time";
/*WiFi连接后连接成功的时间*/
public static final String WIFI_LAST_CONNECT_TIME = "WiFi_last_connect_time";
/*打开设备信息连接成功的时间*/
public static final String OPENINFO_LAST_ONNECT_TIME = "opneinfo_last_connect_time";
@SuppressLint("StaticFieldLeak")
private static ConnectManager sInstance;
private Context mContext;
private MMKV mMMKV = MMKV.mmkvWithID(CommonConfig.MMKV_ID, MMKV.MULTI_PROCESS_MODE);
private ConnectManager(Context context) {
if (context == null) {
throw new RuntimeException("Context is NULL");
}
this.mContext = context;
}
public static void init(Context context) {
if (sInstance == null) {
Log.e(TAG, "init: ");
sInstance = new ConnectManager(context);
}
}
public static ConnectManager getInstance() {
if (sInstance == null) {
throw new IllegalStateException("You must be init ConnectManager first");
}
return sInstance;
}
public long getConnectModeTime(ConnectMode connectMode) {
long time = 0;
switch (connectMode) {
case DEFAULT:
time = 0;
break;
case ONE_MINUTE:
time = ONE_MINUTES_TIME;
break;
case FIFTEEN_MINUTES:
time = FIFTEEN_MINUTES_TIME;
break;
case HALF_HOUR:
time = HALF_HOUR_TIME;
break;
case ONE_HOUR:
time = ONE_HOUR_TIME;
break;
case SIX_HOUR:
time = SIX_HOUR_TIME;
break;
case HALF_DAY:
time = HALF_DAY_TIME;
break;
case ONE_DAY:
time = ONE_DAY_TIME;
break;
case TWO_DAY:
time = TWO_DAY_TIME;
break;
case FOUR_DAY:
time = FOUR_DAY_TIME;
break;
case A_WEEK:
time = A_WEEK_TIME;
break;
default:
}
return time;
}
public boolean isNeedConnect(String key, ConnectMode connectMode) {
if (connectMode == ConnectMode.ONCE) {
return false;
}
long nowTime = System.currentTimeMillis();
long lastTime = mMMKV.decodeLong(key);
// long lastTime = (long) SPUtils.get(mContext, key, 0L);
Log.e(TAG, "isNeedConnect: key = " + key + "\ttime = " + lastTime);
long intervalTime = getConnectModeTime(connectMode);
//防止一分钟内重复请求
boolean refresh = nowTime - lastTime > intervalTime && nowTime - lastTime > ONE_MILLISECOND * 5;
Log.e(TAG, "isNeedConnect: " + refresh);
return refresh;
}
/**
* @return 重启后是否连接
*/
// public boolean isRebootFistConnect() {
// long rebootTime = mMMKV.decodeLong(REBOOT_LAST_ONNECT_TIME, 0);
// //只在开机后15内连接其他情况为service重启
// long time = System.currentTimeMillis() - rebootTime;
// return time < 15 * 1000;
// }
/**
* @return 今天WiFi连接是否有连接
*/
// public boolean isWiFiFistConnect() {
// long time = mMMKV.decodeLong(WIFI_LAST_CONNECT_TIME, 0);
// return !TimeUtils.isTodayTime(time);
// }
/**
* @return 今天打开设备信息后是否连接
*/
// public boolean isOpenInfoFistConnect() {
// long time = mMMKV.decodeLong(OPENINFO_LAST_ONNECT_TIME, 0);
// return !TimeUtils.isTodayTime(time);
// }
/**
* @param WiFiAlias
* @return 今天切换WiFi后是否连接
*/
// public boolean isWiFiCutoverFistConnect(String WiFiAlias) {
// long time = mMMKV.decodeLong(WiFiAlias, 0);
// return !TimeUtils.isTodayTime(time);
// }
}