14f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann/*
24f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * Copyright (C) 2016 The Android Open Source Project
34f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann *
44f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * Licensed under the Apache License, Version 2.0 (the "License");
54f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * you may not use this file except in compliance with the License.
64f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * You may obtain a copy of the License at
74f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann *
84f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann *      http://www.apache.org/licenses/LICENSE-2.0
94f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann *
104f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * Unless required by applicable law or agreed to in writing, software
114f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * distributed under the License is distributed on an "AS IS" BASIS,
124f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
134f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * See the License for the specific language governing permissions and
144f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * limitations under the License.
154f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann */
164f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
174f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmannpackage com.android.packageinstaller;
184f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
194f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmannimport android.content.BroadcastReceiver;
204f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmannimport android.content.Context;
214f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmannimport android.content.Intent;
2274fa089b8c39d84b737607a3e3d2cde4d3b42d24Philip P. Moltmannimport android.support.annotation.NonNull;
234f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
244f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann/**
254f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann * Receives uninstall events and persists them using a {@link EventResultPersister}.
264f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann */
274f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmannpublic class UninstallEventReceiver extends BroadcastReceiver {
284f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    private static final Object sLock = new Object();
294f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    private static EventResultPersister sReceiver;
304f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
314f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    /**
324f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * Get the event receiver persisting the results
334f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     *
344f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @return The event receiver.
354f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     */
364f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    @NonNull private static EventResultPersister getReceiver(@NonNull Context context) {
374f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann        synchronized (sLock) {
384f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann            if (sReceiver == null) {
39cd5a5a60ca028b30969c0dc16c5f0f3c205b494bPhilip P. Moltmann                sReceiver = new EventResultPersister(
40cd5a5a60ca028b30969c0dc16c5f0f3c205b494bPhilip P. Moltmann                        TemporaryFileManager.getUninstallStateFile(context));
414f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann            }
424f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann        }
434f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
444f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann        return sReceiver;
454f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    }
464f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
474f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    @Override
484f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    public void onReceive(Context context, Intent intent) {
49cd5a5a60ca028b30969c0dc16c5f0f3c205b494bPhilip P. Moltmann        getReceiver(context).onEventReceived(context, intent);
504f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    }
514f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
524f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    /**
534f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * Add an observer. If there is already an event for this id, call back inside of this call.
544f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     *
554f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @param context  A context of the current app
564f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @param id       The id the observer is for or {@code GENERATE_NEW_ID} to generate a new one.
574f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @param observer The observer to call back.
584f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     *
594f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @return The id for this event
604f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     */
614f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    static int addObserver(@NonNull Context context, int id,
624f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann            @NonNull EventResultPersister.EventResultObserver observer)
634f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann            throws EventResultPersister.OutOfIdsException {
644f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann        return getReceiver(context).addObserver(id, observer);
654f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    }
664f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
674f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    /**
684f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * Remove a observer.
694f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     *
704f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @param context  A context of the current app
714f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @param id The id the observer was added for
724f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     */
734f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    static void removeObserver(@NonNull Context context, int id) {
744f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann        getReceiver(context).removeObserver(id);
754f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    }
764f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann
774f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    /**
784f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @param context A context of the current app
794f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     *
804f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     * @return A new uninstall id
814f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann     */
824f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    static int getNewId(@NonNull Context context) throws EventResultPersister.OutOfIdsException {
834f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann        return getReceiver(context).getNewId();
844f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann    }
854f78e1fa55255a261247e9a112bc4433cd1a9ab0Philip P. Moltmann}
86