version:1.0
fix: update:更换包名
This commit is contained in:
561
app/src/main/java/com/uiui/zyos/utils/ApkUtils.java
Normal file
561
app/src/main/java/com/uiui/zyos/utils/ApkUtils.java
Normal file
@@ -0,0 +1,561 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageInstaller;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.Binder;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import com.uiui.zyos.BuildConfig;
|
||||
import com.uiui.zyos.R;
|
||||
import com.uiui.zyos.bean.DesktopIcon;
|
||||
import com.uiui.zyos.receiver.InstallResultReceiver;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.text.Collator;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.core.Observable;
|
||||
import io.reactivex.rxjava3.core.ObservableEmitter;
|
||||
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.disposables.Disposable;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
|
||||
public class ApkUtils {
|
||||
private static HashSet<String> excludePackageName = new HashSet<String>() {{
|
||||
this.add(BuildConfig.APPLICATION_ID);
|
||||
this.add("org.chromium.browser");
|
||||
this.add("com.sprd.sprdnote");
|
||||
this.add("com.android.deskclock");
|
||||
this.add("com.android.email");
|
||||
// this.add("com.android.calendar");
|
||||
this.add("com.android.uiuios");
|
||||
this.add("com.uiui.os");
|
||||
this.add("com.uiui.health");
|
||||
this.add("com.uiui.appstore");
|
||||
// this.add("com.tencent.android.qqdownloader");
|
||||
// this.add("com.alldocube.store");
|
||||
}};
|
||||
|
||||
private static HashSet<String> excludeClassName = new HashSet<String>() {{
|
||||
this.add("com.android.dialer.app.calllog.CallLogActivity");
|
||||
}};
|
||||
|
||||
private static HashSet<String> showPackageName = new HashSet<String>() {{
|
||||
this.add("com.uiui.sn");
|
||||
this.add("com.android.dialer");
|
||||
this.add("com.android.gallery3d");
|
||||
this.add("com.android.settings");
|
||||
this.add("com.android.messaging");
|
||||
this.add("com.android.camera2");
|
||||
this.add("com.mediatek.camera");
|
||||
this.add("com.android.mms");
|
||||
this.add("com.uiui.city");
|
||||
this.add("com.android.fmradio");
|
||||
this.add("com.android.documentsui");
|
||||
this.add("com.android.calculator2");
|
||||
this.add("cn.wps.moffice_eng");
|
||||
this.add("com.baidu.searchbox.lite");
|
||||
this.add("com.ss.android.article.video");
|
||||
this.add("com.ss.android.ugc.aweme");
|
||||
this.add("com.smile.gifmaker");
|
||||
this.add("com.kuaikan.comic");
|
||||
this.add("com.jxw.launcher");
|
||||
this.add("com.tencent.android.qqdownloader");
|
||||
this.add("com.alldocube.store");
|
||||
this.add("com.android.calendar");
|
||||
}};
|
||||
private static HashSet<String> allHintPackage = new HashSet<String>() {{
|
||||
this.add("com.android.uiuios");
|
||||
}};
|
||||
private static String TAG = ApkUtils.class.getSimpleName();
|
||||
|
||||
public static ArrayList<ApplicationInfo> getSystemApp(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
// 查询所有已经安装的应用程序
|
||||
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的
|
||||
ArrayList<ApplicationInfo> applicationInfos = new ArrayList<>();
|
||||
|
||||
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
|
||||
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
|
||||
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
|
||||
// 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName
|
||||
List<ResolveInfo> resolveinfoList = pm.queryIntentActivities(resolveIntent, 0);
|
||||
Set<String> allowPackages = new HashSet();
|
||||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||||
Log.i(TAG, "queryFilterAppInfo: " + resolveInfo.activityInfo.packageName);
|
||||
allowPackages.add(resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
for (ApplicationInfo app : appInfos) {
|
||||
if (allHintPackage.contains(app.packageName)) continue;
|
||||
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) > 0) {
|
||||
if (allowPackages.contains(app.packageName)
|
||||
&& !showPackageName.contains(app.packageName)) {
|
||||
applicationInfos.add(app);
|
||||
Log.e(TAG, "getSystemApp: " + app.packageName);
|
||||
}
|
||||
}
|
||||
}
|
||||
applicationInfos.sort(new Comparator<ApplicationInfo>() {
|
||||
@Override
|
||||
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
|
||||
return Collator.getInstance(Locale.CHINESE).compare(o1.loadLabel(pm).toString(), o2.loadLabel(pm).toString());
|
||||
// return o1.loadLabel(pm).toString().compareTo(o2.loadLabel(pm).toString());
|
||||
}
|
||||
});
|
||||
applicationInfos.sort(new Comparator<ApplicationInfo>() {
|
||||
@Override
|
||||
public int compare(ApplicationInfo o1, ApplicationInfo o2) {
|
||||
if ((o1.flags & ApplicationInfo.FLAG_SYSTEM) <= (o2.flags & ApplicationInfo.FLAG_SYSTEM)) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
});
|
||||
return applicationInfos;
|
||||
}
|
||||
|
||||
private static List<String> defaultSort = new ArrayList<String>() {{
|
||||
this.add("com.android.dialer");
|
||||
this.add("com.android.messaging");
|
||||
this.add("com.android.gallery3d");
|
||||
this.add("com.android.camera2");
|
||||
this.add("com.android.settings");
|
||||
this.add("com.uiui.sn");
|
||||
this.add("com.uiui.appstore");
|
||||
}};
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static ArrayList<DesktopIcon> queryFilterAppInfo(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
// 查询所有已经安装的应用程序
|
||||
List<ResolveInfo> resolveInfos = new ArrayList<>();
|
||||
|
||||
// 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
|
||||
Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
|
||||
resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
|
||||
// 通过getPackageManager()的queryIntentActivities方法遍历,得到所有能打开的app的packageName
|
||||
List<ResolveInfo> resolveinfoList = pm.queryIntentActivities(resolveIntent, 0);
|
||||
Set<String> allowPackages = new HashSet();
|
||||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||||
Log.i(TAG, "queryFilterAppInfo: " + resolveInfo.activityInfo.packageName);
|
||||
Log.i(TAG, "queryFilterAppInfo: " + resolveInfo.activityInfo.name);
|
||||
allowPackages.add(resolveInfo.activityInfo.packageName);
|
||||
}
|
||||
String appListString = Settings.System.getString(context.getContentResolver(), "only_jgy_shortcut_list");
|
||||
List<String> packageList = new ArrayList<>();
|
||||
if (!TextUtils.isEmpty(appListString)) {
|
||||
packageList = new ArrayList<>(Arrays.asList(appListString.split(",")));
|
||||
}
|
||||
int setting_other_appInstaller = Settings.Global.getInt(context.getContentResolver(), "setting_other_appInstaller", 1);
|
||||
for (ResolveInfo resolveInfo : resolveinfoList) {
|
||||
String pkg = resolveInfo.activityInfo.packageName;
|
||||
if (appIsDisable(context, pkg)) {
|
||||
Log.e(TAG, "queryFilterAppInfo: disable = " + pkg);
|
||||
continue;
|
||||
}
|
||||
if (isSystemApp(context, pkg))//通过flag排除系统应用,会将电话、短信也排除掉
|
||||
{
|
||||
if (showPackageName.contains(pkg)) {
|
||||
resolveInfos.add(resolveInfo);
|
||||
}
|
||||
} else {
|
||||
if (setting_other_appInstaller == 0) {//不显示自己安装的
|
||||
if (packageList.contains(pkg)) {
|
||||
resolveInfos.add(resolveInfo);
|
||||
}
|
||||
} else {
|
||||
if (allowPackages.contains(pkg) && !excludePackageName.contains(pkg)) {
|
||||
resolveInfos.add(resolveInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Settings.Global.getInt(context.getContentResolver(), "is_activity", 0) == 0) {
|
||||
resolveInfos.removeIf(resolveInfo -> "com.uiui.city".equals(resolveInfo.activityInfo.packageName));
|
||||
// resolveInfos.removeIf(applicationInfo -> "com.uiui.sn".equals(applicationInfo.packageName));
|
||||
}
|
||||
resolveInfos.sort(new Comparator<ResolveInfo>() {
|
||||
@Override
|
||||
public int compare(ResolveInfo o1, ResolveInfo o2) {
|
||||
return Collator.getInstance(Locale.CHINESE).compare(o1.loadLabel(pm).toString(), o2.loadLabel(pm).toString());
|
||||
// return o1.loadLabel(pm).toString().compareTo(o2.loadLabel(pm).toString());
|
||||
}
|
||||
});
|
||||
resolveInfos.sort(new Comparator<ResolveInfo>() {
|
||||
@Override
|
||||
public int compare(ResolveInfo o1, ResolveInfo o2) {
|
||||
try {
|
||||
if ((pm.getApplicationInfo(o1.activityInfo.packageName, 0).flags & ApplicationInfo.FLAG_SYSTEM) <= (pm.getApplicationInfo(o2.activityInfo.packageName, 0).flags & ApplicationInfo.FLAG_SYSTEM)) {
|
||||
return 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
});
|
||||
ArrayList<DesktopIcon> desktopIcons = new ArrayList<>();
|
||||
for (ResolveInfo applicationInfo : resolveInfos) {
|
||||
if (!excludeClassName.contains(applicationInfo.activityInfo.name)) {
|
||||
desktopIcons.add(DesktopIcon.creatDesktopIcon(context, applicationInfo));
|
||||
}
|
||||
}
|
||||
DesktopIcon familyIcon = new DesktopIcon();
|
||||
familyIcon.setIcon(context.getDrawable(R.drawable.icon_family_space));
|
||||
familyIcon.setLable("家庭空间");
|
||||
familyIcon.setPackageName("aios.family");
|
||||
desktopIcons.add(0, familyIcon);
|
||||
|
||||
DesktopIcon exitIcon = new DesktopIcon();
|
||||
exitIcon.setIcon(context.getDrawable(R.drawable.exit_icon));
|
||||
exitIcon.setLable("切换系统");
|
||||
exitIcon.setPackageName("aios.exit");
|
||||
desktopIcons.add(exitIcon);
|
||||
return desktopIcons;
|
||||
}
|
||||
|
||||
public static boolean isSystemApp(Context context, String pkg) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ApplicationInfo applicationInfo = null;
|
||||
try {
|
||||
applicationInfo = pm.getApplicationInfo(pkg, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (applicationInfo == null) return false;
|
||||
return (applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0;
|
||||
}
|
||||
|
||||
public static boolean appIsDisable(Context context, String pkg) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
return pm.getApplicationEnabledSetting(pkg) == PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
|
||||
}
|
||||
|
||||
public static boolean disableApp(Context context, String pkg) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
try {
|
||||
pm.setApplicationEnabledSetting(pkg, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取第三方应用
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static List<String> queryFilterAppList(Context context) {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
// 查询所有已经安装的应用程序
|
||||
List<ApplicationInfo> appInfos = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);// GET_UNINSTALLED_PACKAGES代表已删除,但还有安装目录的
|
||||
List<String> applicationInfos = new ArrayList<>();
|
||||
for (ApplicationInfo app : appInfos) {
|
||||
// Log.e("queryFilterAppInfo", String.valueOf(app.flags));
|
||||
// Log.e("queryFilterAppInfo", String.valueOf((app.flags & mask)));
|
||||
if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
|
||||
//通过flag排除系统应用,会将电话、短信也排除掉
|
||||
} else {
|
||||
applicationInfos.add(app.packageName);
|
||||
Log.e("queryFilterAppInfo", app.packageName);
|
||||
}
|
||||
}
|
||||
return applicationInfos;
|
||||
}
|
||||
|
||||
public static PackageInfo getPackageInfo(Context context, String pkg) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo packageInfo = null;
|
||||
try {
|
||||
packageInfo = packageManager.getPackageInfo(pkg, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return packageInfo;
|
||||
}
|
||||
|
||||
public static ApplicationInfo getApplicationInfo(Context context, String pkg) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
ApplicationInfo applicationInfo = null;
|
||||
try {
|
||||
applicationInfo = packageManager.getApplicationInfo(pkg, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return applicationInfo;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean openPackage(Context context, String packageName) {
|
||||
Context pkgContext = getPackageContext(context, packageName);
|
||||
Intent intent = getAppOpenIntentByPackageName(context, packageName);
|
||||
if (pkgContext != null && intent != null) {
|
||||
pkgContext.startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean openPackage(Context context, String packageName, String className) {
|
||||
if (TextUtils.isEmpty(className)) {
|
||||
return openPackage(context, packageName);
|
||||
}
|
||||
ComponentName cn = new ComponentName(packageName, className);
|
||||
Intent intent = new Intent();
|
||||
intent.setComponent(cn);
|
||||
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
if (context != null) {
|
||||
context.startActivity(intent);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Context getPackageContext(Context context, String packageName) {
|
||||
Context pkgContext = null;
|
||||
if (context.getPackageName().equals(packageName)) {
|
||||
pkgContext = context;
|
||||
} else {
|
||||
// 创建第三方应用的上下文环境
|
||||
try {
|
||||
pkgContext = context.createPackageContext(packageName,
|
||||
Context.CONTEXT_IGNORE_SECURITY
|
||||
| Context.CONTEXT_INCLUDE_CODE);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
return pkgContext;
|
||||
}
|
||||
|
||||
public static Intent getAppOpenIntentByPackageName(Context context, String packageName) {
|
||||
//Activity完整名
|
||||
String mainAct = null;
|
||||
//根据包名寻找
|
||||
PackageManager pkgMag = context.getPackageManager();
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
intent.addCategory(Intent.CATEGORY_LAUNCHER);
|
||||
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
|
||||
List<ResolveInfo> list = pkgMag.queryIntentActivities(intent,
|
||||
PackageManager.GET_ACTIVITIES);
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
ResolveInfo info = list.get(i);
|
||||
if (info.activityInfo.packageName.equals(packageName)) {
|
||||
mainAct = info.activityInfo.name;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (TextUtils.isEmpty(mainAct)) {
|
||||
return null;
|
||||
}
|
||||
intent.setComponent(new ComponentName(packageName, mainAct));
|
||||
return intent;
|
||||
}
|
||||
|
||||
public static String getAPPVersionName(Context context, String packageName) {
|
||||
String versionName = "0";
|
||||
|
||||
if (TextUtils.isEmpty(packageName)) {
|
||||
return versionName;
|
||||
}
|
||||
PackageManager pm = context.getPackageManager();
|
||||
try {
|
||||
PackageInfo packageInfo = pm.getPackageInfo(packageName, 0);
|
||||
versionName = packageInfo.versionName;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return versionName;
|
||||
}
|
||||
|
||||
public static String getAppNameByPackage(Context context, String pkg) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
try {
|
||||
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(pkg, PackageManager.GET_META_DATA);
|
||||
String packageName = packageManager.getApplicationLabel(applicationInfo).toString();
|
||||
return packageName;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查手机上是否安装了指定的软件
|
||||
*/
|
||||
public static boolean isAvailable(Context context, String packageName) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo info = null;
|
||||
try {
|
||||
info = packageManager.getPackageInfo(packageName, 0);
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return info != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查手机上是否安装了指定的软件
|
||||
*/
|
||||
public static boolean isAvailable(Context context, File file) {
|
||||
return isAvailable(context, getPackageName(context, file.getAbsolutePath()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件路径获取包名
|
||||
*/
|
||||
public static String getPackageName(Context context, String filePath) {
|
||||
PackageManager packageManager = context.getPackageManager();
|
||||
PackageInfo info = packageManager.getPackageArchiveInfo(filePath, PackageManager.GET_ACTIVITIES);
|
||||
if (info != null) {
|
||||
ApplicationInfo appInfo = info.applicationInfo;
|
||||
return appInfo.packageName; //得到安装包名称
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 静默卸载应用
|
||||
*
|
||||
* @param context
|
||||
* @param pkg
|
||||
*/
|
||||
public static void UninstallAPP(Context context, String pkg) {
|
||||
Observable.create(new ObservableOnSubscribe<String>() {
|
||||
@Override
|
||||
public void subscribe(ObservableEmitter<String> emitter) throws Exception {
|
||||
Log.e("UninstallAPP", "call: " + Thread.currentThread().getName());
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
ApkUtils.uninstall(context, pkg);
|
||||
} else {
|
||||
ApkUtils.deleteApkInSilence(pkg);
|
||||
}
|
||||
emitter.onNext(pkg);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(new Observer<String>() {
|
||||
@Override
|
||||
public void onSubscribe(Disposable d) {
|
||||
Log.e("UninstallAPP", "onSubscribe: ");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNext(String s) {
|
||||
Log.e("UninstallAPP", "onNext: " + Thread.currentThread().getName());
|
||||
Log.e("UninstallAPP", "onNext: " + s);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Throwable e) {
|
||||
Log.e("UninstallAPP", "onError: " + e.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onComplete() {
|
||||
Log.e("UninstallAPP", "onComplete: ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据包名卸载应用
|
||||
*
|
||||
* @param packageName 包名
|
||||
*/
|
||||
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
||||
public static void uninstall(Context context, String packageName) {
|
||||
Intent broadcastIntent = new Intent(context, InstallResultReceiver.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
|
||||
broadcastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
PackageInstaller packageInstaller = context.getPackageManager().getPackageInstaller();
|
||||
packageInstaller.uninstall(packageName, pendingIntent.getIntentSender());
|
||||
}
|
||||
|
||||
public static void deleteApkInSilence(String packageName) {
|
||||
Class<?> pmService;
|
||||
Class<?> activityTherad;
|
||||
Method method;
|
||||
try {
|
||||
activityTherad = Class.forName("android.app.ActivityThread");
|
||||
Class<?> paramTypes[] = getParamTypes(activityTherad, "getPackageManager");
|
||||
method = activityTherad.getMethod("getPackageManager", paramTypes);
|
||||
Object PackageManagerService = method.invoke(activityTherad);
|
||||
pmService = PackageManagerService.getClass();
|
||||
Class<?> paramTypes1[] = getParamTypes(pmService, "deletePackageAsUser");
|
||||
method = pmService.getMethod("deletePackageAsUser", paramTypes1);
|
||||
//getUserId
|
||||
method.invoke(PackageManagerService, packageName, null, getUserId(Binder.getCallingUid()), 0x00000040);
|
||||
} catch (ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static Class<?>[] getParamTypes(Class<?> cls, String mName) {
|
||||
Class<?> cs[] = null;
|
||||
Method[] mtd = cls.getMethods();
|
||||
for (int i = 0; i < mtd.length; i++) {
|
||||
if (!mtd[i].getName().equals(mName)) {
|
||||
continue;
|
||||
}
|
||||
cs = mtd[i].getParameterTypes();
|
||||
}
|
||||
return cs;
|
||||
}
|
||||
|
||||
public static final int PER_USER_RANGE = 100000;
|
||||
|
||||
public static int getUserId(int uid) {
|
||||
return uid / PER_USER_RANGE;
|
||||
}
|
||||
}
|
||||
168
app/src/main/java/com/uiui/zyos/utils/AppUsedTimeUtils.java
Normal file
168
app/src/main/java/com/uiui/zyos/utils/AppUsedTimeUtils.java
Normal file
@@ -0,0 +1,168 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.provider.Settings;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonParser;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Type;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
public class AppUsedTimeUtils {
|
||||
private static final String TAG = AppUsedTimeUtils.class.getSimpleName();
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static AppUsedTimeUtils sInstance;
|
||||
private Context mContext;
|
||||
|
||||
private SimpleDateFormat ruleSDF = new SimpleDateFormat("HH:mm:ss");
|
||||
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
|
||||
private AppTimeinfo appTimeinfo;
|
||||
|
||||
private AppUsedTimeUtils(Context context) {
|
||||
this.mContext = context;
|
||||
appTimeinfo = getAppTimeinfo();
|
||||
}
|
||||
|
||||
public static void init(Context context) {
|
||||
if (sInstance == null) {
|
||||
sInstance = new AppUsedTimeUtils(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static AppUsedTimeUtils getInstance() {
|
||||
if (sInstance == null) {
|
||||
throw new IllegalStateException("You must be init TimeUtils first");
|
||||
}
|
||||
return sInstance;
|
||||
}
|
||||
|
||||
private static String normalStartTime = "8:00:00";
|
||||
private static String unusualStartTime = "22:00:00";
|
||||
|
||||
static class AppTimeinfo implements Serializable {
|
||||
private static final long serialVersionUID = 5373751133823666192L;
|
||||
|
||||
AppTimeinfo() {
|
||||
this.appPackageName = "";
|
||||
this.endTime = 0;
|
||||
this.startTime = 0;
|
||||
}
|
||||
|
||||
private String appPackageName;
|
||||
private long endTime;
|
||||
private long startTime;
|
||||
|
||||
public String getAppPackageName() {
|
||||
return appPackageName;
|
||||
}
|
||||
|
||||
public void setAppPackageName(String appPackageName) {
|
||||
this.appPackageName = appPackageName;
|
||||
}
|
||||
|
||||
public long getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
public void setEndTime(long endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
public long getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
public void setStartTime(long startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public String toString() {
|
||||
return JsonParser.parseString(new Gson().toJson(this)).getAsJsonObject().toString();
|
||||
}
|
||||
}
|
||||
|
||||
synchronized public void setAppPackageName(String name) {
|
||||
appTimeinfo.setAppPackageName(name);
|
||||
setAppTimeinfo();
|
||||
}
|
||||
|
||||
synchronized public String getAppPackageName() {
|
||||
return appTimeinfo.getAppPackageName();
|
||||
}
|
||||
|
||||
synchronized public void setStartTime(long time) {
|
||||
appTimeinfo.setStartTime(time);
|
||||
setAppTimeinfo();
|
||||
}
|
||||
|
||||
synchronized public long getStartTime() {
|
||||
return appTimeinfo.getStartTime();
|
||||
}
|
||||
|
||||
synchronized public void setEndTime(long time) {
|
||||
appTimeinfo.setEndTime(time);
|
||||
setAppTimeinfo();
|
||||
}
|
||||
|
||||
synchronized public long getEndTime() {
|
||||
return appTimeinfo.getEndTime();
|
||||
}
|
||||
|
||||
synchronized private AppTimeinfo getAppTimeinfo() {
|
||||
String jsonString = Settings.System.getString(mContext.getContentResolver(), "runningAppInfo");
|
||||
if (TextUtils.isEmpty(jsonString)) {
|
||||
return new AppTimeinfo();
|
||||
}
|
||||
Log.e(TAG, "getAppTimeinfo: " + jsonString);
|
||||
Type type = new TypeToken<AppTimeinfo>() {
|
||||
}.getType();
|
||||
Gson gson = new Gson();
|
||||
AppTimeinfo appTimeinfo = gson.fromJson(jsonString, type);
|
||||
return appTimeinfo;
|
||||
}
|
||||
|
||||
synchronized private void setAppTimeinfo() {
|
||||
String jsonString = JsonParser.parseString(appTimeinfo.toString()).getAsJsonObject().toString();
|
||||
Settings.System.putString(mContext.getContentResolver(), "runningAppInfo", jsonString);
|
||||
}
|
||||
|
||||
private static final long DAY_TIME = 1000 * 60 * 60 * 24;
|
||||
|
||||
public boolean isNormalTime() {
|
||||
long nowTime = System.currentTimeMillis();
|
||||
String nowTimeString = ruleSDF.format(new Date(nowTime)); // 时间戳转换日期
|
||||
try {
|
||||
Date startDate = ruleSDF.parse(normalStartTime);
|
||||
Date endDate = ruleSDF.parse(unusualStartTime);
|
||||
Date now = ruleSDF.parse(nowTimeString);
|
||||
Log.e(TAG, "isScreenshot: startDate = " + startDate);
|
||||
Log.e(TAG, "isScreenshot: endDate = " + endDate);
|
||||
Log.e(TAG, "isScreenshot: now = " + now);
|
||||
if (startDate.getTime() <= now.getTime() && now.getTime() <= endDate.getTime()) {
|
||||
return true;
|
||||
} else if (endDate.getTime() < now.getTime() && now.getTime() <= startDate.getTime() + DAY_TIME) {
|
||||
return false;
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, "isScreenshot: " + e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
91
app/src/main/java/com/uiui/zyos/utils/AppUtil.java
Normal file
91
app/src/main/java/com/uiui/zyos/utils/AppUtil.java
Normal file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
* Copyright (C) 2012 www.amsoft.cn
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileReader;
|
||||
|
||||
public class AppUtil {
|
||||
private static String TAG = AppUtil.class.getSimpleName();
|
||||
|
||||
/**
|
||||
* 描述:获取可用内存.
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static long getAvailMemory(Context context) {
|
||||
// 获取android当前可用内存大小
|
||||
ActivityManager activityManager = (ActivityManager) context
|
||||
.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
|
||||
activityManager.getMemoryInfo(memoryInfo);
|
||||
// 当前系统可用内存 ,将获得的内存大小规格化
|
||||
|
||||
return memoryInfo.availMem;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @return 可用的内存大小
|
||||
*/
|
||||
public static long getFreeMemory(Context context) {
|
||||
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
|
||||
activityManager.getMemoryInfo(memoryInfo);
|
||||
long freeMem = memoryInfo.totalMem - memoryInfo.availMem;
|
||||
// Log.e("getHardware", "getFreeMemory: " + freeMem);
|
||||
return freeMem;
|
||||
}
|
||||
|
||||
/**
|
||||
* 描述:总内存.
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static long getTotalMemory(Context context) {
|
||||
// 系统内存信息文件
|
||||
String file = "/proc/meminfo";
|
||||
String memInfo;
|
||||
String[] strs;
|
||||
long memory = 0;
|
||||
|
||||
try {
|
||||
FileReader fileReader = new FileReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader, 8192);
|
||||
// 读取meminfo第一行,系统内存大小
|
||||
memInfo = bufferedReader.readLine();
|
||||
strs = memInfo.split("\\s+");
|
||||
for (String str : strs) {
|
||||
Log.e(TAG, "getTotalMemory: " + str + "\t");
|
||||
}
|
||||
// 获得系统总内存,单位KB
|
||||
memory = Integer.valueOf(strs[1]).intValue();
|
||||
bufferedReader.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Byte转位KB或MB
|
||||
return memory * 1024;
|
||||
}
|
||||
|
||||
}
|
||||
72
app/src/main/java/com/uiui/zyos/utils/BitmapUtils.java
Normal file
72
app/src/main/java/com/uiui/zyos/utils/BitmapUtils.java
Normal file
@@ -0,0 +1,72 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.drawable.AdaptiveIconDrawable;
|
||||
import android.graphics.drawable.BitmapDrawable;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class BitmapUtils {
|
||||
public static Bitmap Bytes2Bimap(byte[] b) {
|
||||
if (b.length != 0) {
|
||||
return BitmapFactory.decodeByteArray(b, 0, b.length);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static byte[] Bitmap2Bytes(Bitmap bitmap) {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
|
||||
byte[] data = baos.toByteArray();
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Drawable转换成一个Bitmap
|
||||
*
|
||||
* @param drawable drawable对象
|
||||
* @return
|
||||
*/
|
||||
public static final Bitmap drawableToBitmap(Drawable drawable) {
|
||||
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
|
||||
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
|
||||
drawable.draw(canvas);
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
|
||||
public static Bitmap drawableToBitamp(Drawable drawable) {
|
||||
Bitmap bitmap;
|
||||
BitmapDrawable bd = (BitmapDrawable) drawable;
|
||||
bitmap = bd.getBitmap();
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public static Bitmap getIconBitmap(Context context, Drawable drawable) {
|
||||
try {
|
||||
if (drawable == null) {
|
||||
return null;
|
||||
}
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && drawable instanceof AdaptiveIconDrawable) {
|
||||
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
|
||||
drawable.draw(canvas);
|
||||
return Utils.getRoundedBitmap(bitmap, context);
|
||||
} else {
|
||||
return Utils.getRoundedBitmap(((BitmapDrawable) drawable).getBitmap(), context);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
142
app/src/main/java/com/uiui/zyos/utils/BrightnessUtils.java
Normal file
142
app/src/main/java/com/uiui/zyos/utils/BrightnessUtils.java
Normal file
@@ -0,0 +1,142 @@
|
||||
|
||||
/*
|
||||
* Copyright (C) 2018 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.util.MathUtils;
|
||||
|
||||
public class BrightnessUtils {
|
||||
|
||||
public static final int GAMMA_SPACE_MIN = 0;
|
||||
public static final int GAMMA_SPACE_MAX = 65535;
|
||||
|
||||
// Hybrid Log Gamma constant values
|
||||
private static final float R = 0.5f;
|
||||
private static final float A = 0.17883277f;
|
||||
private static final float B = 0.28466892f;
|
||||
private static final float C = 0.55991073f;
|
||||
|
||||
/**
|
||||
* A function for converting from the gamma space that the slider works in to the
|
||||
* linear space that the setting works in.
|
||||
* <p>
|
||||
* The gamma space effectively provides us a way to make linear changes to the slider that
|
||||
* result in linear changes in perception. If we made changes to the slider in the linear space
|
||||
* then we'd see an approximately logarithmic change in perception (c.f. Fechner's Law).
|
||||
* <p>
|
||||
* Internally, this implements the Hybrid Log Gamma electro-optical transfer function, which is
|
||||
* a slight improvement to the typical gamma transfer function for displays whose max
|
||||
* brightness exceeds the 120 nit reference point, but doesn't set a specific reference
|
||||
* brightness like the PQ function does.
|
||||
* <p>
|
||||
* Note that this transfer function is only valid if the display's backlight value is a linear
|
||||
* control. If it's calibrated to be something non-linear, then a different transfer function
|
||||
* should be used.
|
||||
*
|
||||
* @param val The slider value.
|
||||
* @param min The minimum acceptable value for the setting.
|
||||
* @param max The maximum acceptable value for the setting.
|
||||
* @return The corresponding setting value.
|
||||
*/
|
||||
public static final int convertGammaToLinear(int val, int min, int max) {
|
||||
final float normalizedVal = MathUtils.norm(GAMMA_SPACE_MIN, GAMMA_SPACE_MAX, val);
|
||||
final float ret;
|
||||
if (normalizedVal <= R) {
|
||||
ret = MathUtils.sq(normalizedVal / R);
|
||||
} else {
|
||||
ret = MathUtils.exp((normalizedVal - C) / A) + B;
|
||||
}
|
||||
|
||||
// HLG is normalized to the range [0, 12], so we need to re-normalize to the range [0, 1]
|
||||
// in order to derive the correct setting value.
|
||||
return Math.round(MathUtils.lerp(min, max, ret / 12));
|
||||
}
|
||||
|
||||
/**
|
||||
* Version of {@link #convertGammaToLinear} that takes and returns float values.
|
||||
* TODO(flc): refactor Android Auto to use float version
|
||||
*
|
||||
* @param val The slider value.
|
||||
* @param min The minimum acceptable value for the setting.
|
||||
* @param max The maximum acceptable value for the setting.
|
||||
* @return The corresponding setting value.
|
||||
*/
|
||||
public static final float convertGammaToLinearFloat(int val, float min, float max) {
|
||||
final float normalizedVal = MathUtils.norm(GAMMA_SPACE_MIN, GAMMA_SPACE_MAX, val);
|
||||
final float ret;
|
||||
if (normalizedVal <= R) {
|
||||
ret = MathUtils.sq(normalizedVal / R);
|
||||
} else {
|
||||
ret = MathUtils.exp((normalizedVal - C) / A) + B;
|
||||
}
|
||||
|
||||
// HLG is normalized to the range [0, 12], ensure that value is within that range,
|
||||
// it shouldn't be out of bounds.
|
||||
final float normalizedRet = MathUtils.constrain(ret, 0, 12);
|
||||
|
||||
// Re-normalize to the range [0, 1]
|
||||
// in order to derive the correct setting value.
|
||||
return MathUtils.lerp(min, max, normalizedRet / 12);
|
||||
}
|
||||
|
||||
/**
|
||||
* A function for converting from the linear space that the setting works in to the
|
||||
* gamma space that the slider works in.
|
||||
* <p>
|
||||
* The gamma space effectively provides us a way to make linear changes to the slider that
|
||||
* result in linear changes in perception. If we made changes to the slider in the linear space
|
||||
* then we'd see an approximately logarithmic change in perception (c.f. Fechner's Law).
|
||||
* <p>
|
||||
* Internally, this implements the Hybrid Log Gamma opto-electronic transfer function, which is
|
||||
* a slight improvement to the typical gamma transfer function for displays whose max
|
||||
* brightness exceeds the 120 nit reference point, but doesn't set a specific reference
|
||||
* brightness like the PQ function does.
|
||||
* <p>
|
||||
* Note that this transfer function is only valid if the display's backlight value is a linear
|
||||
* control. If it's calibrated to be something non-linear, then a different transfer function
|
||||
* should be used.
|
||||
*
|
||||
* @param val The brightness setting value.
|
||||
* @param min The minimum acceptable value for the setting.
|
||||
* @param max The maximum acceptable value for the setting.
|
||||
* @return The corresponding slider value
|
||||
*/
|
||||
public static final int convertLinearToGamma(int val, int min, int max) {
|
||||
return convertLinearToGammaFloat((float) val, (float) min, (float) max);
|
||||
}
|
||||
|
||||
/**
|
||||
* Version of {@link #convertLinearToGamma} that takes float values.
|
||||
* TODO: brightnessfloat merge with above method(?)
|
||||
*
|
||||
* @param val The brightness setting value.
|
||||
* @param min The minimum acceptable value for the setting.
|
||||
* @param max The maximum acceptable value for the setting.
|
||||
* @return The corresponding slider value
|
||||
*/
|
||||
public static final int convertLinearToGammaFloat(float val, float min, float max) {
|
||||
// For some reason, HLG normalizes to the range [0, 12] rather than [0, 1]
|
||||
final float normalizedVal = MathUtils.norm(min, max, val) * 12;
|
||||
final float ret;
|
||||
if (normalizedVal <= 1f) {
|
||||
ret = MathUtils.sqrt(normalizedVal) * R;
|
||||
} else {
|
||||
ret = A * MathUtils.log(normalizedVal - B) + C;
|
||||
}
|
||||
|
||||
return Math.round(MathUtils.lerp(GAMMA_SPACE_MIN, GAMMA_SPACE_MAX, ret));
|
||||
}
|
||||
}
|
||||
103
app/src/main/java/com/uiui/zyos/utils/CmdUtil.java
Normal file
103
app/src/main/java/com/uiui/zyos/utils/CmdUtil.java
Normal file
@@ -0,0 +1,103 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class CmdUtil {
|
||||
private static final String TAG = "CmdUtil";
|
||||
|
||||
private static final String COMMAND_SH = "sh";
|
||||
private static final String COMMAND_EXIT = "exit\n";
|
||||
private static final String COMMAND_LINE_END = "\n";
|
||||
|
||||
|
||||
/**
|
||||
* 运行命令
|
||||
*
|
||||
* @param command 命令
|
||||
* @return 结果
|
||||
*/
|
||||
public static Result execute(String command) {
|
||||
Log.i(TAG, "execute() command = " + command);
|
||||
Result result = new Result();
|
||||
|
||||
if (TextUtils.isEmpty(command)) {
|
||||
Log.w(TAG, "WARNING: command should not be null or empty");
|
||||
return result;
|
||||
}
|
||||
|
||||
Process process = null;
|
||||
DataOutputStream dos = null;
|
||||
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(COMMAND_SH);
|
||||
dos = new DataOutputStream(process.getOutputStream());
|
||||
dos.write(command.trim().getBytes());
|
||||
dos.writeBytes(COMMAND_LINE_END);
|
||||
dos.flush();
|
||||
dos.writeBytes(COMMAND_EXIT);
|
||||
dos.flush();
|
||||
result.code = process.waitFor();
|
||||
result.success = readBuffer(new BufferedReader(new InputStreamReader(process.getInputStream())));
|
||||
result.error = readBuffer(new BufferedReader(new InputStreamReader(process.getErrorStream())));
|
||||
Log.i(TAG, "result = " + result);
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
Log.e(TAG, ioe.getMessage());
|
||||
} catch (InterruptedException ie) {
|
||||
ie.printStackTrace();
|
||||
Log.e(TAG, ie.getMessage());
|
||||
} finally {
|
||||
try {
|
||||
if (null != dos) {
|
||||
dos.close();
|
||||
}
|
||||
} catch (IOException ioe) {
|
||||
ioe.printStackTrace();
|
||||
Log.e(TAG, ioe.getMessage());
|
||||
}
|
||||
if (null != process) {
|
||||
process.destroy();
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String readBuffer(BufferedReader bufferedReader) throws IOException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String s;
|
||||
while ((s = bufferedReader.readLine()) != null) {
|
||||
sb.append(s);
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Command执行结果
|
||||
*/
|
||||
public static final class Result {
|
||||
|
||||
public static final int SUCCESS = 0;
|
||||
public static final int ERROR = -1;
|
||||
|
||||
public int code = ERROR;
|
||||
public String error;
|
||||
public String success;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Result{" +
|
||||
"code=" + code +
|
||||
", error='" + error + '\'' +
|
||||
", success='" + success + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
38
app/src/main/java/com/uiui/zyos/utils/DataUtil.java
Normal file
38
app/src/main/java/com/uiui/zyos/utils/DataUtil.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 作者 mjsheng
|
||||
* 日期 2018/8/31 09:50
|
||||
* 邮箱 501802639@qq.com
|
||||
* 来自:
|
||||
*/
|
||||
|
||||
public class DataUtil {
|
||||
private static SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd HH:mm");
|
||||
private static SimpleDateFormat hour = new SimpleDateFormat("HH:mm");
|
||||
private static SimpleDateFormat minute = new SimpleDateFormat("mm");
|
||||
|
||||
/**
|
||||
* 格式化日期(精确到天)
|
||||
*/
|
||||
public static String formatDateDay() {
|
||||
return day.format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期(hour)
|
||||
*/
|
||||
public static String formatDateHour() {
|
||||
return hour.format(new Date());
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期(minute)
|
||||
*/
|
||||
public static String formatDateMinute() {
|
||||
return minute.format(new Date());
|
||||
}
|
||||
}
|
||||
56
app/src/main/java/com/uiui/zyos/utils/FFmpegUtils.java
Normal file
56
app/src/main/java/com/uiui/zyos/utils/FFmpegUtils.java
Normal file
@@ -0,0 +1,56 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
|
||||
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.rxjava3.annotations.NonNull;
|
||||
import io.reactivex.rxjava3.core.Observable;
|
||||
import io.reactivex.rxjava3.core.ObservableEmitter;
|
||||
import io.reactivex.rxjava3.core.ObservableOnSubscribe;
|
||||
import io.reactivex.rxjava3.core.Observer;
|
||||
import io.reactivex.rxjava3.schedulers.Schedulers;
|
||||
import wseemann.media.FFmpegMediaMetadataRetriever;
|
||||
|
||||
public class FFmpegUtils {
|
||||
/**
|
||||
* 获取在线音频时间长度
|
||||
*
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
public static void getDurationInMilliseconds(String url, Observer<Integer> observer) {
|
||||
Observable.create(new ObservableOnSubscribe<Integer>() {
|
||||
@Override
|
||||
public void subscribe(@NonNull ObservableEmitter<Integer> emitter) throws Throwable {
|
||||
long time = System.currentTimeMillis();
|
||||
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
|
||||
mmr.setDataSource(url);
|
||||
int duration = Integer.parseInt(mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_DURATION));
|
||||
Log.e("AudioUtils", "getDurationInMilliseconds: " + (System.currentTimeMillis() - time));
|
||||
mmr.release();//释放资源
|
||||
emitter.onNext(duration / 1000);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(observer);
|
||||
}
|
||||
|
||||
public static void loadVideoScreenshot(String url, Observer<Bitmap> observer) {
|
||||
Observable.create(new ObservableOnSubscribe<Bitmap>() {
|
||||
@Override
|
||||
public void subscribe(@NonNull ObservableEmitter<Bitmap> emitter) throws Throwable {
|
||||
FFmpegMediaMetadataRetriever mmr = new FFmpegMediaMetadataRetriever();
|
||||
mmr.setDataSource(url);
|
||||
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ALBUM);
|
||||
mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_ARTIST);
|
||||
Bitmap b = mmr.getFrameAtTime(2000000, FFmpegMediaMetadataRetriever.OPTION_CLOSEST); // frame at 2 seconds
|
||||
byte[] artwork = mmr.getEmbeddedPicture();
|
||||
mmr.release();
|
||||
emitter.onNext(b);
|
||||
}
|
||||
}).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(observer);
|
||||
}
|
||||
}
|
||||
54
app/src/main/java/com/uiui/zyos/utils/FileUtil.java
Normal file
54
app/src/main/java/com/uiui/zyos/utils/FileUtil.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.text.TextUtils;
|
||||
|
||||
import java.util.HashSet;
|
||||
|
||||
public class FileUtil {
|
||||
public static String getFileType(String url) {
|
||||
if (url.indexOf("/") == -1) {
|
||||
return url.substring(url.indexOf("."), url.length());
|
||||
} else {
|
||||
String fileName = url.substring(url.lastIndexOf("/"));
|
||||
return fileName.substring(fileName.indexOf("."), fileName.length());
|
||||
}
|
||||
}
|
||||
|
||||
private static HashSet<String> videoFormat = new HashSet<String>() {{
|
||||
this.add(".mp4");
|
||||
this.add(".avi");
|
||||
this.add(".nkv");
|
||||
this.add(".flv");
|
||||
}};
|
||||
private static HashSet<String> pictureFormat = new HashSet<String>() {{
|
||||
this.add(".png");
|
||||
this.add(".jpg");
|
||||
this.add(".jpeg");
|
||||
this.add(".bmp");
|
||||
}};
|
||||
|
||||
public static boolean isVideoFile(String fileName) {
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
return false;
|
||||
} else {
|
||||
if (!fileName.startsWith(".")) {
|
||||
return videoFormat.contains(getFileType(fileName));
|
||||
} else {
|
||||
return videoFormat.contains(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isPictureFile(String fileName) {
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
return false;
|
||||
} else {
|
||||
if (!fileName.startsWith(".")) {
|
||||
return pictureFormat.contains(getFileType(fileName));
|
||||
} else {
|
||||
return pictureFormat.contains(fileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
116
app/src/main/java/com/uiui/zyos/utils/ForegroundAppUtil.java
Normal file
116
app/src/main/java/com/uiui/zyos/utils/ForegroundAppUtil.java
Normal file
@@ -0,0 +1,116 @@
|
||||
package com.uiui.zyos.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 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";
|
||||
private static String TAG = ForegroundAppUtil.class.getSimpleName();
|
||||
|
||||
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 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 (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 (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 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||
UsageStatsManager manager = (UsageStatsManager) context.getApplicationContext().getSystemService(Context.USAGE_STATS_SERVICE);
|
||||
//UsageStatsManager.INTERVAL_WEEKLY,UsageStatsManager的参数定义了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;
|
||||
}
|
||||
}
|
||||
78
app/src/main/java/com/uiui/zyos/utils/GlideLoadUtils.java
Normal file
78
app/src/main/java/com/uiui/zyos/utils/GlideLoadUtils.java
Normal file
@@ -0,0 +1,78 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.util.Log;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.bumptech.glide.Glide;
|
||||
|
||||
/**
|
||||
* Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
|
||||
* Created by Li_Xavier on 2017/6/20 0020.
|
||||
*/
|
||||
public class GlideLoadUtils {
|
||||
private String TAG = "ImageLoader";
|
||||
|
||||
/**
|
||||
* 借助内部类 实现线程安全的单例模式
|
||||
* 属于懒汉式单例,因为Java机制规定,内部类SingletonHolder只有在getInstance()
|
||||
* 方法第一次调用的时候才会被加载(实现了lazy),而且其加载过程是线程安全的。
|
||||
* 内部类加载的时候实例化一次instance。
|
||||
*/
|
||||
public GlideLoadUtils() {
|
||||
}
|
||||
|
||||
private static class GlideLoadUtilsHolder {
|
||||
private final static GlideLoadUtils INSTANCE = new GlideLoadUtils();
|
||||
}
|
||||
|
||||
public static GlideLoadUtils getInstance() {
|
||||
return GlideLoadUtilsHolder.INSTANCE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
|
||||
*
|
||||
* @param context
|
||||
* @param url 加载图片的url地址 String
|
||||
* @param imageView 加载图片的ImageView 控件
|
||||
* @param default_image 图片展示错误的本地图片 id
|
||||
*/
|
||||
public void glideLoad(Context context, String url, ImageView imageView, int default_image) {
|
||||
if (context != null) {
|
||||
Glide.with(context).load(url).centerCrop().error(default_image).into(imageView);
|
||||
} else {
|
||||
Log.i(TAG, "Picture loading failed,context is null");
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
|
||||
public void glideLoad(Activity activity, String url, ImageView imageView, int default_image) {
|
||||
if (!activity.isDestroyed()) {
|
||||
Glide.with(activity).load(url).centerCrop().error(default_image).into(imageView);
|
||||
} else {
|
||||
Log.i(TAG, "Picture loading failed,activity is Destroyed");
|
||||
}
|
||||
}
|
||||
|
||||
public void glideLoad(Fragment fragment, String url, ImageView imageView, int default_image) {
|
||||
if (fragment != null && fragment.getActivity() != null) {
|
||||
Glide.with(fragment).load(url).centerCrop().error(default_image).into(imageView);
|
||||
} else {
|
||||
Log.i(TAG, "Picture loading failed,fragment is null");
|
||||
}
|
||||
}
|
||||
|
||||
public void glideLoad(android.app.Fragment fragment, String url, ImageView imageView, int default_image) {
|
||||
if (fragment != null && fragment.getActivity() != null) {
|
||||
Glide.with(fragment).load(url).centerCrop().error(default_image).into(imageView);
|
||||
} else {
|
||||
Log.i(TAG, "Picture loading failed,android.app.Fragment is null");
|
||||
}
|
||||
}
|
||||
}
|
||||
24
app/src/main/java/com/uiui/zyos/utils/GsonUtils.java
Normal file
24
app/src/main/java/com/uiui/zyos/utils/GsonUtils.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
|
||||
public class GsonUtils {
|
||||
public static JsonObject getJsonObject(String jsonString) {
|
||||
JsonObject jsonObject = JsonParser.parseString(jsonString).getAsJsonObject();
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
// TODO: 2022/3/31 暂时没有实现
|
||||
public static <T> T getJsonFromType(String jsonString, Class clazz) {
|
||||
Gson gson = new Gson();
|
||||
T t = (T) gson.fromJson(jsonString, clazz);
|
||||
return t;
|
||||
}
|
||||
|
||||
public static String toJsonString(Object o) {
|
||||
return new Gson().toJson(o);
|
||||
}
|
||||
}
|
||||
86
app/src/main/java/com/uiui/zyos/utils/HomeWatcher.java
Normal file
86
app/src/main/java/com/uiui/zyos/utils/HomeWatcher.java
Normal file
@@ -0,0 +1,86 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
|
||||
import com.blankj.utilcode.util.LogUtils;
|
||||
|
||||
public class HomeWatcher {
|
||||
|
||||
private static final String TAG = "HomeWatcher";
|
||||
private Context mContext;
|
||||
private IntentFilter mFilter;
|
||||
private OnHomePressedListener mListener;
|
||||
private InnerRecevier mRecevier;
|
||||
|
||||
// 回调接口
|
||||
public interface OnHomePressedListener {
|
||||
public void onHomePressed();
|
||||
public void onHomeLongPressed();
|
||||
}
|
||||
|
||||
public HomeWatcher(Context context) {
|
||||
mContext = context;
|
||||
mFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置监听
|
||||
*
|
||||
* @param listener
|
||||
*/
|
||||
public void setOnHomePressedListener(OnHomePressedListener listener) {
|
||||
mListener = listener;
|
||||
mRecevier = new InnerRecevier();
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始监听,注册广播
|
||||
*/
|
||||
public void startWatch() {
|
||||
if (mRecevier != null) {
|
||||
mContext.registerReceiver(mRecevier, mFilter);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止监听,注销广播
|
||||
*/
|
||||
public void stopWatch() {
|
||||
if (mRecevier != null) {
|
||||
mContext.unregisterReceiver(mRecevier);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播接收者
|
||||
*/
|
||||
class InnerRecevier extends BroadcastReceiver {
|
||||
final String SYSTEM_DIALOG_REASON_KEY = "reason";
|
||||
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
|
||||
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
|
||||
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent){
|
||||
String action = intent.getAction();
|
||||
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
|
||||
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
|
||||
if (reason != null) {
|
||||
LogUtils.i("action:" + action + ",reason:" + reason);
|
||||
if (mListener != null){
|
||||
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)){
|
||||
// 短按home键
|
||||
mListener.onHomePressed();
|
||||
}else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)){
|
||||
// 长按home键
|
||||
mListener.onHomeLongPressed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
128
app/src/main/java/com/uiui/zyos/utils/IconUtils.java
Normal file
128
app/src/main/java/com/uiui/zyos/utils/IconUtils.java
Normal file
@@ -0,0 +1,128 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class IconUtils {
|
||||
public static List<String> appClassNameList = new ArrayList<String>() {{
|
||||
this.add("com.uiui.sn");//设别信息
|
||||
this.add("com.uiui.appstore");//应用市场
|
||||
this.add("com.uiui.weather");//天气
|
||||
this.add("com.android.browser");//浏览器
|
||||
this.add("com.uiui.browser");//浏览器
|
||||
this.add("com.aoleyun.browser");//浏览器
|
||||
this.add("com.android.calculator2");//计算器
|
||||
this.add("com.android.calendar");//日历
|
||||
this.add("com.android.camera");//相机
|
||||
this.add("com.mediatek.camera");//相机
|
||||
this.add("com.android.camera2");//相机
|
||||
this.add("com.android.contacts");//通讯录
|
||||
this.add("com.android.deskclock");//时钟
|
||||
this.add("com.android.dialer");//电话
|
||||
this.add("com.android.dialer");//电话
|
||||
this.add("com.android.gallery3d");//图库
|
||||
this.add("com.android.mms");//信息
|
||||
this.add("com.android.mms.ui");//信息
|
||||
this.add("com.android.messaging");//信息
|
||||
this.add("com.android.music");//音乐
|
||||
this.add("com.android.providers.downloads.ui");//下载
|
||||
this.add("com.android.quicksearchbox");//搜索
|
||||
this.add("com.android.settings");//设置
|
||||
this.add("com.android.soundrecorder");//录音机
|
||||
this.add("com.android.stk.StkMain");//sim卡
|
||||
this.add("com.android.stk");//sim卡
|
||||
this.add("com.android.vdieo");//视频
|
||||
this.add("com.mediatek.filemanager");//文件管理
|
||||
this.add("com.android.documentsui");//下载
|
||||
this.add("com.mediatek.fmradio");//收音机
|
||||
this.add("com.android.fmradio");//收音机
|
||||
this.add("com.android.email");//电子邮件
|
||||
// this.add("com.ss.android.ugc.aweme");//抖音
|
||||
// this.add("com.ss.android.article.news");//头条
|
||||
// this.add("com.tencent.mm");//微信
|
||||
}};
|
||||
|
||||
public static List<String> appIconList = new ArrayList<String>() {{
|
||||
this.add("com_uiui_sn");
|
||||
this.add("com_android_appstore");
|
||||
this.add("com_uiui_weather");
|
||||
this.add("com_android_browser");
|
||||
this.add("com_android_browser");
|
||||
this.add("com_android_browser");
|
||||
this.add("com_android_calculator2");
|
||||
this.add("com_android_calendar");
|
||||
this.add("com_android_camera");
|
||||
this.add("com_android_camera");
|
||||
this.add("com_android_camera");
|
||||
this.add("com_android_contacts");
|
||||
this.add("com_android_deskclock");
|
||||
this.add("com_android_dialer");
|
||||
this.add("com_android_dialer");
|
||||
this.add("com_android_gallery3d_app");
|
||||
this.add("com_android_mms_ui");
|
||||
this.add("com_android_mms_ui");
|
||||
this.add("com_android_mms_ui");
|
||||
this.add("com_android_music");
|
||||
this.add("com_android_providers_downloads_ui");
|
||||
this.add("com_android_quicksearchbox");
|
||||
this.add("com_android_settings");
|
||||
this.add("com_android_soundrecorder");
|
||||
this.add("com_android_stk_stkmain");
|
||||
this.add("com_android_stk_stkmain");
|
||||
this.add("com_android_vdieo");
|
||||
this.add("com_mediatek_filemanager");
|
||||
this.add("com_mediatek_filemanager");
|
||||
this.add("com_mediatek_fmradio");
|
||||
this.add("com_mediatek_fmradio");//收音机
|
||||
this.add("com_android_email");
|
||||
// this.add("com_android_aweme2");
|
||||
// this.add("com_android_news2");
|
||||
// this.add("com_tencent_mm2");
|
||||
}};
|
||||
|
||||
public static List<String> appIconList2 = new ArrayList<String>() {{
|
||||
this.add("com_uiui_sn2");
|
||||
this.add("com_android_appstore2");
|
||||
this.add("com_uiui_weather2");
|
||||
this.add("com_android_browser2");
|
||||
this.add("com_android_browser2");
|
||||
this.add("com_android_browser2");
|
||||
this.add("com_android_calculator2");
|
||||
this.add("com_android_calendar");
|
||||
this.add("com_android_camera2");
|
||||
this.add("com_android_camera2");
|
||||
this.add("com_android_camera2");
|
||||
this.add("com_android_contacts");
|
||||
this.add("com_android_deskclock");
|
||||
this.add("com_android_dialer2");
|
||||
this.add("com_android_dialer2");
|
||||
this.add("com_android_gallery3d_app2");
|
||||
this.add("com_android_mms_ui2");
|
||||
this.add("com_android_mms_ui2");
|
||||
this.add("com_android_mms_ui2");
|
||||
this.add("com_android_music");
|
||||
this.add("com_android_providers_downloads_ui");
|
||||
this.add("com_android_quicksearchbox");
|
||||
this.add("com_android_settings2");
|
||||
this.add("com_android_soundrecorder");
|
||||
this.add("com_android_stk_stkmain");
|
||||
this.add("com_android_stk_stkmain");
|
||||
this.add("com_android_vdieo");
|
||||
this.add("com_mediatek_filemanager");
|
||||
this.add("com_mediatek_filemanager");
|
||||
this.add("com_mediatek_fmradio");
|
||||
this.add("com_mediatek_fmradio");//收音机
|
||||
this.add("com_android_email");
|
||||
this.add("com_android_aweme2");
|
||||
this.add("com_android_news2");
|
||||
this.add("com_tencent_mm2");
|
||||
}};
|
||||
|
||||
static {
|
||||
Log.e("IconUtils", "appClassNameList size: " + appClassNameList.size());
|
||||
Log.e("IconUtils", "appIconList size: " + appIconList.size());
|
||||
Log.e("IconUtils", "appIconList2 size: " + appIconList2.size());
|
||||
}
|
||||
}
|
||||
142
app/src/main/java/com/uiui/zyos/utils/LauncherUtils.java
Normal file
142
app/src/main/java/com/uiui/zyos/utils/LauncherUtils.java
Normal file
@@ -0,0 +1,142 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.app.role.RoleManager;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
import android.os.Build;
|
||||
import android.os.SystemProperties;
|
||||
import android.os.UserHandle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class LauncherUtils {
|
||||
private static final String TAG = LauncherUtils.class.getSimpleName();
|
||||
|
||||
public static void openLauncher3(Context context) {
|
||||
setDefaultDesktop(context, Launcher3, Launcher3Class);
|
||||
if (!ApkUtils.openPackage(context, Launcher3)) {
|
||||
setDefaultDesktop(context, Launcher3, Launcher3Class);
|
||||
gotoLauncher(context);
|
||||
}
|
||||
}
|
||||
|
||||
public static void gotoLauncher(Context context) {
|
||||
Intent i = new Intent(Intent.ACTION_MAIN);
|
||||
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //android123提示如果是服务里调用,必须加入new task标识
|
||||
i.addCategory(Intent.CATEGORY_HOME);
|
||||
context.startActivity(i);
|
||||
}
|
||||
|
||||
public static final String Launcher3 = "com.android.launcher3";
|
||||
public static final String Launcher3Class = "com.android.launcher3.Launcher";
|
||||
|
||||
public static void setDefaultDesktop(Context context, String pkg, String className) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
setRoleHolderAsUser(context, pkg);
|
||||
Log.e(TAG, "setDefaultDesktop: setRoleHolderAsUser");
|
||||
} else {
|
||||
//爱华设置,暂时屏蔽
|
||||
// setDefaultLauncher(mContext, pkg, className);
|
||||
Log.e(TAG, "setDefaultDesktop: setDefaultLauncher");
|
||||
}
|
||||
// String oldDesktop = (String) SPUtils.get(mContext, "default_launcher", "");
|
||||
// if (Objects.equals(oldDesktop, pkg)) {
|
||||
// Log.e(TAG, "setDefaultDesktop: " + "数据一致");
|
||||
// return;
|
||||
// }
|
||||
Intent intent = new Intent("setDefaultLauncher");
|
||||
intent.putExtra("package", pkg);
|
||||
intent.putExtra("className", className);
|
||||
// if (JGYUtils.getInstance().checkAppPlatform() == MTKPlatform) {
|
||||
Log.e(TAG, "setDefaultDesktop: MTK");
|
||||
//爱华定制
|
||||
intent.setComponent(new ComponentName("com.android.settings", "com.android.settings.AoleReceiver"));
|
||||
// TODO: 2022/7/6 有问题
|
||||
setDefaultLauncher(context, "com.android.transfer", "com.android.transfer.MainActivity");
|
||||
SystemProperties.set("persist.sys.launcher.pkgname", pkg);
|
||||
SystemProperties.set("persist.sys.launcher.classname", className);
|
||||
// }
|
||||
intent.setPackage("com.android.settings");
|
||||
context.sendBroadcast(intent);
|
||||
// ApkUtils.openPackage(mContext, pkg);
|
||||
Log.e(TAG, "setDefaultDesktop: " + pkg + ":" + className);
|
||||
Log.e(TAG, "setDefaultDesktop: " + "persist.sys.launcher.pkgname = " + SystemProperties.get("persist.sys.launcher.pkgname"));
|
||||
Log.e(TAG, "setDefaultDesktop: " + "persist.sys.launcher.classname = " + SystemProperties.get("persist.sys.launcher.classname"));
|
||||
}
|
||||
|
||||
public static void setDefaultLauncher(Context context, String defPackageName, String defClassName) {
|
||||
try {
|
||||
if (!TextUtils.isEmpty(defPackageName) && !TextUtils.isEmpty(defClassName)) {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction("android.intent.action.MAIN");
|
||||
filter.addCategory("android.intent.category.HOME");
|
||||
filter.addCategory("android.intent.category.DEFAULT");
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);
|
||||
intent.addCategory(Intent.CATEGORY_HOME);
|
||||
// 返回给定条件的所有ResolveInfo对象(本质上是Activity)
|
||||
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
int bestMatch = 0;
|
||||
final int size = list.size();
|
||||
ComponentName[] set = new ComponentName[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
ResolveInfo ri = list.get(i);
|
||||
set[i] = new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
|
||||
if (ri.match > bestMatch) {
|
||||
bestMatch = ri.match;
|
||||
}
|
||||
}
|
||||
ComponentName preActivity = new ComponentName(defPackageName, defClassName);
|
||||
context.getPackageManager().addPreferredActivity(filter, bestMatch, set, preActivity);
|
||||
}
|
||||
} catch (java.lang.SecurityException e) {
|
||||
e.printStackTrace();
|
||||
Log.e(TAG, "setDefaultLauncher: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void setRoleHolderAsUser(Context context, String packageName) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
String roleName = "android.app.role.HOME";
|
||||
boolean add = true;
|
||||
int flags = 0;
|
||||
UserHandle user = android.os.Process.myUserHandle();
|
||||
Log.i("settingssssssstemf", (add ? "Adding" : "Removing") + " package as role holder, role: "
|
||||
+ roleName + ", package: " + packageName);
|
||||
// if (JGYUtils.getInstance().checkAppPlatform() != JGYUtils.MTKPlatform) {
|
||||
RoleManager roleManager = context.getSystemService(RoleManager.class);
|
||||
Executor executor = context.getMainExecutor();
|
||||
Consumer<Boolean> callback = successful -> {
|
||||
if (successful) {
|
||||
Log.d("settingssssssstemf", "Package " + (add ? "added" : "removed")
|
||||
+ " as role holder, role: " + roleName + ", package: " + packageName);
|
||||
} else {
|
||||
Log.d("settingssssssstemf", "Failed to " + (add ? "add" : "remove")
|
||||
+ " package as role holder, role: " + roleName + ", package: "
|
||||
+ packageName);
|
||||
}
|
||||
};
|
||||
roleManager.addRoleHolderAsUser(roleName, packageName, flags, user, executor, callback);
|
||||
Log.i("settingssssssstemf", "addRoleHolderAsUser done");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isDefaultHome(Context context) {
|
||||
Intent intent = new Intent(Intent.ACTION_MAIN);//Intent.ACTION_VIEW
|
||||
intent.addCategory("android.intent.category.HOME");
|
||||
intent.addCategory("android.intent.category.DEFAULT");
|
||||
PackageManager pm = context.getPackageManager();
|
||||
ResolveInfo info = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
|
||||
boolean isDefault = context.getPackageName().equals(info.activityInfo.packageName);
|
||||
return isDefault;
|
||||
}
|
||||
}
|
||||
112
app/src/main/java/com/uiui/zyos/utils/MD5Util.java
Normal file
112
app/src/main/java/com/uiui/zyos/utils/MD5Util.java
Normal file
@@ -0,0 +1,112 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
public class MD5Util {
|
||||
|
||||
public static String packetMD5(String str) {
|
||||
MessageDigest messageDigest = null;
|
||||
try {
|
||||
messageDigest = MessageDigest.getInstance("MD5");
|
||||
|
||||
messageDigest.reset();
|
||||
|
||||
messageDigest.update(str.getBytes("UTF-8"));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.out.println("NoSuchAlgorithmException caught!");
|
||||
System.exit(-1);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
byte[] byteArray = messageDigest.digest();
|
||||
|
||||
StringBuffer md5StrBuff = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < byteArray.length; i++) {
|
||||
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
|
||||
md5StrBuff.append("0").append(
|
||||
Integer.toHexString(0xFF & byteArray[i]));
|
||||
else
|
||||
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
|
||||
}
|
||||
|
||||
return md5StrBuff.toString();
|
||||
}
|
||||
|
||||
@SuppressLint("DefaultLocale")
|
||||
public static String getUpperMD5Str(String str) {
|
||||
MessageDigest messageDigest = null;
|
||||
|
||||
try {
|
||||
messageDigest = MessageDigest.getInstance("MD5");
|
||||
|
||||
messageDigest.reset();
|
||||
|
||||
messageDigest.update(str.getBytes("UTF-8"));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.out.println("NoSuchAlgorithmException caught!");
|
||||
System.exit(-1);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
byte[] byteArray = messageDigest.digest();
|
||||
|
||||
StringBuffer md5StrBuff = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < byteArray.length; i++) {
|
||||
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
|
||||
md5StrBuff.append("0").append(
|
||||
Integer.toHexString(0xFF & byteArray[i]));
|
||||
else
|
||||
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
|
||||
}
|
||||
|
||||
return md5StrBuff.toString().toUpperCase();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取16位的MD5 值得
|
||||
*
|
||||
* @param str
|
||||
* @return
|
||||
*/
|
||||
@SuppressLint("DefaultLocale")
|
||||
public static String getUpperMD5Str16(String str) {
|
||||
MessageDigest messageDigest = null;
|
||||
|
||||
try {
|
||||
messageDigest = MessageDigest.getInstance("MD5");
|
||||
|
||||
messageDigest.reset();
|
||||
|
||||
messageDigest.update(str.getBytes("UTF-8"));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
System.out.println("NoSuchAlgorithmException caught!");
|
||||
System.exit(-1);
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
byte[] byteArray = messageDigest.digest();
|
||||
|
||||
StringBuffer md5StrBuff = new StringBuffer();
|
||||
|
||||
for (int i = 0; i < byteArray.length; i++) {
|
||||
if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
|
||||
md5StrBuff.append("0").append(
|
||||
Integer.toHexString(0xFF & byteArray[i]));
|
||||
else
|
||||
md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
|
||||
}
|
||||
|
||||
return md5StrBuff.toString().toUpperCase().substring(8, 24);
|
||||
}
|
||||
|
||||
}
|
||||
84
app/src/main/java/com/uiui/zyos/utils/NetStateUtils.java
Normal file
84
app/src/main/java/com/uiui/zyos/utils/NetStateUtils.java
Normal file
@@ -0,0 +1,84 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
public class NetStateUtils {
|
||||
|
||||
/**
|
||||
* 判断网络连接状态
|
||||
*
|
||||
* @param context
|
||||
* @return true:网络已链接, false:网络已断开连接
|
||||
*/
|
||||
public static boolean isNetworkConnected(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mNetworkInfo = mConnectivityManager
|
||||
.getActiveNetworkInfo();
|
||||
if (mNetworkInfo != null) {
|
||||
return mNetworkInfo.isAvailable();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断wifi状态
|
||||
*
|
||||
* @param context
|
||||
* @return true:是wifi情况 ,false:非wifi情况
|
||||
*/
|
||||
public static boolean isWifiConnected(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
|
||||
if (mWiFiNetworkInfo != null) {
|
||||
return mWiFiNetworkInfo.isAvailable();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断移动网络
|
||||
*
|
||||
* @param context
|
||||
* @return true:是移动网络情况, false:非移动网络情况
|
||||
*/
|
||||
public static boolean isMobileConnected(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mMobileNetworkInfo = mConnectivityManager
|
||||
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
|
||||
if (mMobileNetworkInfo != null) {
|
||||
return mMobileNetworkInfo.isAvailable();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取连接类型
|
||||
*
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static int getConnectedType(Context context) {
|
||||
if (context != null) {
|
||||
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
|
||||
.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo mNetworkInfo = mConnectivityManager
|
||||
.getActiveNetworkInfo();
|
||||
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
|
||||
return mNetworkInfo.getType();
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
30
app/src/main/java/com/uiui/zyos/utils/SchemeUtils.java
Normal file
30
app/src/main/java/com/uiui/zyos/utils/SchemeUtils.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
public class SchemeUtils {
|
||||
private static String TAG = SchemeUtils.class.getSimpleName();
|
||||
|
||||
public static final String SCHEME_TONGUE = "uiuihealth://tongue";
|
||||
public static final String SCHEME_FACE = "uiuihealth://face";
|
||||
public static final String SCHEME_HAND = "uiuihealth://hand";
|
||||
|
||||
public static void openScheme(Activity context, String uri) {
|
||||
if (TextUtils.isEmpty(uri)) {
|
||||
return;
|
||||
}
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
|
||||
try {
|
||||
context.startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "openScheme: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
32
app/src/main/java/com/uiui/zyos/utils/ScreenUtils.java
Normal file
32
app/src/main/java/com/uiui/zyos/utils/ScreenUtils.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Resources;
|
||||
|
||||
public class ScreenUtils {
|
||||
/**
|
||||
* 根据手机的分辨率从 dp 的单位 转成为 px(像素)
|
||||
*/
|
||||
public static int dip2px(Context context, float dpValue) {
|
||||
final float scale = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (dpValue * scale + 0.5f);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据手机的分辨率从 px(像素) 的单位 转成为 dp
|
||||
*/
|
||||
public static int px2dip(Context context, float pxValue) {
|
||||
final float scale = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (pxValue / scale + 0.5f);
|
||||
}
|
||||
|
||||
public static int dp2px(Resources resources, float dp) {
|
||||
final float scale = resources.getDisplayMetrics().density;
|
||||
return (int) (dp * scale + 0.5f);
|
||||
}
|
||||
|
||||
public static int sp2px(Resources resources, float sp) {
|
||||
final float scale = resources.getDisplayMetrics().scaledDensity;
|
||||
return (int) (sp * scale);
|
||||
}
|
||||
}
|
||||
24
app/src/main/java/com/uiui/zyos/utils/SystemUtils.java
Normal file
24
app/src/main/java/com/uiui/zyos/utils/SystemUtils.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SystemUtils {
|
||||
|
||||
public static boolean isMainProcessName(Context cxt, int pid) {
|
||||
String packageName = cxt.getPackageName();
|
||||
ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
|
||||
List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
|
||||
if (runningApps == null) {
|
||||
return false;
|
||||
}
|
||||
for (ActivityManager.RunningAppProcessInfo procInfo : runningApps) {
|
||||
if (procInfo.pid == pid) {
|
||||
return procInfo.processName.equals(packageName);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
29
app/src/main/java/com/uiui/zyos/utils/TimeUtils.java
Normal file
29
app/src/main/java/com/uiui/zyos/utils/TimeUtils.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
|
||||
public class TimeUtils {
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public static boolean isTodayTime(long timeStamp) {
|
||||
String time = transferLongToDate(timeStamp);
|
||||
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
||||
LocalDateTime localTime = LocalDateTime.parse(time, dtf);
|
||||
LocalDateTime startTime = LocalDate.now().atTime(0, 0, 0);
|
||||
LocalDateTime endTime = LocalDate.now().atTime(23, 59, 59);
|
||||
return localTime.isAfter(startTime) && localTime.isBefore(endTime);
|
||||
}
|
||||
|
||||
public static String transferLongToDate(Long millSec) {
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
Date date = new Date(millSec);
|
||||
return sdf.format(date);
|
||||
}
|
||||
}
|
||||
93
app/src/main/java/com/uiui/zyos/utils/ToastUtil.java
Normal file
93
app/src/main/java/com/uiui/zyos/utils/ToastUtil.java
Normal file
@@ -0,0 +1,93 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.graphics.Color;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.blankj.utilcode.util.ToastUtils;
|
||||
import com.uiui.zyos.BuildConfig;
|
||||
|
||||
|
||||
/**
|
||||
* Created by haoge on 2017/3/2.
|
||||
*/
|
||||
|
||||
public class ToastUtil {
|
||||
private static final String TAG = ToastUtil.class.getSimpleName();
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
private static Context mContext;
|
||||
private static Handler mainHandler = new Handler(Looper.getMainLooper());
|
||||
private static Toast debugToast;
|
||||
private static Toast toast;
|
||||
|
||||
|
||||
@SuppressLint("ShowToast")
|
||||
public static void init(Context context) {
|
||||
mContext = context;
|
||||
toast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
|
||||
debugToast = Toast.makeText(mContext, "", Toast.LENGTH_SHORT);
|
||||
|
||||
}
|
||||
|
||||
private static long time1 = 0L;
|
||||
private static long time2 = 0L;
|
||||
|
||||
public static void show(final String msg) {
|
||||
ToastUtils.make()
|
||||
// .setBgColor(ColorUtils.getColor(R.color.toast_color))
|
||||
.setTextColor(Color.DKGRAY)
|
||||
// .setGravity(Gravity.CENTER, 0, 0)
|
||||
.setNotUseSystemToast()
|
||||
.show(msg);
|
||||
}
|
||||
|
||||
public static void betaShow(final String msg) {
|
||||
if ( BuildConfig.DEBUG) {
|
||||
ToastUtils.make()
|
||||
// .setBgColor(ColorUtils.getColor(R.color.toast_color))
|
||||
.setTextColor(Color.RED)
|
||||
// .setGravity(Gravity.CENTER, 0, 0)
|
||||
.setNotUseSystemToast()
|
||||
.setDurationIsLong(true)
|
||||
.show(msg);
|
||||
} else {
|
||||
Log.e(TAG, "debugShow: " + msg);
|
||||
}
|
||||
}
|
||||
|
||||
private static Toast mToast = null;
|
||||
|
||||
//android 8.0以后限制
|
||||
//https://www.jianshu.com/p/d9813ad03d59
|
||||
//https://www.jianshu.com/p/050ce052b873
|
||||
public static void showToast(Context context, String text, int duration) {
|
||||
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.P) {
|
||||
Toast.makeText(context, text, duration).show();
|
||||
} else {
|
||||
if (mToast == null) {
|
||||
mToast = Toast.makeText(context, text, duration);
|
||||
} else {
|
||||
mToast.setText(text);
|
||||
mToast.setDuration(duration);
|
||||
}
|
||||
mToast.show();
|
||||
}
|
||||
}
|
||||
|
||||
// public static void showInCenter(String msg) {
|
||||
// mainHandler.post(() -> {
|
||||
// if (toast != null) {
|
||||
// toast.setGravity(Gravity.CENTER, 0, 0);
|
||||
// toast.setText(msg);
|
||||
// toast.show();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
|
||||
}
|
||||
144
app/src/main/java/com/uiui/zyos/utils/Utils.java
Normal file
144
app/src/main/java/com/uiui/zyos/utils/Utils.java
Normal file
@@ -0,0 +1,144 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.content.ContextWrapper;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PorterDuff;
|
||||
import android.graphics.PorterDuffXfermode;
|
||||
import android.os.BatteryManager;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import com.uiui.zyos.BuildConfig;
|
||||
import com.uiui.zyos.R;
|
||||
|
||||
import java.io.File;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class Utils {
|
||||
private static final String TAG = Utils.class.getSimpleName();
|
||||
|
||||
/**
|
||||
* 获取设备序列号
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@SuppressLint("MissingPermission")
|
||||
public static String getSerial() {
|
||||
String serial = "unknow";
|
||||
try {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {//9.0+
|
||||
serial = Build.getSerial();
|
||||
} else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {//8.0+
|
||||
serial = Build.SERIAL;
|
||||
} else {//8.0-
|
||||
Class<?> c = Class.forName("android.os.SystemProperties");
|
||||
Method get = c.getMethod("get", String.class);
|
||||
serial = (String) get.invoke(c, "ro.serialno");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
Log.e("e", "读取设备序列号异常:" + e.toString());
|
||||
}
|
||||
if (BuildConfig.DEBUG) {
|
||||
// return "QNG2DKB00463";
|
||||
}
|
||||
return serial;
|
||||
}
|
||||
|
||||
public static String getDeviceSN() {
|
||||
String serial = null;
|
||||
try {
|
||||
Class<?> c = Class.forName("android.os.SystemProperties");
|
||||
Method get = c.getMethod("get", String.class);
|
||||
serial = (String) get.invoke(c, "persist.sys.hrSerial");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return serial;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取电量
|
||||
*
|
||||
* @param mContext
|
||||
* @return
|
||||
*/
|
||||
synchronized public static int getBatteryLevel(Context mContext) {
|
||||
if (Build.VERSION.SDK_INT >= 21) {
|
||||
return ((BatteryManager) mContext.getSystemService(Context.BATTERY_SERVICE)).getIntProperty(4);
|
||||
} else {
|
||||
Intent intent = (new ContextWrapper(mContext)).registerReceiver(null, new IntentFilter("android.intent.action.BATTERY_CHANGED"));
|
||||
return intent.getIntExtra("level", -1) * 100 / intent.getIntExtra("scale", -1);
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDownLoadPath(Context context) {
|
||||
String path = ContextCompat.getExternalFilesDirs(context, Environment.DIRECTORY_DOWNLOADS)[0].getAbsolutePath();
|
||||
return path + File.separator;
|
||||
}
|
||||
|
||||
public static String getFileNamefromURL(String url) {
|
||||
int position = url.lastIndexOf("/");
|
||||
return url.substring(position + 1);
|
||||
}
|
||||
|
||||
public static Bitmap getRoundedBitmap(Bitmap mBitmap, Context context) {
|
||||
Bitmap bgBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Bitmap.Config.ARGB_8888);
|
||||
|
||||
Bitmap mask = BitmapFactory.decodeResource(context.getResources(), R.drawable.mask);
|
||||
int width = mask.getWidth();
|
||||
int height = mask.getHeight();
|
||||
Bitmap bitmapScale = Bitmap.createScaledBitmap(mBitmap, width, height, true);
|
||||
bitmapScale.setDensity(context.getResources().getDisplayMetrics().densityDpi);
|
||||
// Palette p = Palette.from(mBitmap).generate();
|
||||
// Palette.Swatch vibrant = p.getVibrantSwatch();//有活力的
|
||||
// int color = vibrant.getRgb(); //样本中的像素数量
|
||||
|
||||
Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas();
|
||||
Paint paint = new Paint();
|
||||
|
||||
canvas.setBitmap(result);
|
||||
// canvas.drawColor(color);
|
||||
canvas.drawBitmap(mask, 0, 0, paint);
|
||||
// paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||||
canvas.drawBitmap(bitmapScale, 0, 0, paint);
|
||||
// return result;
|
||||
|
||||
Bitmap result2 = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas2 = new Canvas();
|
||||
Paint paint2 = new Paint();
|
||||
canvas2.setBitmap(result2);
|
||||
canvas2.drawBitmap(mask, 0, 0, paint2);
|
||||
paint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||||
canvas2.drawBitmap(result, 0, 0, paint2);
|
||||
return result2;
|
||||
|
||||
|
||||
// Canvas mCanvas = new Canvas();
|
||||
// mCanvas.setBitmap(bgBitmap);
|
||||
// Paint mPaint = new Paint();
|
||||
// RectF mRectM = new RectF(scaleM, scaleM, mBitmap.getWidth() - scaleM, mBitmap.getHeight() - scaleM); //设置剪裁圆角的区域
|
||||
// Rect mRect = new Rect(0, 0, mBitmap.getWidth(), mBitmap.getHeight());
|
||||
// RectF mRectF = mRectM;
|
||||
//
|
||||
// float roundPx = 15; //圆角半径
|
||||
// mPaint.setAntiAlias(true);
|
||||
// //Log.d("wy"+TAG,"mBitmap.getWidth()="+mBitmap.getWidth()+", mBitmap.getHeight()="+mBitmap.getHeight());
|
||||
// mCanvas.drawRoundRect(mRectF, roundPx, roundPx, mPaint);
|
||||
// mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
|
||||
// mCanvas.drawBitmap(mBitmap, mRect, mRect, mPaint);
|
||||
// return bgBitmap;
|
||||
}
|
||||
|
||||
}
|
||||
119
app/src/main/java/com/uiui/zyos/utils/WakeUpUtils.java
Normal file
119
app/src/main/java/com/uiui/zyos/utils/WakeUpUtils.java
Normal file
@@ -0,0 +1,119 @@
|
||||
package com.uiui.zyos.utils;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.app.Activity;
|
||||
import android.app.KeyguardManager;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.PowerManager;
|
||||
import android.util.Log;
|
||||
import android.view.Window;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
|
||||
public class WakeUpUtils {
|
||||
|
||||
/**
|
||||
* 唤醒手机屏幕并解锁
|
||||
*/
|
||||
public static void wakeUpAndUnlock(Activity activity) {
|
||||
// 获取电源管理器对象
|
||||
PowerManager pm = (PowerManager) activity.getApplicationContext()
|
||||
.getSystemService(Context.POWER_SERVICE);
|
||||
boolean screenOn = pm.isScreenOn();
|
||||
Log.d("WakeScreen0", "screenOn: " + screenOn);
|
||||
if (!screenOn) {
|
||||
// 获取PowerManager.WakeLock对象,后面的参数|表示同时传入两个值,最后的是LogCat里用的Tag
|
||||
@SuppressLint("InvalidWakeLockTag") PowerManager.WakeLock wl = pm.newWakeLock(
|
||||
PowerManager.ACQUIRE_CAUSES_WAKEUP |
|
||||
PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "bright");
|
||||
wl.acquire(10000); // 点亮屏幕
|
||||
wl.release(); // 释放
|
||||
}
|
||||
// 屏幕解锁
|
||||
KeyguardManager keyguardManager = (KeyguardManager) activity.getApplicationContext()
|
||||
.getSystemService(Context.KEYGUARD_SERVICE);
|
||||
KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unLock");
|
||||
// 屏幕锁定
|
||||
// keyguardLock.reenableKeyguard();
|
||||
keyguardLock.disableKeyguard(); // 解锁
|
||||
unLockScreen(activity);
|
||||
}
|
||||
|
||||
private static void unLockScreen(Activity activity) {
|
||||
final Window win = activity.getWindow();
|
||||
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
||||
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
|
||||
|
||||
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
|
||||
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
|
||||
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
|
||||
}
|
||||
|
||||
/**
|
||||
* 唤醒手机屏幕并解锁
|
||||
*/
|
||||
@RequiresApi(api = Build.VERSION_CODES.O)
|
||||
public static void wakeUpAndUnlockScreen(Activity activity) {
|
||||
|
||||
Window win = activity.getWindow();
|
||||
win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
|
||||
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
|
||||
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
|
||||
|
||||
PowerManager pm = (PowerManager) activity.getSystemService(Context.POWER_SERVICE);
|
||||
@SuppressLint("InvalidWakeLockTag")
|
||||
PowerManager.WakeLock wakelock = pm.newWakeLock(
|
||||
PowerManager.FULL_WAKE_LOCK
|
||||
| PowerManager.ACQUIRE_CAUSES_WAKEUP, "xx");
|
||||
wakelock.acquire();
|
||||
wakelock.release();
|
||||
|
||||
KeyguardManager keyguardManager = (KeyguardManager) activity.getApplicationContext()
|
||||
.getSystemService(Context.KEYGUARD_SERVICE);
|
||||
|
||||
if (activity == null) return;
|
||||
keyguardManager.requestDismissKeyguard(activity, new KeyguardManager.KeyguardDismissCallback() {
|
||||
@Override
|
||||
public void onDismissError() {
|
||||
super.onDismissError();
|
||||
Log.d("xxx-->", "1 onDismissError");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismissSucceeded() {
|
||||
super.onDismissSucceeded();
|
||||
Log.d("xxx-->", "1 onDismissSucceeded");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismissCancelled() {
|
||||
super.onDismissCancelled();
|
||||
Log.d("xxx-->", "1 onDismissCancelled");
|
||||
}
|
||||
});
|
||||
|
||||
if (activity == null) return;
|
||||
keyguardManager.requestDismissKeyguard(activity, new KeyguardManager.KeyguardDismissCallback() {
|
||||
@Override
|
||||
public void onDismissError() {
|
||||
super.onDismissError();
|
||||
Log.d("xxx-->", "2 onDismissError");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismissSucceeded() {
|
||||
super.onDismissSucceeded();
|
||||
Log.d("xxx-->", "2 onDismissSucceeded");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDismissCancelled() {
|
||||
super.onDismissCancelled();
|
||||
Log.d("xxx-->", "2 onDismissCancelled");
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user