Un-registering phone accounts for packages which are uninstalled.
Added BroadcastReceiver in telecomm to listen to package removal intents,
and to remove any phone accounts from the PhoneAccountRegistrar for the
removed package.
Bug: 16934114
Change-Id: I49540264b5da6d00ad542d020cdf140cd54557f1
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index a85710e..1a4fae7 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -182,6 +182,13 @@
</intent-filter>
</receiver>
+ <receiver android:name="PhoneAccountBroadcastReceiver">
+ <intent-filter>
+ <action android:name="android.intent.action.PACKAGE_REMOVED" />
+ <data android:scheme="package" />
+ </intent-filter>
+ </receiver>
+
<activity android:name=".RespondViaSmsSettings$Settings"
android:label="@string/respond_via_sms_setting_title"
android:configChanges="orientation|screenSize|keyboardHidden">
diff --git a/src/com/android/telecomm/PhoneAccountBroadcastReceiver.java b/src/com/android/telecomm/PhoneAccountBroadcastReceiver.java
new file mode 100644
index 0000000..8f70211
--- /dev/null
+++ b/src/com/android/telecomm/PhoneAccountBroadcastReceiver.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.telecomm;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.net.Uri;
+
+import java.lang.String;
+
+/**
+ * Captures {@code android.intent.action.PACKAGE_REMOVED} intents and triggers the removal of
+ * associated {@link android.telecomm.PhoneAccount}s via the
+ * {@link com.android.telecomm.PhoneAccountRegistrar}.
+ */
+public class PhoneAccountBroadcastReceiver extends BroadcastReceiver {
+ /**
+ * Receives the intents the class is configured to received.
+ *
+ * @param context The Context in which the receiver is running.
+ * @param intent The Intent being received.
+ */
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
+ Uri uri = intent.getData();
+ if (uri == null) {
+ return;
+ }
+
+ String packageName = uri.getSchemeSpecificPart();
+ handlePackageRemoved(context, packageName);
+ }
+ }
+
+ /**
+ * Handles the removal of a package by calling upon the {@link PhoneAccountRegistrar} to
+ * un-register any {@link android.telecomm.PhoneAccount}s associated with the package.
+ *
+ * @param packageName The name of the removed package.
+ */
+ private void handlePackageRemoved(Context context, String packageName) {
+ TelecommApp telecommApp = TelecommApp.getInstance();
+ if (telecommApp == null) {
+ return;
+ }
+
+ PhoneAccountRegistrar registrar = telecommApp.getPhoneAccountRegistrar();
+ if (registrar == null) {
+ return;
+ }
+
+ registrar.clearAccounts(packageName);
+ }
+}
diff --git a/src/com/android/telecomm/PhoneAccountRegistrar.java b/src/com/android/telecomm/PhoneAccountRegistrar.java
index c9e0e46..46f1a76 100644
--- a/src/com/android/telecomm/PhoneAccountRegistrar.java
+++ b/src/com/android/telecomm/PhoneAccountRegistrar.java
@@ -46,7 +46,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.lang.String;
import java.util.ArrayList;
+import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
@@ -258,18 +260,29 @@
fireAccountsChanged();
}
+ /**
+ * Un-registers all phone accounts associated with a specified package.
+ *
+ * @param packageName The package for which phone accounts will be removed.
+ */
public void clearAccounts(String packageName) {
- for (int i = 0; i < mState.accounts.size(); i++) {
+ boolean accountsRemoved = false;
+ Iterator<PhoneAccount> it = mState.accounts.iterator();
+ while (it.hasNext()) {
+ PhoneAccount phoneAccount = it.next();
if (Objects.equals(
packageName,
- mState.accounts.get(i).getAccountHandle()
- .getComponentName().getPackageName())) {
- mState.accounts.remove(i);
+ phoneAccount.getAccountHandle().getComponentName().getPackageName())) {
+ Log.i(this, "Removing phone account " + phoneAccount.getLabel());
+ it.remove();
+ accountsRemoved = true;
}
}
- write();
- fireAccountsChanged();
+ if (accountsRemoved) {
+ write();
+ fireAccountsChanged();
+ }
}
public void addListener(Listener l) {