1 /*
2  * Copyright (C) 2018 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.app;
18 
19 import android.annotation.Nullable;
20 import android.annotation.SystemService;
21 import android.content.Context;
22 import android.content.pm.ParceledListSlice;
23 import android.os.Handler;
24 import android.os.IBinder;
25 import android.os.RemoteException;
26 import android.os.ServiceManager;
27 import android.util.Singleton;
28 
29 /**
30  * Class for managing an app's permission to access a particular {@link android.net.Uri}.
31  *
32  * @hide
33  */
34 @SystemService(Context.URI_GRANTS_SERVICE)
35 public class UriGrantsManager {
36 
37     private final Context mContext;
38 
UriGrantsManager(Context context, Handler handler)39     UriGrantsManager(Context context, Handler handler) {
40         mContext = context;
41     }
42 
43     /** @hide */
getService()44     public static IUriGrantsManager getService() {
45         return IUriGrantsManagerSingleton.get();
46     }
47 
48     private static final Singleton<IUriGrantsManager> IUriGrantsManagerSingleton =
49             new Singleton<IUriGrantsManager>() {
50                 @Override
51                 protected IUriGrantsManager create() {
52                     final IBinder b = ServiceManager.getService(Context.URI_GRANTS_SERVICE);
53                     return IUriGrantsManager.Stub.asInterface(b);
54                 }
55             };
56 
57     /**
58      * Permits an application to clear the persistent URI permissions granted to another.
59      *
60      * <p>Typically called by Settings, requires {@code CLEAR_APP_GRANTED_URI_PERMISSIONS}.
61      *
62      * @param packageName application to clear its granted permissions
63      *
64      * @hide
65      */
clearGrantedUriPermissions(String packageName)66     public void clearGrantedUriPermissions(String packageName) {
67         try {
68             getService().clearGrantedUriPermissions(packageName, mContext.getUserId());
69         } catch (RemoteException e) {
70             throw e.rethrowFromSystemServer();
71         }
72     }
73 
74     /**
75      * Permits an application to get the persistent URI permissions granted to another.
76      *
77      * <p>Typically called by Settings or DocumentsUI, requires
78      * {@code GET_APP_GRANTED_URI_PERMISSIONS}.
79      *
80      * @param packageName application to look for the granted permissions, or {@code null} to get
81      * granted permissions for all applications
82      * @return list of granted URI permissions
83      *
84      * @hide
85      */
getGrantedUriPermissions( @ullable String packageName)86     public ParceledListSlice<GrantedUriPermission> getGrantedUriPermissions(
87             @Nullable String packageName) {
88         try {
89             @SuppressWarnings("unchecked")
90             final ParceledListSlice<GrantedUriPermission> castedList = getService()
91                     .getGrantedUriPermissions(packageName, mContext.getUserId());
92             return castedList;
93         } catch (RemoteException e) {
94             throw e.rethrowFromSystemServer();
95         }
96     }
97 }
98