Kaynağa Gözat

截屏方案优化;任务列表刷新数据优化

zengjiebin 7 yıl önce
ebeveyn
işleme
825b5f1d78

+ 4 - 0
app/src/main/AndroidManifest.xml

@@ -374,6 +374,10 @@
         <activity
             android:name="com.sheep.gamegroup.view.activity.ActXiaomiGame"
             android:screenOrientation="portrait" />
+        <activity
+            android:name="com.sheep.gamegroup.view.activity.MiddleScreenShotAct"
+            android:theme="@style/MyDialogActivityTheme"
+            android:screenOrientation="user" />
 
         <!--友盟start-->
         <meta-data

+ 0 - 4
app/src/main/java/com/kfzs/duanduan/KFZSApp.java

@@ -12,14 +12,10 @@ 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;
-import com.sheep.jiuyan.samllsheep.service.FloatShotScreenService;
 import com.sheep.jiuyan.samllsheep.utils.G;
 
-
 import java.io.File;
 import java.util.HashMap;
 import java.util.Map;

+ 3 - 6
app/src/main/java/com/kfzs/duanduan/fragment/FgtSmallSheep.java

@@ -180,15 +180,12 @@ public class FgtSmallSheep extends BaseCompatFragment implements SmallSheepContr
                     loadUserInfo();
                     break;
                 case 1:
-                    int position = 0;
+                    tryMakeMoneyAdp.clear();
                     if(!ListUtil.isEmpty(allTaskAcceptedTaskList)){
-                        tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RUN_TASK, allTaskAcceptedTaskList), position++);
+                        tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RUN_TASK, allTaskAcceptedTaskList));
                     }
                     if(!ListUtil.isEmpty(releaseEtyLists)) {
-                        tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RELEASE_TASK, releaseEtyLists), position++);
-                    }
-                    while(position < LIST_COUNT){
-                        tryMakeMoneyAdp.add(NULL, position++);
+                        tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RELEASE_TASK, releaseEtyLists));
                     }
                     tryMakeMoneyAdp.notifyDataSetChanged();
                     checkAndInitView();

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

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

+ 38 - 24
app/src/main/java/com/sheep/gamegroup/util/DataUtil.java

@@ -18,6 +18,7 @@ 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;
@@ -44,6 +45,7 @@ public class DataUtil {
     private ACache aCache;
 
     private DataUtil() {
+        LogUtil.println("DataUtil", "init");
         aCache = ACache.get(SheepApp.getInstance());
         userEntity = getCacheResult(ApiKey.get_info, UserEntity.class);//先从缓存中初始化用户信息
         String id = SpUtils.getToken(SheepApp.mContext);
@@ -281,7 +283,7 @@ public class DataUtil {
         return null;
     }
     //是否使用小绵羊截图方案
-    public static final boolean IS_USE_SCREEN_SHOT = BuildConfig.DEBUG;
+    public static final boolean IS_USE_SCREEN_SHOT = false;//BuildConfig.DEBUG;
     /**
      * 截图路径
      *
@@ -344,7 +346,7 @@ public class DataUtil {
         switch (requestCode) {
             case REQUEST_MEDIA_PROJECTION:
                 if (resultCode == RESULT_OK && data != null) {
-                    DataUtil.getInstance().putData(REQUEST_CAPTURE_PERMISSION, data);
+                    FloatShotScreenService.getInstance().setResultData(data);
                     if (action1 != null)
                         action1.call(0);
                 }
@@ -396,27 +398,39 @@ public class DataUtil {
         }
         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;
-    }
+//    //正在运行的任务
+//    List<TaskAcceptedEty> taskAcceptedEtyList = ListUtil.emptyList();
+//    public void addTaskAcceptedEtyList(List<TaskAcceptedEty> list) {
+//        isInitTaskAcceptedList = true;
+//        taskAcceptedEtyList.clear();
+//        taskAcceptedEtyList.addAll(list);
+//        LogUtil.println("DataUtil", "taskAcceptedEtyList", taskAcceptedEtyList.size());
+//    }
+//
+//    /**
+//     * 通过包名从内存中正在运行的任务列表中获取正在运行的任务
+//     * @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;
+//    }
+//
+//    private boolean isInitTaskAcceptedList = false;
+//
+//    public boolean isInitTaskAcceptedList() {
+//        return isInitTaskAcceptedList;
+//    }
+//
+//    public List<TaskAcceptedEty> getTaskAcceptedEtyList() {
+//        return taskAcceptedEtyList;
+//    }
     //-----------------------------------------其它数据部分-----------------------------------------------------结束
 }

+ 8 - 6
app/src/main/java/com/sheep/gamegroup/util/Jump2View.java

@@ -10,10 +10,6 @@ import android.text.TextUtils;
 import android.view.Gravity;
 import android.view.View;
 
-import com.alibaba.fastjson.JSONObject;
-import com.bumptech.glide.Glide;
-import com.bumptech.glide.load.engine.DiskCacheStrategy;
-import com.bumptech.glide.request.RequestOptions;
 import com.kfzs.duanduan.ActMain;
 import com.kfzs.duanduan.bean.KFIntentKeys;
 import com.sheep.gamegroup.absBase.BaseActivity;
@@ -132,6 +128,7 @@ import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_MY_MONEY;
 import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_NEW_VERSION;
 import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_TASK_RECORD;
 import static com.sheep.gamegroup.util.UMConfigUtils.Event.XIAOMI_GAME_LIST;
+import static com.sheep.jiuyan.samllsheep.service.FloatShotScreenService.SET_SHOW_OR_HIDE;
 
 /**
  * Created by ljy on 2018/3/18.
@@ -982,10 +979,15 @@ public class Jump2View {
      * 启动小绵羊悬浮窗
      * @param context
      */
-    public void startShotScreenFloat(Context context, boolean isShow) {
+    public void startShotScreenFloat(Context context, Object object) {
         if(DataUtil.IS_USE_SCREEN_SHOT) {
             Intent intent = new Intent(context, FloatShotScreenService.class);
-            intent.putExtra("isShow", isShow);
+            if(object instanceof Boolean) {
+                intent.putExtra("isShow", (Boolean) object);
+                intent.putExtra("type", SET_SHOW_OR_HIDE);
+            } else if(object instanceof Integer){
+                intent.putExtra("type", (Integer) object);
+            }
             context.startService(intent);
         }
     }

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

@@ -33,6 +33,7 @@ import com.sheep.gamegroup.model.entity.BaseMessage;
 import com.sheep.gamegroup.model.entity.DialogConfig;
 import com.sheep.gamegroup.model.entity.Version;
 import com.sheep.gamegroup.model.util.SheepSubscriber;
+import com.sheep.jiuyan.samllsheep.BuildConfig;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.ClassFileHelper;
@@ -457,7 +458,7 @@ public class SysAppUtil {
             return;
         }
         boolean isNewApp;
-        if(TestUtil.isSheep()) {
+        if(TextUtils.equals("sheep", BuildConfig.FLAVOR)) {
             if(ignoreMd5 != null && TextUtils.equals(ignoreMd5, version.getVersion_number()+"") && !version.isForceUpdate()){
                 LogUtil.println("showVersionInfo", "忽略", ignoreMd5, version.getVersion_name());
                 if(action1 != null)

+ 50 - 0
app/src/main/java/com/sheep/gamegroup/view/activity/MiddleScreenShotAct.java

@@ -0,0 +1,50 @@
+package com.sheep.gamegroup.view.activity;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.media.projection.MediaProjectionManager;
+import android.os.Bundle;
+import android.support.annotation.Nullable;
+
+import com.sheep.gamegroup.util.Jump2View;
+import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.service.FloatShotScreenService;
+import com.sheep.jiuyan.samllsheep.utils.G;
+
+import static com.sheep.gamegroup.util.DataUtil.REQUEST_MEDIA_PROJECTION;
+
+/**
+ * Created by realicing on 2018/5/10.
+ * realicing@sina.com
+ */
+public class MiddleScreenShotAct extends Activity {
+    @Override
+    protected void onCreate(@Nullable Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.act_middle_screen_shot);
+
+        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
+            //5.0 之后才允许使用屏幕截图
+            MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) this.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
+            if (mediaProjectionManager != null) {
+                startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), REQUEST_MEDIA_PROJECTION);
+                return;
+            }
+        }
+        G.showToast("您的系统版本过低,暂不支持该功能");
+    }
+    @Override
+    public void onActivityResult(int requestCode, int resultCode, Intent data) {
+        switch (requestCode) {
+            case REQUEST_MEDIA_PROJECTION:
+                if (resultCode == RESULT_OK && data != null) {
+                    FloatShotScreenService.getInstance().setResultData(data);
+//                    Jump2View.getInstance().startShotScreenFloat(this, FloatShotScreenService.SCREEN_SHOT);
+                    G.showToast("请重新点击小绵羊悬浮窗进行截图!");
+                    finish();
+                }
+                break;
+        }
+    }
+}

+ 12 - 6
app/src/main/java/com/sheep/gamegroup/view/activity/TaskDetailAct.java

@@ -66,6 +66,7 @@ import com.sheep.gamegroup.view.adapter.AdpTaskDetailChildListview;
 import com.sheep.gamegroup.view.adapter.TaskdetailSonListviewAdp;
 import com.sheep.jiuyan.samllsheep.R;
 import com.sheep.jiuyan.samllsheep.SheepApp;
+import com.sheep.jiuyan.samllsheep.service.FloatShotScreenService;
 import com.sheep.jiuyan.samllsheep.utils.G;
 import com.sheep.jiuyan.samllsheep.utils.PackageUtil;
 import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;
@@ -219,6 +220,16 @@ public class TaskDetailAct extends AbsChooseImageActivity implements TaskDetailC
         detail_task_listview.setAdapter(adpTaskDetailChildListview);
         adpTaskDetailChildListview.notifyDataSetChanged();
         bottom_btn_layout.setVisibility(View.GONE);
+        if(DataUtil.IS_USE_SCREEN_SHOT) {
+            //如果要使用小绵羊截图方案,则在进入任务详情界面启动FloatShotScreenService,并获取截屏权限,并把intent data传递给FloatShotScreenService
+            Jump2View.getInstance().startShotScreenFloat(SheepApp.getInstance(), null);
+            DataUtil.getInstance().requestCapturePermission(activity, new Action1<Integer>() {
+                @Override
+                public void call(Integer integer) {
+                    G.showToast("初始化截屏数据成功");
+                }
+            });
+        }
     }
 
     @Override
@@ -584,12 +595,7 @@ public class TaskDetailAct extends AbsChooseImageActivity implements TaskDetailC
                             ApkUtils.installApk(SheepApp.mContext, PackageUtil.isExistsFile(taskEty.getPackage_names()));
                             break;
                         case DownloadTaskService.STATUS_INSTALLED:
-                            DataUtil.getInstance().requestCapturePermission(activity, new Action1<Integer>() {
-                                @Override
-                                public void call(Integer integer) {
-                                    PackageUtil.startApp(SheepApp.mContext, taskEty.getPackage_names());
-                                }
-                            });
+                            PackageUtil.startApp(SheepApp.mContext, taskEty.getPackage_names());
                             break;
                         case 10:
                             Ext ext;

+ 4 - 7
app/src/main/java/com/sheep/gamegroup/view/fragment/FgtTryMakeMoney.java

@@ -79,16 +79,13 @@ public class FgtTryMakeMoney extends BaseFragment implements TryMakeMoneyContrac
     private static final RecyleObj NULL = RecyleObj.make(RecyleType.NONE, null);//空数据
     private void notifyDataSetChanged(){
         checkAndInitView();
-        int position = 0;
+        tryMakeMoneyAdp.clear();
         if(!ListUtil.isEmpty(acceptedEtyList))
-            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RUN_TASK, acceptedEtyList), position++);
+            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RUN_TASK, acceptedEtyList));
         if(!ListUtil.isEmpty(releaseEtyLists))
-            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RELEASE_TASK, releaseEtyLists), position++);
+            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.RELEASE_TASK, releaseEtyLists));
         if(!ListUtil.isEmpty(releaseEtyListsBegin))
-            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.BEGIN_TASK, releaseEtyListsBegin), position++);
-        while(position < LIST_COUNT){
-            tryMakeMoneyAdp.add(NULL, position++);
-        }
+            tryMakeMoneyAdp.add(RecyleObj.make(RecyleType.BEGIN_TASK, releaseEtyListsBegin));
         tryMakeMoneyAdp.refreshAdapter();
         CommonUtil.getInstance().updateEmptyView(empty_view, tryMakeMoneyAdp.isEmpty() && releaseEtyLists.isEmpty() && releaseEtyListsBegin.isEmpty());
         refresh.setRefreshing(false);

+ 180 - 95
app/src/main/java/com/sheep/jiuyan/samllsheep/service/FloatShotScreenService.java

@@ -19,17 +19,22 @@ import android.os.Build;
 import android.os.Handler;
 import android.os.IBinder;
 import android.os.Message;
-import android.util.Log;
+import android.os.SystemClock;
+import android.util.DisplayMetrics;
+import android.view.Display;
+import android.view.GestureDetector;
 import android.view.Gravity;
 import android.view.MotionEvent;
 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.gamegroup.util.LogUtil;
+import com.sheep.gamegroup.view.activity.MiddleScreenShotAct;
 import com.sheep.jiuyan.samllsheep.R;
+import com.sheep.jiuyan.samllsheep.SheepApp;
 import com.sheep.jiuyan.samllsheep.utils.G;
 
 import java.io.File;
@@ -44,7 +49,6 @@ import rx.functions.Action1;
 import rx.functions.Func1;
 import rx.schedulers.Schedulers;
 
-import static com.sheep.gamegroup.util.DataUtil.REQUEST_CAPTURE_PERMISSION;
 
 /**
  * Created by realicing on 2018/5/8.
@@ -58,12 +62,17 @@ public class FloatShotScreenService extends Service {
     public IBinder onBind(Intent intent) {
         return null;
     }
+    private static FloatShotScreenService instance;
+
+    public static FloatShotScreenService getInstance() {
+        return instance;
+    }
 
     @Override
     public void onCreate() {
         super.onCreate();
+        instance = this;
         createFloatView();
-        createImageReader();
     }
 
     @Override
@@ -73,7 +82,7 @@ public class FloatShotScreenService extends Service {
         if (floatView != null) {
             try {
                 if(isAdded)
-                    wm.removeViewImmediate(floatView);
+                    windowManager.removeViewImmediate(floatView);
             } catch (Exception e) {
                 e.printStackTrace();
             }
@@ -83,15 +92,16 @@ public class FloatShotScreenService extends Service {
         tearDownMediaProjection();
     }
 
-    //    public static final String OPERATION = "operation";
     public static final int OPERATION_SHOW = 100;
     public static final int OPERATION_HIDE = 101;
+    public static final int SCREEN_SHOT = 1;//截图回调
+    public static final int SET_SHOW_OR_HIDE = 2;//设置显示或不显示
 
     private static final int HANDLE_CHECK_ACTIVITY = 200;
 
     private boolean isAdded = false; // 是否已增加悬浮窗
-    private static WindowManager wm;
-    private static WindowManager.LayoutParams params;
+    private WindowManager windowManager;
+    private WindowManager.LayoutParams params;
     private View floatView;
 
 
@@ -100,11 +110,10 @@ public class FloatShotScreenService extends Service {
         if (intent != null) {
             int type = intent.getIntExtra("type", 0);
             switch (type) {
-                case 1:
-                    mResultData = (Intent) DataUtil.getInstance().getData(REQUEST_CAPTURE_PERMISSION);
+                case SCREEN_SHOT:
+                    performClick();
                     break;
-                case 0:
-                default:
+                case SET_SHOW_OR_HIDE:
                     if (intent.getBooleanExtra("isShow", true)) {
                         mHandler.sendEmptyMessageDelayed(OPERATION_SHOW, 1000L);
                     } else {
@@ -112,6 +121,8 @@ public class FloatShotScreenService extends Service {
                         mHandler.sendEmptyMessage(OPERATION_HIDE);
                     }
                     break;
+                default:
+                    break;
             }
         }
         return super.onStartCommand(intent, flags, startId);
@@ -138,8 +149,9 @@ public class FloatShotScreenService extends Service {
                     if (!service.isAdded) {
                         try {
 //                            service.floatView.setAlpha(1.0f);
-//                            wm.updateViewLayout(service.floatView, params);
-                            wm.addView(service.floatView, params);
+//                            windowManager.updateViewLayout(service.floatView, params);
+                            service.resetParams();
+                            service.windowManager.addView(service.floatView, service.params);
                         } catch (Exception e) {
                             e.printStackTrace();
                         }
@@ -151,8 +163,8 @@ public class FloatShotScreenService extends Service {
                     if (service.isAdded) {
                         try {
 //                            service.floatView.setAlpha(0.0f);
-//                            wm.updateViewLayout(service.floatView, params);
-                            wm.removeViewImmediate(service.floatView);
+//                            windowManager.updateViewLayout(service.floatView, params);
+                            service.windowManager.removeViewImmediate(service.floatView);
                         } catch (Exception e) {
                             e.printStackTrace();
                         }
@@ -173,7 +185,7 @@ public class FloatShotScreenService extends Service {
         imageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_crop));
         floatView = imageView;
 
-        wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
+        windowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
         params = new WindowManager.LayoutParams();
 
         // 设置window type
@@ -184,12 +196,12 @@ public class FloatShotScreenService extends Service {
 //        }
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
             //  大于等于 24 即为 7.0 及以上执行内容
-            Log.e("qx", "7.0");
+//            Log.e("qx", "7.0");
             params.type = WindowManager.LayoutParams.TYPE_PHONE; // 7.1
         } else {
             //  低于 24 即为 7.0 以下执行内容
             params.type = WindowManager.LayoutParams.TYPE_TOAST;   // 4.4
-            Log.e("qx", "4.4");
+//            Log.e("qx", "4.4");
 
         }
         /*
@@ -204,111 +216,87 @@ public class FloatShotScreenService extends Service {
                 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
         // 设置悬浮窗的Touch监听
         floatView.setOnTouchListener(new View.OnTouchListener() {
-            int lastX, lastY;
-            int paramX, paramY;
-            private boolean isMove = false;
-            private long downTime = 0;
-
+            GestureDetector mGestureDetector = new GestureDetector(getApplicationContext(), new FloatGestureTouchListener());
+            @Override
             public boolean onTouch(View v, MotionEvent event) {
-                switch (event.getAction()) {
-                    case MotionEvent.ACTION_DOWN:
-                        isMove = false;
-                        downTime = System.currentTimeMillis();
-                        lastX = (int) event.getRawX();
-                        lastY = (int) event.getRawY();
-                        paramX = params.x;
-                        paramY = params.y;
-                        break;
-                    case MotionEvent.ACTION_MOVE:
-                        int dx = (int) event.getRawX() - lastX;
-                        int dy = (int) event.getRawY() - lastY;
-                        params.x = paramX + dx;
-                        params.y = paramY + dy;
-                        // 更新悬浮窗位置
-                        wm.updateViewLayout(floatView, params);
-                        isMove = true;
-                        break;
-                    case MotionEvent.ACTION_UP:
-                        if (!isMove && System.currentTimeMillis() - downTime < 1000L) {//没有移动过并且小于1秒就弹起来就点击
-                            performClick();
-                        }
-                        isMove = false;
-                        break;
-                }
-                return false;
+                if(event == null)
+                    v.performClick();
+                return mGestureDetector.onTouchEvent(event);
             }
         });
 
+//        resetParams();
+//
+//        try {
+//            windowManager.addView(floatView, params);
+//            isAdded = true;
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+    }
+
+    private void resetParams() {
+        Display display = windowManager.getDefaultDisplay();
+        DisplayMetrics metric = new DisplayMetrics();
+        display.getMetrics(metric);
         // 设置悬浮窗的长得宽
-        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
+        params.width = SheepApp.getInstance().getResources().getDimensionPixelSize(R.dimen.content_padding_30);
         params.height = WindowManager.LayoutParams.WRAP_CONTENT;
         params.gravity = Gravity.START | Gravity.TOP;
-        params.x = G.WIDTH;
-        params.y = 100;
-
-        try {
-            wm.addView(floatView, params);
-            isAdded = true;
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
+        params.x = metric.widthPixels;
+        params.y = SheepApp.getInstance().getResources().getDimensionPixelSize(R.dimen.content_padding_100);
     }
 
     private void performClick() {
         //获取当前正在运行的程序的包名
-        String packageName = DataUtil.getInstance().getPackageNameInForeground();
-        if(DataUtil.getInstance().getTaskAcceptedEtyFromPackageName(packageName) == null){
-            //TODO 如果一个任务需要截取其它程序的截图,这里就不行了:如收到的短信
-            G.showToast("请切换到对应的应用后进行截图!");
-            return;
-        }
-        packageNameInForeground = packageName;
+        //不判断是否是正在进行的任务上截图,只在上传截图的地方获取指定包名的截图
+        packageNameInForeground = DataUtil.getInstance().getPackageNameInForeground();
         // 设置点击悬浮窗的响应
         startScreenShot();
     }
     private String packageNameInForeground;
 
     private void startScreenShot() {
-
-        floatView.setVisibility(View.GONE);
-
         Handler handler1 = new Handler();
         handler1.postDelayed(new Runnable() {
             public void run() {
-                //start virtual
-                startVirtual();
+                floatView.setVisibility(View.GONE);
             }
-        }, 5);
-
+        }, 0);
         handler1.postDelayed(new Runnable() {
             public void run() {
-                //capture the screen
-                startCapture();
-
+                //start virtual
+                startVirtual();
             }
-        }, 30);
+        }, 5);
 
     }
 
-    private void createImageReader() {
-
-        mImageReader = ImageReader.newInstance(G.WIDTH, G.HEIGHT, PixelFormat.RGBA_8888, 1);
+    private ImageReader createImageReader(DisplayMetrics metric) {
+        if(mImageReader == null) {
+            mImageReader = ImageReader.newInstance(metric.widthPixels, metric.heightPixels, PixelFormat.RGBA_8888, 1);
+        }
+        return mImageReader;
 
     }
 
     public void startVirtual() {
-        if (mMediaProjection != null) {
-            virtualDisplay();
-        } else if (mResultData == null && (mResultData = (Intent) DataUtil.getInstance().getData(REQUEST_CAPTURE_PERMISSION)) == null) {
-            G.showToast("请重新点击开始试玩");
+        if (mResultData == null) {
+            floatView.setVisibility(View.VISIBLE);
+            Intent intent = new Intent(getApplicationContext(), MiddleScreenShotAct.class);
+            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            startActivity(intent);
+            LogUtil.println("FloatShotScreenService", "没有mResultData");
         } else {
-            setUpMediaProjection();
             virtualDisplay();
         }
     }
 
-    public void setUpMediaProjection() {
-        mMediaProjection = getMediaProjectionManager().getMediaProjection(Activity.RESULT_OK, mResultData);
+    public MediaProjection setUpMediaProjection() {
+        if(mMediaProjection == null) {
+            mMediaProjection = getMediaProjectionManager().getMediaProjection(Activity.RESULT_OK, mResultData);
+        }
+        return mMediaProjection;
     }
 
     private MediaProjectionManager getMediaProjectionManager() {
@@ -317,15 +305,20 @@ public class FloatShotScreenService extends Service {
     }
 
     private void virtualDisplay() {
-        mVirtualDisplay = mMediaProjection.createVirtualDisplay("screen-mirror",
-                G.WIDTH, G.HEIGHT, G.DENSITY_DPI, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
-                mImageReader.getSurface(), null, null);
-    }
+        Display display = windowManager.getDefaultDisplay();
+        DisplayMetrics metric = new DisplayMetrics();
+        display.getMetrics(metric);
 
-    private void startCapture() {
+        mImageReader = createImageReader(metric);
+        mVirtualDisplay = setUpMediaProjection().createVirtualDisplay("screen-mirror",
+                metric.widthPixels,  metric.heightPixels, metric.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
+                mImageReader.getSurface(), null, null);
+        SystemClock.sleep(500);
         Image image = mImageReader.acquireLatestImage();
         if (image == null) {
-            startScreenShot();
+            floatView.setVisibility(View.VISIBLE);
+            G.showToast("请稍候重新截图!");
+            LogUtil.println("FloatShotScreenService", "image");
         } else {
             Observable.just(image)
                     .map(new Func1<Image, Bitmap>() {
@@ -341,7 +334,6 @@ public class FloatShotScreenService extends Service {
                         public void call(Bitmap bitmap) {
                             //预览图片
                             if (bitmap != null) {
-                                DataUtil.getInstance().putData(Bitmap.class.getSimpleName(), bitmap);
                                 new GlobalScreenShot(getApplicationContext()).takeScreenshot(bitmap, new GlobalScreenShot.onScreenShotListener() {
                                     @Override
                                     public void onStartShot() {
@@ -409,6 +401,9 @@ public class FloatShotScreenService extends Service {
 
     private Intent mResultData = null;
 
+    public void setResultData(Intent mResultData) {
+        this.mResultData = mResultData;
+    }
 
     private ImageReader mImageReader;
 
@@ -426,4 +421,94 @@ public class FloatShotScreenService extends Service {
         mVirtualDisplay.release();
         mVirtualDisplay = null;
     }
+
+    private class FloatGestureTouchListener extends GestureDetector.SimpleOnGestureListener {
+        int lastX, lastY;
+        int paramX, paramY;
+
+        /**
+         * onDown():刚刚手指接触到触摸屏的那一刹那,就是触的那一下。
+         */
+        @Override
+        public boolean onDown(MotionEvent event) {
+            lastX = (int) event.getRawX();
+            lastY = (int) event.getRawY();
+            paramX = params.x;
+            paramY = params.y;
+            return true;
+        }
+
+        /**
+         * onShowPress():手指按在触摸屏上,它的时间范围:在按下起后,在长按之前
+         */
+        @Override
+        public void onShowPress(MotionEvent e) {
+
+        }
+
+        /**
+         * onScroll():手指在屏幕上滑动
+         */
+        @Override
+        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
+            int dx = (int) e2.getRawX() - lastX;
+            int dy = (int) e2.getRawY() - lastY;
+            params.x = paramX + dx;
+            params.y = paramY + dy;
+            // 更新悬浮窗位置
+            windowManager.updateViewLayout(floatView, params);
+            return true;
+        }
+
+        /**
+         * onFling():手指在触摸屏上迅速移动,并松开的动作(滑动的比onScroll快)
+         */
+        @Override
+        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
+            return false;
+        }
+
+        /**
+         * onLongPress():手指在屏幕上停留一段时间 后抬起
+         */
+        @Override
+        public void onLongPress(MotionEvent e) {
+
+        }
+
+        /**
+         * onSingleTapUp():手指离开view那一瞬间执行
+         */
+        @Override
+        public boolean onSingleTapUp(MotionEvent e) {
+            return super.onSingleTapUp(e);
+        }
+
+        /**
+         * onSingleTapConfirmed() 同上者,但有附加条件,就是Android会确保单击之后短时间内没有再次单击,才会触发该函数。
+         * 举个列子,如果监听双击事件:onSingleTapUp()会被触发两次。但是onSingleTapConfirmed()一次都不会被触发。
+         * 所以,如果你既想监听单击事件,又想监听双击时间,那么请使用onSingleTapConfirmed()函数。
+         */
+        @Override
+        public boolean onSingleTapConfirmed(MotionEvent e) {
+            performClick();
+            return true;
+        }
+
+        /**
+         * 通知DoubleTap手势中的事件,包含down、up和move事件(这里指的是在双击之间发生的事件,例如在同一个地方双击会产生DoubleTap手势,而在DoubleTap手势里面还会发生down和up事件,这两个事件由该函数通知);双击的第二下Touch down和up都会触发,可用e.getAction()区分
+         */
+        @Override
+        public boolean onDoubleTapEvent(MotionEvent e) {
+            return super.onDoubleTapEvent(e);
+        }
+
+        /**
+         * 在双击的第二下,Touch down时触发
+         */
+        @Override
+        public boolean onDoubleTap(MotionEvent e) {
+            return super.onDoubleTap(e);
+        }
+    }
 }

+ 14 - 0
app/src/main/res/layout/act_middle_screen_shot.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="utf-8"?>
+<merge xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent">
+
+    <TextView
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_gravity="center"
+        android:padding="@dimen/dp_10"
+        android:text="截图中"
+        android:textColor="@android:color/black"
+        android:textSize="20sp" />
+</merge>