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 package com.android.cts.deviceandprofileowner;
17 
18 import android.app.Activity;
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.os.Bundle;
24 import android.os.UserManager;
25 
26 /**
27  * Test activity for setApplicationRestrictions().
28  *
29  * The actual test will set restrictions for this package, and the purpose of this
30  * activity is to listen for the ACTION_APPLICATION_RESTRICTIONS_CHANGED broadcast
31  * and relay the retrieved restriction bundle back to the test for validation.
32  */
33 public class ApplicationRestrictionsActivity extends Activity {
34 
35     // Incoming intent type
36     public static final String FINISH = "finishActivity";
37 
38     // Outgoing broadcast
39     public static final String REGISTERED_ACTION =
40             "com.android.cts.deviceandprofileowner.APP_RESTRICTION_REGISTERED";
41     public static final String RESTRICTION_ACTION =
42             "com.android.cts.deviceandprofileowner.APP_RESTRICTION_VALUE";
43 
44     private UserManager mUserManager;
45 
46     private final BroadcastReceiver mAppRestrictionReceiver = new BroadcastReceiver() {
47         @Override
48         public void onReceive(Context context, Intent intent) {
49             broadcastRestriction();
50         }
51     };
52 
broadcastRestriction()53     private void broadcastRestriction() {
54         Bundle restrictions = mUserManager.getApplicationRestrictions(getPackageName());
55         Intent intent = new Intent(RESTRICTION_ACTION);
56         intent.putExtra("value", restrictions);
57         sendBroadcast(intent);
58     }
59 
60     @Override
onNewIntent(Intent intent)61     protected void onNewIntent(Intent intent) {
62         super.onNewIntent(intent);
63         handleIntent(intent);
64     }
65 
66     @Override
onCreate(android.os.Bundle savedInstanceState)67     protected void onCreate(android.os.Bundle savedInstanceState) {
68         super.onCreate(savedInstanceState);
69         handleIntent(getIntent());
70     }
71 
72     @Override
onResume()73     protected void onResume() {
74         super.onResume();
75         mUserManager = (UserManager) getSystemService(Context.USER_SERVICE);
76         IntentFilter filter = new IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED);
77         registerReceiver(mAppRestrictionReceiver, filter);
78         sendBroadcast(new Intent(REGISTERED_ACTION));
79     }
80 
81     @Override
onPause()82     protected void onPause() {
83         super.onPause();
84         unregisterReceiver(mAppRestrictionReceiver);
85     }
86 
handleIntent(Intent intent)87     private void handleIntent(Intent intent) {
88         if (intent.getBooleanExtra(FINISH, false)) {
89             finish();
90         }
91     }
92 
93 }
94