feat: 优化包名获取逻辑
This commit is contained in:
@ -60,7 +60,7 @@ class PackageCheckDialogFragment : BaseDialogFragment() {
|
||||
private val mDuration = 3000
|
||||
private var mDisposable: Disposable? = null
|
||||
private var mAdapter: PackageCheckAdapter? = null
|
||||
private var mAllInstalledPackages = PackageHelper.getInstalledPackages(HaloApp.getInstance().application, 0)
|
||||
private var mAllInstalledPackages = PackageHelper.getInstalledPackageNameList(HaloApp.getInstance().application, 0)
|
||||
var gameEntity: GameEntity? = null
|
||||
var callBack: ConfirmListener? = null
|
||||
|
||||
@ -326,7 +326,7 @@ class PackageCheckDialogFragment : BaseDialogFragment() {
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
mAllInstalledPackages = PackageHelper.getInstalledPackages(HaloApp.getInstance().application, 0)
|
||||
mAllInstalledPackages = PackageHelper.getInstalledPackageNameList(HaloApp.getInstance().application, 0)
|
||||
gameEntity?.packageDialog?.let {
|
||||
if (isAllPackageInstalled(mAllInstalledPackages, it)) {
|
||||
callBack?.onConfirm()
|
||||
@ -364,7 +364,7 @@ class PackageCheckDialogFragment : BaseDialogFragment() {
|
||||
@Subscribe(threadMode = ThreadMode.MAIN)
|
||||
fun onEventMainThread(busFour: EBPackage) {
|
||||
if (busFour.isInstalledOrUninstalled()) {
|
||||
mAllInstalledPackages = PackageHelper.getInstalledPackages(HaloApp.getInstance().application, 0)
|
||||
mAllInstalledPackages = PackageHelper.getInstalledPackageNameList(HaloApp.getInstance().application, 0)
|
||||
mAdapter?.notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
@ -417,7 +417,7 @@ class PackageCheckDialogFragment : BaseDialogFragment() {
|
||||
return
|
||||
}
|
||||
|
||||
val allInstalledPackages = PackageHelper.getInstalledPackages(HaloApp.getInstance().application, 0)
|
||||
val allInstalledPackages = PackageHelper.getInstalledPackageNameList(HaloApp.getInstance().application, 0)
|
||||
if (isAllPackageInstalled(allInstalledPackages, packageDialogEntity)) {
|
||||
callBack.onConfirm()
|
||||
return
|
||||
@ -454,12 +454,12 @@ class PackageCheckDialogFragment : BaseDialogFragment() {
|
||||
}
|
||||
|
||||
private fun checkDetectionsInstalled(
|
||||
allInstalledPackages: List<PackageInfo>,
|
||||
allInstalledPackages: List<String>,
|
||||
packages: ArrayList<String>
|
||||
): Boolean {
|
||||
var isPackagesInstalled = false
|
||||
packages.forEach { packageName ->
|
||||
val isInstalled = allInstalledPackages.find { it.packageName == packageName } != null
|
||||
val isInstalled = allInstalledPackages.find { it == packageName } != null
|
||||
if (isInstalled) {
|
||||
isPackagesInstalled = true
|
||||
return@forEach
|
||||
@ -470,7 +470,7 @@ class PackageCheckDialogFragment : BaseDialogFragment() {
|
||||
|
||||
|
||||
fun isAllPackageInstalled(
|
||||
allInstalledPackages: List<PackageInfo>,
|
||||
allInstalledPackages: List<String>,
|
||||
packageDialogEntity: PackageDialogEntity
|
||||
): Boolean {
|
||||
var isAllInstalled = true
|
||||
|
||||
@ -24,7 +24,7 @@ class PackageUtilsProviderImpl : IPackageUtilsProvider {
|
||||
}
|
||||
|
||||
override fun getInstalledPackages(context: Context, flag: Int): List<PackageInfo> {
|
||||
return PackageHelper.getInstalledPackages(context, flag)
|
||||
return PackageHelper.getInstalledPackageInfoList(context, flag)
|
||||
}
|
||||
|
||||
override fun getApkSignatureByPackageName(context: Context, packageName: String): Array<String> {
|
||||
|
||||
@ -49,13 +49,8 @@ public class InstallUtils {
|
||||
@Override
|
||||
public void handleMessage(Message msg) {
|
||||
if (msg.what == INSTALL_WHAT && packageManager != null) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
List<PackageInfo> packageInfos = PackageHelper.INSTANCE.getInstalledPackages(context, 0);
|
||||
for (PackageInfo packageInfo : packageInfos) {
|
||||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
list.add(packageInfo.packageName);
|
||||
}
|
||||
}
|
||||
List<String> packageNameList = PackageHelper.INSTANCE.getInstalledPackageNameList(context, 0);
|
||||
ArrayList<String> list = new ArrayList<>(packageNameList);
|
||||
if (installMap != null && installMap.size() != 0) {
|
||||
ArrayList<String> keys = new ArrayList<>();
|
||||
for (String packageName : installMap.keySet()) {
|
||||
|
||||
@ -768,10 +768,10 @@ public class LibaoUtils {
|
||||
}
|
||||
|
||||
public static boolean isAppInstalled(Context context, String packageName) {
|
||||
List<PackageInfo> pinfo = PackageHelper.INSTANCE.getInstalledPackages(context, 0);
|
||||
if (pinfo != null) {
|
||||
for (int i = 0; i < pinfo.size(); i++) {
|
||||
String pn = pinfo.get(i).packageName;
|
||||
List<String> packageNameList = PackageHelper.INSTANCE.getInstalledPackageNameList(context, 0);
|
||||
if (packageNameList != null) {
|
||||
for (int i = 0; i < packageNameList.size(); i++) {
|
||||
String pn = packageNameList.get(i);
|
||||
if (pn.equals(packageName)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@ package com.gh.common.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.pm.ApplicationInfo
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
import android.content.pm.PermissionInfo
|
||||
@ -21,6 +20,7 @@ import com.gh.gamecenter.common.utils.toObject
|
||||
import com.gh.gamecenter.core.GHThreadFactory
|
||||
import com.gh.gamecenter.core.runOnUiThread
|
||||
import com.gh.gamecenter.core.utils.SPUtils
|
||||
import com.gh.gamecenter.core.utils.SentryHelper
|
||||
import com.gh.gamecenter.entity.WhitePackageListEntity
|
||||
import com.gh.gamecenter.feature.entity.GameEntity
|
||||
import com.gh.gamecenter.feature.entity.SettingsEntity
|
||||
@ -42,11 +42,14 @@ object PackageHelper {
|
||||
|
||||
private const val SP_GET_INSTALLED_PACKAGES_AGREED = "get_installed_packages_agreed" // 用户是否同意使用已安装应用列表 API
|
||||
|
||||
private const val SP_GET_INSTALLED_PACKAGES_BY_ALTERNATIVE_API = "get_installed_packages_by_alternative_api" // 是否使用另类方式获取已安装应用列表
|
||||
private const val SP_GET_INSTALLED_PACKAGES_BY_ALTERNATIVE_API =
|
||||
"get_installed_packages_by_alternative_api" // 是否使用另类方式获取已安装应用列表
|
||||
|
||||
private const val SP_USER_USED_GET_INSTALLED_API_SWITCH_PACKAGE = "user_used_get_installed_api_switch_package" // 用户是否使用过含有已安装应用列表获取开关的包
|
||||
private const val SP_USER_USED_GET_INSTALLED_API_SWITCH_PACKAGE =
|
||||
"user_used_get_installed_api_switch_package" // 用户是否使用过含有已安装应用列表获取开关的包
|
||||
|
||||
private const val SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST = "additional_whitelist_package_name_list" // 额外的白名单包名列表 (曾经更正为已安装的包名列表)
|
||||
private const val SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST =
|
||||
"additional_whitelist_package_name_list" // 额外的白名单包名列表 (曾经更正为已安装的包名列表)
|
||||
|
||||
private const val UNKNOWN = -1
|
||||
private const val UNSUPPORTED = 0
|
||||
@ -56,7 +59,8 @@ object PackageHelper {
|
||||
private const val DISABLED = 3
|
||||
|
||||
private var lastSuccessfullyGetInstalledPackagesTimeMills = 0L
|
||||
private var cachedInstalledPackagesList: List<PackageInfo> = arrayListOf()
|
||||
private var cachedInstalledPackageInfoList: List<PackageInfo> = arrayListOf() // 缓存的已安装应用列表
|
||||
private var cachedInstalledPackageNameList: List<String> = arrayListOf() // 缓存的已安装应用包名列表
|
||||
private var additionalWhiteListPackageNameSet: HashSet<String> = hashSetOf()
|
||||
|
||||
private var isGetInstalledPackagesAgreed = false // 用户是否已经同意使用已安装应用列表
|
||||
@ -69,6 +73,8 @@ object PackageHelper {
|
||||
|
||||
private val packageExecutor by lazy { Executors.newSingleThreadExecutor(GHThreadFactory("GH_PACKAGE_THREAD")) }
|
||||
|
||||
private var uploadUIDGapLog = true
|
||||
|
||||
// 评论黑名单包名列表,避免用户安装了 Xposed Installer 这样的工具,也能在包含该安装包的游戏详情页评论
|
||||
private var _commentPackageNameBlackList = arrayListOf<String>()
|
||||
val commentPackageNameBlackList: ArrayList<String> = _commentPackageNameBlackList
|
||||
@ -105,7 +111,8 @@ object PackageHelper {
|
||||
* 用于覆盖安装首次启动时的检查 https://jira.shanqu.cc/browse/GHZSCY-5694
|
||||
*/
|
||||
fun checkIfGetInstalledApiSwitchShouldBeIgnored(context: Context) {
|
||||
val userHasUsedGetInstalledApiSwitchPackage = SPUtils.getBoolean(SP_USER_USED_GET_INSTALLED_API_SWITCH_PACKAGE, false)
|
||||
val userHasUsedGetInstalledApiSwitchPackage =
|
||||
SPUtils.getBoolean(SP_USER_USED_GET_INSTALLED_API_SWITCH_PACKAGE, false)
|
||||
if (!userHasUsedGetInstalledApiSwitchPackage) {
|
||||
val isSupportGetInstalledAppsPermission = isSupportGetInstalledAppsPermission(context)
|
||||
|
||||
@ -182,12 +189,10 @@ object PackageHelper {
|
||||
private fun getAllPackageName(context: Context): HashSet<String> {
|
||||
val set = HashSet<String>()
|
||||
return try {
|
||||
val packageInfos = getInstalledPackages(context, 0)
|
||||
for (packageInfo in packageInfos) {
|
||||
if (packageInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM == 0) {
|
||||
if (context.packageName != packageInfo.packageName) {
|
||||
set.add(packageInfo.packageName)
|
||||
}
|
||||
val packageNameList = getInstalledPackageNameList(context, 0)
|
||||
for (packageName in packageNameList) {
|
||||
if (context.packageName != packageName) {
|
||||
set.add(packageName)
|
||||
}
|
||||
}
|
||||
set
|
||||
@ -240,7 +245,8 @@ object PackageHelper {
|
||||
if (isEnabled) {
|
||||
Utils.log(TAG, "开启获取已安装应用列表限制")
|
||||
|
||||
additionalWhiteListPackageNameSet = SPUtils.getString(SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST).toObject() ?: hashSetOf()
|
||||
additionalWhiteListPackageNameSet =
|
||||
SPUtils.getString(SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST).toObject() ?: hashSetOf()
|
||||
|
||||
Utils.log(TAG, "额外的白名单为 $additionalWhiteListPackageNameSet")
|
||||
|
||||
@ -269,12 +275,12 @@ object PackageHelper {
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户是否已经允许了调用获取已安装应用列表接口
|
||||
* 优先用内存的值,没有再从 SP 中获取并更新
|
||||
* 用户是否已经使用另类方式获取已安装应用列表
|
||||
*/
|
||||
private fun isUseAlternativeWayToGetInstalledPackages(): Boolean {
|
||||
return useAlternativeWayToGetInstalledPackages
|
||||
|| (SPUtils.getBoolean(SP_GET_INSTALLED_PACKAGES_BY_ALTERNATIVE_API).also { useAlternativeWayToGetInstalledPackages = it })
|
||||
|| (SPUtils.getBoolean(SP_GET_INSTALLED_PACKAGES_BY_ALTERNATIVE_API)
|
||||
.also { useAlternativeWayToGetInstalledPackages = it })
|
||||
}
|
||||
|
||||
private fun updateUseAlternativeWayToGetInstalledPackages() {
|
||||
@ -290,20 +296,37 @@ object PackageHelper {
|
||||
/**
|
||||
* 获取已安装应用列表
|
||||
*/
|
||||
fun getInstalledPackages(context: Context?, flags: Int): List<PackageInfo> {
|
||||
Utils.log(TAG, "即将获取已安装应用列表")
|
||||
fun getInstalledPackageInfoList(context: Context?, flags: Int): List<PackageInfo> {
|
||||
return getInstalledListInternal(context, flags, false).first
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已安装应用包名列表
|
||||
*/
|
||||
fun getInstalledPackageNameList(context: Context?, flags: Int): List<String> {
|
||||
return getInstalledListInternal(context, flags, true).second
|
||||
}
|
||||
|
||||
private fun getInstalledListInternal(context: Context?,
|
||||
flags: Int,
|
||||
packageNameOnly: Boolean): Pair<List<PackageInfo>, List<String>> {
|
||||
Utils.log(TAG, "即将获取已安装应用列表,仅获取包名 $packageNameOnly")
|
||||
|
||||
// 用户未同意使用已安装应用列表 API,返回空列表
|
||||
if (!isGetInstalledPackagesAgreed()) {
|
||||
Utils.log(TAG, "用户未同意使用已安装应用列表 API,返回空列表")
|
||||
return cachedInstalledPackagesList
|
||||
return Pair(cachedInstalledPackageInfoList, cachedInstalledPackageNameList)
|
||||
}
|
||||
|
||||
// 简单 debounce 过于频繁的获取已安装应用列表调用
|
||||
if (System.currentTimeMillis() - lastSuccessfullyGetInstalledPackagesTimeMills < 20 * 1000
|
||||
&& cachedInstalledPackagesList.isNotEmpty()) {
|
||||
Utils.log(TAG, "使用了缓存的已安装应用列表")
|
||||
return cachedInstalledPackagesList
|
||||
if (System.currentTimeMillis() - lastSuccessfullyGetInstalledPackagesTimeMills < 20 * 1000) {
|
||||
// 时间间隔合适且对应的列表不为空,直接返回对应的缓存列表数据
|
||||
if ((packageNameOnly && cachedInstalledPackageNameList.isNotEmpty())
|
||||
|| (!packageNameOnly && cachedInstalledPackageInfoList.isNotEmpty())
|
||||
) {
|
||||
Utils.log(TAG, "使用了缓存的已安装应用列表")
|
||||
return Pair(cachedInstalledPackageInfoList, cachedInstalledPackageNameList)
|
||||
}
|
||||
}
|
||||
|
||||
var shouldGetNewInstalledPackagedList = false
|
||||
@ -325,12 +348,17 @@ object PackageHelper {
|
||||
|
||||
if (shouldGetNewInstalledPackagedList) {
|
||||
lastSuccessfullyGetInstalledPackagesTimeMills = System.currentTimeMillis()
|
||||
cachedInstalledPackagesList = getInstalledPackagesInternal(context, flags)
|
||||
|
||||
Utils.log(TAG, "获取已安装应用列表成功,数量为 ${cachedInstalledPackagesList.size}")
|
||||
if (packageNameOnly) {
|
||||
cachedInstalledPackageNameList = getInstalledPackageNameListInternal(context, flags)
|
||||
Utils.log(TAG, "获取已安装应用列表成功,数量为 ${cachedInstalledPackageNameList.size}")
|
||||
} else {
|
||||
cachedInstalledPackageInfoList = getInstalledPackageInfoListInternal(context, flags)
|
||||
Utils.log(TAG, "获取已安装应用列表成功,数量为 ${cachedInstalledPackageInfoList.size}")
|
||||
}
|
||||
}
|
||||
|
||||
return cachedInstalledPackagesList
|
||||
return Pair(cachedInstalledPackageInfoList, cachedInstalledPackageNameList)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -520,7 +548,10 @@ object PackageHelper {
|
||||
additionalWhiteListPackageNameSet.remove(packageName)
|
||||
}
|
||||
|
||||
Utils.log(TAG, "updateAdditionalWhiteListPackageName 更新额外的白名单包名列表 $isAdd $packageName ,结果是 $isUpdated")
|
||||
Utils.log(
|
||||
TAG,
|
||||
"updateAdditionalWhiteListPackageName 更新额外的白名单包名列表 $isAdd $packageName ,结果是 $isUpdated"
|
||||
)
|
||||
|
||||
if (isUpdated) {
|
||||
SPUtils.setString(SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST, additionalWhiteListPackageNameSet.toJson())
|
||||
@ -541,17 +572,25 @@ object PackageHelper {
|
||||
val installedVersionName = PackageUtils.getVersionNameByPackageName(packageName)
|
||||
|
||||
if (PackagesManager.isInstalled(packageName)
|
||||
&& installedVersionName == null) {
|
||||
&& installedVersionName == null
|
||||
) {
|
||||
uninstalledButKeepingWrongStatusPackageNameSet.add(packageName)
|
||||
} else if (PackagesManager.isInstalled(packageName)
|
||||
&& installedVersionName != null
|
||||
&& !PackagesManager.isInstalledWithSpecificVersion(packageName, installedVersionName)) {
|
||||
&& !PackagesManager.isInstalledWithSpecificVersion(packageName, installedVersionName)
|
||||
) {
|
||||
updatedButKeepingWrongStatusPackageNameSet.add(packageName)
|
||||
}
|
||||
}
|
||||
|
||||
Utils.log(TAG, "refreshWrongInstallStatus 需要更新已更新状态的包数量为 ${updatedButKeepingWrongStatusPackageNameSet.size}")
|
||||
Utils.log(TAG, "refreshWrongInstallStatus 需要移除已安装的包数量为 ${uninstalledButKeepingWrongStatusPackageNameSet.size}")
|
||||
Utils.log(
|
||||
TAG,
|
||||
"refreshWrongInstallStatus 需要更新已更新状态的包数量为 ${updatedButKeepingWrongStatusPackageNameSet.size}"
|
||||
)
|
||||
Utils.log(
|
||||
TAG,
|
||||
"refreshWrongInstallStatus 需要移除已安装的包数量为 ${uninstalledButKeepingWrongStatusPackageNameSet.size}"
|
||||
)
|
||||
|
||||
runOnUiThread {
|
||||
if (uninstalledButKeepingWrongStatusPackageNameSet.isNotEmpty()) {
|
||||
@ -560,7 +599,10 @@ object PackageHelper {
|
||||
additionalWhiteListPackageNameSet.remove(packageName)
|
||||
}
|
||||
|
||||
SPUtils.setString(SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST, additionalWhiteListPackageNameSet.toString())
|
||||
SPUtils.setString(
|
||||
SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST,
|
||||
additionalWhiteListPackageNameSet.toString()
|
||||
)
|
||||
}
|
||||
|
||||
if (updatedButKeepingWrongStatusPackageNameSet.isNotEmpty()) {
|
||||
@ -608,9 +650,18 @@ object PackageHelper {
|
||||
}
|
||||
}
|
||||
|
||||
Utils.log(TAG, "refreshWrongInstallStatus 需要更新已安装状态的包数量为 ${installedButKeepingWrongStatusPackageNameSet.size}")
|
||||
Utils.log(TAG, "refreshWrongInstallStatus 需要更新已更新状态的包数量为 ${updatedButKeepingWrongStatusPackageNameSet.size}")
|
||||
Utils.log(TAG, "refreshWrongInstallStatus 需要移除已安装的包数量为 ${uninstalledButKeepingWrongStatusPackageNameSet.size}")
|
||||
Utils.log(
|
||||
TAG,
|
||||
"refreshWrongInstallStatus 需要更新已安装状态的包数量为 ${installedButKeepingWrongStatusPackageNameSet.size}"
|
||||
)
|
||||
Utils.log(
|
||||
TAG,
|
||||
"refreshWrongInstallStatus 需要更新已更新状态的包数量为 ${updatedButKeepingWrongStatusPackageNameSet.size}"
|
||||
)
|
||||
Utils.log(
|
||||
TAG,
|
||||
"refreshWrongInstallStatus 需要移除已安装的包数量为 ${uninstalledButKeepingWrongStatusPackageNameSet.size}"
|
||||
)
|
||||
|
||||
runOnUiThread {
|
||||
if (installedButKeepingWrongStatusPackageNameSet.isNotEmpty()) {
|
||||
@ -619,7 +670,10 @@ object PackageHelper {
|
||||
additionalWhiteListPackageNameSet.add(packageName)
|
||||
}
|
||||
|
||||
SPUtils.setString(SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST, additionalWhiteListPackageNameSet.toString())
|
||||
SPUtils.setString(
|
||||
SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST,
|
||||
additionalWhiteListPackageNameSet.toString()
|
||||
)
|
||||
}
|
||||
|
||||
if (uninstalledButKeepingWrongStatusPackageNameSet.isNotEmpty()) {
|
||||
@ -628,7 +682,10 @@ object PackageHelper {
|
||||
additionalWhiteListPackageNameSet.remove(packageName)
|
||||
}
|
||||
|
||||
SPUtils.setString(SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST, additionalWhiteListPackageNameSet.toString())
|
||||
SPUtils.setString(
|
||||
SP_ADDITIONAL_WHITELIST_PACKAGE_NAME_LIST,
|
||||
additionalWhiteListPackageNameSet.toString()
|
||||
)
|
||||
}
|
||||
|
||||
if (updatedButKeepingWrongStatusPackageNameSet.isNotEmpty()) {
|
||||
@ -640,7 +697,7 @@ object PackageHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getInstalledPackagesInternal(
|
||||
private fun getInstalledPackageInfoListInternal(
|
||||
context: Context,
|
||||
flags: Int
|
||||
): List<PackageInfo> {
|
||||
@ -653,6 +710,26 @@ object PackageHelper {
|
||||
}
|
||||
}
|
||||
|
||||
private fun getInstalledPackageNameListInternal(
|
||||
context: Context,
|
||||
flags: Int
|
||||
): List<String> {
|
||||
if (isUseAlternativeWayToGetInstalledPackages()) {
|
||||
Utils.log(TAG, "调用另类系统 API 获取已安装应用包名列表")
|
||||
return getInstalledPackageNameByAlternative(context)
|
||||
} else {
|
||||
Utils.log(TAG, "调用默认系统 API 获取已安装应用包名列表")
|
||||
val packageInfoList = getInstalledPackageByDefault(context, flags)
|
||||
val packageList = arrayListOf<String>()
|
||||
for (packageInfo in packageInfoList) {
|
||||
if (context.packageName != packageInfo.packageName) {
|
||||
packageList.add(packageInfo.packageName)
|
||||
}
|
||||
}
|
||||
return packageList
|
||||
}
|
||||
}
|
||||
|
||||
private fun getInstalledPackageByDefault(context: Context, flags: Int): List<PackageInfo> {
|
||||
val pm = context.packageManager
|
||||
try {
|
||||
@ -696,4 +773,39 @@ object PackageHelper {
|
||||
return packageList
|
||||
}
|
||||
|
||||
private fun getInstalledPackageNameByAlternative(context: Context): ArrayList<String> {
|
||||
val packageManager = context.getPackageManager()
|
||||
val packageList = arrayListOf<String>()
|
||||
var packagesArray: Array<String>? = null
|
||||
var uid = android.os.Process.FIRST_APPLICATION_UID
|
||||
|
||||
var lastValidUid = 0
|
||||
|
||||
while (uid <= android.os.Process.LAST_APPLICATION_UID) {
|
||||
try {
|
||||
packagesArray = packageManager.getPackagesForUid(uid)
|
||||
if (packagesArray != null && packagesArray.isNotEmpty()) {
|
||||
lastValidUid = uid
|
||||
|
||||
for (packageName in packagesArray) {
|
||||
packageList.add(packageName)
|
||||
}
|
||||
}
|
||||
} catch (securityException: SecurityException) {
|
||||
securityException.printStackTrace()
|
||||
}
|
||||
uid++
|
||||
}
|
||||
|
||||
if (HaloApp.getInstance().isNewForThisVersion && uploadUIDGapLog) {
|
||||
// 仅应用该版本第一次启动的第一次方法调用上报日志
|
||||
uploadUIDGapLog = false
|
||||
|
||||
val uidGap = (android.os.Process.LAST_APPLICATION_UID - lastValidUid) / 100 * 100
|
||||
SentryHelper.onEvent("UID_GAP", "gap", uidGap.toString())
|
||||
}
|
||||
|
||||
return packageList
|
||||
}
|
||||
|
||||
}
|
||||
@ -661,64 +661,32 @@ public class PackageUtils {
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取所有已安装的软件的包名、版本(非系统应用)
|
||||
* 获取所有已安装的软件的包名(包括系统应用)
|
||||
*/
|
||||
public static ArrayList<String> getAllPackageName(Context context) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
List<PackageInfo> packageInfos = PackageHelper.INSTANCE.getInstalledPackages(context, 0);
|
||||
for (PackageInfo packageInfo : packageInfos) {
|
||||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
if (!context.getPackageName().equals(packageInfo.packageName)) {
|
||||
list.add(packageInfo.packageName);
|
||||
}
|
||||
List<String> packageNameList = PackageHelper.INSTANCE.getInstalledPackageNameList(context, 0);
|
||||
for (String packageName : packageNameList) {
|
||||
if (!context.getPackageName().equals(packageName)) {
|
||||
list.add(packageName);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static ArrayList<String> getAllPackageNameIncludeGh(Context context) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
List<PackageInfo> packageInfos = PackageHelper.INSTANCE.getInstalledPackages(context, 0);
|
||||
for (PackageInfo packageInfo : packageInfos) {
|
||||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
list.add(packageInfo.packageName);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/*
|
||||
* 获取所有已安装的软件的包名(包括系统应用)
|
||||
*/
|
||||
public static ArrayList<String> getAllPackageNameIncludeSystemApps(Context context) {
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
List<PackageInfo> packageInfos = PackageHelper.INSTANCE.getInstalledPackages(context, 0);
|
||||
for (PackageInfo packageInfo : packageInfos) {
|
||||
list.add(packageInfo.packageName);
|
||||
}
|
||||
return list;
|
||||
List<String> packageNameList = PackageHelper.INSTANCE.getInstalledPackageNameList(context, 0);
|
||||
return new ArrayList<>(packageNameList);
|
||||
}
|
||||
|
||||
public static JSONArray getAppList(Context context) {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
try {
|
||||
PackageManager pm = context.getPackageManager();
|
||||
List<PackageInfo> packageInfos = PackageHelper.INSTANCE.getInstalledPackages(context, 0);
|
||||
for (PackageInfo packageInfo : packageInfos) {
|
||||
if ((packageInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 这里的 pm.getApplicationLabel 有极小机率会返回 null 值 (明明方法标示了返回值 nonnull)
|
||||
// Attempt to invoke interface method 'java.lang.String java.lang.CharSequence.toString()' on a null object reference
|
||||
// 所以要是为空就 continue,忽略掉它
|
||||
if (pm.getApplicationLabel(packageInfo.applicationInfo) == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
jsonObject.put("name", pm.getApplicationLabel(packageInfo.applicationInfo).toString());
|
||||
jsonObject.put("package", packageInfo.packageName);
|
||||
jsonObject.put("version", packageInfo.versionName);
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
List<String> packageNameList = PackageHelper.INSTANCE.getInstalledPackageNameList(context, 0);
|
||||
for (String packageName : packageNameList) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("package", packageName);
|
||||
jsonArray.put(jsonObject);
|
||||
}
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@ -61,7 +61,7 @@ object BrowserInstallHelper {
|
||||
}
|
||||
|
||||
else -> {
|
||||
val allInstalledPackageList = PackageUtils.getAllPackageNameIncludeSystemApps(mContext)
|
||||
val allInstalledPackageList = PackageUtils.getAllPackageName(mContext)
|
||||
|
||||
if (allInstalledPackageList.isNotEmpty()) {
|
||||
mValidInstalledPackageList = allInstalledPackageList
|
||||
|
||||
@ -7,7 +7,6 @@ import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
@ -189,14 +188,11 @@ public class CleanApkAdapter extends BaseRecyclerAdapter<KcSelectGameViewHolder>
|
||||
}
|
||||
|
||||
private int doType(String packageName) {
|
||||
List<PackageInfo> pakageinfos = PackageHelper.INSTANCE.getInstalledPackages(mContext, 0);
|
||||
for (PackageInfo pi : pakageinfos) {
|
||||
if ((pi.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
|
||||
String pi_packageName = pi.packageName;
|
||||
//如果这个包名在系统已经安装过的应用中存在
|
||||
if (packageName.endsWith(pi_packageName)) {
|
||||
return INSTALLED;
|
||||
}
|
||||
List<String> pakageNameList = PackageHelper.INSTANCE.getInstalledPackageNameList(mContext, 0);
|
||||
for (String pkgName : pakageNameList) {
|
||||
//如果这个包名在系统已经安装过的应用中存在
|
||||
if (packageName.endsWith(pkgName)) {
|
||||
return INSTALLED;
|
||||
}
|
||||
}
|
||||
return UNINSTALLED;
|
||||
|
||||
@ -56,7 +56,7 @@ class SimulatorManagementViewModel(application: Application) :
|
||||
return Single.create<List<SimulatorEntity>> { emitter ->
|
||||
val simulatorEntityList = ArrayList<SimulatorEntity>()
|
||||
val allInstalledPackages =
|
||||
PackageHelper.getInstalledPackages(HaloApp.getInstance().application, PackageManager.GET_ACTIVITIES)
|
||||
PackageHelper.getInstalledPackageInfoList(HaloApp.getInstance().application, PackageManager.GET_ACTIVITIES)
|
||||
allInstalledPackages.forEach {
|
||||
if (it.packageName.contains("com.gh")) {
|
||||
val activityInfo =
|
||||
|
||||
Reference in New Issue
Block a user