1 /*
2  * Copyright (C) 2018 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.content.Intent;
21 import android.os.Bundle;
22 import android.provider.Settings;
23 import android.util.Log;
24 import android.widget.Toast;
25 
26 import com.android.cts.verifier.R;
27 
28 /**
29  * Tests whether location is available in primary and work profiles under various settings.
30  */
31 public class LocationTestActivity extends ByodTestActivityWithPrepare {
32     private static final String TAG = LocationTestActivity.class.getSimpleName();
33 
34     public static final String ACTION_TEST_LOCATION_ENABLED =
35             "com.android.cts.verifier.managedprovisioning.TEST_LOCATION_ENABLED";
36     public static final String ACTION_TEST_LOCATION_DISABLED =
37             "com.android.cts.verifier.managedprovisioning.TEST_LOCATION_DISABLED";
38     public static final String ACTION_TEST_WORK_LOCATION_DISABLED =
39             "com.android.cts.verifier.managedprovisioning.TEST_WORK_LOCATION_DISABLED";
40     public static final String ACTION_TEST_WORK_LOCATION_DISABLED_PRIMARY =
41             "com.android.cts.verifier.managedprovisioning.TEST_WORK_LOCATION_DISABLED_PRIMARY";
42 
43     public static final String TEST_ID_LOCATION_ENABLED = "BYOD_LocationModeEnableTest";
44     public static final String TEST_ID_LOCATION_DISABLED = "BYOD_LocationModeDisableMainTest";
45     public static final String TEST_ID_WORK_LOCATION_DISABLED = "BYOD_LocationModeDisableWorkTest";
46     public static final String TEST_ID_WORK_LOCATION_DISABLED_PRIMARY =
47             "BYOD_PrimaryLocationWhenWorkDisabled";
48 
49     private static final int REQUEST_LOCATION_CHECK = 123;
50     private static final int REQUEST_LOCATION_SETTING = 124;
51 
52     @Override
onCreate(Bundle savedInstanceState)53     protected void onCreate(Bundle savedInstanceState) {
54         super.onCreate(savedInstanceState);
55         getInstructionsView().setText(getInstructions());
56         getGoButton().setOnClickListener(v -> runTest());
57         getPrepareButton().setOnClickListener(v -> runPrepare());
58     }
59 
60     @Override
getTestId()61     public String getTestId() {
62         switch (getIntent().getAction()) {
63             case ACTION_TEST_LOCATION_ENABLED:
64                 return TEST_ID_LOCATION_ENABLED;
65             case ACTION_TEST_LOCATION_DISABLED:
66                 return TEST_ID_LOCATION_DISABLED;
67             case ACTION_TEST_WORK_LOCATION_DISABLED:
68                 return TEST_ID_WORK_LOCATION_DISABLED;
69             case ACTION_TEST_WORK_LOCATION_DISABLED_PRIMARY:
70                 return TEST_ID_WORK_LOCATION_DISABLED_PRIMARY;
71             default:
72                 throw new IllegalArgumentException("unknown test specified");
73         }
74     }
75 
runPrepare()76     private void runPrepare() {
77         Intent locationSettingsIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
78         if (locationSettingsIntent.resolveActivity(getPackageManager()) != null) {
79             startActivityForResult(locationSettingsIntent, REQUEST_LOCATION_SETTING);
80         } else {
81             Log.e(TAG, "Settings.ACTION_LOCATION_SOURCE_SETTINGS could not be resolved");
82         }
83     }
84 
runTest()85     private void runTest() {
86         Intent intent = new Intent(getCheckerIntentAction());
87         startActivityForResult(intent, LocationTestActivity.REQUEST_LOCATION_CHECK);
88     }
89 
showToast(String message)90     private void showToast(String message) {
91         Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
92     }
93 
getInstructions()94     private int getInstructions() {
95         switch (getIntent().getAction()) {
96             case ACTION_TEST_LOCATION_ENABLED:
97                 return R.string.provisioning_byod_location_mode_enable_instruction;
98             case ACTION_TEST_LOCATION_DISABLED:
99                 return R.string.provisioning_byod_location_mode_disable_instruction;
100             case ACTION_TEST_WORK_LOCATION_DISABLED:
101                 return R.string.provisioning_byod_work_location_mode_disable_instruction;
102             case ACTION_TEST_WORK_LOCATION_DISABLED_PRIMARY:
103                 return R.string.provisioning_byod_primary_location_when_work_disabled_instruction;
104             default:
105                 throw new IllegalArgumentException("unknown test specified");
106         }
107     }
108 
getCheckerIntentAction()109     private String getCheckerIntentAction() {
110         switch (getIntent().getAction()) {
111             case ACTION_TEST_LOCATION_ENABLED:
112             case ACTION_TEST_LOCATION_DISABLED:
113             case ACTION_TEST_WORK_LOCATION_DISABLED:
114                 return LocationCheckerActivity.ACTION_CHECK_LOCATION_WORK;
115             case ACTION_TEST_WORK_LOCATION_DISABLED_PRIMARY:
116                 return LocationCheckerActivity.ACTION_CHECK_LOCATION_PRIMARY;
117             default:
118                 throw new IllegalArgumentException("unknown test specified");
119         }
120     }
121 
locationExpected()122     private boolean locationExpected() {
123         switch (getIntent().getAction()) {
124             case ACTION_TEST_LOCATION_ENABLED:
125             case ACTION_TEST_WORK_LOCATION_DISABLED_PRIMARY:
126                 return true;
127             case ACTION_TEST_LOCATION_DISABLED:
128             case ACTION_TEST_WORK_LOCATION_DISABLED:
129                 return false;
130             default:
131                 throw new IllegalArgumentException("unknown test specified");
132         }
133     }
134 
135     @Override
onActivityResult(int requestCode, int resultCode, Intent data)136     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
137         super.onActivityResult(requestCode, resultCode, data);
138         if (requestCode == REQUEST_LOCATION_CHECK && resultCode == Activity.RESULT_OK) {
139             int errorId = data.getIntExtra(LocationCheckerActivity.EXTRA_ERROR_ID, -1);
140 
141             String message;
142             if (errorId != -1) {
143                 message = getString(errorId);
144             } else {
145                 message = getString(R.string.provisioning_byod_location_obtained);
146             }
147 
148             boolean locationObtained = errorId == -1;
149             if (locationObtained == locationExpected()) {
150                 message = getString(R.string.provisioning_byod_pass_message, message);
151                 getPassButton().setEnabled(true);
152             } else {
153                 message = getString(R.string.provisioning_byod_fail_message, message);
154             }
155             getLogView().setText(message);
156         } else if (requestCode == REQUEST_LOCATION_SETTING) {
157             getGoButton().setEnabled(true);
158         }
159     }
160 }
161