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