313 lines
12 KiB
Java
313 lines
12 KiB
Java
package com.gh.common.util;
|
||
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.pm.ApplicationInfo;
|
||
import android.content.pm.PackageInfo;
|
||
import android.content.pm.PackageManager;
|
||
import android.content.pm.PackageManager.NameNotFoundException;
|
||
import android.content.pm.Signature;
|
||
import android.net.Uri;
|
||
import android.os.Bundle;
|
||
import android.text.TextUtils;
|
||
import android.widget.Toast;
|
||
|
||
import com.gh.gamecenter.entity.GameUpdateEntity;
|
||
|
||
import org.json.JSONArray;
|
||
import org.json.JSONException;
|
||
import org.json.JSONObject;
|
||
|
||
import java.io.ByteArrayInputStream;
|
||
import java.io.File;
|
||
import java.security.cert.CertificateException;
|
||
import java.security.cert.CertificateFactory;
|
||
import java.security.cert.X509Certificate;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
|
||
public class PackageUtils {
|
||
|
||
public static final String publicKey = "OpenSSLRSAPublicKey{modulus=a8c4bb5748fec8d5c35db1a7a182d41ba4721a91131a417330af79ef4ddb43f9fa0ff4907b0a613bfe152de0ed8fc1b2e6f94a908aa98a5f7adc1ce814ba7ec919d75d9910bdfd8649b4789da6a90ffb61f0d23ac4f828a78fcd0d6f6120c1c43c1f87f7498a89eb40ca8e32dfc2f9d5c10d612b95192870223674e241e53305abf320d7eed76ded398778576e4db7b17b3bc6a792f13de5e43a6a5fae4276c73e6990ce97f68dff0ec16fc9594f175c8d49cd0d7877340d9de60942ca0efc737e50b6c295dfe0713e4532b4e810e1ea11b702b4a27753e41559cbceb247e7f044ec4e3ab2e8bccd8b9fd71286e63307550bcde86deee95adb8133076269135b,publicExponent=10001}";
|
||
|
||
/*
|
||
* 判断是否可以更新,只判断gh_version的大小
|
||
*/
|
||
public static boolean isCanUpdate(Context context, GameUpdateEntity gameUpdateEntity) {
|
||
// 判断是否gh_version是否存在
|
||
String gh_version = (String) PackageUtils.getMetaData(
|
||
context, gameUpdateEntity.getPackageName(), "gh_version");
|
||
if (gh_version != null) {
|
||
gh_version = gh_version.substring(2);
|
||
// 判断gh_version的大小
|
||
return Long.parseLong(gh_version) < Long.parseLong(gameUpdateEntity.getGhVersion());
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 获取meta-data
|
||
*/
|
||
public static Object getMetaData(Context context, String packageName, String name) {
|
||
try {
|
||
Bundle metaDate = context.getApplicationContext().getPackageManager().getApplicationInfo(
|
||
packageName, PackageManager.GET_META_DATA).metaData;
|
||
if (metaDate != null) {
|
||
return metaDate.get(name);
|
||
}
|
||
} catch (NameNotFoundException e) {
|
||
// e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/*
|
||
* 判断是否是插件包
|
||
*/
|
||
public static boolean isSignature(Context context, String packageName) {
|
||
String signature = getApkSignatureByPackageName(context, packageName);
|
||
return publicKey.equals(signature);
|
||
}
|
||
|
||
/*
|
||
* 根据包名,获取apk的签名信息
|
||
*/
|
||
public static String getApkSignatureByPackageName(Context context, String packageName) {
|
||
try {
|
||
PackageInfo packageInfo = context.getApplicationContext().getPackageManager()
|
||
.getPackageInfo(packageName, PackageManager.GET_SIGNATURES);
|
||
Signature[] signatures = packageInfo.signatures;
|
||
return parseSignature(signatures[0].toByteArray())[0];
|
||
} catch (NameNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/*
|
||
* 解析签名
|
||
*/
|
||
private static String[] parseSignature(byte[] signature) {
|
||
String[] ret = null;
|
||
try {
|
||
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
|
||
X509Certificate cert = (X509Certificate) certFactory.generateCertificate(
|
||
new ByteArrayInputStream(signature));
|
||
ret = new String[]{cert.getPublicKey().toString(), cert.getSerialNumber().toString()};
|
||
} catch (CertificateException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
/*
|
||
* 启动安装应用程序
|
||
*/
|
||
public static void launchSetup(final Context context, final String path) {
|
||
if (isCanLaunchSetup(context, path)) {
|
||
context.startActivity(PackageUtils.getInstallIntent(context, path));
|
||
} else {
|
||
DialogUtils.showPluginDialog(context, new DialogUtils.ConfirmListener() {
|
||
@Override
|
||
public void onConfirm() {
|
||
context.startActivity(PackageUtils.getUninstallIntent(context, path));
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 根据apk路径,获取apk包名、签名 根据包名 判断 是否已安装游戏 根据签名 判断 是否一致
|
||
*/
|
||
public static boolean isCanLaunchSetup(Context context, String path) {
|
||
String packageName = getPackageNameByPath(context, path);
|
||
|
||
if (TextUtils.isEmpty(packageName)) {
|
||
return true;
|
||
}
|
||
boolean isContain = com.gh.gamecenter.manager.PackageManager.isInstalled(packageName);
|
||
if (!isContain) {
|
||
return true;
|
||
}
|
||
|
||
boolean isInstalled = isInstalled(context, packageName);
|
||
if (!isInstalled) {
|
||
return true;
|
||
}
|
||
|
||
String signature = getApkSignatureByPackageName(context, packageName);
|
||
return publicKey.equals(signature);
|
||
}
|
||
|
||
/*
|
||
* 根据路径,获取安装游戏的意图
|
||
*/
|
||
public static Intent getInstallIntent(Context context, String path) {
|
||
Uri uri = Uri.fromFile(new File(path));
|
||
Intent installIntent = new Intent(Intent.ACTION_VIEW);
|
||
installIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
installIntent.setDataAndType(uri, "application/vnd.android.package-archive");
|
||
InstallUtils.getInstance(context).addInstall(getPackageNameByPath(context, path));
|
||
return installIntent;
|
||
}
|
||
|
||
/*
|
||
* 根据包名,获取卸载游戏的意图
|
||
*/
|
||
public static Intent getUninstallIntent(Context context, String path) {
|
||
Intent uninstallIntent = new Intent();
|
||
uninstallIntent.setAction(Intent.ACTION_DELETE);
|
||
uninstallIntent.addCategory(Intent.CATEGORY_DEFAULT);
|
||
String packageName = getPackageNameByPath(context, path);
|
||
uninstallIntent.setData(Uri.parse("package:" + packageName));
|
||
InstallUtils.getInstance(context).addUninstall(packageName);
|
||
return uninstallIntent;
|
||
}
|
||
|
||
/*
|
||
* 根据路径,获取apk的包名
|
||
*/
|
||
public static String getPackageNameByPath(Context context, String path) {
|
||
PackageManager packageManager = context.getApplicationContext().getPackageManager();
|
||
PackageInfo info = packageManager.getPackageArchiveInfo(path, PackageManager.GET_ACTIVITIES);
|
||
if (info != null) {
|
||
ApplicationInfo appInfo = info.applicationInfo;
|
||
return appInfo.packageName;
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/*
|
||
* 根据包名,判断是否已安装该游戏
|
||
*/
|
||
public static boolean isInstalled(Context context, String packageName) {
|
||
Intent intent = context.getApplicationContext().getPackageManager().getLaunchIntentForPackage(packageName);
|
||
return intent != null;
|
||
}
|
||
|
||
/*
|
||
* 获取应用第一次安装的时间
|
||
*/
|
||
public static long getInstalledTime(Context context, String packageName) {
|
||
try {
|
||
PackageInfo packageInfo = context.getApplicationContext().getPackageManager()
|
||
.getPackageInfo(packageName, 0);
|
||
return packageInfo.firstInstallTime;
|
||
} catch (NameNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
/*
|
||
* 返回光环助手的版本信息
|
||
*/
|
||
public static String getVersionName(Context context) {
|
||
try {
|
||
PackageInfo pkgInfo = context.getApplicationContext().getPackageManager().getPackageInfo(
|
||
context.getPackageName(), 0);
|
||
return pkgInfo.versionName;
|
||
} catch (NameNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/*
|
||
* 返回光环助手的版本code
|
||
*/
|
||
public static String getVersionCode(Context context) {
|
||
try {
|
||
PackageInfo pkgInfo = context.getApplicationContext().getPackageManager().getPackageInfo(
|
||
context.getPackageName(), 0);
|
||
return String.valueOf(pkgInfo.versionCode);
|
||
} catch (NameNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/*
|
||
* 获取apk的版本
|
||
*/
|
||
public static String getVersionByPackage(Context context, String packageName) {
|
||
try {
|
||
return context.getApplicationContext().getPackageManager().getPackageInfo(packageName,
|
||
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT).versionName;
|
||
} catch (NameNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/*
|
||
* 获取所有已安装的软件的包名、版本(非系统应用)
|
||
*/
|
||
public static ArrayList<String> getAllPackageName(Context context) {
|
||
ArrayList<String> list = new ArrayList<>();
|
||
List<PackageInfo> packageInfos = context.getApplicationContext().getPackageManager().getInstalledPackages(0);
|
||
for (PackageInfo packageInfo : packageInfos) {
|
||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||
if (!context.getPackageName().equals(packageInfo.packageName)) {
|
||
list.add(packageInfo.packageName);
|
||
}
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public static JSONArray getAppList(Context context) {
|
||
JSONArray jsonArray = new JSONArray();
|
||
try {
|
||
PackageManager pm = context.getPackageManager();
|
||
List<PackageInfo> packageInfos = pm.getInstalledPackages(0);
|
||
for (PackageInfo packageInfo : packageInfos) {
|
||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||
JSONObject jsonObject = new JSONObject();
|
||
jsonObject.put("name", pm.getApplicationLabel(packageInfo.applicationInfo).toString());
|
||
jsonObject.put("package", packageInfo.packageName);
|
||
jsonObject.put("version", packageInfo.versionName);
|
||
jsonArray.put(jsonObject);
|
||
}
|
||
}
|
||
} catch (JSONException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return jsonArray;
|
||
}
|
||
|
||
/*
|
||
* 启动应用
|
||
*/
|
||
public static void launchApplicationByPackageName(Context context, String packageName) {
|
||
try {
|
||
Intent intent = context.getApplicationContext().getPackageManager().getLaunchIntentForPackage(packageName);
|
||
if (intent != null) {
|
||
context.startActivity(intent);
|
||
} else {
|
||
Toast.makeText(context, "启动失败", Toast.LENGTH_SHORT).show();
|
||
}
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
Toast.makeText(context, "启动失败", Toast.LENGTH_SHORT).show();
|
||
}
|
||
}
|
||
|
||
/*
|
||
* 根据包名,获取软件名称
|
||
*/
|
||
public static String getNameByPackageName(Context context, String packageName) {
|
||
try {
|
||
PackageManager pm = context.getApplicationContext().getPackageManager();
|
||
ApplicationInfo applicationInfo = pm.getApplicationInfo(
|
||
packageName, 0);
|
||
return applicationInfo.loadLabel(pm).toString();
|
||
} catch (NameNotFoundException e) {
|
||
e.printStackTrace();
|
||
}
|
||
return null;
|
||
}
|
||
|
||
}
|