Skip to content

Commit

Permalink
[M] AppUtils增加getAppMinSdkVersion, getAppTargetSdkVersion支持
Browse files Browse the repository at this point in the history
  • Loading branch information
YoungBill authored and Blankj committed Oct 14, 2022
1 parent 62bf164 commit f3449a2
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,63 +54,74 @@ class AppActivity : CommonActivity(), Utils.OnAppStatusChangedListener {

override fun bindItems(): MutableList<CommonItem<*>> {
return CollectionUtils.newArrayList(
CommonItemSwitch("registerAppStatusChangedListener", { isRegisterAppStatusChangedListener }, {
CommonItemSwitch(
"registerAppStatusChangedListener",
{ isRegisterAppStatusChangedListener },
{
isRegisterAppStatusChangedListener = it
if (it) {
AppUtils.registerAppStatusChangedListener(this)
} else {
AppUtils.unregisterAppStatusChangedListener(this)
}
}),
CommonItemTitle("isAppRoot", AppUtils.isAppRoot().toString()),
CommonItemTitle("isAppDebug", AppUtils.isAppDebug().toString()),
CommonItemTitle("isAppSystem", AppUtils.isAppSystem().toString()),
CommonItemTitle("isAppForeground", AppUtils.isAppForeground(AppUtils.getAppPackageName()).toString()),
CommonItemTitle("isAppRunning", AppUtils.isAppRunning(AppUtils.getAppPackageName()).toString()),
CommonItemImage("getAppIcon") {
it.setImageDrawable(AppUtils.getAppIcon())
},
CommonItemTitle("getAppPackageName", AppUtils.getAppPackageName()),
CommonItemTitle("getAppName", AppUtils.getAppName()),
CommonItemTitle("getAppPath", AppUtils.getAppPath()),
CommonItemTitle("getAppVersionName", AppUtils.getAppVersionName()),
CommonItemTitle("getAppVersionCode", AppUtils.getAppVersionCode().toString()),
CommonItemTitle("getAppSignaturesSHA1", AppUtils.getAppSignaturesSHA1().toString()),
CommonItemTitle("getAppSignaturesSHA256", AppUtils.getAppSignaturesSHA256().toString()),
CommonItemTitle("getAppSignaturesMD5", AppUtils.getAppSignaturesMD5().toString()),
CommonItemTitle("getAppUid", AppUtils.getAppUid().toString()),
CommonItemTitle("getApkInfo", AppUtils.getApkInfo(AppUtils.getAppPath()).toString()),

CommonItemClick(R.string.app_install) {
if (AppUtils.isAppInstalled(Config.TEST_PKG)) {
ToastUtils.showShort(R.string.app_install_tips)
CommonItemTitle("isAppRoot", AppUtils.isAppRoot().toString()),
CommonItemTitle("isAppDebug", AppUtils.isAppDebug().toString()),
CommonItemTitle("isAppSystem", AppUtils.isAppSystem().toString()),
CommonItemTitle(
"isAppForeground",
AppUtils.isAppForeground(AppUtils.getAppPackageName()).toString()
),
CommonItemTitle(
"isAppRunning",
AppUtils.isAppRunning(AppUtils.getAppPackageName()).toString()
),
CommonItemImage("getAppIcon") {
it.setImageDrawable(AppUtils.getAppIcon())
},
CommonItemTitle("getAppPackageName", AppUtils.getAppPackageName()),
CommonItemTitle("getAppName", AppUtils.getAppName()),
CommonItemTitle("getAppPath", AppUtils.getAppPath()),
CommonItemTitle("getAppVersionName", AppUtils.getAppVersionName()),
CommonItemTitle("getAppVersionCode", AppUtils.getAppVersionCode().toString()),
CommonItemTitle("getAppMinSdkVersion", AppUtils.getAppMinSdkVersion().toString()),
CommonItemTitle("getAppTargetSdkVersion", AppUtils.getAppTargetSdkVersion().toString()),
CommonItemTitle("getAppSignaturesSHA1", AppUtils.getAppSignaturesSHA1().toString()),
CommonItemTitle("getAppSignaturesSHA256", AppUtils.getAppSignaturesSHA256().toString()),
CommonItemTitle("getAppSignaturesMD5", AppUtils.getAppSignaturesMD5().toString()),
CommonItemTitle("getAppUid", AppUtils.getAppUid().toString()),
CommonItemTitle("getApkInfo", AppUtils.getApkInfo(AppUtils.getAppPath()).toString()),

CommonItemClick(R.string.app_install) {
if (AppUtils.isAppInstalled(Config.TEST_PKG)) {
ToastUtils.showShort(R.string.app_install_tips)
} else {
if (!FileUtils.isFileExists(Config.TEST_APK_PATH)) {
ReleaseInstallApkTask(listener).execute()
} else {
if (!FileUtils.isFileExists(Config.TEST_APK_PATH)) {
ReleaseInstallApkTask(listener).execute()
} else {
listener.onReleased()
}
listener.onReleased()
}
},
CommonItemClick(R.string.app_uninstall) {
if (AppUtils.isAppInstalled(Config.TEST_PKG)) {
AppUtils.uninstallApp(Config.TEST_PKG)
} else {
ToastUtils.showShort(R.string.app_uninstall_tips)
}
},
CommonItemClick(R.string.app_launch) {
AppUtils.launchApp(this.packageName)
},
CommonItemClick(R.string.app_relaunch) {
AppUtils.relaunchApp()
},
CommonItemClick(R.string.app_launch_details_settings, true) {
AppUtils.launchAppDetailsSettings()
},
CommonItemClick(R.string.app_exit) {
AppUtils.exitApp()
}
},
CommonItemClick(R.string.app_uninstall) {
if (AppUtils.isAppInstalled(Config.TEST_PKG)) {
AppUtils.uninstallApp(Config.TEST_PKG)
} else {
ToastUtils.showShort(R.string.app_uninstall_tips)
}
},
CommonItemClick(R.string.app_launch) {
AppUtils.launchApp(this.packageName)
},
CommonItemClick(R.string.app_relaunch) {
AppUtils.relaunchApp()
},
CommonItemClick(R.string.app_launch_details_settings, true) {
AppUtils.launchAppDetailsSettings()
},
CommonItemClick(R.string.app_exit) {
AppUtils.exitApp()
}
)
}

Expand All @@ -130,7 +141,8 @@ class AppActivity : CommonActivity(), Utils.OnAppStatusChangedListener {
}
}

class ReleaseInstallApkTask(private val mListener: OnReleasedListener) : ThreadUtils.SimpleTask<Unit>() {
class ReleaseInstallApkTask(private val mListener: OnReleasedListener) :
ThreadUtils.SimpleTask<Unit>() {

override fun doInBackground() {
ResourceUtils.copyFileFromAssets("test_install", Config.TEST_APK_PATH)
Expand Down
2 changes: 2 additions & 0 deletions lib/utilcode/README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ getAppName : 获取 App 名称
getAppPath : 获取 App 路径
getAppVersionName : 获取 App 版本号
getAppVersionCode : 获取 App 版本码
getAppMinSdkVersion : 获取 App 支持最低系统版本号
getAppTargetSdkVersion : 获取 App 目标系统版本号
getAppSignatures : 获取 App 签名
getAppSignaturesSHA1 : 获取应用签名的的 SHA1 值
getAppSignaturesSHA256 : 获取应用签名的的 SHA256 值
Expand Down
2 changes: 2 additions & 0 deletions lib/utilcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ getAppName
getAppPath
getAppVersionName
getAppVersionCode
getAppMinSdkVersion
getAppTargetSdkVersion
getAppSignatures
getAppSignaturesSHA1
getAppSignaturesSHA256
Expand Down
115 changes: 102 additions & 13 deletions lib/utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
* <pre>
* author: Blankj
Expand Down Expand Up @@ -541,6 +541,65 @@ public static int getAppVersionCode(final String packageName) {
}
}

/**
* Return the application's minimum sdk version code.
*
* @return the application's minimum sdk version code
*/
public static int getAppMinSdkVersion() {
return getAppMinSdkVersion(Utils.getApp().getPackageName());
}

/**
* Return the application's minimum sdk version code.
*
* @param packageName The name of the package.
* @return the application's minimum sdk version code
*/
public static int getAppMinSdkVersion(final String packageName) {
if (UtilsBridge.isSpace(packageName)) return -1;
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) return -1;
try {
PackageManager pm = Utils.getApp().getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
if (null == pi) return -1;
ApplicationInfo ai = pi.applicationInfo;
return null == ai ? -1 : ai.minSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return -1;
}
}

/**
* Return the application's target sdk version code.
*
* @return the application's target sdk version code
*/
public static int getAppTargetSdkVersion() {
return getAppTargetSdkVersion(Utils.getApp().getPackageName());
}

/**
* Return the application's target sdk version code.
*
* @param packageName The name of the package.
* @return the application's target sdk version code
*/
public static int getAppTargetSdkVersion(final String packageName) {
if (UtilsBridge.isSpace(packageName)) return -1;
try {
PackageManager pm = Utils.getApp().getPackageManager();
PackageInfo pi = pm.getPackageInfo(packageName, 0);
if (null == pi) return -1;
ApplicationInfo ai = pi.applicationInfo;
return null == ai ? -1 : ai.targetSdkVersion;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return -1;
}
}

/**
* Return the application's signature.
*
Expand Down Expand Up @@ -721,6 +780,8 @@ private static List<String> getAppSignaturesHash(final String packageName, final
* <li>path of package</li>
* <li>version name</li>
* <li>version code</li>
* <li>minimum sdk version code</li>
* <li>target sdk version code</li>
* <li>is system</li>
* </ul>
*
Expand All @@ -740,6 +801,8 @@ public static AppInfo getAppInfo() {
* <li>path of package</li>
* <li>version name</li>
* <li>version code</li>
* <li>minimum sdk version code</li>
* <li>target sdk version code</li>
* <li>is system</li>
* </ul>
*
Expand Down Expand Up @@ -829,27 +892,34 @@ private static AppInfo getBean(final PackageManager pm, final PackageInfo pi) {
String packageName = pi.packageName;
ApplicationInfo ai = pi.applicationInfo;
if (ai == null) {
return new AppInfo(packageName, "", null, "", versionName, versionCode, false);
return new AppInfo(packageName, "", null, "", versionName, versionCode, -1, -1, false);
}
String name = ai.loadLabel(pm).toString();
Drawable icon = ai.loadIcon(pm);
String packagePath = ai.sourceDir;
int minSdkVersion = -1;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
minSdkVersion = ai.minSdkVersion;
}
int targetSdkVersion = ai.targetSdkVersion;
boolean isSystem = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != 0;
return new AppInfo(packageName, name, icon, packagePath, versionName, versionCode, isSystem);
return new AppInfo(packageName, name, icon, packagePath, versionName, versionCode, minSdkVersion, targetSdkVersion, isSystem);
}

/**
* The application's information.
*/
public static class AppInfo {

private String packageName;
private String name;
private String packageName;
private String name;
private Drawable icon;
private String packagePath;
private String versionName;
private int versionCode;
private boolean isSystem;
private String packagePath;
private String versionName;
private int versionCode;
private int minSdkVersion;
private int targetSdkVersion;
private boolean isSystem;

public Drawable getIcon() {
return icon;
Expand Down Expand Up @@ -907,14 +977,31 @@ public void setVersionName(final String versionName) {
this.versionName = versionName;
}

public AppInfo(String packageName, String name, Drawable icon, String packagePath,
String versionName, int versionCode, boolean isSystem) {
public int getMinSdkVersion() {
return minSdkVersion;
}

public void setMinSdkVersion(int minSdkVersion) {
this.minSdkVersion = minSdkVersion;
}

public int getTargetSdkVersion() {
return targetSdkVersion;
}

public void setTargetSdkVersion(int targetSdkVersion) {
this.targetSdkVersion = targetSdkVersion;
}

public AppInfo(String packageName, String name, Drawable icon, String packagePath, String versionName, int versionCode, int minSdkVersion, int targetSdkVersion, boolean isSystem) {
this.setName(name);
this.setIcon(icon);
this.setPackageName(packageName);
this.setPackagePath(packagePath);
this.setVersionName(versionName);
this.setVersionCode(versionCode);
this.setMinSdkVersion(minSdkVersion);
this.setTargetSdkVersion(targetSdkVersion);
this.setSystem(isSystem);
}

Expand All @@ -928,6 +1015,8 @@ public String toString() {
"\n app path: " + getPackagePath() +
"\n app v name: " + getVersionName() +
"\n app v code: " + getVersionCode() +
"\n app v min: " + getMinSdkVersion() +
"\n app v target: " + getTargetSdkVersion() +
"\n is system: " + isSystem() +
"\n}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
import android.view.View.OnClickListener;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ClickUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utildebug.DebugUtils;
import com.blankj.utildebug.R;
import com.blankj.utildebug.base.rv.BaseItem;
import com.blankj.utildebug.base.rv.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;

/**
* <pre>
* author: blankj
Expand All @@ -29,8 +28,8 @@
*/
public class AppInfoItem extends BaseItem<AppInfoItem> {

private String mTitle;
private String mContent;
private String mTitle;
private String mContent;
private OnClickListener mListener;

private TextView titleTv;
Expand Down Expand Up @@ -71,9 +70,9 @@ public static List<AppInfoItem> getAppInfoItems() {
appInfoItems.add(new AppInfoItem(R.string.du_app_info_version_name, AppUtils.getAppVersionName()));
appInfoItems.add(new AppInfoItem(R.string.du_app_info_version_code, String.valueOf(AppUtils.getAppVersionCode())));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
appInfoItems.add(new AppInfoItem(R.string.du_app_info_min_sdk_version, String.valueOf(DebugUtils.getApp().getApplicationInfo().minSdkVersion)));
appInfoItems.add(new AppInfoItem(R.string.du_app_info_min_sdk_version, String.valueOf(AppUtils.getAppMinSdkVersion())));
}
appInfoItems.add(new AppInfoItem(R.string.du_app_info_target_sdk_version, String.valueOf(DebugUtils.getApp().getApplicationInfo().targetSdkVersion)));
appInfoItems.add(new AppInfoItem(R.string.du_app_info_target_sdk_version, String.valueOf(AppUtils.getAppTargetSdkVersion())));
appInfoItems.add(new AppInfoItem(R.string.du_app_info_open_app_info_page, "", new OnClickListener() {
@Override
public void onClick(View v) {
Expand Down

0 comments on commit f3449a2

Please sign in to comment.