Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.content.pm; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
| 21 | import android.content.pm.PackageManagerInternal.PackageListObserver; |
| 22 | |
| 23 | import com.android.server.LocalServices; |
| 24 | |
| 25 | import 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 | */ |
| 33 | public 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 Feng | de8e3b7 | 2019-02-21 14:24:24 -0800 | [diff] [blame] | 48 | public void onPackageAdded(String packageName, int uid) { |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 49 | if (mWrappedObserver != null) { |
Chenbo Feng | de8e3b7 | 2019-02-21 14:24:24 -0800 | [diff] [blame] | 50 | mWrappedObserver.onPackageAdded(packageName, uid); |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
Chenbo Feng | de8e3b7 | 2019-02-21 14:24:24 -0800 | [diff] [blame] | 55 | public void onPackageRemoved(String packageName, int uid) { |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 56 | if (mWrappedObserver != null) { |
Chenbo Feng | de8e3b7 | 2019-02-21 14:24:24 -0800 | [diff] [blame] | 57 | mWrappedObserver.onPackageRemoved(packageName, uid); |
Todd Kennedy | 42d6160 | 2017-12-12 14:44:19 -0800 | [diff] [blame] | 58 | } |
| 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 | } |