应用卸载从这里作为入口吧,这个类位于
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后重启等)
<package name="包名" codePath="/data/app/包名-jhxRKm_1qetEvsc4bi_IPA==" nativeLibraryPath="/data/app/包名-jhxRKm_1qetEvsc4bi_IPA==/lib" primaryCpuAbi="armeabi" publicFlags="945307206" privateFlags="0" ft="17801007718" it="17801008255" ut="17801008255" version="5470" userId="10301" installer="com.miui.packageinstaller">
<sigs count="1" schemeVersion="2">
<cert index="56" key="308203753082025da003020102020473f22add300d06092a864886f70d01010b0500306b310c300a060355040613033038363110300e060355040813076265696a696e673110300e060355040713076265696a696e673111300f060355040a1308686f6d656c696e6b3111300f0603523731355a306b310c300a060355040613033038363110300e060355040813076265696a696e673110300e060355040713076265696a696e673111300f060355040a1308686f6d656c696e6b3111300f060355040b1308686f6d656c696e6b3111300f06035504031308686f6d656c696e6b30820122300d06092a864886f70d01010105000382010f003082010a02820101009c326787b2de4f33005bd5fd7d761f1b1121758e0ffadca9b370c117eed59d5933f8980a0049c6e64cb1cc4f58a4f83d1237584741b8675c5bb1c0f48e16a13db1f81cb4e26c1ad69189240a630ef46160f0e788e5994de794c83de6794990303342c3816afb1fd34a9fc4e8422a7fce010dbbc1379fa582275bd33b72047c22b6ebec4470d7a2b7dfc63671d1fbb937b4bb9cad3ec79725afe17cde1592eb516e814efbf59e0ccb230203010001a321301f301d0603551d0e04160414f4f619133d0eba8a3f478ef3053f364132ec16d3300d06092a864886f70d01010b05000382010100785520071fb47721415dcce763ec40e4d3c5164d655e5b90f957840219f0caa5fd939c17f5764a716be3dc7da0d02ff2f01407861a912721c8547e45efb6dd9737a2bd860a122ead0fd717642258a495b53727e9cb824a719f9bd7652b84fd430923b53e2be38c4318674844c3307537fd060a9ba2ac574039b6e5b9940557f896e4c79fd6240c764d12df89910750818204da35bfecb949958f53fb95a71b7d807cd7b46dcd970c475a517b5be8a36ab6d930756d8770ea32c6f8359a03257de6adc0faaaa9" />
</sigs>
<perms>
<item name="getui.permission.GetuiService.包名" granted="true" flags="0" />
<item name="android.permission.MODIFY_AUDIO_SETTINGS" granted="true" flags="0" />
<item name="android.permission.ACCESS_NOTIFICATION_POLICY" granted="true" flags="0" />
<item name="android.permission.CHANGE_NETWORK_STATE" granted="true" flags="0" />
<item name="android.permission.FOREGROUND_SERVICE" granted="true" flags="0" />
<item name="android.permission.RECEIVE_BOOT_COMPLETED" granted="true" flags="0" />
<item name="包名.permission.PROCESS_PUSH_MSG" granted="true" flags="0" />
<item name="android.permission.CHANGE_WIFI_STATE" granted="true" flags="0" />
<item name="android.permission.FLASHLIGHT" granted="true" flags="0" />
<item name="android.permission.ACCESS_NETWORK_STATE" granted="true" flags="0" />
<item name="包名.permission.MIPUSH_RECEIVE" granted="true" flags="0" />
<item name="android.permission.VIBRATE" granted="true" flags="0" />
<item name="android.permission.ACCESS_WIFI_STATE" granted="true" flags="0" />
<item name="android.permission.WAKE_LOCK" granted="true" flags="0" />
</perms>
<proper-signing-keyset identifier="106" />
<domain-verification packageName="包名" status="2">
<domain name="app.com" />
</domain-verification>
</package>
<package name="com.android.smspush" codePath="/system/app/WAPPushManager" nativeLibraryPath="/system/app/WAPPushManager/lib" publicFlags="805879365" privateFlags="0" ft="3e8" it="3e8" ut="3e8" version="28" userId="10126" isOrphaned="true">
<sigs count="1" schemeVersion="1">
<cert index="7" />
</sigs>
<proper-signing-keyset identifier="2" />
</package>