欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

如何禁止第三方软件不能安装

程序员文章站 2022-03-04 23:25:40
如何禁止第三方软件不能安装类别需求索引类别禁止静默安装install apk问题描述对于应用市场来说,内存大小总是不够大,所以在一定空间后(eg:小于500M后)禁止安装第三方软件。代码关联android/frameworks/base$ git diffdiff --git a/cmds/pm/src/com/android/commands/pm/Pm.java b/cmds/pm/src/com/android/commands/pm/Pm.javaindex a505a69..1...

如何禁止第三方软件不能安装

类别

需求

索引类别

禁止静默安装install apk

问题描述

对于应用市场来说,内存大小总是不够大,所以在一定空间后(eg:小于500M后)禁止安装第三方软件。

代码关联

android/frameworks/base$ git diff
diff --git a/cmds/pm/src/com/android/commands/pm/Pm.java b/cmds/pm/src/com/android/commands/pm/Pm.java
index a505a69..1f819b1 100644
--- a/cmds/pm/src/com/android/commands/pm/Pm.java
+++ b/cmds/pm/src/com/android/commands/pm/Pm.java
@@ -1081,6 +1081,8 @@ public final class Pm {
                 installFlags |= PackageManager.INSTALL_ALLOW_DOWNGRADE;
             } else if (opt.equals("-g")) {
                 installFlags |= PackageManager.INSTALL_GRANT_RUNTIME_PERMISSIONS;
+            } else if (opt.equals("-c")) { 	//这步主要是为了区分对应是否是内置的强制推送安装的apk
+                installFlags |= PackageManager.INSTALL_USE_HEAD_SPACE_400M;
             } else if (opt.equals("--originating-uri")) {
                 originatingUriString = nextOptionData();
                 if (originatingUriString == null) {
diff --git a/core/java/android/content/pm/PackageManager.java b/core/java/android/content/pm/PackageManager.java
index 433ab66..eec7a56 100755
--- a/core/java/android/content/pm/PackageManager.java
+++ b/core/java/android/content/pm/PackageManager.java
@@ -421,6 +421,9 @@ public abstract class PackageManager {
     /** {@hide} */
     public static final int INSTALL_FORCE_VOLUME_UUID = 0x00000200;

+    /* {@hide}Force the use of reserved space */
+    public static final int INSTALL_USE_HEAD_SPACE_400M = 0x00000400;
+

diff --git a/services/core/java/com/android/server/pm/PackageManagerService.java b/services/core/java/com/android/server/pm/PackageManagerService.java
index dd959ec..12a2bfd 100755
--- a/services/core/java/com/android/server/pm/PackageManagerService.java
+++ b/services/core/java/com/android/server/pm/PackageManagerService.java
@@ -147,6 +147,7 @@ import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Debug;
 import android.os.Binder;
+import android.os.StatFs;
 import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
@@ -10116,10 +10117,38 @@ public class PackageManagerService extends IPackageManager.Stub {
                 verificationParams, packageAbiOverride, UserHandle.getCallingUserId());
     }
+	// get sdcard free
+    public Boolean SpaceEnough() {
+       int headspace = 500;     // head 500M
+       StatFs emmc_sd_path = new StatFs(Environment.getExternalStorageDirectory().getPath());
+       long blockSize = emmc_sd_path.getBlockSize();
+       long availableBlocks = emmc_sd_path.getAvailableBlocks();
+       long emmc_sd_free = blockSize * availableBlocks;
+       int sd_free = new Long(emmc_sd_free/1024/1024).intValue();
+       if (sd_free < headspace)
+           return false;
+
+       return true;
+    }
+
     @Override
     public void installPackageAsUser(String originPath, IPackageInstallObserver2 observer,
             int installFlags, String installerPackageName, VerificationParams verificationParams,
             String packageAbiOverride, int userId) {
+       int useHeadspaceFlags = 1088;		// <-- 0x400 | 0x2
+       boolean useHeadspace_400M = "true".equals(SystemProperties.get("persist.evideo.use.headspace","false"));
+       if (useHeadspace_400M && !SpaceEnough()) {
+           if (installFlags == PackageManager.INSTALL_USE_HEAD_SPACE_400M) {
+               installFlags = 66;			// <-- 0x40 | 0x2  (install -r 的命令flag)
+           } else {
+               try {
+                   Slog.w(TAG, "data headspace less than 400M ,please release space !!!");
+                   observer.onPackageInstalled("", INSTALL_FAILED_USER_RESTRICTED, null, null);
+                   return ;
+               } catch (RemoteException re) {
+               }
+           }
+       }
+
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.INSTALL_PACKAGES, null);

         final int callingUid = Binder.getCallingUid();

原理:就是将所有的installFlags拦截下来,除了特定设置的标志位(0x400【INSTALL_USE_HEAD_SPACE_400M】 | 0x40 【INSTALL_ALL_USERS】)才可以安装.另外还有一个就是属性值(persist.evideo.use.headspace)设定是用来区分不需要此功能的。如果是特定的标志位那么执行(0x2 【INSTALL_REPLACE_EXISTING】| 0x40 )静默安装apk。

①Pm.java 添加解释:后续补充如何实现pm install到Pm.java的过程。

②PackageManagerService.java添加解释:installPackageAsUser()这个是静默应用安装apk的接口。

遇到的坑如何禁止第三方软件不能安装

由于更新了api接口导致引发的错误,添加{@hide}将其隐藏,对应编译就可以正常编译通过了。

改进建议

工作记录…

本文地址:https://blog.csdn.net/xcyansmile/article/details/110180433