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