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