代码路径:
frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java
1、禁止安装可以修改PackageManagerService.java,在handleStartCopy方法中添加下面的代码
public void handleStartCopy() throws RemoteException {
int ret = PackageManager.INSTALL_SUCCEEDED;
// If we're already staged, we've firmly committed 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
2、禁止卸载,在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