diff --git a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/app/AppActivity.kt b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/app/AppActivity.kt index 175fa1bc3b..21eb351146 100644 --- a/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/app/AppActivity.kt +++ b/feature/utilcode/pkg/src/main/java/com/blankj/utilcode/pkg/feature/app/AppActivity.kt @@ -54,7 +54,10 @@ class AppActivity : CommonActivity(), Utils.OnAppStatusChangedListener { override fun bindItems(): MutableList> { return CollectionUtils.newArrayList( - CommonItemSwitch("registerAppStatusChangedListener", { isRegisterAppStatusChangedListener }, { + CommonItemSwitch( + "registerAppStatusChangedListener", + { isRegisterAppStatusChangedListener }, + { isRegisterAppStatusChangedListener = it if (it) { AppUtils.registerAppStatusChangedListener(this) @@ -62,55 +65,63 @@ class AppActivity : CommonActivity(), Utils.OnAppStatusChangedListener { 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() + } ) } @@ -130,7 +141,8 @@ class AppActivity : CommonActivity(), Utils.OnAppStatusChangedListener { } } -class ReleaseInstallApkTask(private val mListener: OnReleasedListener) : ThreadUtils.SimpleTask() { +class ReleaseInstallApkTask(private val mListener: OnReleasedListener) : + ThreadUtils.SimpleTask() { override fun doInBackground() { ResourceUtils.copyFileFromAssets("test_install", Config.TEST_APK_PATH) diff --git a/lib/utilcode/README-CN.md b/lib/utilcode/README-CN.md index f7523783f9..641e96ff1d 100644 --- a/lib/utilcode/README-CN.md +++ b/lib/utilcode/README-CN.md @@ -72,6 +72,8 @@ getAppName : 获取 App 名称 getAppPath : 获取 App 路径 getAppVersionName : 获取 App 版本号 getAppVersionCode : 获取 App 版本码 +getAppMinSdkVersion : 获取 App 支持最低系统版本号 +getAppTargetSdkVersion : 获取 App 目标系统版本号 getAppSignatures : 获取 App 签名 getAppSignaturesSHA1 : 获取应用签名的的 SHA1 值 getAppSignaturesSHA256 : 获取应用签名的的 SHA256 值 diff --git a/lib/utilcode/README.md b/lib/utilcode/README.md index 9d2025ebb0..b88bcbcfbd 100644 --- a/lib/utilcode/README.md +++ b/lib/utilcode/README.md @@ -72,6 +72,8 @@ getAppName getAppPath getAppVersionName getAppVersionCode +getAppMinSdkVersion +getAppTargetSdkVersion getAppSignatures getAppSignaturesSHA1 getAppSignaturesSHA256 diff --git a/lib/utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java b/lib/utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java index 8f03e1cf1f..3a07653fb0 100644 --- a/lib/utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java +++ b/lib/utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java @@ -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; - /** *
  *     author: Blankj
@@ -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.
      *
@@ -721,6 +780,8 @@ private static List getAppSignaturesHash(final String packageName, final
      * 
  • path of package
  • *
  • version name
  • *
  • version code
  • + *
  • minimum sdk version code
  • + *
  • target sdk version code
  • *
  • is system
  • * * @@ -740,6 +801,8 @@ public static AppInfo getAppInfo() { *
  • path of package
  • *
  • version name
  • *
  • version code
  • + *
  • minimum sdk version code
  • + *
  • target sdk version code
  • *
  • is system
  • * * @@ -829,13 +892,18 @@ 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); } /** @@ -843,13 +911,15 @@ private static AppInfo getBean(final PackageManager pm, final PackageInfo pi) { */ 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; @@ -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); } @@ -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}"; } diff --git a/lib/utildebug/src/main/java/com/blankj/utildebug/debug/tool/appInfo/AppInfoItem.java b/lib/utildebug/src/main/java/com/blankj/utildebug/debug/tool/appInfo/AppInfoItem.java index 76351e4b10..54895ee71f 100644 --- a/lib/utildebug/src/main/java/com/blankj/utildebug/debug/tool/appInfo/AppInfoItem.java +++ b/lib/utildebug/src/main/java/com/blankj/utildebug/debug/tool/appInfo/AppInfoItem.java @@ -5,10 +5,12 @@ 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; @@ -16,9 +18,6 @@ import java.util.ArrayList; import java.util.List; -import androidx.annotation.NonNull; -import androidx.annotation.StringRes; - /** *
      *     author: blankj
    @@ -29,8 +28,8 @@
      */
     public class AppInfoItem extends BaseItem {
     
    -    private String          mTitle;
    -    private String          mContent;
    +    private String mTitle;
    +    private String mContent;
         private OnClickListener mListener;
     
         private TextView titleTv;
    @@ -71,9 +70,9 @@ public static List 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) {