1 /*
2  * Copyright (C) 2021 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 import android.annotation.NonNull;
18 import android.annotation.Nullable;
19 import android.annotation.UserIdInt;
20 import android.content.ComponentName;
21 import android.content.Intent;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.IPackageDataObserver;
24 import android.content.pm.IPackageMoveObserver;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ProviderInfo;
27 import android.content.pm.ResolveInfo;
28 import android.os.Bundle;
29 import android.os.Handler;
30 import android.os.RemoteException;
31 import android.os.UserHandle;
32 
33 import java.util.List;
34 
35 public class PackageManagerCompat {
36     public static final int DELETE_SUCCEEDED = PackageManager.DELETE_SUCCEEDED;
37     /**
38      * Error code that is passed to the {@link IPackageMoveObserver} when the
39      * package hasn't been successfully moved by the system because of
40      * insufficient memory on specified media.
41      */
42     public static final int MOVE_FAILED_INSUFFICIENT_STORAGE =
43             PackageManager.MOVE_FAILED_INSUFFICIENT_STORAGE;
44 
45     /**
46      * Error code that is passed to the {@link IPackageMoveObserver} if the
47      * specified package doesn't exist.
48      */
49     public static final int MOVE_FAILED_DOESNT_EXIST =
50             PackageManager.MOVE_FAILED_DOESNT_EXIST;
51 
52     /**
53      * Error code that is passed to the {@link IPackageMoveObserver} if the
54      * specified package cannot be moved since it's a system package.
55      */
56     public static final int MOVE_FAILED_SYSTEM_PACKAGE =
57             PackageManager.MOVE_FAILED_SYSTEM_PACKAGE;
58 
59     /**
60      * Error code that is passed to the {@link IPackageMoveObserver} if the
61      * specified package cannot be moved to the specified location.
62      */
63     public static final int MOVE_FAILED_INVALID_LOCATION =
64             PackageManager.MOVE_FAILED_INVALID_LOCATION;
65 
66     /**
67      * Error code that is passed to the {@link IPackageMoveObserver} if the
68      * specified package cannot be moved to the specified location.
69      */
70     public static final int MOVE_FAILED_INTERNAL_ERROR =
71             PackageManager.MOVE_FAILED_INTERNAL_ERROR;
72 
73     /**
74      * Error code that is passed to the {@link IPackageMoveObserver} if the
75      * specified package cannot be moved since it contains a device admin.
76      */
77     public static final int MOVE_FAILED_DEVICE_ADMIN =
78             PackageManager.MOVE_FAILED_DEVICE_ADMIN;
79 
80     public static final int MOVE_SUCCEEDED =
81             PackageManager.MOVE_SUCCEEDED;
82 
83     private final PackageManager mManager;
84 
PackageManagerCompat(PackageManager manager)85     public PackageManagerCompat(PackageManager manager) {
86         this.mManager = manager;
87     }
88 
movePackage(@onNull String packageName, @NonNull VolumeInfoCompat vol)89     public int movePackage(@NonNull String packageName, @NonNull VolumeInfoCompat vol) {
90         return mManager.movePackage(packageName, vol.mVolumeInfo);
91     }
92 
movePrimaryStorage(@onNull VolumeInfoCompat vol)93     public int movePrimaryStorage(@NonNull VolumeInfoCompat vol) {
94         return this.mManager.movePrimaryStorage(vol.mVolumeInfo);
95     }
96 
97     public @NonNull
getPrimaryStorageCandidateVolumes()98     List<VolumeInfoCompat> getPrimaryStorageCandidateVolumes() {
99         return VolumeInfoCompat.convert(this.mManager.getPrimaryStorageCandidateVolumes());
100     }
101 
getApplicationInfoAsUser(@onNull String packageName, int flags, @UserIdInt int userId)102     public ApplicationInfo getApplicationInfoAsUser(@NonNull String packageName, int flags,
103             @UserIdInt int userId) throws PackageManager.NameNotFoundException {
104         return this.mManager.getApplicationInfoAsUser(packageName, flags, userId);
105     }
106 
getPackageUidAsUser(@onNull String packageName, @UserIdInt int userId)107     public int getPackageUidAsUser(@NonNull String packageName, @UserIdInt int userId)
108             throws PackageManager.NameNotFoundException {
109         return mManager.getPackageUidAsUser(packageName, userId);
110     }
111 
112     public interface PackageDataObserver {
onRemoveCompleted(String s, boolean b)113         void onRemoveCompleted(String s, boolean b);
114     }
115 
deleteApplicationCacheFiles( @onNull String packageName, @Nullable PackageDataObserver observer)116     public void deleteApplicationCacheFiles(
117             @NonNull String packageName, @Nullable PackageDataObserver observer) {
118         mManager.deleteApplicationCacheFiles(packageName, new IPackageDataObserver.Stub() {
119             @Override
120             public void onRemoveCompleted(String s, boolean b) throws RemoteException {
121                 observer.onRemoveCompleted(s, b);
122             }
123         });
124     }
125 
getPackageUidAsUser(@onNull String packageName, @NonNull PackageManager.PackageInfoFlags flags, @UserIdInt int userId)126     public int getPackageUidAsUser(@NonNull String packageName,
127             @NonNull PackageManager.PackageInfoFlags flags, @UserIdInt int userId)
128             throws PackageManager.NameNotFoundException {
129         return mManager.getPackageUidAsUser(packageName, flags, userId);
130     }
131 
132     public @NonNull
getServicesSystemSharedLibraryPackageName()133     String getServicesSystemSharedLibraryPackageName() {
134         return mManager.getServicesSystemSharedLibraryPackageName();
135     }
136 
getPermissionControllerPackageName()137     public String getPermissionControllerPackageName() {
138         return mManager.getPermissionControllerPackageName();
139     }
140 
getPackageCandidateVolumes(@onNull ApplicationInfo app)141     public List<VolumeInfoCompat> getPackageCandidateVolumes(@NonNull ApplicationInfo app) {
142         return VolumeInfoCompat.convert(mManager.getPackageCandidateVolumes(app));
143     }
144 
getSharedSystemSharedLibraryPackageName()145     public String getSharedSystemSharedLibraryPackageName() {
146         return mManager.getSharedSystemSharedLibraryPackageName();
147     }
148 
queryIntentServicesAsUser( @onNull Intent intent, int flags, @UserIdInt int userId)149     public List<ResolveInfo> queryIntentServicesAsUser(
150             @NonNull Intent intent, int flags, @UserIdInt int userId) {
151         return mManager.queryIntentServicesAsUser(intent, flags, userId);
152     }
153 
queryBroadcastReceiversAsUser( @onNull Intent intent, int flags, @UserIdInt int userId)154     public List<ResolveInfo> queryBroadcastReceiversAsUser(
155             @NonNull Intent intent, int flags, @UserIdInt int userId) {
156         return mManager.queryBroadcastReceiversAsUser(intent, flags, userId);
157     }
158 
queryIntentActivitiesAsUser( @onNull Intent intent, int flags, @UserIdInt int userId)159     public List<ResolveInfo> queryIntentActivitiesAsUser(
160             @NonNull Intent intent, int flags, @UserIdInt int userId) {
161         return mManager.queryIntentActivitiesAsUser(intent, flags, userId);
162     }
163 
164     public @Nullable
getPackageCurrentVolume(@onNull ApplicationInfo app)165     VolumeInfoCompat getPackageCurrentVolume(@NonNull ApplicationInfo app) {
166         return new VolumeInfoCompat(mManager.getPackageCurrentVolume(app));
167     }
168 
resolveActivityAsUser(@onNull Intent intent, @NonNull PackageManager.ResolveInfoFlags flags, @UserIdInt int userId)169     public ResolveInfo resolveActivityAsUser(@NonNull Intent intent,
170             @NonNull PackageManager.ResolveInfoFlags flags, @UserIdInt int userId) {
171         return this.mManager.resolveActivityAsUser(intent, flags, userId);
172     }
173 
resolveActivityAsUser( @onNull Intent intent, int flags, @UserIdInt int userId)174     public ResolveInfo resolveActivityAsUser(
175             @NonNull Intent intent, int flags, @UserIdInt int userId) {
176         return this.mManager.resolveActivityAsUser(intent, flags, userId);
177     }
178 
getHomeActivities(@onNull List<ResolveInfo> outActivities)179     public ComponentName getHomeActivities(@NonNull List<ResolveInfo> outActivities) {
180         return mManager.getHomeActivities(outActivities);
181     }
182 
getPrimaryStorageCurrentVolume()183     public VolumeInfoCompat getPrimaryStorageCurrentVolume() {
184         return new VolumeInfoCompat(this.mManager.getPrimaryStorageCurrentVolume());
185     }
186 
getInstallReason(@onNull String packageName, @NonNull UserHandle user)187     public int getInstallReason(@NonNull String packageName, @NonNull UserHandle user) {
188         return this.mManager.getInstallReason(packageName, user);
189     }
190 
getInstalledApplicationsAsUser(int flags, @UserIdInt int userId)191     public List<ApplicationInfo> getInstalledApplicationsAsUser(int flags, @UserIdInt int userId) {
192         return this.mManager.getInstalledApplicationsAsUser(flags, userId);
193     }
194 
195 
resolveContentProviderAsUser( @onNull String providerName, int flags, @UserIdInt int userId)196     public ProviderInfo resolveContentProviderAsUser(
197             @NonNull String providerName, int flags, @UserIdInt int userId) {
198         return mManager.resolveContentProviderAsUser(providerName, flags, userId);
199     }
200 
201     public abstract static class MoveCallback {
onCreated(int moveId, Bundle extras)202         public void onCreated(int moveId, Bundle extras) {
203         }
204 
onStatusChanged(int moveId, int status, long estMillis)205         public abstract void onStatusChanged(int moveId, int status, long estMillis);
206     }
207 
208     private PackageManager.MoveCallback mMoveCallback;
209 
registerMoveCallback( @onNull MoveCallback callback, @NonNull Handler handler)210     public void registerMoveCallback(
211             @NonNull MoveCallback callback, @NonNull Handler handler) {
212         mMoveCallback = new PackageManager.MoveCallback() {
213             @Override
214             public void onStatusChanged(int moveId, int status, long estMillis) {
215                 callback.onStatusChanged(moveId, status, estMillis);
216             }
217         };
218         mManager.registerMoveCallback(mMoveCallback, handler);
219     }
220 
unregisterMoveCallback(@onNull MoveCallback callback)221     public void unregisterMoveCallback(@NonNull MoveCallback callback) {
222         if (mMoveCallback != null) {
223             mManager.unregisterMoveCallback(mMoveCallback);
224         }
225     }
226 
isMoveStatusFinished(int status)227     public static boolean isMoveStatusFinished(int status) {
228         return PackageManager.isMoveStatusFinished(status);
229     }
230 }
231