在日常开发中,尤其是定制ROM,有些设备里面的应用不想被卸载,就需要限制用户,当然如果将不能卸载的应用预置到system/app目录下也不会被卸载,但是我们还有其他方式,比如在PMS里面假如卸载限制,这样也可以实现这个功能,而且adb 命令或者代码都不能卸载该应用,接下来我们看下实现方法。
首先我们先看下PMS的源码如下:
private static boolean isPrivilegedApp(PackageParser.Package pkg) {
return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_PRIVILEGED) != 0;
}
private static boolean isSystemApp(ApplicationInfo info) {
return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
private static boolean isSystemApp(PackageSetting ps) {
return (ps.pkgFlags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
private static boolean isUpdatedSystemApp(PackageSetting ps) {
return (ps.pkgFlags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
}
private static boolean isUpdatedSystemApp(PackageParser.Package pkg) {
return (pkg.applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0;
}
private int packageFlagsToInstallFlags(PackageSetting ps) {
int installFlags = 0;
if (isExternal(ps)) {
installFlags |= PackageManager.INSTALL_EXTERNAL;
}
if (isForwardLocked(ps)) {
installFlags |= PackageManager.INSTALL_FORWARD_LOCK;
}
return installFlags;
}
private void deleteTempPackageFiles() {
final FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("vmdl") && name.endsWith(".tmp");
}
};
deleteTempPackageFilesInDirectory(mAppInstallDir, filter);
deleteTempPackageFilesInDirectory(mDrmAppPrivateInstallDir, filter);
}
private static final void deleteTempPackageFilesInDirectory(File directory,
FilenameFilter filter) {
final String[] tmpFilesList = directory.list(filter);
if (tmpFilesList == null) {
return;
}
for (int i = 0; i < tmpFilesList.length; i++) {
final File tmpFile = new File(directory, tmpFilesList[i]);
tmpFile.delete();
}
}
private File createTempPackageFile(File installDir) {
File tmpPackageFile;
try {
tmpPackageFile = File.createTempFile("vmdl", ".tmp", installDir);
} catch (IOException e) {
Slog.e(TAG, "Couldn't create temp file for downloaded package file.");
return null;
}
try {
FileUtils.setPermissions(
tmpPackageFile.getCanonicalPath(), FileUtils.S_IRUSR|FileUtils.S_IWUSR,
-1, -1);
if (!SELinux.restorecon(tmpPackageFile)) {
return null;
}
} catch (IOException e) {
Slog.e(TAG, "Trouble getting the canoncical path for a temp file.");
return null;
}
return tmpPackageFile;
}
@Override
public void deletePackageAsUser(final String packageName,
final IPackageDeleteObserver observer,
final int userId, final int flags) {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.DELETE_PACKAGES, null);
final int uid = Binder.getCallingUid();
if (UserHandle.getUserId(uid) != userId) {
mContext.enforceCallingPermission(
android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
"deletePackage for user " + userId);
}
if (isUserRestricted(userId, UserManager.DISALLOW_UNINSTALL_APPS)||packageName.contains("packageName")) {
try {
observer.packageDeleted(packageName, PackageManager.DELETE_FAILED_USER_RESTRICTED);
} catch (RemoteException re) {
}
return;
}
if (DEBUG_REMOVE) Slog.d(TAG, "deletePackageAsUser: pkg=" + packageName + " user=" + userId);
// Queue up an async operation since the package deletion may take a little while.
mHandler.post(new Runnable() {
public void run() {
mHandler.removeCallbacks(this);
final int returnCode = deletePackageX(packageName, userId, flags);
if (observer != null) {
try {
observer.packageDeleted(packageName, returnCode);
} catch (RemoteException e) {
Log.i(TAG, "Observer no longer exists.");
} //end catch
} //end if
} //end run
});
}
private boolean isPackageDeviceAdmin(String packageName, int userId) {
IDevicePolicyManager dpm = IDevicePolicyManager.Stub.asInterface(
ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
try {
if (dpm != null && (dpm.packageHasActiveAdmins(packageName, userId)
|| dpm.isDeviceOwner(packageName))) {
return true;
}
} catch (RemoteException e) {
}
return false;
}
上面代码中,系统卸载应用的API就是如下代码:
@Override
public void deletePackageAsUser(final String packageName,
final IPackageDeleteObserver observer,
final int userId, final int flags) {
mContext.enforceCallingOrSelfPermission(
android.Manifest.permission.DELETE_PACKAGES, null);
final int uid = Binder.getCallingUid();
if (UserHandle.getUserId(uid) != userId) {
mContext.enforceCallingPermission(
android.Manifest.permission.INTERACT_ACROSS_USERS_FULL,
"deletePackage for user " + userId);
}
if (isUserRestricted(userId, UserManager.DISALLOW_UNINSTALL_APPS)||packageName.contains("packageName")) {
try {
observer.packageDeleted(packageName, PackageManager.DELETE_FAILED_USER_RESTRICTED);
} catch (RemoteException re) {
}
return;
}
if (DEBUG_REMOVE) Slog.d(TAG, "deletePackageAsUser: pkg=" + packageName + " user=" + userId);
// Queue up an async operation since the package deletion may take a little while.
mHandler.post(new Runnable() {
public void run() {
mHandler.removeCallbacks(this);
final int returnCode = deletePackageX(packageName, userId, flags);
if (observer != null) {
try {
observer.packageDeleted(packageName, returnCode);
} catch (RemoteException e) {
Log.i(TAG, "Observer no longer exists.");
} //end catch
} //end if
} //end run
});
}
具体实现可以参考如下代码:
if (isUserRestricted(userId, UserManager.DISALLOW_UNINSTALL_APPS)||packageName.contains("packageName")) {
try {
observer.packageDeleted(packageName, PackageManager.DELETE_FAILED_USER_RESTRICTED);
} catch (RemoteException re) {
}
return;
}
这只是单一的功能,我们可以将需要限制的应用包名存放一个集合,再通过比对的方式限制
QQ交流:3151365988