Skip to content

Commit 14b2d7f

Browse files
author
Blankj
committed
see 02/26 log
1 parent ba9dd00 commit 14b2d7f

3 files changed

Lines changed: 171 additions & 1 deletion

File tree

‎utilcode/README-CN.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ getAppVersionName : 获取 App 版本号
6464
getAppVersionCode : 获取 App 版本码
6565
getAppSignature : 获取 App 签名
6666
getAppSignatureSHA1 : 获取应用签名的的 SHA1 值
67+
getAppInfo : 获取 App 信息
68+
getAppsInfo : 获取所有已安装 App 信息
6769
```
6870

6971
* ### 栏相关 -> [BarUtils.java][bar.java] -> [Demo][bar.demo]

‎utilcode/README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ getAppVersionName
6464
getAppVersionCode
6565
getAppSignature
6666
getAppSignatureSHA1
67+
getAppInfo
68+
getAppsInfo
6769
```
6870

6971
* ### About Bar -> [BarUtils.java][bar.java] -> [Demo][bar.demo]
@@ -170,7 +172,6 @@ urlDecode
170172
base64Encode
171173
base64Encode2String
172174
base64Decode
173-
base64UrlSafeEncode
174175
htmlEncode
175176
htmlDecode
176177
```

‎utilcode/src/main/java/com/blankj/utilcode/util/AppUtils.java‎

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.io.File;
1818
import java.security.MessageDigest;
1919
import java.security.NoSuchAlgorithmException;
20+
import java.util.ArrayList;
2021
import java.util.List;
2122

2223
/**
@@ -578,6 +579,80 @@ public static String getAppSignatureSHA1(@NonNull final String packageName) {
578579
replaceAll("(?<=[0-9A-F]{2})[0-9A-F]{2}", ":$0");
579580
}
580581

582+
/**
583+
* Return the application's information.
584+
* <ul>
585+
* <li>name of package</li>
586+
* <li>icon</li>
587+
* <li>name</li>
588+
* <li>path of package</li>
589+
* <li>version name</li>
590+
* <li>version code</li>
591+
* <li>is system</li>
592+
* </ul>
593+
*
594+
* @return the application's information
595+
*/
596+
public static AppInfo getAppInfo() {
597+
return getAppInfo(Utils.getApp().getPackageName());
598+
}
599+
600+
/**
601+
* Return the application's information.
602+
* <ul>
603+
* <li>name of package</li>
604+
* <li>icon</li>
605+
* <li>name</li>
606+
* <li>path of package</li>
607+
* <li>version name</li>
608+
* <li>version code</li>
609+
* <li>is system</li>
610+
* </ul>
611+
*
612+
* @param packageName The name of the package.
613+
* @return 当前应用的 AppInfo
614+
*/
615+
public static AppInfo getAppInfo(final String packageName) {
616+
try {
617+
PackageManager pm = Utils.getApp().getPackageManager();
618+
PackageInfo pi = pm.getPackageInfo(packageName, 0);
619+
return getBean(pm, pi);
620+
} catch (PackageManager.NameNotFoundException e) {
621+
e.printStackTrace();
622+
return null;
623+
}
624+
}
625+
626+
/**
627+
* Return the applications' information.
628+
*
629+
* @return the applications' information
630+
*/
631+
public static List<AppInfo> getAppsInfo() {
632+
List<AppInfo> list = new ArrayList<>();
633+
PackageManager pm = Utils.getApp().getPackageManager();
634+
List<PackageInfo> installedPackages = pm.getInstalledPackages(0);
635+
for (PackageInfo pi : installedPackages) {
636+
AppInfo ai = getBean(pm, pi);
637+
if (ai == null) continue;
638+
list.add(ai);
639+
}
640+
return list;
641+
}
642+
643+
private static AppInfo getBean(final PackageManager pm, final PackageInfo pi) {
644+
if (pm == null || pi == null) return null;
645+
ApplicationInfo ai = pi.applicationInfo;
646+
String packageName = pi.packageName;
647+
String name = ai.loadLabel(pm).toString();
648+
Drawable icon = ai.loadIcon(pm);
649+
String packagePath = ai.sourceDir;
650+
String versionName = pi.versionName;
651+
int versionCode = pi.versionCode;
652+
boolean isSystem = (ApplicationInfo.FLAG_SYSTEM & ai.flags) != 0;
653+
return new AppInfo(packageName, name, icon, packagePath, versionName, versionCode, isSystem);
654+
}
655+
581656
private static boolean isFileExists(final File file) {
582657
return file != null && file.exists();
583658
}
@@ -638,4 +713,96 @@ private static String bytes2HexString(final byte[] bytes) {
638713
}
639714
return new String(ret);
640715
}
716+
717+
/**
718+
* The application's information.
719+
*/
720+
public static class AppInfo {
721+
722+
private String packageName;
723+
private String name;
724+
private Drawable icon;
725+
private String packagePath;
726+
private String versionName;
727+
private int versionCode;
728+
private boolean isSystem;
729+
730+
public Drawable getIcon() {
731+
return icon;
732+
}
733+
734+
public void setIcon(final Drawable icon) {
735+
this.icon = icon;
736+
}
737+
738+
public boolean isSystem() {
739+
return isSystem;
740+
}
741+
742+
public void setSystem(final boolean isSystem) {
743+
this.isSystem = isSystem;
744+
}
745+
746+
public String getPackageName() {
747+
return packageName;
748+
}
749+
750+
public void setPackageName(final String packageName) {
751+
this.packageName = packageName;
752+
}
753+
754+
public String getName() {
755+
return name;
756+
}
757+
758+
public void setName(final String name) {
759+
this.name = name;
760+
}
761+
762+
public String getPackagePath() {
763+
return packagePath;
764+
}
765+
766+
public void setPackagePath(final String packagePath) {
767+
this.packagePath = packagePath;
768+
}
769+
770+
public int getVersionCode() {
771+
return versionCode;
772+
}
773+
774+
public void setVersionCode(final int versionCode) {
775+
this.versionCode = versionCode;
776+
}
777+
778+
public String getVersionName() {
779+
return versionName;
780+
}
781+
782+
public void setVersionName(final String versionName) {
783+
this.versionName = versionName;
784+
}
785+
786+
public AppInfo(String packageName, String name, Drawable icon, String packagePath,
787+
String versionName, int versionCode, boolean isSystem) {
788+
this.setName(name);
789+
this.setIcon(icon);
790+
this.setPackageName(packageName);
791+
this.setPackagePath(packagePath);
792+
this.setVersionName(versionName);
793+
this.setVersionCode(versionCode);
794+
this.setSystem(isSystem);
795+
}
796+
797+
@Override
798+
public String toString() {
799+
return "pkg name: " + getPackageName() +
800+
"\napp icon: " + getIcon() +
801+
"\napp name: " + getName() +
802+
"\napp path: " + getPackagePath() +
803+
"\napp v name: " + getVersionName() +
804+
"\napp v code: " + getVersionCode() +
805+
"\nis system: " + isSystem();
806+
}
807+
}
641808
}

0 commit comments

Comments
 (0)