1 /*
2  * Copyright (C) 2015 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 com.android.cts.verifier.managedprovisioning;
18 
19 import android.app.admin.DevicePolicyManager;
20 import android.content.ActivityNotFoundException;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.pm.ApplicationInfo;
25 import android.content.pm.PackageManager;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.provider.Settings;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.Button;
32 import android.widget.ImageView;
33 import android.widget.RadioGroup;
34 import android.widget.TextView;
35 import android.widget.Toast;
36 
37 import com.android.cts.verifier.PassFailButtons;
38 import com.android.cts.verifier.R;
39 
40 public class PermissionLockdownTestActivity extends PassFailButtons.Activity
41         implements RadioGroup.OnCheckedChangeListener {
42     private static final String PERMISSION_APP_PACKAGE = "com.android.cts.permissionapp";
43 
44     // Alias used for starting the activity from ByodFlowTestActivity (Managed profile tests).
45     static final String ACTIVITY_ALIAS
46             = "com.android.cts.verifier.managedprovisioning" +
47                     ".ManagedProfilePermissionLockdownTestActivity";
48 
49     private static final String MANAGED_PROVISIONING_ACTION_PREFIX
50             = "com.android.cts.verifier.managedprovisioning.action.";
51     // Indicates that activity is started for device owner case.
52     static final String ACTION_CHECK_PERMISSION_LOCKDOWN
53             = MANAGED_PROVISIONING_ACTION_PREFIX + "CHECK_PERMISSION_LOCKDOWN";
54     // Indicates that activity is started for profile owner case.
55     static final String ACTION_MANAGED_PROFILE_CHECK_PERMISSION_LOCKDOWN
56             = MANAGED_PROVISIONING_ACTION_PREFIX + "MANAGED_PROFILE_CHECK_PERMISSION_LOCKDOWN";
57 
58     // Permission grant states will be set on this permission.
59     private static final String CONTACTS_PERMISSION = android.Manifest.permission.READ_CONTACTS;
60 
61     private boolean mDeviceOwnerTest;
62     private DevicePolicyManager mDevicePolicyManager;
63     private ComponentName mAdmin;
64 
65     @Override
onCreate(Bundle savedInstanceState)66     public void onCreate(Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68         setContentView(R.layout.permission_lockdown);
69 
70         mDevicePolicyManager =
71                 (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
72         mAdmin = DeviceAdminTestReceiver.getReceiverComponentName();
73 
74         mDeviceOwnerTest =
75                 ACTION_CHECK_PERMISSION_LOCKDOWN.equals(getIntent().getAction()) ? true : false;
76         // Return immediately if we are neither profile nor device owner.
77         if (!isProfileOrDeviceOwner()) {
78             setTestResultAndFinish(false);
79             return;
80         }
81 
82         buildLayout();
83     }
84 
buildLayout()85     private void buildLayout() {
86         PackageManager packageManager = getPackageManager();
87         ApplicationInfo applicationInfo = null;
88         try {
89             // We need to make sure that the CtsPermissionApp is installed on the device or
90             // work profile.
91             applicationInfo = packageManager.getApplicationInfo(
92                     PERMISSION_APP_PACKAGE, 0 /* flags */);
93         } catch (PackageManager.NameNotFoundException e) {
94             showToast(getString(R.string.package_not_found, PERMISSION_APP_PACKAGE));
95             setTestResultAndFinish(false);
96             return;
97         }
98 
99         ImageView packageIconImageView = (ImageView) findViewById(R.id.package_icon);
100         packageIconImageView.setImageDrawable(packageManager.getApplicationIcon(applicationInfo));
101         TextView packageNameTextView = (TextView) findViewById(R.id.package_name);
102         packageNameTextView.setText(packageManager.getApplicationLabel(applicationInfo));
103 
104         TextView permissionNameTextView = (TextView) findViewById(R.id.permission_name);
105         permissionNameTextView.setText(CONTACTS_PERMISSION);
106 
107         // Get the current permission grant state for initializing the RadioGroup.
108         int currentPermissionState = mDevicePolicyManager.getPermissionGrantState(mAdmin,
109                     PERMISSION_APP_PACKAGE, CONTACTS_PERMISSION);
110         RadioGroup permissionRadioGroup = (RadioGroup) findViewById(R.id.permission_group);
111         switch (currentPermissionState) {
112             case DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED: {
113                 permissionRadioGroup.check(R.id.permission_allow);
114             } break;
115             case DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT: {
116                 permissionRadioGroup.check(R.id.permission_default);
117             } break;
118             case  DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED: {
119                 permissionRadioGroup.check(R.id.permission_deny);
120             } break;
121         }
122         permissionRadioGroup.setOnCheckedChangeListener(this);
123 
124         addFinishOrPassFailButtons();
125     }
126 
addFinishOrPassFailButtons()127     private void addFinishOrPassFailButtons() {
128         // In case of device owner, we include the pass-fail buttons where as in case of profile
129         // owner, we add a finish button.
130         ViewGroup parentView = (ViewGroup) findViewById(R.id.permission_lockdown_activity);
131         if (mDeviceOwnerTest) {
132             parentView.addView(
133                     getLayoutInflater().inflate(R.layout.pass_fail_buttons, parentView, false));
134             setInfoResources(R.string.device_profile_owner_permission_lockdown_test,
135                     R.string.device_owner_permission_lockdown_test_info, 0);
136             setPassFailButtonClickListeners();
137         } else {
138             Button finishButton = new Button(this);
139             finishButton.setText(R.string.finish_button_label);
140             finishButton.setOnClickListener(new View.OnClickListener() {
141                 @Override
142                 public void onClick(View v) {
143                     PermissionLockdownTestActivity.this.setTestResultAndFinish(false);
144                 }
145             });
146             parentView.addView(finishButton);
147         }
148     }
149 
150     // Dispatches an intent to open the Settings screen for CtsPermissionApp.
openSettings(View v)151     public void openSettings(View v) {
152         Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
153                 .setData(Uri.fromParts("package", PERMISSION_APP_PACKAGE, null))
154                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
155         try {
156             startActivity(intent);
157         } catch (ActivityNotFoundException e) {
158             showToast(getString(R.string.activity_not_found, intent));
159         }
160     }
161 
162     @Override
onCheckedChanged(RadioGroup radioGroup, int checkedId)163     public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
164         int permissionGrantState = -1;
165         switch (checkedId) {
166             case R.id.permission_allow: {
167                 permissionGrantState = DevicePolicyManager.PERMISSION_GRANT_STATE_GRANTED;
168             } break;
169             case R.id.permission_default: {
170                 permissionGrantState = DevicePolicyManager.PERMISSION_GRANT_STATE_DEFAULT;
171             } break;
172             case R.id.permission_deny: {
173                 permissionGrantState = DevicePolicyManager.PERMISSION_GRANT_STATE_DENIED;
174             } break;
175         }
176         mDevicePolicyManager.setPermissionGrantState(mAdmin, PERMISSION_APP_PACKAGE,
177                 CONTACTS_PERMISSION, permissionGrantState);
178     }
179 
isProfileOrDeviceOwner()180     private boolean isProfileOrDeviceOwner() {
181         String adminPackage = mAdmin.getPackageName();
182         if (mDeviceOwnerTest && !mDevicePolicyManager.isDeviceOwnerApp(adminPackage)) {
183             showToast(getString(R.string.not_device_owner, adminPackage));
184             return false;
185         } else if (!mDeviceOwnerTest && !mDevicePolicyManager.isProfileOwnerApp(adminPackage)) {
186             showToast(getString(R.string.not_profile_owner, adminPackage));
187             return false;
188         }
189         return true;
190     }
191 
showToast(String toast)192     private void showToast(String toast) {
193         Toast.makeText(this, toast, Toast.LENGTH_LONG).show();
194     }
195 }