Home | History | Annotate | Download | only in packageinstaller
      1 /*
      2  * Copyright (C) 2016 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.packageinstaller;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.support.annotation.NonNull;
     23 
     24 /**
     25  * Receives uninstall events and persists them using a {@link EventResultPersister}.
     26  */
     27 public class UninstallEventReceiver extends BroadcastReceiver {
     28     private static final Object sLock = new Object();
     29     private static EventResultPersister sReceiver;
     30 
     31     /**
     32      * Get the event receiver persisting the results
     33      *
     34      * @return The event receiver.
     35      */
     36     @NonNull private static EventResultPersister getReceiver(@NonNull Context context) {
     37         synchronized (sLock) {
     38             if (sReceiver == null) {
     39                 sReceiver = new EventResultPersister(
     40                         TemporaryFileManager.getUninstallStateFile(context));
     41             }
     42         }
     43 
     44         return sReceiver;
     45     }
     46 
     47     @Override
     48     public void onReceive(Context context, Intent intent) {
     49         getReceiver(context).onEventReceived(context, intent);
     50     }
     51 
     52     /**
     53      * Add an observer. If there is already an event for this id, call back inside of this call.
     54      *
     55      * @param context  A context of the current app
     56      * @param id       The id the observer is for or {@code GENERATE_NEW_ID} to generate a new one.
     57      * @param observer The observer to call back.
     58      *
     59      * @return The id for this event
     60      */
     61     static int addObserver(@NonNull Context context, int id,
     62             @NonNull EventResultPersister.EventResultObserver observer)
     63             throws EventResultPersister.OutOfIdsException {
     64         return getReceiver(context).addObserver(id, observer);
     65     }
     66 
     67     /**
     68      * Remove a observer.
     69      *
     70      * @param context  A context of the current app
     71      * @param id The id the observer was added for
     72      */
     73     static void removeObserver(@NonNull Context context, int id) {
     74         getReceiver(context).removeObserver(id);
     75     }
     76 
     77     /**
     78      * @param context A context of the current app
     79      *
     80      * @return A new uninstall id
     81      */
     82     static int getNewId(@NonNull Context context) throws EventResultPersister.OutOfIdsException {
     83         return getReceiver(context).getNewId();
     84     }
     85 }
     86