update:2021.01.29
fix:增加锁屏管控,增加时间管控,
add:
This commit is contained in:
2021-01-29 16:35:57 +08:00
parent ea2c9676b4
commit c87b898829
17 changed files with 712 additions and 75 deletions

View File

@@ -107,6 +107,14 @@ public class ApkUtils {
return;
}
public static void openApp(Context context, String packageName) {
Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
if (intent != null) {
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
}
/**
* 安装一个apk文件
*/

View File

@@ -215,5 +215,8 @@ public class Configure {
//获取霸屏状态
public final static String GET_DESKTOP = HTTP_TAG_HEAD_NEW + "Sn/getSnDesktop";
//获取默认桌面升级
public final static String GET_SN_TIME_CONTROL = HTTP_TAG_HEAD_NEW + "Sn/getSnTimeControl";
//获取时间管控
public final static String GET_TOP_APP_CONTROL = HTTP_TAG_HEAD_NEW + "Sn/getSnAppControl";
//获取时间管控
}

View File

@@ -0,0 +1,131 @@
package com.mjsheng.myappstore.utils;
import android.app.ActivityManager;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
public class ForegroundAppUtil {
private static final long END_TIME = System.currentTimeMillis();
private static final long TIME_INTERVAL = 7 * 24 * 60 * 60 * 1000L;
private static final long START_TIME = END_TIME - TIME_INTERVAL;
public static final String TOPAPP_KEY = "TOP_ALWAYS_SHOW_APP_NAME";
public static String getForegroundPackageName(Context context) {
//系统应用可以直接获取
ActivityManager mActivityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTaskInfos = mActivityManager.getRunningTasks(1);
return runningTaskInfos.get(0).topActivity.getPackageName();
}
public static void openTopApp(Context context) {
String packages = ForegroundAppUtil.getForegroundPackageName(context);
String topAppName = (String) SPUtils.get(context, ForegroundAppUtil.TOPAPP_KEY, "");
Log.e("openTopApp", "old:" + topAppName);
if (!TextUtils.isEmpty(topAppName)) {
if (!packages.equals(topAppName)) {
ApkUtils.openApp(context, topAppName);
}
} else {
Log.e("openTopApp", ":" + packages);
}
}
/**
* 获取栈顶的应用包名
*/
public static String getForegroundActivityName(Context context) {
String currentClassName = "";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
ActivityManager manager = (ActivityManager) context.getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
currentClassName = manager.getRunningTasks(1).get(0).topActivity.getPackageName();
} else {
UsageStats initStat = getForegroundUsageStats(context, START_TIME, END_TIME);
if (initStat != null) {
currentClassName = initStat.getPackageName();
}
}
return currentClassName;
}
/**
* 判断当前应用是否在前台
*/
public static boolean isForegroundApp(Context context) {
return TextUtils.equals(getForegroundActivityName(context), context.getPackageName());
}
/**
* 获取时间段内,
*/
public static long getTotleForegroundTime(Context context) {
UsageStats usageStats = getCurrentUsageStats(context, START_TIME, END_TIME);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return usageStats != null ? usageStats.getTotalTimeInForeground() : 0;
}
return 0;
}
/**
* 获取记录前台应用的UsageStats对象
*/
private static UsageStats getForegroundUsageStats(Context context, long startTime, long endTime) {
UsageStats usageStatsResult = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
List<UsageStats> usageStatses = getUsageStatsList(context, startTime, endTime);
if (usageStatses == null || usageStatses.isEmpty()) return null;
for (UsageStats usageStats : usageStatses) {
if (usageStatsResult == null || usageStatsResult.getLastTimeUsed() < usageStats.getLastTimeUsed()) {
usageStatsResult = usageStats;
}
}
}
return usageStatsResult;
}
/**
* 获取记录当前应用的UsageStats对象
*/
public static UsageStats getCurrentUsageStats(Context context, long startTime, long endTime) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
List<UsageStats> usageStatses = getUsageStatsList(context, startTime, endTime);
if (usageStatses == null || usageStatses.isEmpty()) return null;
for (UsageStats usageStats : usageStatses) {
if (TextUtils.equals(usageStats.getPackageName(), context.getPackageName())) {
return usageStats;
}
}
}
return null;
}
/**
* 通过UsageStatsManager获取List<UsageStats>集合
*/
public static List<UsageStats> getUsageStatsList(Context context, long startTime, long endTime) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
UsageStatsManager manager = (UsageStatsManager) context.getApplicationContext().getSystemService(Context.USAGE_STATS_SERVICE);
//UsageStatsManager.INTERVAL_WEEKLYUsageStatsManager的参数定义了5个具体查阅源码
List<UsageStats> usageStatses = manager.queryUsageStats(UsageStatsManager.INTERVAL_BEST, startTime, endTime);
if (usageStatses == null || usageStatses.size() == 0) {// 没有权限,获取不到数据
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.getApplicationContext().startActivity(intent);
return null;
}
return usageStatses;
}
return null;
}
}

View File

@@ -0,0 +1,171 @@
package com.mjsheng.myappstore.utils;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import com.mjsheng.myappstore.server.InitJpushServer;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class TimeUtils {
private static DateFormat df = new SimpleDateFormat("HH:mm");
public static final String START_TIME_KEY = "START_TIME";
public static final String END_TIME_KEY = "END_TIME";
public static long dayTime = 60 * 60 * 24 * 1000;
public static String getNowTime() {
long nowTime = System.currentTimeMillis();
DateFormat df = ContralTime.getDf();
return df.format(nowTime);
}
// public static boolean CurrentInTimeScope(ContralTime contralTime) {
// boolean result = true;
// final long aDayInMillis = 1000 * 60 * 60 * 24;
// long currentTimeMillis = System.currentTimeMillis();
//
// }
public static ContralTime String2ContralTime(Context context, @NonNull String timeText) {
DateFormat df = ContralTime.getDf();
String[] time = timeText.trim().split("-");
if (time.length != 2) {
throw new RuntimeException("Time format error!");
}
try {
SPUtils.put(context, START_TIME_KEY, time[0].trim());
SPUtils.put(context, END_TIME_KEY, time[1].trim());
Date startDate = df.parse(time[0].trim());
Date endDate = df.parse(time[1].trim());
ContralTime contralTime = new ContralTime();
// if (date1.getTime() < date2.getTime()) {
contralTime.setStartTime(df.format(startDate));
contralTime.setEndTime(df.format(endDate));
// } else {
// contralTime.setStartTime(df.format(date2));
// contralTime.setEndTime(df.format(date1));
// }
return contralTime;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
public static ContralTime getDefaltContralTime(Context context) {
String startTime = (String) SPUtils.get(context, START_TIME_KEY, "00:00");
String endTime = (String) SPUtils.get(context, END_TIME_KEY, "00:00");
if (null == startTime || null == endTime || (startTime.equals("00:00") && endTime.equals("00:00"))) {
return null;
} else {
try {
Date startDate = df.parse(startTime.trim());
Date endDate = df.parse(endTime.trim());
ContralTime contralTime = new ContralTime();
contralTime.setStartTime(df.format(startDate));
contralTime.setEndTime(df.format(endDate));
return contralTime;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
}
public static void setEmpty(Context context) {
SPUtils.put(context, START_TIME_KEY, "00:00");
SPUtils.put(context, END_TIME_KEY, "00:00");
Intent intent = new Intent();
intent.setAction(InitJpushServer.TimeChangedReceiver.ACTION_UPDATE);
context.sendBroadcast(intent);
}
public static class ContralTime {
//format HH:mm
static String startTime;
static String endTime;
public ContralTime() {
}
public ContralTime(String startT, String endT) {
startTime = startT;
endTime = endT;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startT) {
startTime = startT;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endT) {
endTime = endT;
}
public static DateFormat getDf() {
return df;
}
public static void setDf(DateFormat d) {
df = d;
}
public String getNowTimeString(long time) {
return df.format(new Date(time));
}
public boolean inControlTime(long time) {
return inControlTime(df.format(new Date(time)));
}
public boolean inControlTime(String time) {
if (TextUtils.isEmpty(time)) {
throw new RuntimeException("Time is empty");
} else {
if (!time.contains(":")) {
throw new RuntimeException("Time format error");
}
}
try {
Date startDate = df.parse(startTime);
Date endDate = df.parse(endTime);
Date nowDate = df.parse(time);
if (startDate.getTime() > endDate.getTime()) {
//开始时间大于结束时间 列 1600-0100
endDate.setTime(endDate.getTime() + dayTime);
}
if (nowDate.getTime() >= startDate.getTime() && nowDate.getTime() <= endDate.getTime()) {
return true;
} else {
return false;
}
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
@NonNull
@Override
public String toString() {
return startTime + "\t-\t" + endTime;
}
}
}