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 String packageName = uri.getSchemeSpecificPart(); 65 handlePackageRemoved(context, packageName); 66 handleUninstallOfCallScreeningService(context, packageName); 67 } 68 } 69 70 /** 71 * Handles the removal of a package by calling upon the {@link PhoneAccountRegistrar} to 72 * un-register any {@link android.telecom.PhoneAccount}s associated with the package. 73 * 74 * @param packageName The name of the removed package. 75 */ handlePackageRemoved(Context context, String packageName)76 private void handlePackageRemoved(Context context, String packageName) { 77 final TelecomManager telecomManager = TelecomManager.from(context); 78 if (telecomManager != null) { 79 telecomManager.clearAccountsForPackage(packageName); 80 } 81 } 82 handleUninstallOfCallScreeningService(Context context, String packageName)83 private void handleUninstallOfCallScreeningService(Context context, String packageName) { 84 ComponentName componentName = null; 85 String defaultCallScreeningApp = Settings.Secure 86 .getStringForUser(context.getContentResolver(), 87 Settings.Secure.CALL_SCREENING_DEFAULT_COMPONENT, UserHandle.USER_CURRENT); 88 89 if (defaultCallScreeningApp != null) { 90 componentName = ComponentName.unflattenFromString(defaultCallScreeningApp); 91 } 92 93 if (componentName != null && componentName.getPackageName().equals(packageName)) { 94 Settings.Secure.putStringForUser(context.getContentResolver(), 95 Settings.Secure.CALL_SCREENING_DEFAULT_COMPONENT, null, UserHandle.USER_CURRENT); 96 } 97 } 98 } 99