android 禁止第三方apk安装和卸载的方法详解

时间:2021-05-20

需求是这样的,客户要求提供系统的接口来控制apk的安装和卸载,接口如下

boolean setAppInstallationPolicies(int mode, String[] appPackageNames)mode:应用名单类型0:黑名单(应用包名列表中的所有项都不允许安装);1:白名单(只允许安装应用包名列表中的项)。appPackageNames:应用包名列表。当appPackageNames为空时,取消所有已设定的应用。成功返回true;失败返回false。String[] getAppInstallationPolicies()返回值为当前应用安装管控状态string[0]:功能模式,参见setAppInstallationPolicies方法的mode参数。string[1]至string[n-1]:应用包名列表。 boolean setAppUninstallationPolicies(int mode, String[] appPackageNames)mode:应用名单类型0:黑名单(应用包名列表中的所有项均强制卸载);1:白名单(应用包名列表中的所有项禁止卸载)。appPackageNames:应用包名列表。当appPackageNames为空时,取消所有已设定的应用。成功返回true;失败返回false。String[] getAppUninstallationPolicies()返回值为当前应用卸载管控状态string[0]:功能模式,参见setAppUninstallationPolicies方法的mode参数。string[1]至string[n-1]:应用包名列表。

android版本为9.0,首先想到的是在系统里面添加一个自己的service,分别在frameworks/base/core/java/android/app/添加IPolicyManager.aidl,frameworks/base/services/core/java/com/android/server/添加PolicyManagerService.java,在frameworks/base/添加policy/java/ga/mdm/PolicyManager.java,内容如下

package android.app; /** {@hide} */interface IPolicyManager{ boolean setAppInstallationPolicies(int mode,inout String[] appPackageNames); String[] getAppInstallationPolicies(); boolean setAppUninstallationPolicies(int mode,inout String[] appPackageNames); String[] getAppUninstallationPolicies(); }package com.android.server; import android.content.Context;import android.content.Intent;import android.content.IntentFilter; import android.os.ServiceManager;import android.os.SystemProperties;import android.provider.Settings;import android.util.Slog; import java.lang.reflect.Field;import java.util.ArrayList; import android.app.IPolicyManager;import android.net.wifi.WifiManager;import android.content.pm.PackageManager;import android.app.ActivityManager;import android.content.pm.IPackageDataObserver; public class PolicyManagerService extends IPolicyManager.Stub { private final String TAG = "PolicyManagerService"; private Context mContext; private String[] mAppPackageNames = null; private String[] mAppUninstallPackageNames = null; public PolicyManagerService(Context context) { mContext = context; } @Override public boolean setAppInstallationPolicies(int mode, String[] appPackageNames){ if(mode==0){ Settings.System.putInt(mContext.getContentResolver(),"customer_app_status", 0); }else if(mode==1){ Settings.System.putInt(mContext.getContentResolver(),"customer_app_status", 1); }else{ return false; } mAppPackageNames = appPackageNames; return true; } @Override public String[] getAppInstallationPolicies(){ return mAppPackageNames; } @Override public boolean setAppUninstallationPolicies(int mode,String[] appPackageNames){ if(mode==0){ Settings.System.putInt(mContext.getContentResolver(),"customer_appuninstall_status", 0); }else if(mode==1){ Settings.System.putInt(mContext.getContentResolver(),"customer_appuninstall_status", 1); }else{ return false; } mAppUninstallPackageNames = appPackageNames; return true; } @Override public String[] getAppUninstallationPolicies(){ return mAppUninstallPackageNames; }}package ga.mdm; import android.util.Slog;import android.os.RemoteException;import android.content.Context;import android.app.IPolicyManager; public class PolicyManager { private final String TAG = "PolicyManager"; Context mContext; private final IPolicyManager mService; public PolicyManager(Context context,IPolicyManager mService) { mContext = context; this.mService = mService; } public boolean setAppInstallationPolicies(int mode,String[] appPackageNames){ try { return mService.setAppInstallationPolicies(mode,appPackageNames); } catch (RemoteException ex) { ex.printStackTrace(); return false; } } public String[] getAppInstallationPolicies(){ try { return mService.getAppInstallationPolicies(); } catch (RemoteException ex) { ex.printStackTrace(); return null; } } public boolean setAppUninstallationPolicies(int mode,String[] appPackageNames){ try { return mService.setAppUninstallationPolicies(mode,appPackageNames); } catch (RemoteException ex) { ex.printStackTrace(); return false; } } public String[] getAppUninstallationPolicies(){ try { return mService.getAppUninstallationPolicies(); } catch (RemoteException ex) { ex.printStackTrace(); return null; } } }

同时在frameworks/base/policy/添加Android.mk

# Copyright (C) 2014 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://mitted to an install location if (origin.staged) { if (origin.file != null) { installFlags |= PackageManager.INSTALL_INTERNAL; installFlags &= ~PackageManager.INSTALL_EXTERNAL; } else { throw new IllegalStateException("Invalid stage location"); } } final boolean onSd = (installFlags & PackageManager.INSTALL_EXTERNAL) != 0; final boolean onInt = (installFlags & PackageManager.INSTALL_INTERNAL) != 0; final boolean ephemeral = (installFlags & PackageManager.INSTALL_INSTANT_APP) != 0; PackageInfoLite pkgLite = null; if (onInt && onSd) { // Check if both bits are set. Slog.w(TAG, "Conflicting flags specified for installing on both internal and external"); ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; } else if (onSd && ephemeral) { Slog.w(TAG, "Conflicting flags specified for installing ephemeral on external"); ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; } else { pkgLite = mContainerService.getMinimalPackageInfo(origin.resolvedPath, installFlags, packageAbiOverride); //add by jueme PolicyManager policyManager = (PolicyManager)mContext.getSystemService("policy"); String[] appNames = policyManager.getAppInstallationPolicies(); if(appNames!=null && appNames.length>0){ int app_status = android.provider.Settings.System.getInt(mContext.getContentResolver(),"customer_app_status", -1); Slog.w(TAG,"app_status "+app_status); if(app_status==0){ for (int i = 0; i < appNames.length; i++) { Slog.w(TAG,"appNames 0 "+appNames[i]); if (pkgLite.packageName.equals(appNames[i])){ ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; break; } } }else if(app_status==1){ for (int i = 0; i < appNames.length; i++) { Slog.w(TAG,"appNames 1 "+appNames[i]); if (pkgLite.packageName.equals(appNames[i])){ ret = PackageManager.INSTALL_SUCCEEDED; break; }else{ ret = PackageManager.INSTALL_FAILED_INVALID_INSTALL_LOCATION; } } } } //add end

这样在安装时候就会报安装位置不对的信息。

接着是禁止卸载,在PackageInstallerService.java的uninstall添加下面的方法。

@Override public void uninstall(VersionedPackage versionedPackage, String callerPackageName, int flags, IntentSender statusReceiver, int userId) throws RemoteException { //add by jueme PolicyManager policyManager = (PolicyManager)mContext.getSystemService("policy"); String[] appNames = policyManager.getAppUninstallationPolicies(); if(appNames!=null && appNames.length>0){ int appuninstall_status = android.provider.Settings.System.getInt(mContext.getContentResolver(),"customer_appuninstall_status", -1); Slog.w(TAG,"appuninstall_status "+appuninstall_status+" mInstallerPackageName "+versionedPackage.getPackageName()); boolean isUninstall = true;//默认都是可卸载 if(appuninstall_status==0){ for (int i = 0; i < appNames.length; i++) { if (versionedPackage.getPackageName().equals(appNames[i])){ isUninstall = true; break; }else{ isUninstall = false; } } if(!isUninstall){ return; } }else if(appuninstall_status==1){ //应用包名列表中的所有项禁止卸载 for (int i = 0; i < appNames.length; i++) { if (versionedPackage.getPackageName().equals(appNames[i])){ isUninstall = false; break; }else{ isUninstall = true; } } if(!isUninstall){ return; } } } //add end

到此这篇关于android 禁止第三方apk安装和卸载的方法详解的文章就介绍到这了,更多相关android 禁止第三方apk内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章