version:2.8

fix:修复无网络连接时请求接口报错
update:优化网络请求
This commit is contained in:
2023-01-10 18:09:07 +08:00
parent cc5951ad0f
commit 74a0f631e2
31 changed files with 597 additions and 822 deletions

View File

@@ -5,35 +5,27 @@ import android.content.Intent;
import android.provider.Settings;
import android.util.Log;
import com.fuying.sn.bean.AppAndWhiteBean;
import com.fuying.sn.bean.AppStart;
import com.fuying.sn.bean.BaseResponse;
import com.fuying.sn.bean.MyAppUsageBean;
import com.fuying.sn.bean.SystemSettingsSet;
import com.fuying.sn.config.CommonConfig;
import com.fuying.sn.desktop.RunningAppManager;
import com.fuying.sn.disklrucache.CacheHelper;
import com.fuying.sn.gson.GsonUtils;
import com.fuying.sn.manager.ControlManager;
import com.fuying.sn.network.NetInterfaceManager;
import com.fuying.sn.network.UrlAddress;
import com.fuying.sn.service.ManagerService;
import com.fuying.sn.utils.JGYUtils;
import com.fuying.sn.utils.SPUtils;
import com.fuying.sn.utils.ServiceAliveUtils;
import com.fuying.sn.utils.StringUtils;
import com.fuying.sn.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.tencent.mmkv.MMKV;
import com.trello.rxlifecycle4.RxLifecycle;
import com.trello.rxlifecycle4.android.ActivityEvent;
import java.util.List;
import java.util.stream.Collectors;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
@@ -43,7 +35,7 @@ import io.reactivex.rxjava3.subjects.BehaviorSubject;
* @author jgy02
*/
public class MainSPresenter implements MainSContact.Presenter {
private String TAG = MainSPresenter.class.getSimpleName();
private static final String TAG = MainSPresenter.class.getSimpleName();
private MainSContact.MainView mView;
private Context mContext;
@@ -75,42 +67,14 @@ public class MainSPresenter implements MainSContact.Presenter {
this.mView = null;
}
@Override
public void getAppAndWhite() {
NetInterfaceManager.getInstance()
.getAppAndWhiteApiObservable()
.subscribe(new Observer<BaseResponse<List<AppAndWhiteBean>>>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
Log.e("getAppAndWhite", "onSubscribe: ");
}
@Override
public void onNext(@NonNull BaseResponse<List<AppAndWhiteBean>> baseResponse) {
Log.e("getAppAndWhite", "onSubscribe: " + baseResponse);
if (baseResponse.code == 200) {
List<AppAndWhiteBean> appAndWhiteList = baseResponse.data;
if (appAndWhiteList != null) {
List<String> pkgList = appAndWhiteList.stream().map(AppAndWhiteBean::getApp_package).collect(Collectors.toList());
String pkgString = StringUtils.join(",", pkgList);
mMMKV.encode(UrlAddress.GET_APP_AND_WHITE, pkgString);
Log.e("getAppAndWhite", "onNext: " + pkgString);
}
}
}
@Override
public void onError(@NonNull Throwable e) {
Log.e("getAppAndWhite", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e("getAppAndWhite", "onComplete: ");
}
});
mView.getAppAndWhiteFinish();
NetInterfaceManager.getInstance().getAppAndWhite(true, getLifecycle(), new NetInterfaceManager.CompleteCallback() {
@Override
public void onComplete() {
mView.getAppAndWhiteFinish();
}
});
}
@Override
@@ -155,29 +119,28 @@ public class MainSPresenter implements MainSContact.Presenter {
@Override
public void getSnInfo() {
NetInterfaceManager.getInstance()
.getSnInfo(true, getLifecycle(), new NetInterfaceManager.ObserverCallback() {
@Override
public void onSubscribe(Disposable d) {
Log.e("getSnInfo", "onSubscribe: ");
}
NetInterfaceManager.getInstance().getSnInfo(true, getLifecycle(), new NetInterfaceManager.ObserverCallback() {
@Override
public void onSubscribe(Disposable d) {
Log.e("getSnInfo", "onSubscribe: ");
}
@Override
public void onNext(BaseResponse response) {
Log.e("getSnInfo", "onNext: ");
mView.setSnInfo(response);
}
@Override
public void onNext(BaseResponse response) {
Log.e("getSnInfo", "onNext: ");
mView.setSnInfo(response);
}
@Override
public void onError(Throwable e) {
Log.e("getSnInfo", "onError: " + e.getMessage());
}
@Override
public void onError(Throwable e) {
Log.e("getSnInfo", "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e("getSnInfo", "onComplete: ");
}
});
@Override
public void onComplete() {
Log.e("getSnInfo", "onComplete: ");
}
});
}
@Override
@@ -242,45 +205,14 @@ public class MainSPresenter implements MainSContact.Presenter {
Log.e("getForceInstall", "onComplete: ");
}
});
}
@Override
public void getSystemSettings() {
NetInterfaceManager.getInstance()
.getsettingControl()
.compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
.subscribe(new Observer<BaseResponse<SystemSettingsSet>>() {
@Override
public void onSubscribe(Disposable d) {
Log.e("getSystemSettings", "onSubscribe: ");
}
@Override
public void onNext(BaseResponse<SystemSettingsSet> systemSettingsBaseResponse) {
Log.e("getSystemSettings", "onNext: " + systemSettingsBaseResponse);
SPUtils.put(mContext, "is_first_connection", 0);
int code = systemSettingsBaseResponse.code;
if (code == 200) {
String data = new Gson().toJson(systemSettingsBaseResponse.data.getUser());
ControlManager.getInstance().setSystemSetting(data);
} else {
ControlManager.getInstance().setDisableSetting();
}
}
@Override
public void onError(Throwable e) {
if ((int) SPUtils.get(mContext, "is_first_connection", 1) == 1) {
ControlManager.getInstance().setDisableSetting();
}
Log.e("getSystemSettings", "onError: " + e.getMessage());
onComplete();
}
.getSystemSettings(true, getLifecycle(), new NetInterfaceManager.CompleteCallback() {
@Override
public void onComplete() {
Log.e("getSystemSettings", "onComplete: ");
mView.setSystemSettings();
}
});
@@ -289,82 +221,6 @@ public class MainSPresenter implements MainSContact.Presenter {
@Override
public void getBrowserLabel() {
mView.setBrowserLabel();
// NetInterfaceManager.getInstance().getBrowserLabel(true, getLifecycle(), new NetInterfaceManager.CompleteCallback() {
// @Override
// public void onComplete() {
// mView.setBrowserLabel();
// }
// });
// NetInterfaceManager.getInstance().getLabelControl()
// .compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
// .subscribe(new Observer<BaseResponse<Label>>() {
// @Override
// public void onSubscribe(Disposable d) {
// Log.e("getBrowserLabel", "onSubscribe: ");
// }
//
// @Override
// public void onNext(BaseResponse<Label> labelBaseResponse) {
// Log.e("getBrowserLabel", "onNext: " + labelBaseResponse);
// if (labelBaseResponse.code == 200) {
// String home_page = labelBaseResponse.data.getHome_page();
// Settings.System.putString(mContext.getContentResolver(), "homepagURL", home_page);
// //主页
// Intent homepag = new Intent("qch_app_brower_homepage");
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// homepag.setPackage("com.android.settings")
// .setPackage("com.android.browser");
// }
// if (!TextUtils.isEmpty(home_page)) {
// homepag.putExtra("homepage", home_page);
// } else {
// homepag.putExtra("homepage", "");
// }
// mContext.sendBroadcast(homepag);
//
// //书签
// String label_page = labelBaseResponse.data.getLabel_page();
// Intent websiteBookMark = new Intent("qch_app_brower_website");
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// websiteBookMark.setPackage("com.android.settings")
// .setPackage("com.android.browser");
// }
// if (!TextUtils.isEmpty(label_page)) {
// websiteBookMark.putExtra("websiteBookMark", label_page);
// } else {
// websiteBookMark.putExtra("websiteBookMark", "");
// }
// mContext.sendBroadcast(websiteBookMark);
// } else {
// Intent intent1 = new Intent("qch_app_brower_homepage");
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// intent1.setPackage("com.android.settings")
// .setPackage("com.android.browser");
// }
// intent1.putExtra("homepage", "");
// mContext.sendBroadcast(intent1);
// Intent intent2 = new Intent("qch_app_brower_website");
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// intent2.setPackage("com.android.settings")
// .setPackage("com.android.browser");
// }
// intent2.putExtra("websiteBookMark", "");
// mContext.sendBroadcast(intent2);
// }
// }
//
// @Override
// public void onError(Throwable e) {
// Log.e("getBrowserLabel", "onError: " + e.getMessage());
// onComplete();
// }
//
// @Override
// public void onComplete() {
// Log.e("getBrowserLabel", "onComplete: ");
// mView.setBrowserLabel();
// }
// });
}
/**
@@ -373,61 +229,6 @@ public class MainSPresenter implements MainSContact.Presenter {
@Override
public void getBrowserWhiteList() {
mView.setBrowserWhiteList();
// NetInterfaceManager.getInstance().getBrowserWhiteList(true, getLifecycle(), new NetInterfaceManager.CompleteCallback() {
// @Override
// public void onComplete() {
// mView.setBrowserWhiteList();
// }
// });
// NetInterfaceManager.getInstance().getBrowserControl()
// .compose(RxLifecycle.bindUntilEvent(getLifecycle(), ActivityEvent.DESTROY))
// .subscribe(new Observer<BaseResponse<BrowserApiData>>() {
// @Override
// public void onSubscribe(Disposable d) {
// Log.e("getBrowserWhiteList", "onSubscribe: ");
// }
//
// @Override
// public void onNext(BaseResponse<BrowserApiData> response) {
// Log.e("getBrowserWhiteList", "onNext: " + response);
// if (response.code == 200) {
// //白名单
// List<BrowserBean> white = response.data.getWhiteList();
// if (null != white && white.size() != 0) {
// boolean whiteList = Settings.System.putString(mContext.getContentResolver(), "DeselectBrowserArray",
// white.stream().map(BrowserBean::getAddress).collect(Collectors.joining(",")));
// Log.e("getBrowserWhiteList", "setBrowserList_white:" + white + ":" + whiteList);
// } else {
// Settings.System.putString(mContext.getContentResolver(), "DeselectBrowserArray", " ");
// }
// //黑名单
// List<BrowserBean> black = response.data.getBlackList();
// if (null != black && black.size() != 0) {
// boolean blackList = Settings.System.putString(mContext.getContentResolver(), "qch_webblack_url",
// black.stream().map(BrowserBean::getAddress).collect(Collectors.joining(",")));
// Log.e("getBrowserWhiteList", "setBrowserList_black:" + black + ":" + blackList);
// } else {
// Settings.System.putString(mContext.getContentResolver(), "qch_webblack_url", " ");
// }
// } else {
// Settings.System.putString(mContext.getContentResolver(), "DeselectBrowserArray", " ");
// Settings.System.putString(mContext.getContentResolver(), "qch_webblack_url", " ");
// }
// }
//
// @Override
// public void onError(Throwable e) {
// Log.e("getBrowserWhiteList", "onError: " + e.getMessage());
// onComplete();
// }
//
// @Override
// public void onComplete() {
// new URLUtils(mContext).setBrowserList();
// mView.setBrowserWhiteList();
// Log.e("getBrowserWhiteList", "onComplete: ");
// }
// });
}
@Override
@@ -565,6 +366,4 @@ public class MainSPresenter implements MainSContact.Presenter {
}
});
}
}

View File

@@ -1,6 +1,5 @@
package com.fuying.sn.service.main;
import android.annotation.SuppressLint;
import android.app.AlarmManager;
import android.app.Service;
import android.content.BroadcastReceiver;
@@ -30,7 +29,6 @@ import com.fuying.sn.bean.BaseResponse;
import com.fuying.sn.bean.SnInfo;
import com.fuying.sn.config.CommonConfig;
import com.fuying.sn.manager.AmapManager;
import com.fuying.sn.manager.ConnectManager;
import com.fuying.sn.manager.ControlManager;
import com.fuying.sn.network.NetInterfaceManager;
import com.fuying.sn.rlog.LogDBManager;
@@ -40,6 +38,7 @@ import com.fuying.sn.service.StepService;
import com.fuying.sn.utils.ApkUtils;
import com.fuying.sn.utils.CacheUtils;
import com.fuying.sn.utils.JGYUtils;
import com.fuying.sn.utils.NetStateUtils;
import com.fuying.sn.utils.SPUtils;
import com.fuying.sn.utils.TimeUtils;
import com.fuying.sn.utils.ToastUtil;
@@ -74,11 +73,12 @@ import io.reactivex.rxjava3.subjects.BehaviorSubject;
public class MainService extends Service implements MainSContact.MainView, NetworkUtils.OnNetworkStatusChangedListener, LifecycleProvider<ActivityEvent> {
private String TAG = MainService.class.getSimpleName();
@SuppressLint("StaticFieldLeak")
public static MainSPresenter mPresenter;
public MainSPresenter mPresenter;
private MMKV mMMKV = MMKV.defaultMMKV();
//执行所有请求的时间
long runningTime;
//MainService上次执行时间
long startCommandTime = 0;
@@ -114,26 +114,22 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
ToastUtil.show("网络已连接");
String WiFiAlias = Utils.getWifiAlias(this);
Log.e(TAG, "onConnected: WiFiAlias = " + WiFiAlias);
if (ConnectManager.getInstance().isWiFiFistConnect()) {
start.onstar(ConnectManager.WIFI_LAST_CONNECT_TIME);
} else {
if (ConnectManager.getInstance().isWiFiCutoverFistConnect(WiFiAlias)) {
start.onstar(WiFiAlias);
}
}
// NTPTimeTask task = new NTPTimeTask();
// task.execute("ntp.aliyun.com");
//第一次开机联网后直接连接
// TODO: 2022/11/21 有些设备联网了但是立刻请求还是失败
Log.e(TAG, "onConnected run: 15秒后请求网络");
Handler.getMain().postDelayed(new Runnable() {
@Override
public void run() {
if ((int) SPUtils.get(MainService.this, CommonConfig.JGY_FIRST_BOOT, 0) == 0) {
NetInterfaceManager.getInstance().updateAdminInfo();
if (JGYUtils.getInstance().isScreenOn()) {
mPresenter.getSnInfo();
mPresenter.getAppAndWhite();
SPUtils.put(MainService.this, CommonConfig.JGY_FIRST_BOOT, 1);
}
}
}, 15000);
sendDeviceInfo();
}
@Override
@@ -159,12 +155,17 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
mPresenter.setLifecycle(lifecycleSubject);
mPresenter.attachView(this);
Log.e(TAG, "onCreate run: 20秒后请求网络");
Handler.getMain().postDelayed(new Runnable() {
@Override
public void run() {
NTPTimeTask task = new NTPTimeTask();
task.execute("ntp.aliyun.com");
if (NetStateUtils.isNetworkConnected(MainService.this)) {
NetInterfaceManager.getInstance().getAppWhiteList(() -> {
Log.e("TimeObserver", "onNext: getAppWhiteList");
});
mPresenter.getSnInfo();
mPresenter.getAppAndWhite();
}
}
}, 20000);
@@ -184,21 +185,12 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
Log.e(TAG, "onCreate: " + e.getMessage());
}
Observable.create(subscribe)
.throttleFirst(6, TimeUnit.HOURS)
.subscribe(timeObserver);
Observable.create(configsubscribe)
.throttleLast(15, TimeUnit.SECONDS)
.subscribe(configObserver);
if ((int) SPUtils.get(this, CommonConfig.JGY_FIRST_CONNECT, 0) == 0) {
start.onstar(ConnectManager.REBOOT_LAST_ONNECT_TIME);
}
versionIsUpdate();
registerReceivers();
// ControlManager.getInstance().setDefaultUSBstate();
sendDeviceInfo();
startService();
}
private void versionIsUpdate() {
@@ -220,29 +212,11 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
e.printStackTrace();
}
} else {
if (ConnectManager.getInstance().isRebootFistConnect()) {
start.onstar(ConnectManager.REBOOT_LAST_ONNECT_TIME);
runningTime = SystemClock.elapsedRealtime();
} else {
Log.e(TAG, "onCreate: " + "Service Reboot");
// mPresenter.getLockedState();
}
}
SPUtils.put(this, CommonConfig.SN_VERSIONCODE, BuildConfig.VERSION_CODE);
}
private void sendDeviceInfo() {
Handler.getMain().postDelayed(new Runnable() {
@Override
public void run() {
initConfig();
NetInterfaceManager.getInstance().SendAppInstallInfo();
NetInterfaceManager.getInstance().updateAdminInfo();
}
}, 15000);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e(TAG, "onStartCommand: " + System.currentTimeMillis());
@@ -252,7 +226,7 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
private void initConfig() {
tPushInit();
Utils.getPublicIP(MainService.this);
// Utils.getPublicIP(MainService.this);
LocationClient locationClient = AmapManager.getInstance().getLocationClient();
locationClient.stop();
locationClient.start();
@@ -260,7 +234,6 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
@Override
public void onReceiveLocation(BDLocation bdLocation) {
Log.e("initConfig", "onReceiveLocation: ");
// NetInterfaceManager.getInstance().updateAdminInfo();
}
});
}
@@ -310,10 +283,6 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
unregisterReceivers();
}
public static MainSPresenter getPresenter() {
return mPresenter;
}
private void startService() {
startService(new Intent(this, GuardService.class));
startService(new Intent(this, StepService.class));
@@ -349,12 +318,7 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
@Override
public void onSuccess(Object data, int flag) {
Log.e("TPush", "onSuccess, data:" + data + ", flag:" + flag);
NetInterfaceManager.getInstance().setPushTags(true, lifecycleSubject, new NetInterfaceManager.CompleteCallback() {
@Override
public void onComplete() {
Log.e("TimeTask", "onComplete: ");
}
});
NetInterfaceManager.getInstance().setPushTags();
}
@Override
@@ -431,9 +395,6 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
if (!oldSSID.equals(newSSID)) {
LogDBManager.getInstance().creatNetChangeLog("", oldSSID + " change to: " + newSSID);
//直接获取数据
if (ConnectManager.getInstance().isWiFiCutoverFistConnect(newSSID)) {
start.onstar(newSSID);
}
} else {
LogDBManager.getInstance().creatNetChangeLog("接入WiFi",
"connect to: " + newSSID + " time: "
@@ -500,52 +461,6 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
}
}
private interface Start {
void onstar(String key);
}
private Start start;
private ObservableOnSubscribe<String> subscribe = new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
start = emitter::onNext;
}
};
private Observer<String> timeObserver = new Observer<String>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(String string) {
Log.e("TimeObserver", "onNext: " + string);
mMMKV.encode(string, System.currentTimeMillis());
startCommandTime = runningTime = SystemClock.elapsedRealtime();
NTPTimeTask task = new NTPTimeTask();
task.execute("ntp.aliyun.com");
NetInterfaceManager.getInstance().getAppWhiteList(() -> {
Log.e("TimeObserver", "onNext: getAppWhiteList");
});
mPresenter.getSnInfo();
NetInterfaceManager.getInstance().SendAppInstallInfo();
// NetInterfaceManager.getInstance().updateAdminInfo();
startService();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
};
@Override
public void getAppAndWhiteFinish() {
mPresenter.checkUpdate();
@@ -580,7 +495,7 @@ public class MainService extends Service implements MainSContact.MainView, Netwo
if (code == 200) {
mPresenter.getLocked();
SnInfo snInfo = response.data;
NetInterfaceManager.getInstance().checkDeviceType(snInfo.getType_id());
// NetInterfaceManager.getInstance().checkDeviceType(snInfo.getType_id());
}
//设备没有绑定
else if (code == 300) {