|
|
@@ -1,18 +1,26 @@
|
|
|
package com.sheep.gamegroup.util;
|
|
|
|
|
|
+import android.annotation.SuppressLint;
|
|
|
import android.app.Activity;
|
|
|
+import android.app.usage.UsageStats;
|
|
|
+import android.app.usage.UsageStatsManager;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
import android.media.projection.MediaProjectionManager;
|
|
|
+import android.os.Build;
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.sheep.gamegroup.model.api.BaseMessageConverter;
|
|
|
+import com.sheep.gamegroup.model.entity.AppUsage;
|
|
|
import com.sheep.gamegroup.model.entity.BaseMessage;
|
|
|
+import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
|
|
|
import com.sheep.gamegroup.model.entity.UserEntity;
|
|
|
+import com.sheep.gamegroup.model.util.AutoTaskListUtil;
|
|
|
import com.sheep.jiuyan.samllsheep.BuildConfig;
|
|
|
import com.sheep.jiuyan.samllsheep.SheepApp;
|
|
|
import com.sheep.jiuyan.samllsheep.service.FloatShotScreenService;
|
|
|
+import com.sheep.jiuyan.samllsheep.utils.FileUtil;
|
|
|
import com.sheep.jiuyan.samllsheep.utils.G;
|
|
|
import com.sheep.jiuyan.samllsheep.utils.SpUtils;
|
|
|
|
|
|
@@ -24,6 +32,7 @@ import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
+import java.util.TreeMap;
|
|
|
|
|
|
import rx.functions.Action1;
|
|
|
|
|
|
@@ -36,6 +45,7 @@ import static android.app.Activity.RESULT_OK;
|
|
|
public class DataUtil {
|
|
|
private static DataUtil instance = new DataUtil();
|
|
|
private ACache aCache;
|
|
|
+
|
|
|
private DataUtil() {
|
|
|
aCache = ACache.get(SheepApp.getInstance());
|
|
|
userEntity = getCacheResult(ApiKey.get_info, UserEntity.class);//先从缓存中初始化用户信息
|
|
|
@@ -172,6 +182,7 @@ public class DataUtil {
|
|
|
|
|
|
/**
|
|
|
* 重新设置api为key的数据的时间为saveTime,若本身没有数据,则保存空数据
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param saveTime 单位秒
|
|
|
*/
|
|
|
@@ -187,6 +198,7 @@ public class DataUtil {
|
|
|
|
|
|
/**
|
|
|
* 获取缓存的数据列表,为空代表没有获取过,为空字符串代表之前获取数据为空列表
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param classT
|
|
|
* @param <T>
|
|
|
@@ -196,7 +208,7 @@ public class DataUtil {
|
|
|
String cacheData = aCache.getAsString(SheepApp.getInstance().getConnectAddress().getAppUrl() + "v1/" + key);
|
|
|
if (cacheData == null) {
|
|
|
return null;
|
|
|
- } else if(cacheData.isEmpty()){
|
|
|
+ } else if (cacheData.isEmpty()) {
|
|
|
return ListUtil.emptyList();
|
|
|
}
|
|
|
if (BuildConfig.XXTEA_ENCRYPT) {
|
|
|
@@ -222,28 +234,33 @@ public class DataUtil {
|
|
|
|
|
|
//-----------------------------------------缓存数据部分-----------------------------------------------------结束
|
|
|
//-----------------------------------------进行任务部分-----------------------------------------------------开始
|
|
|
+
|
|
|
/**
|
|
|
* 是否首页正在运行的任务与发布的任务列表发生变化
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean isTaskListChange(Class<?> tagClass) {
|
|
|
return isTaskListChange(tagClass.getName());
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 是否首页正在运行的任务与发布的任务列表发生变化
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
public boolean isTaskListChange(String tag) {
|
|
|
- if(tagList.contains(tag)){//包含tag,说明使用该tag的地方已经刷新数据
|
|
|
+ if (tagList.contains(tag)) {//包含tag,说明使用该tag的地方已经刷新数据
|
|
|
return false;
|
|
|
} else {
|
|
|
tagList.add(tag);
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
private List<String> tagList = ListUtil.emptyList();
|
|
|
|
|
|
- public void setTaskListChanged(){
|
|
|
+ public void setTaskListChanged() {
|
|
|
tagList.clear();
|
|
|
}
|
|
|
|
|
|
@@ -251,58 +268,86 @@ public class DataUtil {
|
|
|
|
|
|
//-----------------------------------------其它数据部分-----------------------------------------------------开始
|
|
|
private Map<String, Object> objectMap = new HashMap<>();
|
|
|
+
|
|
|
/**
|
|
|
* 存放共享数据
|
|
|
+ *
|
|
|
* @param key
|
|
|
* @param data
|
|
|
*/
|
|
|
public void putData(String key, Object data) {
|
|
|
objectMap.put(key, data);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 获取共享数据并删除
|
|
|
+ *
|
|
|
* @param key
|
|
|
*/
|
|
|
public Object getData(String key) {
|
|
|
- if(objectMap.containsKey(key)) {
|
|
|
+ if (objectMap.containsKey(key)) {
|
|
|
return objectMap.remove(key);
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
-
|
|
|
+ //是否使用小绵羊截图方案
|
|
|
+ public static final boolean IS_USE_SCREEN_SHOT = true;
|
|
|
/**
|
|
|
* 截图路径
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
- public File getScreenShotsFile() {
|
|
|
- //TODO 这里还差个包名
|
|
|
- return new File(SheepApp.getInstance().getDir("ScreenShots", Context.MODE_PRIVATE), String.format(Locale.CHINA, "%s.png", TimeUtil.getDate("yyyy-MM-dd-hh-mm-ss")));
|
|
|
+ public File getScreenShotsFile(String packageName) {
|
|
|
+ File dir = getScreenShotsDir(packageName);
|
|
|
+ if(!dir.exists())
|
|
|
+ dir.mkdirs();
|
|
|
+ return new File(dir, TimeUtil.getDate("yyyy-MM-dd-hh-mm-ss")+".png");
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 截图目录
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
public File getScreenShotsDir() {
|
|
|
return SheepApp.getInstance().getDir("ScreenShots", Context.MODE_PRIVATE);
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 指定程序的截图目录
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public File getScreenShotsDir(String packageName) {
|
|
|
+ return new File(SheepApp.getInstance().getDir("ScreenShots", Context.MODE_PRIVATE), packageName);
|
|
|
+ }
|
|
|
|
|
|
- //-----------------------------------------其它数据部分-----------------------------------------------------结束
|
|
|
+ /**
|
|
|
+ * 删除所有截图
|
|
|
+ */
|
|
|
+ public void clearScreenShots() {
|
|
|
+ FileUtil.deleteFile(getScreenShotsDir(), false);
|
|
|
+ }
|
|
|
|
|
|
public static final int REQUEST_MEDIA_PROJECTION = 18;
|
|
|
public static final String REQUEST_CAPTURE_PERMISSION = "requestCapturePermission";//获取录屏权限
|
|
|
|
|
|
private Action1<Integer> action1;
|
|
|
+
|
|
|
public void requestCapturePermission(Activity activity, Action1<Integer> action1) {
|
|
|
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
|
|
|
- this.action1 = action1;
|
|
|
- //5.0 之后才允许使用屏幕截图
|
|
|
- MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
|
|
|
- if (mediaProjectionManager != null) {
|
|
|
- activity.startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
|
|
|
- return;
|
|
|
+ if(IS_USE_SCREEN_SHOT) {//使用小绵羊截图方案
|
|
|
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ this.action1 = action1;
|
|
|
+ //5.0 之后才允许使用屏幕截图
|
|
|
+ MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) activity.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
|
|
|
+ if (mediaProjectionManager != null) {
|
|
|
+ activity.startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
|
|
|
+ return;
|
|
|
+ }
|
|
|
}
|
|
|
+ G.showToast("您的系统版本过低,暂不支持该功能");
|
|
|
+ } else {
|
|
|
+ action1.call(0);
|
|
|
}
|
|
|
- G.showToast("您的系统版本过低,暂不支持该功能");
|
|
|
}
|
|
|
|
|
|
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
|
|
@@ -310,11 +355,78 @@ public class DataUtil {
|
|
|
case REQUEST_MEDIA_PROJECTION:
|
|
|
if (resultCode == RESULT_OK && data != null) {
|
|
|
DataUtil.getInstance().putData(REQUEST_CAPTURE_PERMISSION, data);
|
|
|
- if(action1 != null)
|
|
|
+ if (action1 != null)
|
|
|
action1.call(0);
|
|
|
}
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取前台运行的程序的包名
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SuppressLint("NewApi")
|
|
|
+ public String getPackageNameInForeground() {
|
|
|
+ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) {
|
|
|
+ UsageStatsManager mUsageStatsManager = (UsageStatsManager) SheepApp.getInstance().getSystemService(Context.USAGE_STATS_SERVICE);//usagestats
|
|
|
+ long time = System.currentTimeMillis();
|
|
|
+ if (mUsageStatsManager != null) {
|
|
|
+ List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, AutoTaskListUtil.getInstance().getReceiveTaskTime(), time);
|
|
|
+
|
|
|
+ if (!ListUtil.isEmpty(usageStatsList)) {
|
|
|
+ TreeMap<Long, UsageStats> mySortedMap = new TreeMap<>();
|
|
|
+ for (UsageStats item : usageStatsList) {
|
|
|
+ mySortedMap.put(item.getLastTimeUsed(), item);
|
|
|
+ }
|
|
|
+
|
|
|
+ UsageStats usageStats = mySortedMap.get(mySortedMap.lastKey());
|
|
|
+
|
|
|
+ return usageStats.getPackageName();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ android.app.ActivityManager.RunningAppProcessInfo process = getAppProcessInfoInForeground();
|
|
|
+ if (process != null) {
|
|
|
+ return process.processName;
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private android.app.ActivityManager.RunningAppProcessInfo getAppProcessInfoInForeground() {
|
|
|
+ android.app.ActivityManager manager = (android.app.ActivityManager) SheepApp.getInstance().getSystemService(Context.ACTIVITY_SERVICE);
|
|
|
+ List<android.app.ActivityManager.RunningAppProcessInfo> processList = manager == null ? null : manager.getRunningAppProcesses();
|
|
|
+ if (!ListUtil.isEmpty(processList)) {
|
|
|
+ for (android.app.ActivityManager.RunningAppProcessInfo item : processList) {
|
|
|
+ if (item != null && item.importance == android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
|
|
|
+ return item;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //正在运行的任务
|
|
|
+ List<TaskAcceptedEty> taskAcceptedEtyList = ListUtil.emptyList();
|
|
|
+ public void addTaskAcceptedEtyList(List<TaskAcceptedEty> list) {
|
|
|
+ taskAcceptedEtyList.clear();
|
|
|
+ taskAcceptedEtyList.addAll(list);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过包名从内存中正在运行的任务列表中获取正在运行的任务
|
|
|
+ * @param pacageName
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public TaskAcceptedEty getTaskAcceptedEtyFromPackageName(String pacageName) {
|
|
|
+ if(!TextUtils.isEmpty(pacageName))
|
|
|
+ for (TaskAcceptedEty taskAcceptedEty : taskAcceptedEtyList) {
|
|
|
+ if(taskAcceptedEty != null && taskAcceptedEty.getRelease_task() != null && taskAcceptedEty.getRelease_task().getTask() != null
|
|
|
+ && TextUtils.equals(taskAcceptedEty.getRelease_task().getTask().getPackage_names(), pacageName)){
|
|
|
+ return taskAcceptedEty;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ //-----------------------------------------其它数据部分-----------------------------------------------------结束
|
|
|
}
|