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.AlertDialog;
20 import android.content.Intent;
21 import android.database.DataSetObserver;
22 import android.os.Bundle;
23 import android.provider.Settings;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.widget.EditText;
27 import android.widget.RadioGroup;
28 
29 import com.android.cts.verifier.ArrayTestListAdapter;
30 import com.android.cts.verifier.PassFailButtons;
31 import com.android.cts.verifier.R;
32 import com.android.cts.verifier.IntentDrivenTestActivity.ButtonInfo;
33 
34 import com.android.compatibility.common.util.WifiConfigCreator;
35 import static com.android.compatibility.common.util.WifiConfigCreator.SECURITY_TYPE_NONE;
36 import static com.android.compatibility.common.util.WifiConfigCreator.SECURITY_TYPE_WPA;
37 import static com.android.compatibility.common.util.WifiConfigCreator.SECURITY_TYPE_WEP;
38 
39 /**
40  * Activity to test WiFi configuration lockdown functionality. A locked down WiFi config
41  * must not be editable/forgettable in Settings.
42  */
43 public class WifiLockdownTestActivity extends PassFailButtons.TestListActivity {
44     private static final String TAG = "WifiLockdownTestActivity";
45 
46     private static final int NONE = R.id.device_owner_keymgmnt_none;
47     private static final int WPA = R.id.device_owner_keymgmnt_wpa;
48     private static final int WEP = R.id.device_owner_keymgmnt_wep;
49 
50     private static final String CONFIG_MODIFIABLE_WHEN_UNLOCKED_TEST_ID = "UNLOCKED_MODIFICATION";
51     private static final String CONFIG_NOT_MODIFIABLE_WHEN_LOCKED_TEST_ID = "LOCKED_MODIFICATION";
52     private static final String CONFIG_CONNECTABLE_WHEN_LOCKED_TEST_ID = "LOCKED_CONNECT";
53     private static final String CONFIG_REMOVABLE_WHEN_UNLOCKED_TEST_ID = "UNLOCKED_REMOVE";
54 
55     private WifiConfigCreator mConfigCreator;
56     private ButtonInfo[] mSwitchLockdownOffButtonInfos;
57     private ButtonInfo[] mSwitchLockdownOnButtonInfos;
58 
59     @Override
onCreate(Bundle savedInstanceState)60     protected void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62         mConfigCreator = new WifiConfigCreator(this);
63         setContentView(R.layout.wifi_lockdown);
64         setInfoResources(R.string.device_owner_wifi_lockdown_test,
65                 R.string.device_owner_wifi_lockdown_info, 0);
66         setPassFailButtonClickListeners();
67 
68         final ArrayTestListAdapter adapter = new ArrayTestListAdapter(this);
69 
70         final ButtonInfo goToWifiSettings = new ButtonInfo(
71                 R.string.wifi_lockdown_go_settings_wifi_button,
72                 new Intent(Settings.ACTION_WIFI_SETTINGS));
73         mSwitchLockdownOffButtonInfos = new ButtonInfo[] { new ButtonInfo(
74                 R.string.switch_wifi_lockdown_off_button,
75                 new Intent(this, CommandReceiverActivity.class)
76                         .putExtra(CommandReceiverActivity.EXTRA_COMMAND,
77                                 CommandReceiverActivity.COMMAND_SET_GLOBAL_SETTING)
78                         .putExtra(CommandReceiverActivity.EXTRA_SETTING,
79                                 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN)
80                         .putExtra(CommandReceiverActivity.EXTRA_VALUE, "0"
81                 )), goToWifiSettings };
82         mSwitchLockdownOnButtonInfos = new ButtonInfo[] { new ButtonInfo(
83                 R.string.switch_wifi_lockdown_on_button,
84                 new Intent(this, CommandReceiverActivity.class)
85                         .putExtra(CommandReceiverActivity.EXTRA_COMMAND,
86                                 CommandReceiverActivity.COMMAND_SET_GLOBAL_SETTING)
87                         .putExtra(CommandReceiverActivity.EXTRA_SETTING,
88                                 Settings.Global.WIFI_DEVICE_OWNER_CONFIGS_LOCKDOWN)
89                         .putExtra(CommandReceiverActivity.EXTRA_VALUE, "1"
90                 )), goToWifiSettings };
91 
92         addTestsToAdapter(adapter);
93 
94         adapter.registerDataSetObserver(new DataSetObserver() {
95             @Override
96             public void onChanged() {
97                 updatePassButton();
98             }
99         });
100 
101         setTestListAdapter(adapter);
102 
103         View createConfigButton = findViewById(R.id.create_wifi_config_button);
104         createConfigButton .setOnClickListener(new OnClickListener() {
105             @Override
106             public void onClick(View v) {
107                 EditText ssidEditor = (EditText) findViewById(R.id.device_owner_wifi_ssid);
108                 RadioGroup authMethods = (RadioGroup) findViewById(
109                         R.id.device_owner_keyManagementMethods);
110                 int checkedRadioId = authMethods.getCheckedRadioButtonId();
111                 if (checkedRadioId == -1) {
112                     checkedRadioId = NONE;
113                 }
114                 int netId;
115                 try {
116                     netId = mConfigCreator.addNetwork(ssidEditor.getText().toString(), false,
117                             convertKeyManagement(checkedRadioId), "defaultpassword");
118                 } catch (InterruptedException e) {
119                     netId = -1;
120                 }
121                 if (netId == -1) {
122                     new AlertDialog.Builder(
123                             WifiLockdownTestActivity.this)
124                             .setIcon(android.R.drawable.ic_dialog_info)
125                             .setTitle(R.string.wifi_lockdown_add_network_failed_dialog_title)
126                             .setMessage(R.string.wifi_lockdown_add_network_failed_dialog_text)
127                             .setPositiveButton(android.R.string.ok, null)
128                             .show();
129                 }
130             }
131         });
132     }
133 
addTestsToAdapter(final ArrayTestListAdapter adapter)134     private void addTestsToAdapter(final ArrayTestListAdapter adapter) {
135         adapter.add(Utils.createInteractiveTestItem(this,
136                 CONFIG_MODIFIABLE_WHEN_UNLOCKED_TEST_ID,
137                 R.string.device_owner_wifi_config_unlocked_modification_test,
138                 R.string.device_owner_wifi_config_unlocked_modification_test_info,
139                 mSwitchLockdownOffButtonInfos));
140         adapter.add(Utils.createInteractiveTestItem(this,
141                 CONFIG_NOT_MODIFIABLE_WHEN_LOCKED_TEST_ID,
142                 R.string.device_owner_wifi_config_locked_modification_test,
143                 R.string.device_owner_wifi_config_locked_modification_test_info,
144                 mSwitchLockdownOnButtonInfos));
145         adapter.add(Utils.createInteractiveTestItem(this,
146                 CONFIG_CONNECTABLE_WHEN_LOCKED_TEST_ID,
147                 R.string.device_owner_wifi_config_locked_connection_test,
148                 R.string.device_owner_wifi_config_locked_connection_test_info,
149                 mSwitchLockdownOnButtonInfos));
150         adapter.add(Utils.createInteractiveTestItem(this,
151                 CONFIG_REMOVABLE_WHEN_UNLOCKED_TEST_ID,
152                 R.string.device_owner_wifi_config_unlocked_removal_test,
153                 R.string.device_owner_wifi_config_unlocked_removal_test_info,
154                 mSwitchLockdownOffButtonInfos));
155     }
156 
convertKeyManagement(int radioButtonId)157     private int convertKeyManagement(int radioButtonId) {
158         switch (radioButtonId) {
159             case NONE: {
160                 return SECURITY_TYPE_NONE;
161             }
162             case WPA: {
163                 return SECURITY_TYPE_WPA;
164             }
165             case WEP: {
166                 return SECURITY_TYPE_WEP;
167             }
168         }
169         return SECURITY_TYPE_NONE;
170     }
171 }
172