version:1.1
fix: add:应用使用数据统计
This commit is contained in:
144
app/src/main/java/com/uiui/sn/service/DownloadService.java
Normal file
144
app/src/main/java/com/uiui/sn/service/DownloadService.java
Normal file
@@ -0,0 +1,144 @@
|
||||
package com.uiui.sn.service;
|
||||
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Service;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.IBinder;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.task.DownloadTask;
|
||||
import com.blankj.utilcode.util.ToastUtils;
|
||||
import com.uiui.sn.KeepAliveConnection;
|
||||
import com.uiui.sn.R;
|
||||
import com.uiui.sn.utils.ApkUtils;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
// 下载管理服务
|
||||
public class DownloadService extends Service {
|
||||
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
startService(new Intent(this, StepService.class));
|
||||
startService(new Intent(this, GuardService.class));
|
||||
startService(new Intent(this, MainService.class));
|
||||
startService(new Intent(this, ManagerService.class));
|
||||
Aria.download(this).register();
|
||||
//恢复所有未完成的下载任务
|
||||
Aria.download(this).resumeAllTask();
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return new KeepAliveConnection.Stub() {
|
||||
};
|
||||
}
|
||||
|
||||
private void getFile(final Bundle bundle) {
|
||||
String url = bundle.getString("url");
|
||||
|
||||
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
|
||||
path.mkdirs();
|
||||
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1));
|
||||
|
||||
if (file.exists() && file.isFile()) {
|
||||
// AlertDialog.Builder builder = new AlertDialog.Builder(this)
|
||||
// .setTitle("软件更新")
|
||||
// .setIcon(R.mipmap.ic_launcher)
|
||||
// .setCancelable(false)
|
||||
// .setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
|
||||
// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
// @Override
|
||||
// public void onClick(DialogInterface dialogInterface, int i) {
|
||||
ApkUtils.installApp(DownloadService.this, file.getAbsolutePath());
|
||||
// dialogInterface.dismiss();
|
||||
// }
|
||||
// });
|
||||
// AlertDialog ad = builder.create();
|
||||
// ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
||||
// ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
|
||||
// ad.show();
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private void update(final Bundle bundle) {
|
||||
String url = bundle.getString("url");
|
||||
|
||||
final File path = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/");
|
||||
path.mkdirs();
|
||||
final File file = new File(Environment.getExternalStoragePublicDirectory("Download") + "/Sninfo/" + url.substring(url.lastIndexOf("/") + 1));
|
||||
|
||||
if (file.exists() && file.isFile()) {
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this)
|
||||
.setTitle("软件更新")
|
||||
.setIcon(R.mipmap.ic_launcher)
|
||||
.setCancelable(false)
|
||||
.setMessage("发现新版本,点击确定更新\n" + "更新内容:" + bundle.getString("content"))
|
||||
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialogInterface, int i) {
|
||||
ApkUtils.installApk(DownloadService.this, file);
|
||||
dialogInterface.dismiss();
|
||||
}
|
||||
});
|
||||
AlertDialog ad = builder.create();
|
||||
ad.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
|
||||
ad.setCanceledOnTouchOutside(false); //点击外面区域不会让dialog消失
|
||||
ad.show();
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//在这里处理任务执行中的状态,如进度进度条的刷新
|
||||
@Download.onTaskRunning
|
||||
void running(DownloadTask task) {
|
||||
Log.e("aria running", "正在下载:" + task.getState() + "-" + task.getPercent() + "--" + task.getExtendField());
|
||||
String appName = "";
|
||||
try {
|
||||
String jsonString = task.getExtendField();
|
||||
JSONObject jsonObject = JSON.parseObject(jsonString);
|
||||
if (!TextUtils.isEmpty(jsonString) && jsonObject != null) {
|
||||
appName = jsonObject.getString("app_name");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
Log.e("running", "running: " + e.getMessage());
|
||||
}
|
||||
ToastUtils.showShort("正在下载:" + appName + "-" + task.getPercent() + "%");
|
||||
}
|
||||
|
||||
@Download.onTaskComplete
|
||||
void taskComplete(DownloadTask task) {
|
||||
//在这里处理任务完成的状态
|
||||
ApkUtils.installApp(DownloadService.this, task.getFilePath());
|
||||
Log.e("taskComplete", task.getExtendField());
|
||||
Aria.download(this).load(task.getDownloadEntity().getId()).cancel();
|
||||
}
|
||||
}
|
||||
66
app/src/main/java/com/uiui/sn/service/GuardService.java
Normal file
66
app/src/main/java/com/uiui/sn/service/GuardService.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package com.uiui.sn.service;
|
||||
|
||||
/**
|
||||
* 作者 mjsheng
|
||||
* 日期 2019/4/1 10:58
|
||||
* 邮箱 501802639@qq.com
|
||||
* 来自:
|
||||
*/
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.uiui.sn.KeepAliveConnection;
|
||||
import com.uiui.sn.utils.ServiceAliveUtils;
|
||||
|
||||
|
||||
/**
|
||||
* 守护进程 双进程通讯
|
||||
*
|
||||
* @author LiGuangMin
|
||||
* @time Created by 2018/8/17 11:27
|
||||
*/
|
||||
public class GuardService extends Service {
|
||||
private final static String TAG = GuardService.class.getSimpleName();
|
||||
private ServiceConnection mServiceConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||||
Log.e(TAG, "GuardService:建立链接");
|
||||
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice(GuardService.this, getClass().getName());
|
||||
if (!isServiceRunning) {
|
||||
Intent i = new Intent(GuardService.this, DownloadService.class);
|
||||
startService(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName componentName) {
|
||||
// 断开链接
|
||||
startService(new Intent(GuardService.this, StepService.class));
|
||||
// 重新绑定
|
||||
bindService(new Intent(GuardService.this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return new KeepAliveConnection.Stub() {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// 绑定建立链接
|
||||
bindService(new Intent(this, StepService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
return START_STICKY;
|
||||
}
|
||||
}
|
||||
|
||||
414
app/src/main/java/com/uiui/sn/service/MainService.java
Normal file
414
app/src/main/java/com/uiui/sn/service/MainService.java
Normal file
@@ -0,0 +1,414 @@
|
||||
package com.uiui.sn.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.os.IBinder;
|
||||
import android.os.SystemClock;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.blankj.utilcode.util.NetworkUtils;
|
||||
import com.uiui.sn.activity.main.MainActivity;
|
||||
import com.uiui.sn.activity.main.MainContact;
|
||||
import com.uiui.sn.activity.main.MainPresenter;
|
||||
import com.uiui.sn.bean.zuoye.BaseResponse;
|
||||
import com.uiui.sn.bean.zuoye.UserInfo;
|
||||
import com.uiui.sn.bean.gankao.AvailableProduct;
|
||||
import com.uiui.sn.config.Configs;
|
||||
import com.uiui.sn.jpush.TagAliasOperatorHelper;
|
||||
import com.uiui.sn.manager.ControlManager;
|
||||
import com.uiui.sn.network.HTTPInterface;
|
||||
import com.uiui.sn.utils.SPUtils;
|
||||
import com.uiui.sn.utils.ToastUtil;
|
||||
import com.uiui.sn.utils.Utils;
|
||||
import com.trello.rxlifecycle2.LifecycleProvider;
|
||||
import com.trello.rxlifecycle2.LifecycleTransformer;
|
||||
import com.trello.rxlifecycle2.RxLifecycle;
|
||||
import com.trello.rxlifecycle2.android.ActivityEvent;
|
||||
import com.trello.rxlifecycle2.android.RxLifecycleAndroid;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import cn.jpush.android.api.JPushInterface;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableEmitter;
|
||||
import io.reactivex.ObservableOnSubscribe;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.subjects.BehaviorSubject;
|
||||
|
||||
import static com.uiui.sn.jpush.TagAliasOperatorHelper.ACTION_SET;
|
||||
|
||||
/**
|
||||
* @author jgy02
|
||||
*/
|
||||
public class MainService extends Service implements MainContact.MainView, NetworkUtils.OnNetworkStatusChangedListener, LifecycleProvider<ActivityEvent> {
|
||||
private String TAG = MainService.class.getSimpleName();
|
||||
public static MainPresenter mPresenter;
|
||||
|
||||
//执行所有请求的时间
|
||||
long runningTime = 0;
|
||||
|
||||
//MainService上次执行时间
|
||||
long startCommandTime = 0;
|
||||
|
||||
private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Observable<ActivityEvent> lifecycle() {
|
||||
return lifecycleSubject.hide();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T> LifecycleTransformer<T> bindUntilEvent(@NotNull ActivityEvent event) {
|
||||
return RxLifecycle.bindUntilEvent(lifecycleSubject, event);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <T> LifecycleTransformer<T> bindToLifecycle() {
|
||||
return RxLifecycleAndroid.bindActivity(lifecycleSubject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisconnected() {
|
||||
Log.e(TAG, "网络未连接");
|
||||
ToastUtil.show("网络未连接");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(NetworkUtils.NetworkType networkType) {
|
||||
mPresenter.getUserInfo();
|
||||
HTTPInterface.checkUpdate(this);
|
||||
Log.e(TAG, "网络已连接");
|
||||
ToastUtil.show("网络已连接");
|
||||
Log.e(TAG, "onConnected: wifi ssid = " + Utils.getWifiSSID(this));
|
||||
SPUtils.put(this, "wifi_last_connect_time", System.currentTimeMillis());
|
||||
mPresenter.checkUpdate();
|
||||
}
|
||||
|
||||
private interface Start {
|
||||
void onstar(long time);
|
||||
}
|
||||
|
||||
private Start start;
|
||||
|
||||
private final ObservableOnSubscribe<Long> subscribe = new ObservableOnSubscribe<Long>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter emitter) throws Exception {
|
||||
start = new Start() {
|
||||
@Override
|
||||
public void onstar(long time) {
|
||||
emitter.onNext(time);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
private Observer<Long> timeObserver = new Observer<Long>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Long aLong) {
|
||||
Log.e("TimeObserver", "onNext: $aLong");
|
||||
startCommandTime = runningTime = SystemClock.elapsedRealtime();
|
||||
mPresenter.checkUpdate();
|
||||
mPresenter.initAmap();
|
||||
mPresenter.getUserInfo();
|
||||
HTTPInterface.sendAppUsed(MainService.this);
|
||||
// HTTPInterface.sendRunningApp(MainService.this);
|
||||
HTTPInterface.getAPPinfo(MainService.this);
|
||||
startService();
|
||||
mPresenter.setAlias();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
lifecycleSubject.onNext(ActivityEvent.CREATE);
|
||||
JPushInterface.init(this);
|
||||
mPresenter = new MainPresenter(this);
|
||||
mPresenter.setProvider(this);
|
||||
mPresenter.attachView(this);
|
||||
NetworkUtils.registerNetworkStatusChangedListener(this);
|
||||
Observable.create(subscribe)
|
||||
.throttleFirst(60, TimeUnit.SECONDS)
|
||||
.subscribe(timeObserver);
|
||||
PackageManager packageManager = getPackageManager();
|
||||
try {
|
||||
// packageManager.setApplicationEnabledSetting("com.jiaoguanyi.appstore", PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0)
|
||||
// packageManager.setApplicationEnabledSetting("com.jiaoguanyi.store", PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0)
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "onCreate: " + e.getMessage());
|
||||
}
|
||||
ControlManager.getInstance().setDefaultUSBstate();
|
||||
mPresenter.setAlias();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
JPushInterface.init(this);
|
||||
if (MainActivity.isForeground) {
|
||||
Log.e(TAG, "onStartCommand: MainActivity: isForeground: " + MainActivity.isForeground);
|
||||
} else {
|
||||
JPushInterface.init(this);
|
||||
start.onstar(startCommandTime);
|
||||
Log.e(TAG, "onStartCommand: " + (SystemClock.elapsedRealtime() - startCommandTime) + "ms");
|
||||
}
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
lifecycleSubject.onNext(ActivityEvent.DESTROY);
|
||||
NetworkUtils.unregisterNetworkStatusChangedListener(this);
|
||||
mPresenter.detachView();
|
||||
}
|
||||
|
||||
private void startService() {
|
||||
startService(new Intent(this, GuardService.class));
|
||||
startService(new Intent(this, StepService.class));
|
||||
startService(new Intent(this, DownloadService.class));
|
||||
}
|
||||
|
||||
public static MainPresenter getPresenter() {
|
||||
return mPresenter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPowerUserList(String date) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkSNFinish(int code) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGankaoUID(String gankaoUID) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAvailableProduct(List<AvailableProduct> availableProductList, String gankaoUID) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void activeUserFinish(int code) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlias() {
|
||||
setJpushAlias();
|
||||
Log.e(TAG, "setAlias: " + "finish");
|
||||
mPresenter.getBatch();
|
||||
}
|
||||
|
||||
private void setJpushAlias() {
|
||||
String alias = Utils.getSerial();
|
||||
TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
|
||||
tagAliasBean.action = ACTION_SET;
|
||||
TagAliasOperatorHelper.sequence++;
|
||||
tagAliasBean.alias = alias;
|
||||
tagAliasBean.isAliasAction = true;
|
||||
TagAliasOperatorHelper.getInstance().handleAction(this, TagAliasOperatorHelper.sequence, tagAliasBean);
|
||||
}
|
||||
|
||||
private void setTag(Set set) {
|
||||
TagAliasOperatorHelper.TagAliasBean tagAliasBean = new TagAliasOperatorHelper.TagAliasBean();
|
||||
tagAliasBean.action = ACTION_SET;
|
||||
TagAliasOperatorHelper.sequence++;
|
||||
tagAliasBean.tags = set;
|
||||
tagAliasBean.isAliasAction = false;
|
||||
TagAliasOperatorHelper.getInstance().handleAction(this, TagAliasOperatorHelper.sequence, tagAliasBean);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBatch(String batch) {
|
||||
HashSet hashSet = new HashSet();
|
||||
hashSet.add(batch);
|
||||
hashSet.removeIf(o -> TextUtils.isEmpty(o.toString()));
|
||||
if (hashSet.size() != 0) {
|
||||
setTag(hashSet);
|
||||
} else {
|
||||
Log.e(TAG, "setBatch: " + "hashSet size is 0");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setQRCode(Bitmap bitmap) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSnInfo(BaseResponse<UserInfo> response) {
|
||||
if (response != null) {
|
||||
int code = response.code;
|
||||
//设备已经绑定
|
||||
if (code == 200) {
|
||||
UserInfo userInfo = response.data;
|
||||
boolean username = Settings.System.putString(getContentResolver(), "UserInfo_username", userInfo.getSn_name());
|
||||
boolean gread = Settings.System.putString(getContentResolver(), "UserInfo_grade", userInfo.getGrade());
|
||||
SPUtils.put(this, Configs.isLogined, 1);
|
||||
SPUtils.put(this, "member_id", userInfo.getMember_id());
|
||||
SPUtils.put(this, "sn_id", userInfo.getId());
|
||||
if (!TextUtils.isEmpty(userInfo.getSn_name())) {
|
||||
SPUtils.put(this, "USERINFO_NAME", userInfo.getSn_name());
|
||||
}
|
||||
if (!TextUtils.isEmpty(userInfo.getSchool())) {
|
||||
SPUtils.put(this, "USERINFO_SCHOOL", userInfo.getSchool());
|
||||
}
|
||||
if (!TextUtils.isEmpty(userInfo.getGrade())) {
|
||||
SPUtils.put(this, "USERINFO_GRADE", userInfo.getGrade());
|
||||
}
|
||||
|
||||
mPresenter.getLocked();
|
||||
}
|
||||
//设备没有绑定
|
||||
else if (code == 300) {
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
}
|
||||
//没有授权的设备
|
||||
else if (code == 400) {
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
}
|
||||
}
|
||||
mPresenter.getSnUid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSnUid(JSONObject jsonObject) {
|
||||
if (jsonObject != null) {
|
||||
String uid = jsonObject.getString("uid");
|
||||
if (!TextUtils.isEmpty(uid)) {
|
||||
Settings.System.putString(getContentResolver(), "gankaoUID", uid);
|
||||
}
|
||||
// String uid = jsonObject.getString("uid");
|
||||
// long activate_time = jsonObject.getLong("activate_time");
|
||||
// String card_info = jsonObject.getString("card_info");
|
||||
// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
// Date d1 = new Date(activate_time * 1000);
|
||||
// int grade = jsonObject.getIntValue("grade");
|
||||
// SPUtils.put(MainService.this, "int_grade", grade);
|
||||
// String t1 = format.format(d1);
|
||||
} else {
|
||||
mPresenter.registerGankao();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setGankao(String gankaoUID) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setUserInfo() {
|
||||
mPresenter.getSnInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setLocked(int lockedStatus) {
|
||||
switch (lockedStatus) {
|
||||
case 0:
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
break;
|
||||
case 1:
|
||||
break;
|
||||
case 2:
|
||||
ControlManager.getInstance().setDisableSetting();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
mPresenter.checkUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLocationChanged() {
|
||||
HTTPInterface.updateAdminInfo(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkUpdateFinish() {
|
||||
mPresenter.getAllApp();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getAllAppFinish() {
|
||||
mPresenter.getAppInside();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAppInside() {
|
||||
mPresenter.getForceInstall();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setForceInstall() {
|
||||
mPresenter.getSystemSettings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSystemSettings() {
|
||||
mPresenter.getBrowserLabel();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBrowserLabel() {
|
||||
mPresenter.getBrowserWhiteList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBrowserWhiteList() {
|
||||
mPresenter.getAppStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAppStart() {
|
||||
mPresenter.getTimeControl();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTimeControl() {
|
||||
mPresenter.getScreenLock();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setScreenLock() {
|
||||
|
||||
}
|
||||
}
|
||||
535
app/src/main/java/com/uiui/sn/service/ManagerService.java
Normal file
535
app/src/main/java/com/uiui/sn/service/ManagerService.java
Normal file
@@ -0,0 +1,535 @@
|
||||
package com.uiui.sn.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.WindowManager;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.blankj.utilcode.util.NetworkUtils;
|
||||
import com.uiui.sn.R;
|
||||
import com.uiui.sn.bean.zuoye.BaseResponse;
|
||||
import com.uiui.sn.manager.ControlManager;
|
||||
import com.uiui.sn.manager.NetInterfaceManager;
|
||||
import com.uiui.sn.network.HTTPInterface;
|
||||
import com.uiui.sn.receiver.APKinstallReceiver;
|
||||
import com.uiui.sn.receiver.BootReceiver;
|
||||
import com.uiui.sn.utils.SPUtils;
|
||||
import com.uiui.sn.utils.TimeUtils;
|
||||
import com.uiui.sn.utils.Utils;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.ObservableEmitter;
|
||||
import io.reactivex.ObservableOnSubscribe;
|
||||
import io.reactivex.Observer;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.Disposable;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
|
||||
|
||||
/**
|
||||
* @author jgy02
|
||||
*/
|
||||
public class ManagerService extends Service implements NetworkUtils.OnNetworkStatusChangedListener {
|
||||
private String TAG = ManagerService.class.getSimpleName();
|
||||
|
||||
public static String ACTION_LOCK = "LockScreenReceiver_lockscreen";
|
||||
public static String ACTION_UNLOCK = "LockScreenReceiver_unlockscreen";
|
||||
public static String ACTION_UPDATE = "TimeChangedReceiver_update";
|
||||
private WindowManager windowManager;
|
||||
private View topView;
|
||||
private boolean screenlocked = false;
|
||||
private boolean timelocked = false;
|
||||
|
||||
@Override
|
||||
public void onDisconnected() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(NetworkUtils.NetworkType networkType) {
|
||||
getScreenLockState();
|
||||
}
|
||||
|
||||
|
||||
private interface Start {
|
||||
void onstar(long time);
|
||||
}
|
||||
|
||||
private Start start;
|
||||
|
||||
private final ObservableOnSubscribe<Long> subscribe = new ObservableOnSubscribe<Long>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter emitter) throws Exception {
|
||||
start = new Start() {
|
||||
@Override
|
||||
public void onstar(long time) {
|
||||
emitter.onNext(time);
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
private Observer<Long> timeObserver = new Observer<Long>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(Long aLong) {
|
||||
Log.e("TimeObserver", "onNext: " + aLong);
|
||||
Handler.getMain().postDelayed(() -> HTTPInterface.checkUpdate(ManagerService.this), 1234);
|
||||
Handler.getMain().postDelayed(() -> HTTPInterface.checkUpdate(ManagerService.this, "com.uiui.appstore"), 3456);
|
||||
Handler.getMain().postDelayed(() -> HTTPInterface.checkUpdate(ManagerService.this, "com.uiui.browser"), 5678);
|
||||
Handler.getMain().postDelayed(() -> HTTPInterface.checkUpdate(ManagerService.this, "com.uiui.os"), 6789);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
NetworkUtils.registerNetworkStatusChangedListener(this);
|
||||
registLockReceiver();
|
||||
registerTimeReceiver();
|
||||
registerScreenLockReceiver();
|
||||
registAppReceive();
|
||||
registBootReceive();
|
||||
registerBatteryReceiver();
|
||||
setFloatingWindow();
|
||||
Observable.create(subscribe)
|
||||
.throttleFirst(3, TimeUnit.HOURS)
|
||||
.subscribe(timeObserver);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
start.onstar(System.currentTimeMillis());
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
private void getScreenLockState() {
|
||||
NetInterfaceManager.getInstance()
|
||||
.getScreenLockControl()
|
||||
.getScreenshot(Utils.getSerial())
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<BaseResponse>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
Log.e("getScreenLockState", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(BaseResponse baseResponse) {
|
||||
Log.e("getScreenLockState", "onNext: ");
|
||||
int code = baseResponse.code;
|
||||
if (code == 200) {
|
||||
JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(baseResponse.data));
|
||||
int is_screen_lock = jsonObject.getIntValue("is_screen_lock");
|
||||
setLockedState(is_screen_lock);
|
||||
} else {
|
||||
if (!timelocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
screenlocked = false;
|
||||
SPUtils.put(ManagerService.this, LOCK_STATE, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("getScreenLockState", "onError: " + e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("getScreenLockState", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void setLockedState(int lockedState) {
|
||||
if (lockedState == 1) {
|
||||
if (!timelocked) {
|
||||
showFloatingWindow("屏幕已锁定");
|
||||
}
|
||||
screenlocked = true;
|
||||
SPUtils.put(ManagerService.this, LOCK_STATE, 1);
|
||||
} else {
|
||||
if (!timelocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
screenlocked = false;
|
||||
SPUtils.put(ManagerService.this, LOCK_STATE, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public static String LOCK_STATE = "SCRENN_LOOCKED_STATE";
|
||||
|
||||
private void showFloatingWindow(String name) {
|
||||
if (Settings.canDrawOverlays(this)) {
|
||||
// 获取WindowManager服务
|
||||
if (null == windowManager) {
|
||||
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
|
||||
}
|
||||
DisplayMetrics dm = new DisplayMetrics();
|
||||
windowManager.getDefaultDisplay().getRealMetrics(dm);
|
||||
int width = dm.widthPixels; // 屏幕宽度(像素)
|
||||
int height = dm.heightPixels; // 屏幕高度(像素)
|
||||
// 新建悬浮窗控件
|
||||
final Button button = new Button(getApplicationContext());
|
||||
button.setText("霸屏测试");
|
||||
button.setAlpha(0.9f);
|
||||
button.setBackgroundColor(Color.BLACK);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
// windowManager.removeView(button);
|
||||
}
|
||||
});
|
||||
if (null == topView) {
|
||||
topView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.activity_screen_lock, null);
|
||||
TextView textView = topView.findViewById(R.id.textView);
|
||||
textView.setText(name);
|
||||
} else {
|
||||
if ("added".equals(topView.getTag())) {
|
||||
TextView textView = topView.findViewById(R.id.textView);
|
||||
textView.setText(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// topView.setAlpha(0.8f);
|
||||
// 设置LayoutParam
|
||||
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
layoutParams.type = WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG;
|
||||
//TYPE_SYSTEM_OVERLAY可以下滑通知栏
|
||||
// layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
|
||||
} else {
|
||||
layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
|
||||
}
|
||||
layoutParams.flags |= WindowManager.LayoutParams.FLAG_BLUR_BEHIND
|
||||
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
||||
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
|
||||
layoutParams.format = PixelFormat.RGBA_8888;
|
||||
layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
|
||||
//systemUiVisibility 关闭通知栏和导航栏
|
||||
layoutParams.systemUiVisibility =
|
||||
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
|
||||
| View.SYSTEM_UI_FLAG_IMMERSIVE
|
||||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
|
||||
layoutParams.x = 0;
|
||||
layoutParams.y = 0;
|
||||
// 将悬浮窗控件添加到WindowManager
|
||||
windowManager.addView(topView, layoutParams);
|
||||
topView.setTag("added");
|
||||
}
|
||||
}
|
||||
|
||||
private void hideFloatingWindow() {
|
||||
if (null == windowManager) {
|
||||
return;
|
||||
}
|
||||
if (null != topView) {
|
||||
windowManager.removeView(topView);
|
||||
topView = null;
|
||||
}
|
||||
}
|
||||
|
||||
private LockScreenReceiver lockScreenReceiver;
|
||||
|
||||
private void registLockReceiver() {
|
||||
if (null == lockScreenReceiver) {
|
||||
lockScreenReceiver = new LockScreenReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(ACTION_LOCK);
|
||||
filter.addAction(ACTION_UNLOCK);
|
||||
registerReceiver(lockScreenReceiver, filter);
|
||||
}
|
||||
}
|
||||
|
||||
private class LockScreenReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (TextUtils.isEmpty(action)) {
|
||||
getScreenLockState();
|
||||
return;
|
||||
}
|
||||
if (ACTION_LOCK.equals(action)) {
|
||||
// String name = intent.getStringExtra("name");
|
||||
String name = "屏幕已锁定";
|
||||
if (!timelocked) {
|
||||
showFloatingWindow(name);
|
||||
}
|
||||
screenlocked = true;
|
||||
} else if (ACTION_UNLOCK.equals(action)) {
|
||||
if (!timelocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
screenlocked = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private TimeChangedReceiver mTimeChangedReceiver;
|
||||
|
||||
/**
|
||||
* 监听时间和日期变化
|
||||
*/
|
||||
private void registerTimeReceiver() {
|
||||
mTimeChangedReceiver = new TimeChangedReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(Intent.ACTION_DATE_CHANGED);
|
||||
filter.addAction(Intent.ACTION_TIME_CHANGED);
|
||||
filter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
|
||||
filter.addAction(Intent.ACTION_TIME_TICK);
|
||||
filter.addAction(ACTION_UPDATE);
|
||||
registerReceiver(mTimeChangedReceiver, filter);
|
||||
}
|
||||
|
||||
private class TimeChangedReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (Intent.ACTION_DATE_CHANGED.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "data changed");
|
||||
} else if (Intent.ACTION_TIME_CHANGED.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "time changed");
|
||||
} else if (Intent.ACTION_TIMEZONE_CHANGED.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "timezone changed");
|
||||
} else if (Intent.ACTION_TIME_TICK.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "time tick");
|
||||
setFloatingWindow();
|
||||
} else if (ACTION_UPDATE.equals(action)) {
|
||||
Log.e(TAG, "TimeChangedReceiver:" + "date update");
|
||||
setFloatingWindow();
|
||||
}
|
||||
|
||||
// String packages = ForegroundAppUtil.getForegroundPackageName(context);
|
||||
// if (!packages.equals("com.estrongs.android.pop")) {
|
||||
// ApkUtils.openApp(context, "com.estrongs.android.pop");
|
||||
// }
|
||||
// Log.e("TimeChangedReceiver", "packages:" + packages);
|
||||
}
|
||||
}
|
||||
|
||||
private void setFloatingWindow() {
|
||||
TimeUtils.ContralTime workingContralTime = TimeUtils.getWorkingDayContralTime(ManagerService.this);
|
||||
TimeUtils.ContralTime weekContralTime = TimeUtils.getWeekDayContralTime(ManagerService.this);
|
||||
// if (null != workingContralTime) {
|
||||
if (TimeUtils.inContralTime(workingContralTime, weekContralTime)) {
|
||||
if (!screenlocked) {
|
||||
showFloatingWindow("可用时间:\n" + TimeUtils.getNowTimeString(ManagerService.this));
|
||||
} else {
|
||||
TextView textView = topView.findViewById(R.id.textView);
|
||||
textView.setText("可用时间:\n" + TimeUtils.getNowTimeString(ManagerService.this));
|
||||
}
|
||||
timelocked = true;
|
||||
} else {
|
||||
// getScreenLockState();
|
||||
int is_screen_lock = (int) SPUtils.get(ManagerService.this, LOCK_STATE, 0);
|
||||
setLockedState(is_screen_lock);
|
||||
if (!screenlocked) {
|
||||
hideFloatingWindow();
|
||||
}
|
||||
timelocked = false;
|
||||
}
|
||||
// } else {
|
||||
// if (!screenlocked) {
|
||||
// hideFloatingWindow()
|
||||
// }
|
||||
// getScreenLockState()
|
||||
// timelocked = false
|
||||
// }
|
||||
}
|
||||
|
||||
private ScreenLockReceiver screenLockReceiver;
|
||||
|
||||
private void registerScreenLockReceiver() {
|
||||
if (null == screenLockReceiver) {
|
||||
screenLockReceiver = new ScreenLockReceiver();
|
||||
}
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(Intent.ACTION_SCREEN_OFF);
|
||||
filter.addAction(Intent.ACTION_SCREEN_ON);
|
||||
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
|
||||
filter.addAction(Intent.ACTION_USER_PRESENT);
|
||||
filter.addAction(Intent.ACTION_SHUTDOWN);
|
||||
filter.addAction(Intent.ACTION_FACTORY_RESET);
|
||||
filter.addAction(Intent.ACTION_MASTER_CLEAR);
|
||||
registerReceiver(screenLockReceiver, filter);
|
||||
}
|
||||
|
||||
private class ScreenLockReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
Log.e(TAG, "onReceive:" + action);
|
||||
if (TextUtils.isEmpty(action)) {
|
||||
Log.e(TAG, "onReceive: is NULL");
|
||||
return;
|
||||
}
|
||||
switch (action) {
|
||||
case Intent.ACTION_USER_PRESENT:
|
||||
case Intent.ACTION_SCREEN_ON:
|
||||
sendScreenState(1);
|
||||
break;
|
||||
case Intent.ACTION_SCREEN_OFF:
|
||||
case Intent.ACTION_SHUTDOWN:
|
||||
case Intent.ACTION_FACTORY_RESET:
|
||||
case Intent.ACTION_MASTER_CLEAR:
|
||||
sendScreenState(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void sendScreenState(int stateCode) {
|
||||
Log.e(TAG, "sendScreenState: code:" + stateCode);
|
||||
// Log.e(TAG, "sendScreenState: sn: " + Utils.getSerial());
|
||||
NetInterfaceManager.getInstance().setScreen()
|
||||
.setScreenState(Utils.getSerial(), stateCode)
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<BaseResponse>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
Log.e("sendScreenState", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(BaseResponse baseResponse) {
|
||||
Log.e("sendScreenState", "onNext: " + baseResponse.msg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("sendScreenState", "onError: " + e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("sendScreenState", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private APKinstallReceiver apKinstallReceiver;
|
||||
|
||||
private void registAppReceive() {
|
||||
if (null == apKinstallReceiver) {
|
||||
apKinstallReceiver = new APKinstallReceiver();
|
||||
}
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
|
||||
filter.addAction(Intent.ACTION_PACKAGE_REPLACED);
|
||||
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
|
||||
filter.addDataScheme("package");
|
||||
registerReceiver(apKinstallReceiver, filter);
|
||||
}
|
||||
|
||||
private BootReceiver bootReceiver;
|
||||
|
||||
private void registBootReceive() {
|
||||
if (null == bootReceiver) {
|
||||
bootReceiver = new BootReceiver();
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(Intent.ACTION_USER_PRESENT);
|
||||
registerReceiver(bootReceiver, filter);
|
||||
}
|
||||
}
|
||||
|
||||
private BatteryReceiver batteryReceiver;
|
||||
|
||||
private void registerBatteryReceiver() {
|
||||
if (null == batteryReceiver) {
|
||||
batteryReceiver = new BatteryReceiver();
|
||||
}
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
|
||||
registerReceiver(batteryReceiver, filter);
|
||||
}
|
||||
|
||||
private class BatteryReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
|
||||
|
||||
} else if (Intent.ACTION_POWER_CONNECTED.equals(action) || Intent.ACTION_POWER_DISCONNECTED.equals(action)) {
|
||||
ControlManager.getInstance().setDefaultUSBstate();
|
||||
} else if (Intent.ACTION_BATTERY_LOW.equals(action) || Intent.ACTION_BATTERY_OKAY.equals(action)) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
NetworkUtils.unregisterNetworkStatusChangedListener(this);
|
||||
if (null != mTimeChangedReceiver) {
|
||||
unregisterReceiver(mTimeChangedReceiver);
|
||||
}
|
||||
if (null != lockScreenReceiver) {
|
||||
unregisterReceiver(lockScreenReceiver);
|
||||
}
|
||||
if (null != screenLockReceiver) {
|
||||
unregisterReceiver(screenLockReceiver);
|
||||
}
|
||||
if (null != apKinstallReceiver) {
|
||||
unregisterReceiver(apKinstallReceiver);
|
||||
}
|
||||
if (null != bootReceiver) {
|
||||
unregisterReceiver(bootReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
78
app/src/main/java/com/uiui/sn/service/RemoteService.java
Normal file
78
app/src/main/java/com/uiui/sn/service/RemoteService.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package com.uiui.sn.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.os.RemoteException;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import com.uiui.sn.IGetInfoInterface;
|
||||
import com.uiui.sn.utils.JGYUtils;
|
||||
import com.uiui.sn.utils.SPUtils;
|
||||
import com.uiui.sn.utils.Utils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class RemoteService extends Service {
|
||||
private String TAG = RemoteService.class.getSimpleName();
|
||||
|
||||
public RemoteService() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
Log.e(TAG, "onBind: ");
|
||||
return mBinde;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
Log.e(TAG, "onCreate: ");
|
||||
super.onCreate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Log.e(TAG, "onStartCommand: ");
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
||||
private IBinder mBinde = new IGetInfoInterface.Stub() {
|
||||
@Override
|
||||
public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUserInfo() throws RemoteException {
|
||||
if (MainService.mPresenter != null) {
|
||||
MainService.mPresenter.getUserInfo();
|
||||
}
|
||||
String username = Settings.System.getString(getContentResolver(), "UserInfo_username");
|
||||
String avatar = Settings.System.getString(getContentResolver(), "UserInfo_avatar");
|
||||
// String gread = Settings.System.getString(getContentResolver(), "UserInfo_grade");
|
||||
int gread = (int) SPUtils.get(RemoteService.this, "int_grade", 0);
|
||||
String gankaoUID = Settings.System.getString(getContentResolver(), "gankaoUID");
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
jsonObject.addProperty("username", username);
|
||||
jsonObject.addProperty("avatar", avatar);
|
||||
jsonObject.addProperty("grade", gread);
|
||||
jsonObject.addProperty("userid", gankaoUID);
|
||||
jsonObject.addProperty("sn", Utils.getSerial());
|
||||
Log.e(TAG, "getUserInfo:" + jsonObject.toString());
|
||||
return jsonObject.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getHideAPP() throws RemoteException {
|
||||
return JGYUtils.getInstance().getHideList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getForbidAPP() throws RemoteException {
|
||||
return JGYUtils.getInstance().getForbidList();
|
||||
}
|
||||
};
|
||||
}
|
||||
323
app/src/main/java/com/uiui/sn/service/StepService.java
Normal file
323
app/src/main/java/com/uiui/sn/service/StepService.java
Normal file
@@ -0,0 +1,323 @@
|
||||
package com.uiui.sn.service;
|
||||
|
||||
/**
|
||||
* 作者 mjsheng
|
||||
* 日期 2019/4/1 10:57
|
||||
* 邮箱 501802639@qq.com
|
||||
* 来自:
|
||||
*/
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Binder;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.blankj.utilcode.util.NetworkUtils;
|
||||
import com.uiui.sn.BuildConfig;
|
||||
import com.uiui.sn.KeepAliveConnection;
|
||||
import com.uiui.sn.utils.ServiceAliveUtils;
|
||||
import com.uiui.sn.utils.Utils;
|
||||
import com.uiui.sn.websocket.JWebSocketClient;
|
||||
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
/**
|
||||
* 主进程 双进程通讯
|
||||
*
|
||||
* @author LiGuangMin
|
||||
* @time Created by 2018/8/17 11:26
|
||||
*/
|
||||
public class StepService extends Service implements NetworkUtils.OnNetworkStatusChangedListener {
|
||||
private final static String TAG = StepService.class.getSimpleName();
|
||||
|
||||
public JWebSocketClient client;
|
||||
private JWebSocketClientBinder mBinder = new JWebSocketClientBinder();
|
||||
|
||||
@Override
|
||||
public void onDisconnected() {
|
||||
Log.i("JWebSocketClientService", "网络断开连接");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onConnected(NetworkUtils.NetworkType networkType) {
|
||||
Log.i("JWebSocketClientService", "网络已连接");
|
||||
connect();
|
||||
}
|
||||
|
||||
//用于Activity和service通讯
|
||||
public class JWebSocketClientBinder extends Binder {
|
||||
public StepService getService() {
|
||||
return StepService.this;
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceConnection mServiceConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||||
Log.w(TAG, "StepService:建立链接");
|
||||
boolean isServiceRunning = ServiceAliveUtils.isServiceAlice(StepService.this, GuardService.class.getName());
|
||||
if (!isServiceRunning) {
|
||||
Intent i = new Intent(StepService.this, GuardService.class);
|
||||
startService(i);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onServiceDisconnected(ComponentName componentName) {
|
||||
// 断开链接
|
||||
startService(new Intent(StepService.this, GuardService.class));
|
||||
// 重新绑定
|
||||
bindService(new Intent(StepService.this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
}
|
||||
};
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return new KeepAliveConnection.Stub() {
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
NetworkUtils.registerNetworkStatusChangedListener(this);
|
||||
registerScreenLockReceiver();
|
||||
//初始化websocket
|
||||
initSocketClient();
|
||||
mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//开启心跳检测
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
// 绑定建立链接
|
||||
Log.e("JWebSocketClientService", "onStartCommand: ");
|
||||
bindService(new Intent(this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
private ScreenLockReceiver screenLockReceiver;
|
||||
|
||||
private void registerScreenLockReceiver() {
|
||||
if (null == screenLockReceiver) {
|
||||
screenLockReceiver = new ScreenLockReceiver();
|
||||
}
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||||
filter.addAction(Intent.ACTION_SCREEN_OFF);
|
||||
filter.addAction(Intent.ACTION_SCREEN_ON);
|
||||
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
|
||||
filter.addAction(Intent.ACTION_USER_PRESENT);
|
||||
filter.addAction(Intent.ACTION_SHUTDOWN);
|
||||
filter.addAction(Intent.ACTION_FACTORY_RESET);
|
||||
filter.addAction(Intent.ACTION_MASTER_CLEAR);
|
||||
registerReceiver(screenLockReceiver, filter);
|
||||
}
|
||||
|
||||
private class ScreenLockReceiver extends BroadcastReceiver {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
String action = intent.getAction();
|
||||
Log.e(TAG, "onReceive:" + action);
|
||||
if (TextUtils.isEmpty(action)) {
|
||||
Log.e(TAG, "onReceive: is NULL");
|
||||
return;
|
||||
}
|
||||
switch (action) {
|
||||
case Intent.ACTION_BOOT_COMPLETED:
|
||||
case Intent.ACTION_USER_PRESENT:
|
||||
case Intent.ACTION_SCREEN_ON:
|
||||
sendMsg(1);
|
||||
break;
|
||||
case Intent.ACTION_SCREEN_OFF:
|
||||
case Intent.ACTION_SHUTDOWN:
|
||||
case Intent.ACTION_FACTORY_RESET:
|
||||
case Intent.ACTION_MASTER_CLEAR:
|
||||
sendMsg(0);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
NetworkUtils.unregisterNetworkStatusChangedListener(this);
|
||||
closeConnect();
|
||||
if (screenLockReceiver != null) {
|
||||
unregisterReceiver(screenLockReceiver);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化websocket连接
|
||||
*/
|
||||
private void initSocketClient() {
|
||||
URI uri = URI.create(BuildConfig.WEBSOCKET_URL);
|
||||
// URI uri = URI.create("ws://echo.websocket.org");
|
||||
// URI uri = URI.create("ws://123.207.136.134:9010/ajaxchattest");
|
||||
client = new JWebSocketClient(uri) {
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
Log.i("JWebSocketClientService", "收到服务器发来的消息:" + message);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake handshakedata) {
|
||||
super.onOpen(handshakedata);
|
||||
Log.i("JWebSocketClientService", "websocket连接成功");
|
||||
sendMsg(1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(int code, String reason, boolean remote) {
|
||||
super.onClose(code, reason, remote);
|
||||
Log.i("JWebSocketClientService", "websocket连接关闭:" + reason);
|
||||
// client.close();
|
||||
// initSocketClient();
|
||||
mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//开启心跳检测
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception ex) {
|
||||
super.onError(ex);
|
||||
Log.i("JWebSocketClientService", "websocket连接错误:" + ex.getMessage());
|
||||
// client.close();
|
||||
// initSocketClient();
|
||||
mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//开启心跳检测
|
||||
|
||||
}
|
||||
};
|
||||
connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接websocket
|
||||
*/
|
||||
private void connect() {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
//connectBlocking多出一个等待操作,会先连接再发送,否则未连接发送会报错
|
||||
Log.i("JWebSocketClientService", "websocket链接中");
|
||||
client.connectBlocking();
|
||||
} catch (Exception e) {
|
||||
Log.i("JWebSocketClientService", e.getMessage());
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*/
|
||||
public void sendMsg() {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("sn", Utils.getSerial());
|
||||
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
if (!pm.isScreenOn()) {
|
||||
jsonObject.put("online", 0);
|
||||
//熄屏状态
|
||||
} else {
|
||||
jsonObject.put("online", 1);
|
||||
}
|
||||
if (null != client) {
|
||||
Log.i("JWebSocketClientService", "发送的消息:" + jsonObject.toJSONString());
|
||||
client.send(jsonObject.toJSONString());
|
||||
}
|
||||
}
|
||||
|
||||
public void sendMsg(int state) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("sn", Utils.getSerial());
|
||||
jsonObject.put("online", state);
|
||||
if (null != client) {
|
||||
Log.i("JWebSocketClientService", "发送的消息:" + jsonObject.toJSONString());
|
||||
try {
|
||||
client.send(jsonObject.toJSONString());
|
||||
} catch (Exception e) {
|
||||
Log.i(TAG, "sendMsg :" + e.getLocalizedMessage());
|
||||
}
|
||||
} else {
|
||||
Log.i("JWebSocketClientService", "未连接");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
private void closeConnect() {
|
||||
try {
|
||||
if (null != client) {
|
||||
client.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
client = null;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------websocket心跳检测------------------------------------------------
|
||||
private static final long HEART_BEAT_RATE = 30 * 1000;//每隔50秒进行一次对长连接的心跳检测
|
||||
private Handler mHandler = new Handler();
|
||||
private Runnable heartBeatRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.i("JWebSocketClientService", "心跳包检测websocket连接状态");
|
||||
if (client != null) {
|
||||
if (client.isOpen()) {
|
||||
Log.i("JWebSocketClientService", "websocket已连接");
|
||||
sendMsg();
|
||||
} else if (client.isClosed()) {
|
||||
Log.i("JWebSocketClientService", "websocket重连中");
|
||||
reconnectWs();
|
||||
}
|
||||
} else {
|
||||
//如果client已为空,重新初始化连接
|
||||
client = null;
|
||||
initSocketClient();
|
||||
}
|
||||
//每隔一定的时间,对长连接进行一次心跳检测
|
||||
mHandler.postDelayed(this, HEART_BEAT_RATE);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 开启重连
|
||||
*/
|
||||
private void reconnectWs() {
|
||||
mHandler.removeCallbacks(heartBeatRunnable);
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Log.i("JWebSocketClientService", "开启重连");
|
||||
client.reconnectBlocking();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user