version:1.9
fix: update:修改包名,添加推送
This commit is contained in:
17
app/src/main/java/com/uiui/aios/service/AlarmService.java
Normal file
17
app/src/main/java/com/uiui/aios/service/AlarmService.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package com.uiui.aios.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
|
||||
public class AlarmService extends Service {
|
||||
public AlarmService() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.uiui.aios.service.main;
|
||||
|
||||
import com.uiui.aios.base.BasePresenter;
|
||||
import com.uiui.aios.base.BaseView;
|
||||
import com.uiui.aios.bean.AlarmClockData;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class MainSContact {
|
||||
public interface Presenter extends BasePresenter<MainSView> {
|
||||
//设置所有信息
|
||||
void getAlarmClock();
|
||||
}
|
||||
|
||||
public interface MainSView extends BaseView {
|
||||
//获取所有信息
|
||||
void setAlarmClock(List<AlarmClockData> dataList);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
package com.uiui.aios.service.main;
|
||||
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import com.tencent.mmkv.MMKV;
|
||||
import com.trello.rxlifecycle4.android.ActivityEvent;
|
||||
import com.uiui.aios.bean.AlarmClockData;
|
||||
import com.uiui.aios.bean.BaseResponse;
|
||||
import com.uiui.aios.network.NetInterfaceManager;
|
||||
import com.uiui.aios.utils.AlarmUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
import io.reactivex.rxjava3.subjects.BehaviorSubject;
|
||||
|
||||
|
||||
/**
|
||||
* MainActivity和MainService 的 Presenter
|
||||
*
|
||||
* @author jgy02
|
||||
*/
|
||||
public class MainSPresenter implements MainSContact.Presenter {
|
||||
private static final String TAG = MainSPresenter.class.getSimpleName();
|
||||
private static final int OK = 200;
|
||||
private MainSContact.MainSView mView;
|
||||
private Context mContext;
|
||||
|
||||
private BehaviorSubject<ActivityEvent> lifecycle;
|
||||
|
||||
public void setLifecycle(BehaviorSubject<ActivityEvent> lifecycle) {
|
||||
this.lifecycle = lifecycle;
|
||||
}
|
||||
|
||||
public BehaviorSubject<ActivityEvent> getLifecycle() {
|
||||
return lifecycle;
|
||||
}
|
||||
|
||||
public MainSPresenter(Context context) {
|
||||
this.mContext = context;
|
||||
Log.e(TAG, "MainSPresenter: " + context.getClass());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void attachView(@androidx.annotation.NonNull MainSContact.MainSView view) {
|
||||
this.mView = view;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detachView() {
|
||||
this.mView = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void getAlarmClock() {
|
||||
MMKV mmkv = MMKV.defaultMMKV();
|
||||
NetInterfaceManager.getInstance().getAlarmClockApiObservable()
|
||||
.subscribe(new Observer<BaseResponse<List<AlarmClockData>>>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
Log.e("getAlarmClock", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(BaseResponse<List<AlarmClockData>> listBaseResponse) {
|
||||
Log.e("getAlarmClock", "onNext: "+listBaseResponse);
|
||||
if (listBaseResponse.code == 200) {
|
||||
List<AlarmClockData> data = listBaseResponse.data;
|
||||
AlarmUtils.getInstance().setAlarmClockData(data);
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("getAlarmClock", "onError: " + e.getMessage());
|
||||
onComplete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("getAlarmClock", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
397
app/src/main/java/com/uiui/aios/service/main/MainService.java
Normal file
397
app/src/main/java/com/uiui/aios/service/main/MainService.java
Normal file
@@ -0,0 +1,397 @@
|
||||
package com.uiui.aios.service.main;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
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.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.NetworkUtils;
|
||||
import com.uiui.aios.BuildConfig;
|
||||
import com.uiui.aios.activity.NoticeActivity;
|
||||
import com.uiui.aios.base.BaseService;
|
||||
import com.uiui.aios.bean.AlarmClockData;
|
||||
import com.uiui.aios.bean.BaseResponse;
|
||||
import com.uiui.aios.network.NetInterfaceManager;
|
||||
import com.uiui.aios.utils.ApkUtils;
|
||||
import com.uiui.aios.utils.CmdUtil;
|
||||
import com.uiui.aios.utils.ForegroundAppUtil;
|
||||
import com.uiui.aios.utils.AppUsedTimeUtils;
|
||||
import com.uiui.aios.utils.ToastUtil;
|
||||
import com.uiui.aios.utils.Utils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
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 MainSContact.MainSView, NetworkUtils.OnNetworkStatusChangedListener {
|
||||
private static final String TAG = MainService.class.getSimpleName();
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
public static MainSPresenter 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: ");
|
||||
ApkUtils.UninstallAPP(this, "com.joytv.live");
|
||||
ApkUtils.UninstallAPP(this, "com.tencent.android.qqdownloader");
|
||||
|
||||
Aria.init(this);
|
||||
Aria.download(this).register();
|
||||
mPresenter = new MainSPresenter(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) {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user