1 /* 2 * Copyright (C) 2017 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 package com.android.cts.delegate; 17 18 import static android.app.admin.DevicePolicyManager.ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED; 19 import static android.app.admin.DevicePolicyManager.EXTRA_DELEGATION_SCOPES; 20 21 import android.app.Activity; 22 import android.app.admin.DevicePolicyManager; 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.os.Bundle; 28 import android.os.Process; 29 import android.util.Log; 30 31 import java.util.List; 32 33 /** 34 * Simple activity that registers a {@code BroadcastReceiver} for intercepting 35 * {@link DevicePolicyManager#ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED} intents sent when the 36 * a DO/PO calls grants this app new delegation scopes via 37 * {@link DevicePolicyManager#setDelegatedScopes}. 38 */ 39 public class DelegatedScopesReceiverActivity extends Activity { 40 41 private static final String TAG = DelegatedScopesReceiverActivity.class.getSimpleName(); 42 43 /** 44 * Broadcast action sent reporting the scopes delegated to this app. 45 */ 46 private static final String ACTION_REPORT_SCOPES = "com.android.cts.delegate.report_scopes"; 47 48 /** 49 * Broadcast action sent reporting that this app is running. 50 */ 51 private static final String ACTION_RUNNING = "com.android.cts.delegate.running"; 52 53 private final BroadcastReceiver mScopesChangedReceiver = new BroadcastReceiver() { 54 @Override 55 public void onReceive(Context context, Intent intent) { 56 Log.d(TAG, "Received intent " + intent.getAction() + " on UID " + Process.myUid()); 57 if (ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED.equals(intent.getAction())) { 58 handleIntent(intent); 59 } 60 } 61 }; 62 63 @Override onCreate(Bundle savedInstanceState)64 protected void onCreate(Bundle savedInstanceState) { 65 super.onCreate(savedInstanceState); 66 handleIntent(getIntent()); 67 } 68 69 @Override onNewIntent(Intent intent)70 protected void onNewIntent(Intent intent) { 71 super.onNewIntent(intent); 72 handleIntent(intent); 73 } 74 75 @Override onStart()76 protected void onStart() { 77 super.onStart(); 78 IntentFilter filter = new IntentFilter(ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED); 79 registerReceiver(mScopesChangedReceiver, filter); 80 sendRunningBroadcast(); 81 } 82 83 @Override onStop()84 protected void onStop() { 85 super.onStop(); 86 unregisterReceiver(mScopesChangedReceiver); 87 } 88 handleIntent(Intent intent)89 private void handleIntent(Intent intent) { 90 Log.d(TAG, "handleIntent(): " + intent); 91 if (intent == null) { 92 return; 93 } 94 95 if (ACTION_APPLICATION_DELEGATION_SCOPES_CHANGED.equals(intent.getAction())) { 96 final List<String> scopes = intent.getStringArrayListExtra(EXTRA_DELEGATION_SCOPES); 97 sendScopeReportBroadcast(scopes); 98 finish(); 99 } 100 } 101 sendScopeReportBroadcast(List<String> scopes)102 private void sendScopeReportBroadcast(List<String> scopes) { 103 Intent intent = new Intent(); 104 intent.setAction(ACTION_REPORT_SCOPES); 105 intent.putExtra(EXTRA_DELEGATION_SCOPES, scopes.toArray(new String[scopes.size()])); 106 Log.d(TAG, "Broadcasting " + ACTION_REPORT_SCOPES + " with scopes " + scopes); 107 sendBroadcast(intent); 108 } 109 sendRunningBroadcast()110 private void sendRunningBroadcast() { 111 Intent intent = new Intent(); 112 intent.setAction(ACTION_RUNNING); 113 sendBroadcast(intent); 114 } 115 } 116