应用卸载从这里作为入口吧,这个类位于
com.android.settings进程中,com.android.settings.SubSettings
package com.android.settings;
import android.util.Log;
/**
* Stub class for showing sub-settings; we can't use the main Settings class
* since for our app it is a special singleTask class.
*/
public class SubSettings extends SettingsActivity {
@Override
public boolean onNavigateUp() {
finish();
return true;
}
@Override
protected boolean isValidFragment(String fragmentName) {
Log.d("SubSettings", "Launching fragment " + fragmentName);
return true;
}
}
啥都没有,真正的实现在
com.android.settings.applications.appinfo.AppButtonsPreferenceController中的UninstallAndDisableButtonListener内部类中
private class UninstallAndDisableButtonListener implements View.OnClickListener {
@Override
public void onClick(View v) {
final String packageName = mAppEntry.info.packageName;
// Uninstall
....
....
....
....
// 一堆判断,判断用户类型,需要卸载的app类型
} else if ((mAppEntry.info.flags & ApplicationInfo.FLAG_INSTALLED) == 0) {
uninstallPkg(packageName, true, false);
} else {
// 一般三方应用都在到这里
uninstallPkg(packageName, false, false);
}
}
}
// 最终还是用过隐试匹配到com.android.packageinstaller进程中的com.android.packageinstaller.UninstallerActivity
void uninstallPkg(String packageName, boolean allUsers, boolean andDisable) {
stopListeningToPackageRemove();
// 应用包名
Uri packageUri = Uri.parse("package:" + packageName);
Intent uninstallIntent = new Intent("android.intent.action.UNINSTALL_PACKAGE", packageUri);
uninstallIntent.putExtra("android.intent.extra.UNINSTALL_ALL_USERS", allUsers);
mMetricsFeatureProvider.action(
mActivity, SettingsEnums.ACTION_SETTINGS_UNINSTALL_APP);
mFragment.startActivityForResult(uninstallIntent, mRequestUninstall);
mDisableAfterUninstall = andDisable;
}
最终唤起一个卸载对话框
UninstallerUninstalling
点击确定就会从UninstallerActivity跳转到UninstallerUninstalling中,是不是比较熟悉,就是安装的逆向操作
// 首先还是注册监听,这次对应的文件就是uninstall_results.xml
mUninstallId = UninstallEventReceiver.addObserver(this,
EventResultPersister.GENERATE_NEW_ID, this);
// 这里是一个IPC的过程,注意这里如果需要调试的话,不是packageinstaller进程中的PackageInstaller类
// 这里同样包装了一个延迟意图,删除后PMS的回调
// 而是直接在PackageInstallerService中uninstall方法,卸载的时候没有用PackageInstaller做桥接
// ActivityThread->PackageManagerService->PackageInstallerService->uninstall方法
ActivityThread.getPackageManager().getPackageInstaller().uninstall(
new VersionedPackage(mAppInfo.packageName,
PackageManager.VERSION_CODE_HIGHEST),
getPackageName(), flags, pendingIntent.getIntentSender(),
user.getIdentifier());
PackageManagerService
通过上面的代码会调用到PackageManagerService中的deletePackageVersioned方法
// mHandler的类型还是PackageHandler
mHandler.post(() -> {
int returnCode;
// 获取所卸载应用的PackageSetting信息,就是安装时候全量解析出来的信息
final PackageSetting ps = mSettings.mPackages.get(internalPackageName);
boolean doDeletePackage = true;
if (ps != null) {
final boolean targetIsInstantApp =
ps.getInstantApp(UserHandle.getUserId(callingUid));
doDeletePackage = !targetIsInstantApp
|| canViewInstantApps;
}
if (doDeletePackage) {
if (!deleteAllUsers) {
// 卸载第三方应用一般都会走到这里
returnCode = deletePackageX(internalPackageName, versionCode,
userId, deleteFlags);
}
....
....
....
try {
// 这里会将之前包装的延迟意图发出,回调给UninstallerUninstalling中注册的回调
observer.onPackageDeleted(packageName, returnCode, null);
} catch (RemoteException e) {
Log.i(TAG, "Observer no longer exists.");
} //end catch
});
PMS中的mPackages中保存了所有应用的信息
deletePackageX中的代码比较多,主要执行了下面逻辑
- 移除mPackages和mSettings中保存的应用信息
- 清理应用在/data/app/包名的所有文件
- 杀掉应用进程
- mSettings中的部分信息回写package.xml
- 通知packageinstaller卸载成功
package.xml是PMS用来记录应用部分信息的文件,PMS中经常出现,以某个应用举例看看他记录的内容
其实就是包名,签名,abi,版本号,权限等信息;
packages.xml在PMS启动的时候会进行读取(系统启动,WatchDog杀死PMS后重启等)