|
|
@@ -8,6 +8,7 @@ import android.app.usage.UsageStats;
|
|
|
import android.app.usage.UsageStatsManager;
|
|
|
import android.content.Context;
|
|
|
import android.content.Intent;
|
|
|
+import android.graphics.Bitmap;
|
|
|
import android.os.Build;
|
|
|
import android.os.IBinder;
|
|
|
import android.support.annotation.Nullable;
|
|
|
@@ -23,11 +24,12 @@ import com.sheep.jiuyan.samllsheep.R;
|
|
|
import com.sheep.jiuyan.samllsheep.utils.FileUtil;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
import java.util.List;
|
|
|
+import java.util.Locale;
|
|
|
import java.util.TreeMap;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
+import go.kfzssafe.Kfzssafe;
|
|
|
import rx.Observable;
|
|
|
import rx.android.schedulers.AndroidSchedulers;
|
|
|
import rx.functions.Action1;
|
|
|
@@ -44,7 +46,9 @@ public class ListenerShotService extends Service {
|
|
|
@Override
|
|
|
public void onCreate() {
|
|
|
super.onCreate();
|
|
|
+ //初始化获取当前应用的工具类
|
|
|
runningTaskUtil = new RunningTaskUtil(getApplicationContext());
|
|
|
+ //开启监听
|
|
|
manager = ScreenShotListenManager.newInstance(getApplicationContext());
|
|
|
manager.setListener(
|
|
|
new ScreenShotListenManager.OnScreenShotListener() {
|
|
|
@@ -54,23 +58,23 @@ public class ListenerShotService extends Service {
|
|
|
}
|
|
|
);
|
|
|
manager.startListen();
|
|
|
-
|
|
|
+ //发送一个通知栏,保证存活
|
|
|
Notification notification = new Notification();
|
|
|
//通知栏没有展开时的显示内容
|
|
|
notification.icon = R.mipmap.icon;
|
|
|
notification.tickerText = "回到小绵羊";
|
|
|
|
|
|
//下拉通知栏的显示内容
|
|
|
- notification.contentView = new RemoteViews(getPackageName() , R.layout.notification_sheep_shot_screen);
|
|
|
+ notification.contentView = new RemoteViews(getPackageName(), R.layout.notification_sheep_shot_screen);
|
|
|
|
|
|
//点击通知栏跳转到相应的应用里面
|
|
|
- Intent intent = new Intent(this , ActMain.class);
|
|
|
+ Intent intent = new Intent(this, ActMain.class);
|
|
|
//这一句加不加没什么影响
|
|
|
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
- notification.contentIntent = PendingIntent.getActivity(this , 1 , intent , 0);
|
|
|
+ notification.contentIntent = PendingIntent.getActivity(this, 1, intent, 0);
|
|
|
|
|
|
//这里的id不能是0
|
|
|
- startForeground(1 , notification);
|
|
|
+ startForeground(113922, notification);
|
|
|
}
|
|
|
|
|
|
@Nullable
|
|
|
@@ -78,7 +82,9 @@ public class ListenerShotService extends Service {
|
|
|
public IBinder onBind(Intent intent) {
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
private ScreenShotListenManager manager;
|
|
|
+
|
|
|
@Override
|
|
|
public void onDestroy() {
|
|
|
super.onDestroy();
|
|
|
@@ -89,19 +95,21 @@ public class ListenerShotService extends Service {
|
|
|
|
|
|
/**
|
|
|
* 获取前台运行的程序的包名
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
@SuppressLint("NewApi")
|
|
|
public String getPackageNameInForeground1(Context context) {
|
|
|
String topPackageName = runningTaskUtil.getTopRunningTasks();
|
|
|
- if(TextUtils.isEmpty(topPackageName)){
|
|
|
+ if (TextUtils.isEmpty(topPackageName)) {
|
|
|
return getAppProcessInfoInForeground(context);
|
|
|
}
|
|
|
- return topPackageName;
|
|
|
+ return topPackageName;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取前台运行的程序的包名
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
@SuppressLint("NewApi")
|
|
|
@@ -110,7 +118,7 @@ public class ListenerShotService extends Service {
|
|
|
UsageStatsManager mUsageStatsManager = (UsageStatsManager) context.getSystemService(Context.USAGE_STATS_SERVICE);//usagestats
|
|
|
long time = System.currentTimeMillis();
|
|
|
if (mUsageStatsManager != null) {
|
|
|
- List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, time-5000L, time);
|
|
|
+ List<UsageStats> usageStatsList = mUsageStatsManager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, time - 5000L, time);
|
|
|
|
|
|
if (!ListUtil.isEmpty(usageStatsList)) {
|
|
|
TreeMap<Long, UsageStats> mySortedMap = new TreeMap<>();
|
|
|
@@ -142,11 +150,10 @@ public class ListenerShotService extends Service {
|
|
|
|
|
|
public void addPng(final String imagePath) {
|
|
|
final String packageName = getPackageNameInForeground1(getApplicationContext());
|
|
|
- if(TextUtils.isEmpty(packageName)){
|
|
|
+ if (TextUtils.isEmpty(packageName)) {
|
|
|
LogUtil.println("ScreenShotListenManager", "无法获取到当前运行的应用的包名");
|
|
|
return;
|
|
|
}
|
|
|
-// File shotFile = getScreenShotsFile(packageName);
|
|
|
LogUtil.println("ScreenShotListenManager", packageName);
|
|
|
Observable.just(1).delay(1, TimeUnit.SECONDS)
|
|
|
.subscribeOn(Schedulers.io())
|
|
|
@@ -155,15 +162,21 @@ public class ListenerShotService extends Service {
|
|
|
@Override
|
|
|
public void call(Integer integer) {
|
|
|
try {
|
|
|
- FileUtil.copyFileToDir(new File(imagePath), getScreenShotsDir(getApplicationContext(), packageName));
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- LogUtil.println("ScreenShotListenManager", "复制截屏图片失败");
|
|
|
+ File toDir = getScreenShotsDir(getApplicationContext(), packageName);
|
|
|
+ if(Kfzssafe.isPng(imagePath)){
|
|
|
+ FileUtil.copyFileToDir(new File(imagePath), toDir);
|
|
|
+ } else {
|
|
|
+ String toFilePath = String.format(Locale.CHINA, "%s%s%s_%d.png", toDir.getAbsolutePath(), File.separator, packageName, System.currentTimeMillis());
|
|
|
+ FileUtil.convertImage(imagePath, toFilePath, Bitmap.CompressFormat.PNG);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.println("ScreenShotListenManager", "复制截屏图片失败", e.getMessage());
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 截图目录
|
|
|
*
|
|
|
@@ -172,6 +185,7 @@ public class ListenerShotService extends Service {
|
|
|
public static File getScreenShotsDir(Context context) {
|
|
|
return context.getDir("ScreenShots", Context.MODE_PRIVATE);
|
|
|
}
|
|
|
+
|
|
|
/**
|
|
|
* 指定程序的截图目录
|
|
|
*
|
|
|
@@ -179,7 +193,7 @@ public class ListenerShotService extends Service {
|
|
|
*/
|
|
|
public static File getScreenShotsDir(Context context, String packageName) {
|
|
|
File dir = new File(getScreenShotsDir(context), packageName);
|
|
|
- if(!dir.exists())
|
|
|
+ if (!dir.exists())
|
|
|
dir.mkdirs();
|
|
|
return dir;
|
|
|
}
|