|
@@ -1,14 +1,21 @@
|
|
|
package com.sheep.jiuyan.samllsheep.service;
|
|
package com.sheep.jiuyan.samllsheep.service;
|
|
|
|
|
|
|
|
import android.accessibilityservice.AccessibilityService;
|
|
import android.accessibilityservice.AccessibilityService;
|
|
|
|
|
+import android.accessibilityservice.AccessibilityServiceInfo;
|
|
|
|
|
+import android.app.Activity;
|
|
|
import android.content.Context;
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
import android.content.Intent;
|
|
|
|
|
+import android.net.Uri;
|
|
|
|
|
+import android.os.Build;
|
|
|
import android.provider.Settings;
|
|
import android.provider.Settings;
|
|
|
import android.text.TextUtils;
|
|
import android.text.TextUtils;
|
|
|
import android.view.accessibility.AccessibilityEvent;
|
|
import android.view.accessibility.AccessibilityEvent;
|
|
|
|
|
+import android.view.accessibility.AccessibilityManager;
|
|
|
|
|
+import android.view.accessibility.AccessibilityNodeInfo;
|
|
|
|
|
|
|
|
import com.sheep.gamegroup.model.entity.AppRecord;
|
|
import com.sheep.gamegroup.model.entity.AppRecord;
|
|
|
import com.sheep.gamegroup.model.entity.TaskListen;
|
|
import com.sheep.gamegroup.model.entity.TaskListen;
|
|
|
|
|
+import com.sheep.gamegroup.model.util.AutoTaskListUtil;
|
|
|
import com.sheep.gamegroup.util.Jump2View;
|
|
import com.sheep.gamegroup.util.Jump2View;
|
|
|
import com.sheep.gamegroup.util.ListUtil;
|
|
import com.sheep.gamegroup.util.ListUtil;
|
|
|
import com.sheep.gamegroup.util.LogUtil;
|
|
import com.sheep.gamegroup.util.LogUtil;
|
|
@@ -24,19 +31,19 @@ import java.util.List;
|
|
|
public class MonitorAppService extends AccessibilityService {
|
|
public class MonitorAppService extends AccessibilityService {
|
|
|
@Override
|
|
@Override
|
|
|
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
|
|
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
|
|
|
- TaskListen taskEty = SheepApp.getInstance().getTaskEty();
|
|
|
|
|
- if(taskEty == null) {//当前没有时长任务时不操作数据库
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
// 此方法是在主线程中回调过来的,所以消息是阻塞执行的
|
|
// 此方法是在主线程中回调过来的,所以消息是阻塞执行的
|
|
|
// 获取包名
|
|
// 获取包名
|
|
|
CharSequence packageName = accessibilityEvent.getPackageName();
|
|
CharSequence packageName = accessibilityEvent.getPackageName();
|
|
|
CharSequence className = accessibilityEvent.getClassName();
|
|
CharSequence className = accessibilityEvent.getClassName();
|
|
|
int eventType = accessibilityEvent.getEventType();
|
|
int eventType = accessibilityEvent.getEventType();
|
|
|
List<CharSequence> textList = accessibilityEvent.getText();
|
|
List<CharSequence> textList = accessibilityEvent.getText();
|
|
|
|
|
+ LogUtil.println("MonitorAppService", "onAccessibilityEvent", eventType,"pkgName:", packageName, "className:", className, "source:", ListUtil.getIndex(textList, 0));
|
|
|
|
|
+ if(AutoTaskListUtil.getInstance().isEmpty()) {//当前没有应用自动审核任务时不操作数据库
|
|
|
|
|
+// kfDisableSelf();
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
switch (eventType) {
|
|
switch (eventType) {
|
|
|
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
|
|
case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
|
|
|
- LogUtil.println("MonitorAppService TYPE_WINDOW_STATE_CHANGED: " + " pkgName: " + packageName + " className: " + className + " source: " + ListUtil.getIndex(textList, 0));
|
|
|
|
|
|
|
|
|
|
long curTime = System.currentTimeMillis();
|
|
long curTime = System.currentTimeMillis();
|
|
|
AppRecord appRecord = new AppRecord();
|
|
AppRecord appRecord = new AppRecord();
|
|
@@ -46,15 +53,17 @@ public class MonitorAppService extends AccessibilityService {
|
|
|
if(ListUtil.getIndex(textList, 0) != null)
|
|
if(ListUtil.getIndex(textList, 0) != null)
|
|
|
appRecord.setTag(textList.get(0).toString());
|
|
appRecord.setTag(textList.get(0).toString());
|
|
|
|
|
|
|
|
- AppRecord lastAppRecord = SheepApp.getInstance().getLastAppRecord();
|
|
|
|
|
- long lastTime = SheepApp.getInstance().getLastTime();
|
|
|
|
|
|
|
+ AppRecord lastAppRecord = AutoTaskListUtil.getInstance().getLastAppRecord();
|
|
|
|
|
+ long lastTime = AutoTaskListUtil.getInstance().getLastTime();
|
|
|
if(lastAppRecord != null && lastTime != 0)
|
|
if(lastAppRecord != null && lastTime != 0)
|
|
|
lastAppRecord.setTime(lastAppRecord.getTime() + curTime - lastTime);
|
|
lastAppRecord.setTime(lastAppRecord.getTime() + curTime - lastTime);
|
|
|
- if(lastAppRecord != null && TextUtils.equals(taskEty.getPackage_names(), lastAppRecord.getPackageName()))
|
|
|
|
|
|
|
+ if(lastAppRecord != null && AutoTaskListUtil.getInstance().hasTask(lastAppRecord)) {
|
|
|
MyDbManager.getInstance().saveOrUpdate(lastAppRecord);
|
|
MyDbManager.getInstance().saveOrUpdate(lastAppRecord);
|
|
|
|
|
+ LogUtil.println("MonitorAppService", "MyDbManager", "saveOrUpdate", "lastAppRecord", lastAppRecord.getPackageName(), lastAppRecord.getTime());
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- SheepApp.getInstance().setLastTime(curTime);
|
|
|
|
|
- SheepApp.getInstance().setLastAppRecord(appRecord);
|
|
|
|
|
|
|
+ AutoTaskListUtil.getInstance().setLastTime(curTime);
|
|
|
|
|
+ AutoTaskListUtil.getInstance().setLastAppRecord(appRecord);
|
|
|
break;
|
|
break;
|
|
|
// case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
|
|
// case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
|
|
|
// System.out.println("MonitorAppService TYPE_WINDOW_CONTENT_CHANGED: " + " packageName: " + packageName + " source: " + ListUtil.getIndex(textList, 0));
|
|
// System.out.println("MonitorAppService TYPE_WINDOW_CONTENT_CHANGED: " + " packageName: " + packageName + " source: " + ListUtil.getIndex(textList, 0));
|
|
@@ -65,25 +74,58 @@ public class MonitorAppService extends AccessibilityService {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ private void kfDisableSelf() {
|
|
|
|
|
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
|
+ disableSelf();
|
|
|
|
|
+ } else {
|
|
|
|
|
+ stopSelf();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
|
|
- Jump2View.getInstance().clearAlertDialog();
|
|
|
|
|
|
|
+ AutoTaskListUtil.getInstance().clearAlertDialog();
|
|
|
return super.onStartCommand(intent, flags, START_STICKY);
|
|
return super.onStartCommand(intent, flags, START_STICKY);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public void onInterrupt() {
|
|
public void onInterrupt() {
|
|
|
- Jump2View.getInstance().startAccessibility(getApplicationContext());
|
|
|
|
|
|
|
+// Jump2View.getInstance().startAccessibility(getApplicationContext());
|
|
|
|
|
+ AutoTaskListUtil.getInstance().clearAlertDialog();
|
|
|
LogUtil.println("MonitorAppService onInterrupt");
|
|
LogUtil.println("MonitorAppService onInterrupt");
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public boolean onUnbind(Intent intent) {
|
|
public boolean onUnbind(Intent intent) {
|
|
|
- Jump2View.getInstance().startAccessibility(getApplicationContext());
|
|
|
|
|
|
|
+// Jump2View.getInstance().startAccessibility(getApplicationContext());
|
|
|
|
|
+ AutoTaskListUtil.getInstance().clearAlertDialog();
|
|
|
LogUtil.println("MonitorAppService onUnbind");
|
|
LogUtil.println("MonitorAppService onUnbind");
|
|
|
return super.onUnbind(intent);
|
|
return super.onUnbind(intent);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static void tryCloseAccessibility(Activity activity) {
|
|
|
|
|
+ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
+ if (!Settings.System.canWrite(SheepApp.getInstance())) {
|
|
|
|
|
+ Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + SheepApp.getInstance().getPackageName()));
|
|
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
+ activity.startActivityForResult(intent, -1);
|
|
|
|
|
+ } else {
|
|
|
|
|
+// 有了权限,你要做什么呢?具体的动作
|
|
|
|
|
+ try {
|
|
|
|
|
+ closeAccessibility();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } else {
|
|
|
|
|
+ try {
|
|
|
|
|
+ closeAccessibility();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static void closeAccessibility() {
|
|
public static void closeAccessibility() {
|
|
|
String service = SheepApp.getInstance().getPackageName() + "/" + MonitorAppService.class.getCanonicalName();
|
|
String service = SheepApp.getInstance().getPackageName() + "/" + MonitorAppService.class.getCanonicalName();
|
|
|
// 这个key保存已经辅助服务的组件名列表,以“:”隔开,要关闭自己的组件,只要从这个key的值中移除该字符串
|
|
// 这个key保存已经辅助服务的组件名列表,以“:”隔开,要关闭自己的组件,只要从这个key的值中移除该字符串
|
|
@@ -91,10 +133,20 @@ public class MonitorAppService extends AccessibilityService {
|
|
|
// 这个key控制辅助服务的开关
|
|
// 这个key控制辅助服务的开关
|
|
|
Settings.Secure.putInt(SheepApp.getInstance().getApplicationContext().getContentResolver(),Settings.Secure.ACCESSIBILITY_ENABLED, 0);
|
|
Settings.Secure.putInt(SheepApp.getInstance().getApplicationContext().getContentResolver(),Settings.Secure.ACCESSIBILITY_ENABLED, 0);
|
|
|
}
|
|
}
|
|
|
|
|
+ private static String getId(){
|
|
|
|
|
+ String pacakgeName = SheepApp.getInstance().getPackageName();
|
|
|
|
|
+ return pacakgeName+"/"+MonitorAppService.class.getCanonicalName().replace(pacakgeName, "");
|
|
|
|
|
+ }
|
|
|
|
|
+ private static String getId2(){
|
|
|
|
|
+ String pacakgeName = SheepApp.getInstance().getPackageName();
|
|
|
|
|
+ return pacakgeName+"/"+MonitorAppService.class.getCanonicalName();
|
|
|
|
|
+ }
|
|
|
|
|
+ private static boolean isThisService(String id){
|
|
|
|
|
+ return getId2().equalsIgnoreCase(id) || getId().equalsIgnoreCase(id);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
public static boolean isAccessibilitySettingsOn(Context mContext) {
|
|
public static boolean isAccessibilitySettingsOn(Context mContext) {
|
|
|
int accessibilityEnabled = 0;
|
|
int accessibilityEnabled = 0;
|
|
|
- final String service = mContext.getPackageName() + "/" + MonitorAppService.class.getCanonicalName();
|
|
|
|
|
// com.z.buildingaccessibilityservices/android.accessibilityservice.AccessibilityService
|
|
// com.z.buildingaccessibilityservices/android.accessibilityservice.AccessibilityService
|
|
|
try {
|
|
try {
|
|
|
accessibilityEnabled = Settings.Secure.getInt(mContext.getApplicationContext().getContentResolver(),
|
|
accessibilityEnabled = Settings.Secure.getInt(mContext.getApplicationContext().getContentResolver(),
|
|
@@ -106,13 +158,14 @@ public class MonitorAppService extends AccessibilityService {
|
|
|
if (accessibilityEnabled == 1) {
|
|
if (accessibilityEnabled == 1) {
|
|
|
String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(),
|
|
String settingValue = Settings.Secure.getString(mContext.getApplicationContext().getContentResolver(),
|
|
|
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
|
|
Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
|
|
|
|
|
+ LogUtil.println("MonitorAppService","isAccessibilitySettingsOn", "getString",settingValue);
|
|
|
//com.sheep.jiuyan.samllsheep/com.sheep.jiuyan.samllsheep.service.MonitorAppService
|
|
//com.sheep.jiuyan.samllsheep/com.sheep.jiuyan.samllsheep.service.MonitorAppService
|
|
|
if (settingValue != null) {
|
|
if (settingValue != null) {
|
|
|
mStringColonSplitter.setString(settingValue);
|
|
mStringColonSplitter.setString(settingValue);
|
|
|
while (mStringColonSplitter.hasNext()) {
|
|
while (mStringColonSplitter.hasNext()) {
|
|
|
String accessibilityService = mStringColonSplitter.next();
|
|
String accessibilityService = mStringColonSplitter.next();
|
|
|
|
|
|
|
|
- if (accessibilityService.equalsIgnoreCase(service)) {
|
|
|
|
|
|
|
+ if (isThisService(accessibilityService)) {
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|