|
@@ -1,5 +1,6 @@
|
|
package xn.huaxue.update.service;
|
|
package xn.huaxue.update.service;
|
|
|
|
|
|
|
|
+import android.app.Activity;
|
|
import android.app.Notification;
|
|
import android.app.Notification;
|
|
import android.app.NotificationChannel;
|
|
import android.app.NotificationChannel;
|
|
import android.app.NotificationManager;
|
|
import android.app.NotificationManager;
|
|
@@ -8,14 +9,44 @@ import android.content.Intent;
|
|
import android.content.IntentFilter;
|
|
import android.content.IntentFilter;
|
|
import android.os.Build;
|
|
import android.os.Build;
|
|
import android.os.IBinder;
|
|
import android.os.IBinder;
|
|
|
|
+import android.text.TextUtils;
|
|
|
|
+import android.util.Log;
|
|
|
|
|
|
|
|
+import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.core.app.NotificationCompat;
|
|
import androidx.core.app.NotificationCompat;
|
|
|
|
+import androidx.core.util.Pair;
|
|
|
|
+import androidx.work.ExistingPeriodicWorkPolicy;
|
|
|
|
+import androidx.work.PeriodicWorkRequest;
|
|
|
|
+import androidx.work.WorkManager;
|
|
|
|
|
|
|
|
+import com.blankj.utilcode.util.ActivityUtils;
|
|
import com.blankj.utilcode.util.LogUtils;
|
|
import com.blankj.utilcode.util.LogUtils;
|
|
|
|
+import com.blankj.utilcode.util.NetworkUtils;
|
|
|
|
+import com.blankj.utilcode.util.SPUtils;
|
|
|
|
+import com.blankj.utilcode.util.ThreadUtils;
|
|
|
|
+import com.hjq.permissions.OnPermissionCallback;
|
|
|
|
+import com.hjq.permissions.Permission;
|
|
|
|
+import com.hjq.permissions.XXPermissions;
|
|
|
|
+import com.lxj.xpopup.XPopup;
|
|
|
|
|
|
|
|
+import org.greenrobot.eventbus.EventBus;
|
|
|
|
+import org.greenrobot.eventbus.Subscribe;
|
|
|
|
+import org.greenrobot.eventbus.ThreadMode;
|
|
|
|
+import org.json.JSONObject;
|
|
|
|
+
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.concurrent.ThreadLocalRandom;
|
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
+
|
|
|
|
+import okhttp3.Response;
|
|
|
|
+import xn.huaxue.update.MainActivity;
|
|
import xn.huaxue.update.R;
|
|
import xn.huaxue.update.R;
|
|
|
|
+import xn.huaxue.update.Tool;
|
|
|
|
+import xn.huaxue.update.event.TaskEvent;
|
|
|
|
+import xn.huaxue.update.http.HttpTool;
|
|
import xn.huaxue.update.receiver.TimeTickReceiver;
|
|
import xn.huaxue.update.receiver.TimeTickReceiver;
|
|
|
|
+import xn.huaxue.update.works.TaskWork;
|
|
|
|
|
|
public class TaskService extends Service {
|
|
public class TaskService extends Service {
|
|
|
|
|
|
@@ -30,11 +61,16 @@ public class TaskService extends Service {
|
|
@Override
|
|
@Override
|
|
public void onCreate() {
|
|
public void onCreate() {
|
|
super.onCreate();
|
|
super.onCreate();
|
|
|
|
+ initDev();
|
|
|
|
+ PeriodicWorkRequest taskRequest = new PeriodicWorkRequest.Builder(TaskWork.class, 15, TimeUnit.MINUTES).build();
|
|
|
|
+ WorkManager.getInstance(getApplicationContext()).enqueueUniquePeriodicWork("Task", ExistingPeriodicWorkPolicy.CANCEL_AND_REENQUEUE, taskRequest);
|
|
createNotificationChannel();
|
|
createNotificationChannel();
|
|
LogUtils.d(getClass().getName(), "onCreate");
|
|
LogUtils.d(getClass().getName(), "onCreate");
|
|
timeTickReceiver = new TimeTickReceiver();
|
|
timeTickReceiver = new TimeTickReceiver();
|
|
// 监听分钟广播
|
|
// 监听分钟广播
|
|
registerReceiver(timeTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
|
|
registerReceiver(timeTickReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
|
|
|
|
+
|
|
|
|
+ requestPermission();
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -81,4 +117,110 @@ public class TaskService extends Service {
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
|
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
|
|
return builder.build();
|
|
return builder.build();
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ private void initDev() {
|
|
|
|
+ Tool.INSTANCE.startMasterApp();
|
|
|
|
+ Tool.INSTANCE.openAdb();
|
|
|
|
+ Tool.INSTANCE.setLauncher();
|
|
|
|
+ SPUtils.getInstance().put("IP", NetworkUtils.getIPAddress(true));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private void requestPermission() {
|
|
|
|
+ // 全量权限申请
|
|
|
|
+ XXPermissions.with(ActivityUtils.getTopActivity())
|
|
|
|
+ .permission(Permission.READ_EXTERNAL_STORAGE)
|
|
|
|
+ .permission(Permission.WRITE_EXTERNAL_STORAGE)
|
|
|
|
+ // 通知栏权限
|
|
|
|
+ .permission(Permission.NOTIFICATION_SERVICE)
|
|
|
|
+ .request(new OnPermissionCallback() {
|
|
|
|
+ @Override
|
|
|
|
+ public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
|
|
|
|
+ if (allGranted) {
|
|
|
|
+ terminalAuth();
|
|
|
|
+ } else {
|
|
|
|
+ new XPopup.Builder(TaskService.this)
|
|
|
|
+ .dismissOnBackPressed(false)
|
|
|
|
+ .dismissOnTouchOutside(false)
|
|
|
|
+ .asConfirm("Tips", "您必须同意所有权限才可以继续使用", ()
|
|
|
|
+ -> requestPermission())
|
|
|
|
+ .show();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
|
|
|
|
+ OnPermissionCallback.super.onDenied(permissions, doNotAskAgain);
|
|
|
|
+ if (doNotAskAgain) {
|
|
|
|
+ new XPopup.Builder(TaskService.this)
|
|
|
|
+ .dismissOnBackPressed(false)
|
|
|
|
+ .dismissOnTouchOutside(false)
|
|
|
|
+ .asConfirm("Tips", "您必须同意所有权限才可以继续使用", ()
|
|
|
|
+ -> XXPermissions.startPermissionActivity(TaskService.this, permissions))
|
|
|
|
+ .show();
|
|
|
|
+ } else {
|
|
|
|
+ new XPopup.Builder(TaskService.this)
|
|
|
|
+ .dismissOnBackPressed(false)
|
|
|
|
+ .dismissOnTouchOutside(false)
|
|
|
|
+ .asConfirm("Tips", "您必须同意所有权限才可以继续使用", ()
|
|
|
|
+ -> requestPermission())
|
|
|
|
+ .show();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ ThreadUtils.SimpleTask<Boolean> simpleTask = new ThreadUtils.SimpleTask<Boolean>() {
|
|
|
|
+ @Override
|
|
|
|
+ public Boolean doInBackground() throws Throwable {
|
|
|
|
+ return NetworkUtils.isAvailableByPing(Tool.INSTANCE.getBaseUrl().host());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onSuccess(Boolean result) {
|
|
|
|
+ if (result) {
|
|
|
|
+ EventBus.getDefault().post(new TaskEvent("鉴权中..."));
|
|
|
|
+ ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Pair<Boolean, String>>() {
|
|
|
|
+ @Override
|
|
|
|
+ public Pair<Boolean, String> doInBackground() throws Throwable {
|
|
|
|
+ try {
|
|
|
|
+ SPUtils.getInstance().put("TerminalAuth", "");
|
|
|
|
+ Response response = HttpTool.INSTANCE.terminalAuth();
|
|
|
|
+ if (response.isSuccessful()) {
|
|
|
|
+ String json = response.body().string();
|
|
|
|
+ JSONObject jsonObject = new JSONObject(json);
|
|
|
|
+ int code = jsonObject.getInt("code");
|
|
|
|
+ if (200 == code) {
|
|
|
|
+ String data = jsonObject.getString("data");
|
|
|
|
+ SPUtils.getInstance().put("TerminalAuth", TextUtils.isEmpty(data) ? "" : data);
|
|
|
|
+ return Pair.create(true, "");
|
|
|
|
+ } else {
|
|
|
|
+ return Pair.create(false, jsonObject.getString("message"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ LogUtils.e(Log.getStackTraceString(e));
|
|
|
|
+ }
|
|
|
|
+ return Pair.create(false, "鉴权异常,请联系管理员!");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Override
|
|
|
|
+ public void onSuccess(Pair<Boolean, String> result) {
|
|
|
|
+ if (result.first) {
|
|
|
|
+ EventBus.getDefault().post(new TaskEvent(result.second));
|
|
|
|
+ ThreadUtils.cancel(simpleTask);
|
|
|
|
+ } else {
|
|
|
|
+ EventBus.getDefault().post(new TaskEvent(result.second));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ EventBus.getDefault().post(new TaskEvent("无法连接到服务器,请联系管理员!"));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+
|
|
|
|
+ private void terminalAuth() {
|
|
|
|
+ ThreadUtils.executeByCachedAtFixRate(simpleTask, ThreadLocalRandom.current().nextInt(10, 20), TimeUnit.SECONDS);
|
|
|
|
+ }
|
|
}
|
|
}
|