1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.server.telecom.components; 18 19 import com.android.server.telecom.PhoneAccountRegistrar; 20 21 import android.content.BroadcastReceiver; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.net.Uri; 26 import android.os.UserHandle; 27 import android.provider.Settings; 28 import android.telecom.TelecomManager; 29 30 import java.lang.String; 31 32 /** 33 * Captures {@code android.intent.action.ACTION_PACKAGE_FULLY_REMOVED} intents and triggers the 34 * removal of associated {@link android.telecom.PhoneAccount}s via the 35 * {@link com.android.telecom.PhoneAccountRegistrar}. 36 * 37 * Note: This class listens for the {@code PACKAGE_FULLY_REMOVED} intent rather than 38 * {@code PACKAGE_REMOVED} as {@code PACKAGE_REMOVED} is triggered on re-installation of the same 39 * package, where {@code PACKAGE_FULLY_REMOVED} is triggered only when an application is completely 40 * uninstalled. 41 * 42 * This is desirable as we do not wish to un-register all 43 * {@link android.telecom.PhoneAccount}s associated with a package being re-installed to ensure 44 * the enabled state of the accounts is retained. 45 * 46 * When default call screening application is removed, set 47 * {@link Settings.Secure.CALL_SCREENING_DEFAULT_APPLICATION} as null into provider. 48 */ 49 public class AppUninstallBroadcastReceiver extends BroadcastReceiver { 50 /** 51 * Receives the intents the class is configured to received. 52 * 53 * @param context The Context in which the receiver is running. 54 * @param intent The Intent being received. 55 */ 56 @Override onReceive(Context context, Intent intent)57 public void onReceive(Context context, Intent intent) { 58 if (Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(intent.getAction())) { 59 Uri uri = intent.getData(); 60 if (uri == null) { 61 return; 62 } 63 64 final PendingResult result = goAsync(); 65 // Move computation off into a separate thread to prevent ANR. 66 new Thread(() -> { 67 String packageName = uri.getSchemeSpecificPart(); 68 handlePackageRemoved(context, packageName); 69 handleUninstallOfCallScreeningService(context, packageName); 70 result.finish(); 71 }).start(); 72 } 73 } 74 75 /** 76 * Handles the removal of a package by calling upon the {@link PhoneAccountRegistrar} to 77 * un-register any {@link android.telecom.PhoneAccount}s associated with the package. 78 * 79 * @param packageName The name of the removed package. 80 */ handlePackageRemoved(Context context, String packageName)81 private void handlePackageRemoved(Context context, String packageName) { 82 final TelecomManager telecomManager = context.getSystemService(TelecomManager.class); 83 if (telecomManager != null) { 84 telecomManager.clearAccountsForPackage(packageName); 85 } 86 } 87 handleUninstallOfCallScreeningService(Context context, String packageName)88 private void handleUninstallOfCallScreeningService(Context context, String packageName) { 89 ComponentName componentName = null; 90 String defaultCallScreeningApp = Settings.Secure 91 .getStringForUser(context.getContentResolver(), 92 Settings.Secure.CALL_SCREENING_DEFAULT_COMPONENT, UserHandle.USER_CURRENT); 93 94 if (defaultCallScreeningApp != null) { 95 componentName = ComponentName.unflattenFromString(defaultCallScreeningApp); 96 } 97 98 if (componentName != null && componentName.getPackageName().equals(packageName)) { 99 Settings.Secure.putStringForUser(context.getContentResolver(), 100 Settings.Secure.CALL_SCREENING_DEFAULT_COMPONENT, null, UserHandle.USER_CURRENT); 101 } 102 } 103 } 104