238 lines
8.8 KiB
Java
238 lines
8.8 KiB
Java
package com.aoleyun.os.service;
|
||
|
||
import android.app.Service;
|
||
import android.content.BroadcastReceiver;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.IntentFilter;
|
||
import android.os.IBinder;
|
||
import android.os.PowerManager;
|
||
import android.text.TextUtils;
|
||
import android.util.Log;
|
||
|
||
import com.aoleyun.os.BuildConfig;
|
||
import com.aoleyun.os.uiuiutils.APKUtils;
|
||
import com.aoleyun.os.uiuiutils.CmdUtil;
|
||
import com.aoleyun.os.uiuiutils.Utils;
|
||
import com.aoleyun.os.network.bean.BaseResponse;
|
||
import com.aoleyun.os.network.NetworkManager;
|
||
import com.aoleyun.os.uiuiutils.ForegroundAppUtil;
|
||
import com.aoleyun.os.uiuiutils.TimeUtils;
|
||
import com.arialyy.annotations.Download;
|
||
import com.arialyy.aria.core.Aria;
|
||
import com.arialyy.aria.core.task.DownloadTask;
|
||
|
||
import java.io.File;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
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;
|
||
import okhttp3.MediaType;
|
||
import okhttp3.MultipartBody;
|
||
import okhttp3.RequestBody;
|
||
|
||
public class MainService extends Service {
|
||
private String TAG = MainService.class.getSimpleName();
|
||
|
||
public MainService() {
|
||
}
|
||
|
||
@Override
|
||
public IBinder onBind(Intent intent) {
|
||
return null;
|
||
}
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
Log.e(TAG, "onCreate: ");
|
||
registerTimeReceiver();
|
||
Aria.download(this).register();
|
||
super.onCreate();
|
||
}
|
||
|
||
@Override
|
||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||
return START_STICKY;
|
||
}
|
||
|
||
//监听时间和日期变化
|
||
public 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);
|
||
registerReceiver(mTimeChangedReceiver, filter);
|
||
}
|
||
|
||
private TimeChangedReceiver mTimeChangedReceiver;
|
||
|
||
private class TimeChangedReceiver extends BroadcastReceiver {
|
||
|
||
@Override
|
||
public void onReceive(Context context, Intent intent) {
|
||
if (Intent.ACTION_DATE_CHANGED.equals(intent.getAction())) {
|
||
Log.e(TAG, "TimeChangedReceiver:" + "data changed");
|
||
} else if (Intent.ACTION_TIME_CHANGED.equals(intent.getAction())) {
|
||
Log.e(TAG, "TimeChangedReceiver:" + "time changed");
|
||
} else if (Intent.ACTION_TIMEZONE_CHANGED.equals(intent.getAction())) {
|
||
Log.e(TAG, "TimeChangedReceiver:" + "timezone changed");
|
||
} else if (Intent.ACTION_TIME_TICK.equals(intent.getAction())) {
|
||
Log.e(TAG, "TimeChangedReceiver:" + "time tick");
|
||
isScreenshot();
|
||
}
|
||
}
|
||
}
|
||
|
||
private final static long ONE_HOURS_TIME = 60 * 60 * 1000;
|
||
private final static long TEN_MINUTES_TIME = 60 * 10 * 1000;
|
||
|
||
private void isScreenshot() {
|
||
//1、检测应用使用情况,如果设备长时间运行一个应用,超过1小时,启动截屏一次。
|
||
//2、检测设备在非正常时间使用时,使用第三方应用,在运行10分钟后,启动截屏功能一次
|
||
//屏幕未点亮时不用截图
|
||
// TODO: 2021/12/20 计算当前app打开时间
|
||
String topPackageName = ForegroundAppUtil.getForegroundPackageName(MainService.this);
|
||
Log.e(TAG, "isScreenshot: " + topPackageName);
|
||
String pkg = TimeUtils.getInstance().getAppPackageName();
|
||
if (TextUtils.isEmpty(pkg) || BuildConfig.APPLICATION_ID.equals(pkg)) {
|
||
return;
|
||
}
|
||
|
||
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||
//true为打开,false为关闭
|
||
boolean screenOn = powerManager.isInteractive();
|
||
Log.e(TAG, "isScreenshot: screenOn = " + screenOn);
|
||
if (!screenOn) return;
|
||
|
||
long startTime = TimeUtils.getInstance().getStartTime();
|
||
if (TimeUtils.getInstance().isNormalTime()) {//正常时间段
|
||
if (System.currentTimeMillis() - startTime >= ONE_HOURS_TIME) {
|
||
Log.e(TAG, "isScreenshot: " + "截图");
|
||
doscreenshot(System.currentTimeMillis() / 1000);
|
||
}
|
||
} else {//非正常时间段
|
||
if (System.currentTimeMillis() - startTime >= TEN_MINUTES_TIME) {
|
||
Log.e(TAG, "isScreenshot: " + "截图");
|
||
doscreenshot(System.currentTimeMillis() / 1000);
|
||
}
|
||
}
|
||
}
|
||
|
||
public void doscreenshot(final long time) {
|
||
Observable.create(new ObservableOnSubscribe<Integer>() {
|
||
@Override
|
||
public void subscribe(ObservableEmitter<Integer> e) throws Exception {
|
||
String filepath = getExternalFilesDir("db").getAbsolutePath();
|
||
int n = CmdUtil.execute("screencap -p " + filepath + File.separator + time + ".db").code;
|
||
e.onNext(n);
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Observer<Integer>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onNext(Integer integer) {
|
||
if (integer == 0) {
|
||
uplaodImage(time);
|
||
} else {
|
||
Log.e("doss", "失败");
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
Log.e("doss", "Throwable=" + e.getMessage());
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
|
||
}
|
||
});
|
||
}
|
||
|
||
private void uplaodImage(long time) {
|
||
String filepath = getExternalFilesDir("db").getAbsolutePath();
|
||
// String filepath = mContext.getFileStreamPath("screenshot").getAbsolutePath();
|
||
//放在app内部data下面
|
||
File file = new File(filepath + File.separator + time + ".db");
|
||
//不要直接使用常用图片格式
|
||
if (!file.exists()) {
|
||
Log.e("uplaodImage", "File does not exists");
|
||
return;
|
||
}
|
||
//设置图片格式
|
||
// RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpg"), file);
|
||
//File转RequestBody
|
||
MediaType mediaType = MediaType.Companion.parse("image/png");
|
||
RequestBody fileBody = RequestBody.Companion.create(file, mediaType);
|
||
//设置一个file文件
|
||
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), fileBody);
|
||
Map<String, String> params = new HashMap<>();
|
||
params.put("sn", Utils.getSerial());
|
||
params.put("createtime", String.valueOf(time));
|
||
NetworkManager.getInstance().getScreenshotApi()
|
||
.sendScreenshot(params, body)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Observer<BaseResponse>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
Log.e("uplaodImage", "onSubscribe: ");
|
||
}
|
||
|
||
@Override
|
||
public void onNext(BaseResponse baseResponse) {
|
||
Log.e("uplaodImage", "onNext: " + baseResponse.msg);
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
Log.e("uplaodImage", "onError: " + e.getMessage());
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
Log.e("uplaodImage", "onComplete: ");
|
||
}
|
||
});
|
||
}
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
super.onDestroy();
|
||
if (mTimeChangedReceiver != null) {
|
||
unregisterReceiver(mTimeChangedReceiver);
|
||
}
|
||
}
|
||
|
||
@Download.onTaskRunning
|
||
void taskRunning(DownloadTask task) {
|
||
Log.e("aria running", "正在下载:" + task.getState() + "--" + task.getPercent() + "--" + task.getExtendField());
|
||
}
|
||
|
||
@Download.onTaskComplete
|
||
void taskComplete(DownloadTask task) {
|
||
APKUtils.installApp(MainService.this, task.getFilePath());
|
||
Aria.download(this).load(task.getDownloadEntity().getId()).cancel();
|
||
}
|
||
|
||
@Download.onTaskFail
|
||
void taskFail(DownloadTask task) {
|
||
Log.e(TAG, "taskFail: " + task.getDownloadEntity().getUrl());
|
||
}
|
||
}
|