version:1.8
fix:应用使用记录 add:
This commit is contained in:
@@ -1,15 +1,42 @@
|
||||
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.Launcher;
|
||||
import com.aoleyun.os.TTUtils.APKUtils;
|
||||
import com.aoleyun.os.TTUtils.CmdUtil;
|
||||
import com.aoleyun.os.TTUtils.Utils;
|
||||
import com.aoleyun.os.network.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();
|
||||
|
||||
@@ -24,6 +51,7 @@ public class MainService extends Service {
|
||||
@Override
|
||||
public void onCreate() {
|
||||
Log.e(TAG, "onCreate: ");
|
||||
registerTimeReceiver();
|
||||
Aria.download(this).register();
|
||||
super.onCreate();
|
||||
}
|
||||
@@ -33,6 +61,165 @@ public class MainService extends Service {
|
||||
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;
|
||||
private final static long TEN_MINUTES_TIME = 60 * 10;
|
||||
|
||||
|
||||
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)) {
|
||||
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());
|
||||
|
||||
Reference in New Issue
Block a user