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
17package android.service.restrictions;
18
19import android.app.admin.DevicePolicyManager;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.RestrictionsManager;
24import 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 */
40public 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_TYPE_LOCAL_APPROVAL
62     * @see RestrictionsManager#REQUEST_KEY_ID
63     */
64    public abstract void onRequestPermission(Context context,
65            String packageName, String requestType, String requestId, PersistableBundle request);
66
67    /**
68     * Intercept standard Restrictions Provider broadcasts.  Implementations
69     * should not override this method; it is better to implement the
70     * convenience callbacks for each action.
71     */
72    @Override
73    public void onReceive(Context context, Intent intent) {
74        String action = intent.getAction();
75
76        if (RestrictionsManager.ACTION_REQUEST_PERMISSION.equals(action)) {
77            String packageName = intent.getStringExtra(RestrictionsManager.EXTRA_PACKAGE_NAME);
78            String requestType = intent.getStringExtra(RestrictionsManager.EXTRA_REQUEST_TYPE);
79            String requestId = intent.getStringExtra(RestrictionsManager.EXTRA_REQUEST_ID);
80            PersistableBundle request = (PersistableBundle)
81                    intent.getParcelableExtra(RestrictionsManager.EXTRA_REQUEST_BUNDLE);
82            onRequestPermission(context, packageName, requestType, requestId, request);
83        }
84    }
85}
86