blob: f78175868860e28d7f985dd9ccf91caf9076b374 [file] [log] [blame]
Todd Kennedy42d61602017-12-12 14:44:19 -08001/*
2 * Copyright (C) 2017 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 android.content.pm;
18
19import android.annotation.NonNull;
20import android.annotation.Nullable;
21import android.content.pm.PackageManagerInternal.PackageListObserver;
22
23import com.android.server.LocalServices;
24
25import java.util.List;
26
27/**
28 * All of the package name installed on the system.
29 * <p>A self observable list that automatically removes the listener when it goes out of scope.
30 *
31 * @hide Only for use within the system server.
32 */
33public class PackageList implements PackageListObserver, AutoCloseable {
34 private final PackageListObserver mWrappedObserver;
35 private final List<String> mPackageNames;
36
37 /**
38 * Create a new object.
39 * <p>Ownership of the given {@link List} transfers to this object and should not
40 * be modified by the caller.
41 */
42 public PackageList(@NonNull List<String> packageNames, @Nullable PackageListObserver observer) {
43 mPackageNames = packageNames;
44 mWrappedObserver = observer;
45 }
46
47 @Override
Chenbo Fengde8e3b72019-02-21 14:24:24 -080048 public void onPackageAdded(String packageName, int uid) {
Todd Kennedy42d61602017-12-12 14:44:19 -080049 if (mWrappedObserver != null) {
Chenbo Fengde8e3b72019-02-21 14:24:24 -080050 mWrappedObserver.onPackageAdded(packageName, uid);
Todd Kennedy42d61602017-12-12 14:44:19 -080051 }
52 }
53
54 @Override
Chenbo Fengde8e3b72019-02-21 14:24:24 -080055 public void onPackageRemoved(String packageName, int uid) {
Todd Kennedy42d61602017-12-12 14:44:19 -080056 if (mWrappedObserver != null) {
Chenbo Fengde8e3b72019-02-21 14:24:24 -080057 mWrappedObserver.onPackageRemoved(packageName, uid);
Todd Kennedy42d61602017-12-12 14:44:19 -080058 }
59 }
60
61 @Override
62 public void close() throws Exception {
63 LocalServices.getService(PackageManagerInternal.class).removePackageListObserver(this);
64 }
65
66 /**
67 * Returns the names of packages installed on the system.
68 * <p>The list is a copy-in-time and the actual set of installed packages may differ. Real
69 * time updates to the package list are sent via the {@link PackageListObserver} callback.
70 */
71 public @NonNull List<String> getPackageNames() {
72 return mPackageNames;
73 }
74}