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
17package com.android.server.telecom;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.net.Uri;
23
24import java.lang.String;
25
26/**
27 * Captures {@code android.intent.action.ACTION_PACKAGE_FULLY_REMOVED} intents and triggers the
28 * removal of associated {@link android.telecom.PhoneAccount}s via the
29 * {@link com.android.telecom.PhoneAccountRegistrar}.
30 * Note: This class listens for the {@code PACKAGE_FULLY_REMOVED} intent rather than
31 * {@code PACKAGE_REMOVED} as {@code PACKAGE_REMOVED} is triggered on re-installation of the same
32 * package, where {@code PACKAGE_FULLY_REMOVED} is triggered only when an application is completely
33 * uninstalled.  This is desirable as we do not wish to un-register all
34 * {@link android.telecom.PhoneAccount}s associated with a package being re-installed to ensure
35 * the enabled state of the accounts is retained.
36 */
37public class PhoneAccountBroadcastReceiver extends BroadcastReceiver {
38    /**
39     * Receives the intents the class is configured to received.
40     *
41     * @param context The Context in which the receiver is running.
42     * @param intent The Intent being received.
43     */
44    @Override
45    public void onReceive(Context context, Intent intent) {
46        if (Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(intent.getAction())) {
47            Uri uri = intent.getData();
48            if (uri == null) {
49                return;
50            }
51
52            String packageName = uri.getSchemeSpecificPart();
53            handlePackageRemoved(context, packageName);
54        }
55    }
56
57    /**
58     * Handles the removal of a package by calling upon the {@link PhoneAccountRegistrar} to
59     * un-register any {@link android.telecom.PhoneAccount}s associated with the package.
60     *
61     * @param packageName The name of the removed package.
62     */
63    private void handlePackageRemoved(Context context, String packageName) {
64        final CallsManager callsManager = CallsManager.getInstance();
65        if (callsManager != null) {
66            callsManager.getPhoneAccountRegistrar().clearAccounts(packageName);
67        }
68    }
69}
70