404 lines
15 KiB
Java
404 lines
15 KiB
Java
package com.uiui.os.service;
|
||
|
||
import android.content.BroadcastReceiver;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.IntentFilter;
|
||
import android.content.pm.ApplicationInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.graphics.Bitmap;
|
||
import android.graphics.BitmapFactory;
|
||
import android.os.IBinder;
|
||
import android.os.PowerManager;
|
||
import android.text.TextUtils;
|
||
import android.text.format.Formatter;
|
||
import android.util.Log;
|
||
|
||
import com.arialyy.annotations.Download;
|
||
import com.arialyy.aria.core.Aria;
|
||
import com.arialyy.aria.core.task.DownloadTask;
|
||
import com.blankj.utilcode.util.ImageUtils;
|
||
import com.blankj.utilcode.util.NetworkUtils;
|
||
import com.uiui.os.BuildConfig;
|
||
import com.uiui.os.activity.MainContact;
|
||
import com.uiui.os.activity.MainPresenter;
|
||
import com.uiui.os.activity.NoticeActivity;
|
||
import com.uiui.os.base.BaseService;
|
||
import com.uiui.os.bean.AlarmClockData;
|
||
import com.uiui.os.bean.BaseResponse;
|
||
import com.uiui.os.network.NetInterfaceManager;
|
||
import com.uiui.os.utils.APKUtils;
|
||
import com.uiui.os.utils.CmdUtil;
|
||
import com.uiui.os.utils.ForegroundAppUtil;
|
||
import com.uiui.os.utils.AppUsedTimeUtils;
|
||
import com.uiui.os.utils.ToastUtil;
|
||
import com.uiui.os.utils.Utils;
|
||
|
||
import java.io.ByteArrayInputStream;
|
||
import java.io.ByteArrayOutputStream;
|
||
import java.io.File;
|
||
import java.io.FileNotFoundException;
|
||
import java.io.FileOutputStream;
|
||
import java.io.InputStream;
|
||
import java.util.List;
|
||
|
||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||
import io.reactivex.rxjava3.core.Observable;
|
||
import io.reactivex.rxjava3.core.ObservableEmitter;
|
||
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
||
import io.reactivex.rxjava3.core.ObservableSource;
|
||
import io.reactivex.rxjava3.core.Observer;
|
||
import io.reactivex.rxjava3.disposables.Disposable;
|
||
import io.reactivex.rxjava3.functions.Function;
|
||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||
import okhttp3.MediaType;
|
||
import okhttp3.MultipartBody;
|
||
import okhttp3.RequestBody;
|
||
|
||
|
||
public class MainService extends BaseService implements MainContact.MainView, NetworkUtils.OnNetworkStatusChangedListener {
|
||
private static final String TAG = MainService.class.getSimpleName();
|
||
public static MainPresenter mPresenter;
|
||
|
||
public MainService() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onDisconnected() {
|
||
|
||
}
|
||
|
||
@Override
|
||
public void onConnected(NetworkUtils.NetworkType networkType) {
|
||
|
||
}
|
||
|
||
@Override
|
||
public IBinder onBind(Intent intent) {
|
||
Log.e(TAG, "onBind: ");
|
||
return null;
|
||
}
|
||
|
||
|
||
@Override
|
||
public void onCreate() {
|
||
super.onCreate();
|
||
Log.e(TAG, "onCreate: ");
|
||
Aria.init(this);
|
||
Aria.download(this).register();
|
||
mPresenter = new MainPresenter(this);
|
||
mPresenter.attachView(this);
|
||
mPresenter.setLifecycle(lifecycleSubject);
|
||
NetworkUtils.registerNetworkStatusChangedListener(this);
|
||
mPresenter.getAlarmClock();
|
||
registerAlarmReceiver();
|
||
registerTimeReceiver();
|
||
registerLockScreenReceiver();
|
||
}
|
||
|
||
@Override
|
||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||
Log.e(TAG, "onStartCommand: ");
|
||
return START_STICKY;
|
||
}
|
||
|
||
@Override
|
||
public void onDestroy() {
|
||
super.onDestroy();
|
||
mPresenter.detachView();
|
||
NetworkUtils.unregisterNetworkStatusChangedListener(this);
|
||
if (alarmReceiver != null) {
|
||
unregisterReceiver(alarmReceiver);
|
||
}
|
||
if (mTimeChangedReceiver != null) {
|
||
unregisterReceiver(mTimeChangedReceiver);
|
||
}
|
||
}
|
||
|
||
@Override
|
||
public void setAlarmClock(List<AlarmClockData> dataList) {
|
||
|
||
}
|
||
|
||
public static final String ALARMWAKEUP = "ALARM_WAKEUP";
|
||
|
||
private void registerAlarmReceiver() {
|
||
if (alarmReceiver == null) {
|
||
alarmReceiver = new AlarmReceiver();
|
||
}
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||
filter.addAction(ALARMWAKEUP);
|
||
registerReceiver(alarmReceiver, filter);
|
||
}
|
||
|
||
private AlarmReceiver alarmReceiver = new AlarmReceiver();
|
||
|
||
private class AlarmReceiver extends BroadcastReceiver {
|
||
@Override
|
||
public void onReceive(Context context, Intent intent) {
|
||
String action = intent.getAction();
|
||
if (TextUtils.isEmpty(action)) return;
|
||
Log.e(TAG, "onReceive: " + action);
|
||
String title = intent.getStringExtra("title");
|
||
int code = intent.getIntExtra("id", -1);
|
||
Log.e(TAG, "onReceive: title = " + title);
|
||
if (ALARMWAKEUP.equals(action)) {
|
||
Intent noticeIntent = new Intent();
|
||
noticeIntent.putExtra("id", code);
|
||
noticeIntent.setClass(MainService.this, NoticeActivity.class);
|
||
noticeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
startActivity(noticeIntent);
|
||
}
|
||
}
|
||
}
|
||
|
||
//监听时间和日期变化
|
||
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 = AppUsedTimeUtils.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 = AppUsedTimeUtils.getInstance().getStartTime();
|
||
if (AppUsedTimeUtils.getInstance().isNormalTime()) {//正常时间段
|
||
if (System.currentTimeMillis() - startTime >= ONE_HOURS_TIME) {
|
||
Log.e(TAG, "isScreenshot: " + "截图");
|
||
doscreenshot(this);
|
||
}
|
||
} else {//非正常时间段
|
||
if (System.currentTimeMillis() - startTime >= TEN_MINUTES_TIME) {
|
||
Log.e(TAG, "isScreenshot: " + "截图");
|
||
doscreenshot(this);
|
||
}
|
||
}
|
||
}
|
||
|
||
private static Observable<Integer> getScreenshot(Context context, String filePath) {
|
||
Observable<Integer> screenshotObservable = Observable.create(new ObservableOnSubscribe<Integer>() {
|
||
@Override
|
||
public void subscribe(ObservableEmitter<Integer> e) {
|
||
int code = CmdUtil.execute("screencap -p " + filePath).code;
|
||
e.onNext(code);
|
||
e.onComplete();
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread());
|
||
return screenshotObservable;
|
||
}
|
||
|
||
private static Observable<BaseResponse> getSendFile(String path, MultipartBody.Part body) {
|
||
return NetInterfaceManager.getInstance()
|
||
.getScreenshotApi()
|
||
.sendScreenshot(Utils.getSerial(), body)
|
||
.subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread());
|
||
}
|
||
|
||
public static void doscreenshot(Context context) {
|
||
long time = System.currentTimeMillis();
|
||
String path = context.getExternalFilesDir("db").getAbsolutePath();
|
||
String filePath = path + File.separator + time + ".png";
|
||
getScreenshot(context, filePath).concatMap(new Function<Integer, ObservableSource<BaseResponse>>() {
|
||
@Override
|
||
public ObservableSource<BaseResponse> apply(Integer integer) throws Exception {
|
||
if (integer != 0) {
|
||
throw new FileNotFoundException();
|
||
}
|
||
File file = new File(filePath);
|
||
if (!file.exists()) {
|
||
throw new FileNotFoundException(filePath);
|
||
}
|
||
// Bitmap bitmap = BitmapFactory.decodeFile(filePath);
|
||
// if (bitmap.getWidth() < bitmap.getHeight()) {
|
||
// bitmap = ImageUtils.rotate(bitmap, -90, bitmap.getWidth(), bitmap.getHeight());
|
||
// }
|
||
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||
// file.createNewFile();
|
||
// FileOutputStream fos = new FileOutputStream(file);
|
||
// InputStream is = new ByteArrayInputStream(baos.toByteArray());
|
||
// int x;
|
||
// byte[] b = new byte[1024 * 100];
|
||
// while ((x = is.read(b)) != -1) {
|
||
// fos.write(b, 0, x);
|
||
// }
|
||
// fos.close();
|
||
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
|
||
MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), requestFile);
|
||
return getSendFile(filePath, body);
|
||
}
|
||
}).subscribeOn(Schedulers.io())
|
||
.observeOn(AndroidSchedulers.mainThread())
|
||
.subscribe(new Observer<BaseResponse>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
Log.e("screenshot", "onSubscribe: ");
|
||
}
|
||
|
||
@Override
|
||
public void onNext(BaseResponse baseResponse) {
|
||
Log.e("screenshot", "onNext: " + baseResponse);
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
Log.e("screenshot", "onError: " + e.getMessage());
|
||
onComplete();
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
Log.e("screenshot", "onComplete: ");
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
private LockScreenReceiver lockScreenReceiver;
|
||
|
||
private void registerLockScreenReceiver() {
|
||
if (lockScreenReceiver == null) {
|
||
lockScreenReceiver = new LockScreenReceiver();
|
||
}
|
||
IntentFilter filter = new IntentFilter();
|
||
filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
|
||
filter.addAction(Intent.ACTION_USER_PRESENT);
|
||
filter.addAction(Intent.ACTION_SCREEN_ON);
|
||
filter.addAction(Intent.ACTION_SCREEN_OFF);
|
||
registerReceiver(lockScreenReceiver, filter);
|
||
}
|
||
|
||
static class LockScreenReceiver extends BroadcastReceiver {
|
||
@Override
|
||
public void onReceive(Context context, Intent intent) {
|
||
String action = intent.getAction();
|
||
if (TextUtils.isEmpty(action)) return;
|
||
Log.e(TAG, "onReceive: " + action);
|
||
switch (action) {
|
||
default:
|
||
break;
|
||
case Intent.ACTION_USER_PRESENT:
|
||
resetStartTime(context);
|
||
break;
|
||
case Intent.ACTION_SCREEN_ON:
|
||
|
||
break;
|
||
case Intent.ACTION_SCREEN_OFF:
|
||
//关闭屏幕上传数据
|
||
sendAppUsed(context);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private static void resetStartTime(Context context) {
|
||
String topPackageName = ForegroundAppUtil.getForegroundPackageName(context);
|
||
Log.e(TAG, "resetStartTime: " + topPackageName);
|
||
AppUsedTimeUtils.getInstance().setAppPackageName(topPackageName);
|
||
AppUsedTimeUtils.getInstance().setStartTime(System.currentTimeMillis());
|
||
}
|
||
|
||
private static void sendAppUsed(Context context) {
|
||
String packagename = AppUsedTimeUtils.getInstance().getAppPackageName();
|
||
Log.e(TAG, "onResume packagename == " + packagename);
|
||
// AppUsedTimeUtils.getInstance().setEndTime(System.currentTimeMillis());
|
||
if (!TextUtils.isEmpty(packagename)) {
|
||
NetInterfaceManager.getInstance().getAppUsageRecordControl()
|
||
.sendappUsageRecord(Utils.getSerial(),
|
||
APKUtils.getAppNameByPackage(context, packagename),
|
||
packagename,
|
||
AppUsedTimeUtils.getInstance().getStartTime() / 1000,
|
||
AppUsedTimeUtils.getInstance().getEndTime() / 1000)
|
||
.subscribe(new Observer<BaseResponse>() {
|
||
@Override
|
||
public void onSubscribe(Disposable d) {
|
||
Log.e("sendAppUsed", "onSubscribe: ");
|
||
}
|
||
|
||
@Override
|
||
public void onNext(BaseResponse baseResponse) {
|
||
Log.e("sendAppUsed", "onNext: " + baseResponse);
|
||
}
|
||
|
||
@Override
|
||
public void onError(Throwable e) {
|
||
Log.e("sendAppUsed", "onError: " + e.getMessage());
|
||
onComplete();
|
||
}
|
||
|
||
@Override
|
||
public void onComplete() {
|
||
Log.e("sendAppUsed", "onComplete: ");
|
||
}
|
||
});
|
||
} else {
|
||
Log.e("fht", "app = null" + packagename);
|
||
}
|
||
}
|
||
|
||
//在这里处理任务执行中的状态,如进度进度条的刷新
|
||
@Download.onTaskRunning
|
||
protected void running(DownloadTask task) {
|
||
Log.e("aria", "正在下载:" + task.getPercent() + ":" + task.getExtendField());
|
||
ToastUtil.show("正在下载:" + task.getPercent() + "%" + "\t" + Formatter.formatFileSize(MainService.this, task.getSpeed()) + "/s");
|
||
}
|
||
|
||
@Download.onTaskComplete
|
||
void taskComplete(DownloadTask task) {
|
||
//在这里处理任务完成的状态
|
||
|
||
}
|
||
|
||
@Download.onTaskFail
|
||
void taskFail(DownloadTask task, Exception e) {
|
||
|
||
}
|
||
}
|