Procházet zdrojové kódy

添加 小绵羊截图方案,并且截图根据包名分类

zengjiebin před 7 roky
rodič
revize
1ef1307ce3

+ 3 - 1
app/src/main/java/com/kfzs/duanduan/KFZSApp.java

@@ -12,6 +12,7 @@ import com.arialyy.aria.core.Aria;
 import com.kfzs.appstore.utils.restful.KFZSNetwork;
 import com.kfzs.duanduan.utils.ApkUtils;
 import com.sheep.gamegroup.util.ActivityManager;
+import com.sheep.gamegroup.util.DataUtil;
 import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.service.DownloadService;
 import com.sheep.jiuyan.samllsheep.service.FloatService;
@@ -101,7 +102,8 @@ public class KFZSApp extends MultiDexApplication {
             Aria.download(this).stopAllTask();
             stopService(new Intent(this, DownloadService.class));
             stopService(new Intent(this, FloatService.class));
-            stopService(new Intent(this, FloatShotScreenService.class));
+            if(DataUtil.IS_USE_SCREEN_SHOT)
+                stopService(new Intent(this, FloatShotScreenService.class));
             ActivityManager.getInstance().finishAllActivity();
         } catch (Exception e) {
             Log.e(TAG, "quit error" + e.getMessage() + "\n" + e.getCause());

+ 1 - 0
app/src/main/java/com/sheep/gamegroup/model/util/AutoTaskListUtil.java

@@ -88,6 +88,7 @@ public class AutoTaskListUtil {
     }
 
     public void loadList(List<TaskAcceptedEty> list) {
+        DataUtil.getInstance().addTaskAcceptedEtyList(list);//添加正在运行的任务列表到内存中
         clear();
         if(list != null) {
             for (TaskAcceptedEty item : list) {

+ 6 - 3
app/src/main/java/com/sheep/gamegroup/util/CommonUtil.java

@@ -517,10 +517,13 @@ public class CommonUtil {
      * 添加应用自动审核任务
      */
     public void setTAskEnty(TaskEty tAskEnty){
+        DataUtil.getInstance().clearScreenShots();//领取或者取消任务时删除所有截图
         if(tAskEnty != null){
-            AutoTaskListUtil.getInstance().addTask(tAskEnty);//添加任务时
-            AutoTaskListUtil.getInstance().resetReceiveTaskTime();//重置比对时间  5.1及以上使用
-            AppUsageManager.getInstance().tryOpenLookAppUsageStatsPermisson(true);//尝试打开权限对话框 5.1及以上使用
+            if(AutoTaskListUtil.getInstance().addTask(tAskEnty)) {
+                AutoTaskListUtil.getInstance().resetReceiveTaskTime();//重置比对时间  5.1及以上使用
+                AppUsageManager.getInstance().tryOpenLookAppUsageStatsPermisson(true);//尝试打开权限对话框 5.1及以上使用
+            }
+            AutoTaskListUtil.getInstance().initTaskList();
         } else {//每次取消任务都重置记录
             AutoTaskListUtil.getInstance().initTaskList();
             MyDbManager.getInstance().removeAppRecord();//删除记录表 5.0及以下使用

+ 130 - 18
app/src/main/java/com/sheep/gamegroup/util/DataUtil.java

@@ -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;
+    }
+    //-----------------------------------------其它数据部分-----------------------------------------------------结束
 }

+ 5 - 3
app/src/main/java/com/sheep/gamegroup/util/Jump2View.java

@@ -1010,9 +1010,11 @@ public class Jump2View {
      * @param context
      */
     public void startShotScreenFloat(Context context, boolean isShow) {
-        Intent intent = new Intent(context, FloatShotScreenService.class);
-        intent.putExtra("isShow", isShow);
-        context.startService(intent);
+        if(DataUtil.IS_USE_SCREEN_SHOT) {
+            Intent intent = new Intent(context, FloatShotScreenService.class);
+            intent.putExtra("isShow", isShow);
+            context.startService(intent);
+        }
     }
 
 

+ 3 - 1
app/src/main/java/com/sheep/gamegroup/util/SysAppUtil.java

@@ -132,6 +132,7 @@ public class SysAppUtil {
                     public Integer call(Integer integer) {
                         Glide.get(SheepApp.getInstance()).clearDiskCache();//清理图片
                         ClassFileHelper.getInstance().clearDir();//清理下载文件
+                        DataUtil.getInstance().clearScreenShots();//清除所有截图
                         return integer;
                     }
                 })
@@ -155,11 +156,12 @@ public class SysAppUtil {
 //            File cacheDir = SheepApp.getInstance().getExternalCacheDir();
 //            long filesDirSize = getFolderSize(filesDir);
 //            long cacheSize = getFolderSize(cacheDir);
+            long screenShotsSize = getFolderSize(DataUtil.getInstance().getScreenShotsDir());
             long apkSize = getFolderSize(new File(ClassFileHelper.DIR));
             File cacheDirPath = SheepApp.getInstance().getCacheDir();
             long glideSize = getFolderSize(new File( cacheDirPath, InternalCacheDiskCacheFactory.DEFAULT_DISK_CACHE_DIR));
             return getFormatSize(//filesDirSize+cacheSize+
-                     apkSize+glideSize);
+                    screenShotsSize+apkSize+glideSize);
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 12 - 6
app/src/main/java/com/sheep/gamegroup/util/TestUtil.java

@@ -295,14 +295,20 @@ public class TestUtil {
                     public void onClick(DialogInterface dialog, int which) {
                         switch (items[which]) {
                             case "查看截图":
-                                File dir = DataUtil.getInstance().getScreenShotsDir();
-                                if(dir.exists()) {
-                                    G.showToast("截图个数为:" + dir.list().length);
+                                File root = DataUtil.getInstance().getScreenShotsDir();
+                                if(!root.exists() || root.listFiles().length <= 0){
+                                    G.showToast("暂无截图");
                                 } else {
-                                    G.showToast("截图目录不存在");
+                                    File dir = DataUtil.getInstance().getScreenShotsDir().listFiles()[0];
+                                    if (dir.exists()) {
+                                        G.showToast("截图个数为:" + dir.list().length);
+                                    } else {
+                                        G.showToast("截图目录不存在");
+                                    }
+                                    if (dir.list().length > 0) {
+                                        Jump2View.getInstance().selectImgFiles(activity, 7, dir.listFiles());
+                                    }
                                 }
-                                if(dir.list().length > 0)
-                                    Jump2View.getInstance().selectImgFiles(activity, 7, dir.listFiles());
                                 break;
                             case "小米游戏":
                                 Jump2View.getInstance().goXiaomiGameList(activity, null);

+ 11 - 1
app/src/main/java/com/sheep/jiuyan/samllsheep/service/FloatShotScreenService.java

@@ -26,6 +26,7 @@ import android.view.View;
 import android.view.WindowManager;
 import android.widget.ImageView;
 
+import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
 import com.sheep.gamegroup.util.DataUtil;
 import com.sheep.gamegroup.util.GlobalScreenShot;
 import com.sheep.jiuyan.samllsheep.R;
@@ -252,9 +253,18 @@ public class FloatShotScreenService extends Service {
     }
 
     private void performClick() {
+        //获取当前正在运行的程序的包名
+        String packageName = DataUtil.getInstance().getPackageNameInForeground();
+        if(DataUtil.getInstance().getTaskAcceptedEtyFromPackageName(packageName) == null){
+            //TODO 如果一个任务需要截取其它程序的截图,这里就不行了:如收到的短信
+            G.showToast("请切换到对应的应用后进行截图!");
+            return;
+        }
+        packageNameInForeground = packageName;
         // 设置点击悬浮窗的响应
         startScreenShot();
     }
+    private String packageNameInForeground;
 
     private void startScreenShot() {
 
@@ -365,7 +375,7 @@ public class FloatShotScreenService extends Service {
         File fileImage = null;
         if (bitmap != null) {
             try {
-                fileImage = DataUtil.getInstance().getScreenShotsFile();
+                fileImage = DataUtil.getInstance().getScreenShotsFile(packageNameInForeground);
                 if (!fileImage.exists()) {
                     fileImage.createNewFile();
                 }