Home | History | Annotate | Download | only in managedprovisioning
      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.Activity;
     20 import android.app.AlertDialog;
     21 import android.app.admin.DevicePolicyManager;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.pm.PackageManager;
     26 import android.database.DataSetObserver;
     27 import android.os.Bundle;
     28 import android.os.UserManager;
     29 import android.provider.Settings;
     30 import android.util.Log;
     31 import android.view.View;
     32 import android.view.View.OnClickListener;
     33 
     34 import com.android.cts.verifier.ArrayTestListAdapter;
     35 import com.android.cts.verifier.IntentDrivenTestActivity;
     36 import com.android.cts.verifier.IntentDrivenTestActivity.ButtonInfo;
     37 import com.android.cts.verifier.PassFailButtons;
     38 import com.android.cts.verifier.R;
     39 import com.android.cts.verifier.TestListAdapter.TestListItem;
     40 import com.android.cts.verifier.TestResult;
     41 
     42 /**
     43  * Activity that lists all positive device owner tests. Requires the following adb command be issued
     44  * by the user prior to starting the tests:
     45  *
     46  * adb shell dpm set-device-owner
     47  *  'com.android.cts.verifier/com.android.cts.verifier.managedprovisioning.DeviceAdminTestReceiver'
     48  */
     49 public class DeviceOwnerPositiveTestActivity extends PassFailButtons.TestListActivity {
     50     private static final String TAG = "DeviceOwnerPositiveTestActivity";
     51 
     52     static final String EXTRA_COMMAND = "extra-command";
     53     static final String EXTRA_TEST_ID = "extra-test-id";
     54     static final String COMMAND_SET_POLICY = "set-policy";
     55     static final String EXTRA_POLICY = "extra-policy";
     56     static final String EXTRA_PARAMETER_1 = "extra_parameter_1";
     57     static final String EXTRA_PARAMETER_2 = "extra_parameter_2";
     58     static final String COMMAND_ADD_USER_RESTRICTION = "add-user-restriction";
     59     static final String COMMAND_CLEAR_USER_RESTRICTION = "clear-user-restriction";
     60     static final String EXTRA_RESTRICTION = "extra-restriction";
     61     static final String COMMAND_TEAR_DOWN = "tear-down";
     62     static final String COMMAND_CHECK_DEVICE_OWNER = "check-device-owner";
     63     static final String COMMAND_SET_GLOBAL_SETTING = "set-global-setting";
     64     static final String COMMAND_SET_STATUSBAR_DISABLED = "set-statusbar-disabled";
     65     static final String COMMAND_SET_KEYGUARD_DISABLED = "set-keyguard-disabled";
     66     static final String COMMAND_CHECK_PERMISSION_LOCKDOWN = "check-permission-lockdown";
     67     static final String EXTRA_SETTING = "extra-setting";
     68 
     69     private static final String CHECK_DEVICE_OWNER_TEST_ID = "CHECK_DEVICE_OWNER";
     70     private static final String DEVICE_ADMIN_SETTINGS_ID = "DEVICE_ADMIN_SETTINGS";
     71     private static final String WIFI_LOCKDOWN_TEST_ID = WifiLockdownTestActivity.class.getName();
     72     private static final String DISABLE_STATUS_BAR_TEST_ID = "DISABLE_STATUS_BAR";
     73     private static final String DISABLE_KEYGUARD_TEST_ID = "DISABLE_KEYGUARD";
     74     private static final String CHECK_PERMISSION_LOCKDOWN_TEST_ID =
     75             PermissionLockdownTestActivity.class.getName();
     76     private static final String DISALLOW_CONFIG_BT_ID = "DISALLOW_CONFIG_BT";
     77     private static final String DISALLOW_CONFIG_WIFI_ID = "DISALLOW_CONFIG_WIFI";
     78     private static final String REMOVE_DEVICE_OWNER_TEST_ID = "REMOVE_DEVICE_OWNER";
     79 
     80     @Override
     81     protected void onCreate(Bundle savedInstanceState) {
     82         super.onCreate(savedInstanceState);
     83         setContentView(R.layout.positive_device_owner);
     84         setInfoResources(R.string.device_owner_positive_tests,
     85                 R.string.device_owner_positive_tests_info, 0);
     86         setPassFailButtonClickListeners();
     87 
     88         final ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
     89         adapter.add(TestListItem.newCategory(this, R.string.device_owner_positive_category));
     90 
     91         addTestsToAdapter(adapter);
     92 
     93         adapter.registerDataSetObserver(new DataSetObserver() {
     94             @Override
     95             public void onChanged() {
     96                 updatePassButton();
     97             }
     98         });
     99 
    100         setTestListAdapter(adapter);
    101 
    102         View setDeviceOwnerButton = findViewById(R.id.set_device_owner_button);
    103         setDeviceOwnerButton.setOnClickListener(new OnClickListener() {
    104             @Override
    105             public void onClick(View v) {
    106                 new AlertDialog.Builder(
    107                         DeviceOwnerPositiveTestActivity.this)
    108                         .setIcon(android.R.drawable.ic_dialog_info)
    109                         .setTitle(R.string.set_device_owner_dialog_title)
    110                         .setMessage(R.string.set_device_owner_dialog_text)
    111                         .setPositiveButton(android.R.string.ok, null)
    112                         .show();
    113             }
    114         });
    115 
    116     }
    117 
    118     @Override
    119     public void finish() {
    120         // Pass and fail buttons are known to call finish() when clicked, and this is when we want
    121         // to remove the device owner.
    122         startActivity(createTearDownIntent());
    123         super.finish();
    124     }
    125 
    126     /**
    127      * Enable Pass Button when all tests passed.
    128      */
    129     private void updatePassButton() {
    130         getPassButton().setEnabled(mAdapter.allTestsPassed());
    131     }
    132 
    133     private void addTestsToAdapter(final ArrayTestListAdapter adapter) {
    134         adapter.add(createTestItem(this, CHECK_DEVICE_OWNER_TEST_ID,
    135                 R.string.device_owner_check_device_owner_test,
    136                 new Intent(this, CommandReceiver.class)
    137                         .putExtra(EXTRA_COMMAND, COMMAND_CHECK_DEVICE_OWNER)
    138                         ));
    139 
    140         // device admin settings
    141         adapter.add(createInteractiveTestItem(this, DEVICE_ADMIN_SETTINGS_ID,
    142                 R.string.device_owner_device_admin_visible,
    143                 R.string.device_owner_device_admin_visible_info,
    144                 new ButtonInfo(
    145                         R.string.device_owner_settings_go,
    146                         new Intent(Settings.ACTION_SECURITY_SETTINGS))));
    147 
    148         PackageManager packageManager = getPackageManager();
    149         if (packageManager.hasSystemFeature(PackageManager.FEATURE_WIFI)) {
    150             // WiFi Lock down tests
    151             adapter.add(createTestItem(this, WIFI_LOCKDOWN_TEST_ID,
    152                     R.string.device_owner_wifi_lockdown_test,
    153                     new Intent(this, WifiLockdownTestActivity.class)));
    154 
    155             // DISALLOW_CONFIG_WIFI
    156             adapter.add(createInteractiveTestItem(this, DISALLOW_CONFIG_WIFI_ID,
    157                     R.string.device_owner_disallow_config_wifi,
    158                     R.string.device_owner_disallow_config_wifi_info,
    159                     new ButtonInfo[] {
    160                             new ButtonInfo(
    161                                     R.string.device_owner_user_restriction_set,
    162                                     createSetUserRestrictionIntent(
    163                                             UserManager.DISALLOW_CONFIG_WIFI)),
    164                             new ButtonInfo(
    165                                     R.string.device_owner_settings_go,
    166                                     new Intent(Settings.ACTION_WIFI_SETTINGS))}));
    167         }
    168 
    169         // DISALLOW_CONFIG_BLUETOOTH
    170         if (packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)) {
    171             adapter.add(createInteractiveTestItem(this, DISALLOW_CONFIG_BT_ID,
    172                     R.string.device_owner_disallow_config_bt,
    173                     R.string.device_owner_disallow_config_bt_info,
    174                     new ButtonInfo[] {
    175                             new ButtonInfo(
    176                                     R.string.device_owner_user_restriction_set,
    177                                     createSetUserRestrictionIntent(
    178                                             UserManager.DISALLOW_CONFIG_BLUETOOTH)),
    179                             new ButtonInfo(
    180                                     R.string.device_owner_settings_go,
    181                                     new Intent(Settings.ACTION_BLUETOOTH_SETTINGS))}));
    182         }
    183 
    184         // setStatusBarDisabled
    185         adapter.add(createInteractiveTestItem(this, DISABLE_STATUS_BAR_TEST_ID,
    186                 R.string.device_owner_disable_statusbar_test,
    187                 R.string.device_owner_disable_statusbar_test_info,
    188                 new ButtonInfo[] {
    189                         new ButtonInfo(
    190                                 R.string.device_owner_disable_statusbar_button,
    191                                 createDeviceOwnerIntentWithBooleanParameter(
    192                                         COMMAND_SET_STATUSBAR_DISABLED, true)),
    193                         new ButtonInfo(
    194                                 R.string.device_owner_reenable_statusbar_button,
    195                                 createDeviceOwnerIntentWithBooleanParameter(
    196                                         COMMAND_SET_STATUSBAR_DISABLED, false))}));
    197 
    198         // setKeyguardDisabled
    199         adapter.add(createInteractiveTestItem(this, DISABLE_KEYGUARD_TEST_ID,
    200                 R.string.device_owner_disable_keyguard_test,
    201                 R.string.device_owner_disable_keyguard_test_info,
    202                 new ButtonInfo[] {
    203                         new ButtonInfo(
    204                                 R.string.device_owner_disable_keyguard_button,
    205                                 createDeviceOwnerIntentWithBooleanParameter(
    206                                         COMMAND_SET_KEYGUARD_DISABLED, true)),
    207                         new ButtonInfo(
    208                                 R.string.device_owner_reenable_keyguard_button,
    209                                 createDeviceOwnerIntentWithBooleanParameter(
    210                                         COMMAND_SET_KEYGUARD_DISABLED, false))}));
    211 
    212         // setPermissionGrantState
    213         adapter.add(createTestItem(this, CHECK_PERMISSION_LOCKDOWN_TEST_ID,
    214                 R.string.device_profile_owner_permission_lockdown_test,
    215                 new Intent(PermissionLockdownTestActivity.ACTION_CHECK_PERMISSION_LOCKDOWN)));
    216 
    217         // removeDeviceOwner
    218         adapter.add(createInteractiveTestItem(this, REMOVE_DEVICE_OWNER_TEST_ID,
    219                 R.string.device_owner_remove_device_owner_test,
    220                 R.string.device_owner_remove_device_owner_test_info,
    221                 new ButtonInfo(
    222                         R.string.remove_device_owner_button,
    223                         createTearDownIntent())));
    224     }
    225 
    226     static TestListItem createInteractiveTestItem(Activity activity, String id, int titleRes,
    227             int infoRes, ButtonInfo buttonInfo) {
    228         return createInteractiveTestItem(activity, id, titleRes, infoRes,
    229                 new ButtonInfo[] { buttonInfo });
    230     }
    231 
    232     static TestListItem createInteractiveTestItem(Activity activity, String id, int titleRes,
    233             int infoRes, ButtonInfo[] buttonInfos) {
    234         return TestListItem.newTest(activity, titleRes,
    235                 id, new Intent(activity, IntentDrivenTestActivity.class)
    236                 .putExtra(IntentDrivenTestActivity.EXTRA_ID, id)
    237                 .putExtra(IntentDrivenTestActivity.EXTRA_TITLE, titleRes)
    238                 .putExtra(IntentDrivenTestActivity.EXTRA_INFO, infoRes)
    239                 .putExtra(IntentDrivenTestActivity.EXTRA_BUTTONS, buttonInfos),
    240                 null);
    241     }
    242 
    243     static TestListItem createTestItem(Activity activity, String id, int titleRes,
    244             Intent intent) {
    245         return TestListItem.newTest(activity, titleRes, id, intent.putExtra(EXTRA_TEST_ID, id),
    246                 null);
    247     }
    248 
    249     private Intent createTearDownIntent() {
    250         return new Intent(this, CommandReceiver.class)
    251                 .putExtra(EXTRA_COMMAND, COMMAND_TEAR_DOWN);
    252     }
    253 
    254     private Intent createDeviceOwnerIntentWithBooleanParameter(String command, boolean value) {
    255         return new Intent(this, CommandReceiver.class)
    256                 .putExtra(EXTRA_COMMAND, command)
    257                 .putExtra(EXTRA_PARAMETER_1, value);
    258     }
    259 
    260     private Intent createSetUserRestrictionIntent(String restriction) {
    261         return new Intent(this, CommandReceiver.class)
    262                 .putExtra(EXTRA_COMMAND, COMMAND_ADD_USER_RESTRICTION)
    263                 .putExtra(EXTRA_RESTRICTION, restriction);
    264     }
    265 
    266     public static class CommandReceiver extends Activity {
    267         @Override
    268         public void onCreate(Bundle savedInstanceState) {
    269             super.onCreate(savedInstanceState);
    270             Intent intent = getIntent();
    271             String command = intent.getStringExtra(EXTRA_COMMAND);
    272             try {
    273                 DevicePolicyManager dpm = (DevicePolicyManager)
    274                         getSystemService(Context.DEVICE_POLICY_SERVICE);
    275                 ComponentName admin = DeviceAdminTestReceiver.getReceiverComponentName();
    276                 Log.i(TAG, "Command: " + command);
    277 
    278                 if (COMMAND_ADD_USER_RESTRICTION.equals(command)) {
    279                     String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION);
    280                     dpm.addUserRestriction(admin, restrictionKey);
    281                     Log.i(TAG, "Added user restriction " + restrictionKey);
    282                 } else if (COMMAND_CLEAR_USER_RESTRICTION.equals(command)) {
    283                     String restrictionKey = intent.getStringExtra(EXTRA_RESTRICTION);
    284                     dpm.clearUserRestriction(admin, restrictionKey);
    285                     Log.i(TAG, "Cleared user restriction " + restrictionKey);
    286                 } else if (COMMAND_TEAR_DOWN.equals(command)) {
    287                     tearDown(dpm, admin);
    288                 } else if (COMMAND_SET_GLOBAL_SETTING.equals(command)) {
    289                     final String setting = intent.getStringExtra(EXTRA_SETTING);
    290                     final String value = intent.getStringExtra(EXTRA_PARAMETER_1);
    291                     dpm.setGlobalSetting(admin, setting, value);
    292                 } else if (COMMAND_SET_STATUSBAR_DISABLED.equals(command)) {
    293                     final boolean value = intent.getBooleanExtra(EXTRA_PARAMETER_1, false);
    294                     dpm.setStatusBarDisabled(admin, value);
    295                 } else if (COMMAND_SET_KEYGUARD_DISABLED.equals(command)) {
    296                     final boolean value = intent.getBooleanExtra(EXTRA_PARAMETER_1, false);
    297                     if (value) {
    298                         dpm.resetPassword(null, 0);
    299                     }
    300                     dpm.setKeyguardDisabled(admin, value);
    301                 } else if (COMMAND_CHECK_DEVICE_OWNER.equals(command)) {
    302                     if (dpm.isDeviceOwnerApp(getPackageName())) {
    303                         TestResult.setPassedResult(this, intent.getStringExtra(EXTRA_TEST_ID),
    304                                 null, null);
    305                     } else {
    306                         TestResult.setFailedResult(this, intent.getStringExtra(EXTRA_TEST_ID),
    307                                 getString(R.string.device_owner_incorrect_device_owner), null);
    308                     }
    309                 } else {
    310                     Log.e(TAG, "Invalid command: " + command);
    311                 }
    312             } catch (Exception e) {
    313                 Log.e(TAG, "Command " + command + " failed with exception " + e);
    314             } finally {
    315                 // No matter what happened, don't let the activity run
    316                 finish();
    317             }
    318         }
    319 
    320         private void tearDown(DevicePolicyManager dpm, ComponentName admin) {
    321             if (dpm == null || !dpm.isDeviceOwnerApp(getPackageName())) {
    322                 return;
    323             }
    324 
    325             dpm.setStatusBarDisabled(admin, false);
    326             dpm.setKeyguardDisabled(admin, false);
    327             dpm.clearUserRestriction(admin, UserManager.DISALLOW_CONFIG_BLUETOOTH);
    328             dpm.clearUserRestriction(admin, UserManager.DISALLOW_CONFIG_WIFI);
    329             dpm.clearDeviceOwnerApp(getPackageName());
    330         }
    331     }
    332 }
    333 
    334