Skip to content

Commit

Permalink
see 10/21 log
Browse files Browse the repository at this point in the history
  • Loading branch information
Blankj committed Oct 21, 2016
1 parent e7b11ce commit 2ca18ef
Show file tree
Hide file tree
Showing 15 changed files with 1,346 additions and 326 deletions.
68 changes: 47 additions & 21 deletions utilcode/src/main/java/com/blankj/utilcode/utils/AppUtils.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.blankj.utilcode.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
Expand Down Expand Up @@ -361,8 +362,37 @@ public static boolean isSystemApp(Context context, String packageName) {
return ai != null && (ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
}

/**
* 判断App是否是Debug版本
*
* @param context 上下文
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isAppDebug(Context context) {
return isAppDebug(context, context.getPackageName());
}

/**
* 判断App是否是Debug版本
*
* @param context 上下文
* @param packageName 包名
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isAppDebug(Context context, String packageName) {
if (StringUtils.isSpace(packageName)) return false;
try {
PackageManager pm = context.getPackageManager();
ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
return ai != null && (ai.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return false;
}
return false;
}

/**
Expand All @@ -382,11 +412,12 @@ public static Signature[] getAppSignature(Context context) {
* @param packageName 包名
* @return App签名
*/
@SuppressLint("PackageManagerGetSignatures")
public static Signature[] getAppSignature(Context context, String packageName) {
if (StringUtils.isSpace(packageName)) return null;
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
PackageInfo pi = pm.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
return pi == null ? null : pi.signatures;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
Expand Down Expand Up @@ -427,33 +458,28 @@ public static String getAppSignatureSHA1(Context context, String packageName) {
* @return {@code true}: 是<br>{@code false}: 否
*/
public static boolean isAppForeground(Context context) {
return ProcessUtils.isAppForeground(context);
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
if (infos == null || infos.size() == 0) return false;
for (ActivityManager.RunningAppProcessInfo info : infos) {
if (info.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return info.processName.equals(context.getPackageName());
}
}
return false;
}

/**
* 判断某个App是否处于前台(系统应用调用)
* <p>API < 21,需要添加 {@code <uses-permission android:name="android.permission.GET_TASKS"/>} 权限</p>
* <p>API >= 22,需要添加 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>} 权限</p>
* 判断App是否处于前台
* <p>当不是查看当前App,且SDK >= 22时,
* 需添加权限 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>}</p>
*
* @param context 上下文
* @param packageName 包名
* @return {@code true}: 是<br>{@code false}: 否
*/
@Deprecated
public static boolean isAppForeground(Context context, String packageName) {
return ProcessUtils.isAppForeground(context, packageName);;
}

/**
* 获取前台应用包名(系统应用调用)
* <p>API < 21,需要添加 {@code <uses-permission android:name="android.permission.GET_TASKS"/>} 权限</p>
* <p>API >= 22,需要添加 {@code <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS"/>} 权限</p>
*
* @param context 上下文
* @return 前台应用包名
*/
public String getForegroundApp(Context context) {
return ProcessUtils.getForegroundPackage(context);
return !StringUtils.isSpace(packageName) && packageName.equals(ProcessUtils.getForegroundProcessName(context));
}

/**
Expand Down Expand Up @@ -662,4 +688,4 @@ public static boolean cleanAppData(Context context, File... dirs) {
}
return isSuccess;
}
}
}
Loading

0 comments on commit 2ca18ef

Please sign in to comment.