1 /*
2  * Copyright (C) 2014 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.service.restrictions;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.BroadcastReceiver;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.RestrictionsManager;
24 import android.os.PersistableBundle;
25 
26 /**
27  * Abstract implementation of a Restrictions Provider BroadcastReceiver. To implement a
28  * Restrictions Provider, extend from this class and implement the abstract methods.
29  * Export this receiver in the manifest. A profile owner device admin can then register this
30  * component as a Restrictions Provider using
31  * {@link DevicePolicyManager#setRestrictionsProvider(ComponentName, ComponentName)}.
32  * <p>
33  * The function of a Restrictions Provider is to transport permission requests from apps on this
34  * device to an administrator (most likely on a remote device or computer) and deliver back
35  * responses. The response should be sent back to the app via
36  * {@link RestrictionsManager#notifyPermissionResponse(String, PersistableBundle)}.
37  *
38  * @see RestrictionsManager
39  */
40 public abstract class RestrictionsReceiver extends BroadcastReceiver {
41 
42     private static final String TAG = "RestrictionsReceiver";
43 
44     /**
45      * An asynchronous permission request made by an application for an operation that requires
46      * authorization by a local or remote administrator other than the user. The Restrictions
47      * Provider should transfer the request to the administrator and deliver back a response, when
48      * available. The calling application is aware that the response could take an indefinite
49      * amount of time.
50      * <p>
51      * If the request bundle contains the key {@link RestrictionsManager#REQUEST_KEY_NEW_REQUEST},
52      * then a new request must be sent. Otherwise the provider can look up any previous response
53      * to the same requestId and return the cached response.
54      *
55      * @param packageName the application requesting permission.
56      * @param requestType the type of request, which determines the content and presentation of
57      * the request data.
58      * @param request the request data bundle containing at a minimum a request id.
59      *
60      * @see RestrictionsManager#REQUEST_TYPE_APPROVAL
61      * @see RestrictionsManager#REQUEST_KEY_ID
62      */
onRequestPermission(Context context, String packageName, String requestType, String requestId, PersistableBundle request)63     public abstract void onRequestPermission(Context context,
64             String packageName, String requestType, String requestId, PersistableBundle request);
65 
66     /**
67      * Intercept standard Restrictions Provider broadcasts.  Implementations
68      * should not override this method; it is better to implement the
69      * convenience callbacks for each action.
70      */
71     @Override
onReceive(Context context, Intent intent)72     public void onReceive(Context context, Intent intent) {
73         String action = intent.getAction();
74 
75         if (RestrictionsManager.ACTION_REQUEST_PERMISSION.equals(action)) {
76             String packageName = intent.getStringExtra(RestrictionsManager.EXTRA_PACKAGE_NAME);
77             String requestType = intent.getStringExtra(RestrictionsManager.EXTRA_REQUEST_TYPE);
78             String requestId = intent.getStringExtra(RestrictionsManager.EXTRA_REQUEST_ID);
79             PersistableBundle request = (PersistableBundle)
80                     intent.getParcelableExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE);
81             onRequestPermission(context, packageName, requestType, requestId, request);
82         }
83     }
84 }
85