RK7.1 系统添加白名单功能(只允许某些指定的apk安装升级)

系统禁止安装apk,只允许指定的apk安装升级;在系统中添加whiteListApps.txt,里面放入允许安装的apk的包名;

代码逻辑基本是一样的,只是在不同地方拦截apk安装。patch链接如下:
https://download.csdn.net/download/m1126125223/10980997

具体代码如下:

复制whiteListApps到指定目录

device/rockchip/common/device.mk
PRODUCT_COPY_FILES += \
	$(LOCAL_PATH)/whiteListApps.txt:system/etc/whiteListApps.txt
	

framework层拦截apk的安装
frameworks/base/cmds/pm/src/com/android/commands/pm/Pm.java


+    private static final int GET_SIGNATURES            = 0x00000040;
+    private static final int MATCH_DIRECT_BOOT_UNAWARE = 0x00040000;
+    private static final int MATCH_DIRECT_BOOT_AWARE   = 0x00080000;
+
     private static final String PM_NOT_RUNNING_ERR =
         "Error: Could not access the Package Manager.  Is the system running?";
 

+    private boolean isWhiteListApp(String pkgName){ // pkgName和whiteListApps对比
+        final File systemDir;
+        final File whiteListFile;
+        final ArrayList<String> whiteListApps = new ArrayList<String>();
+        systemDir = new File("/system/", "etc");
+        whiteListFile = new File(systemDir, "whiteListApps.txt");
+
+        if (!whiteListFile.exists()) {
+            Log.e(TAG, "isWhiteListApp: whiteListApps.txt file no exists");
+            return false;
+        }
+        try {
+            whiteListApps.clear();
+            BufferedReader br = new BufferedReader(new FileReader(whiteListFile));
+            String line = br.readLine();
+            while (line != null) {
+                //Log.d(TAG, "whiteListApps readLine:" + line);
+                whiteListApps.add(line);
+                line = br.readLine();
+            }
+            br.close();
+        } catch (IOException e) {
+            Log.e(TAG, "IO Exception happened while reading whiteListApps");
+            e.printStackTrace();
+            return false;
+        }
+        Iterator<String> it = whiteListApps.iterator();
+        while (it.hasNext()) {
+            String whitelistItem = it.next();
+            if (pkgName.equals(whitelistItem)) {
+                Log.e(TAG, "isWhiteListApp: find matching package name "+pkgName);
+                return true;
+            }
+        }
+        Log.e(TAG, "isWhiteListApp: cann't find matching package name "+pkgName);
+        return false;
+    }
+
+    private PackageInfo getPackageInfoFromPath(String pkgFilePath, int flags) {
+        final PackageParser parser = new PackageParser();
+        final File apkFile = new File(pkgFilePath);
+        try {
+            if ((flags & (MATCH_DIRECT_BOOT_UNAWARE | MATCH_DIRECT_BOOT_AWARE)) != 0) {
+                // Caller expressed an explicit opinion about what encryption
+                // aware/unaware components they want to see, so fall through and
+                // give them what they want
+            } else {
+                // Caller expressed no opinion, so match everything
+                flags |= MATCH_DIRECT_BOOT_AWARE | MATCH_DIRECT_BOOT_UNAWARE;
+            }
+
+            PackageParser.Package pkg = parser.parseMonolithicPackage(apkFile, 0);
+            if ((flags & GET_SIGNATURES) != 0) {
+                PackageParser.collectCertificates(pkg, 0);
+            }
+            PackageUserState state = new PackageUserState();
+            return PackageParser.generatePackageInfo(pkg, null, flags, 0, 0, null, state);
+        } catch (PackageParserException e) {
+            return null;
+        }
+    }
+
     public static void main(String[] args) {
         int exitCode = 1;
         try {
@@ -397,6 +475,19 @@ public final class Pm {
                 System.err.println("Error: must either specify a package size or an APK file");
                 return 1;
             }
+
+            if (SystemProperties.getBoolean("persist.neo.WhiteList", false)) {
+                PackageInfo info = getPackageInfoFromPath(inPath, PackageManager.GET_ACTIVITIES);
+                String installerPackageName = info.packageName;
+                Log.e(TAG, "---lpz--- apkFilePath = "+inPath+", installerPackageName = "+installerPackageName);
+                if(!isWhiteListApp(installerPackageName)) {
+                    System.err.println("Error: the app is not in app whitelist packageName " + installerPackageName);
+                    return 1;
+                }
+            }

frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java

		同上面的方法 :isWhiteListApp(String pkgName)

+        if (SystemProperties.getBoolean("persist.neo.WhiteList", false)) {//拦截adb安卓的apk
+            if(!isWhiteListApp(pkg.packageName)) {
+                res.setError(PackageManager.INSTALL_FAILED_VERIFICATION_FAILURE,"app is not in the whitelist. packageName:" + pkg.packageName);
+                return;
+            }
+        }
         // Get rid of all references to package scan path via parser.
         pp = null;
         String oldCodePath = null;

PackageInstaller 层拦截
packages/apps/PackageInstaller/src/com/android/packageinstaller/PackageInstallerActivity.java


同上面的方法 :isWhiteListApp(String pkgName)

     private void startInstallConfirm() {
         ((TextView) findViewById(R.id.install_confirm_question))
                 .setText(R.string.install_confirm_question);
@@ -248,20 +298,6 @@ public class PackageInstallerActivity extends OverlayTouchActivity implements On
 
     @Override
     public Dialog onCreateDialog(int id, Bundle bundle) {
         switch (id) {
         case DLG_UNKNOWN_SOURCES:
             return new AlertDialog.Builder(this)
@@ -510,6 +546,18 @@ public class PackageInstallerActivity extends OverlayTouchActivity implements On
      */
     private void checkIfAllowedAndInitiateInstall(boolean ignoreUnknownSourcesSettings) {
         // Block the install attempt on the Unknown Sources setting if necessary.
+        if (SystemProperties.getBoolean("persist.neo.WhiteList", false)) {
+            if (!isWhiteListApp(mPkgInfo.packageName)) {//拦截用户安卓的apk
+                Toast.makeText(this, R.string.unknown_apps_admin_dlg_text, Toast.LENGTH_LONG).show();//show unknown apps not allow install
+                Log.i(TAG, "is no White List App, cannt install allow !");
+                this.finish();
+                return;
+            }
+        }


  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心有纤纤结

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值