1 /* 2 * Copyright (C) 2017 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.admin.tapjacking; 18 19 import android.app.admin.DevicePolicyManager; 20 import android.content.ComponentName; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.os.SystemClock; 24 import android.util.Log; 25 import android.view.View; 26 import android.widget.Button; 27 28 import com.android.cts.verifier.PassFailButtons; 29 import com.android.cts.verifier.R; 30 31 /** 32 * Test that checks that device admin activate button does not allow taps when another window 33 * is obscuring the device admin details 34 */ 35 public class DeviceAdminTapjackingTestActivity extends PassFailButtons.Activity implements 36 View.OnClickListener { 37 38 private static final String TAG = DeviceAdminTapjackingTestActivity.class.getSimpleName(); 39 private static final String ADMIN_ACTIVATED_BUNDLE_KEY = "admin_activated"; 40 private static final String ACTIVITIES_FINISHED_IN_ORDER_KEY = "activities_finished_in_order"; 41 private static final int REQUEST_ENABLE_ADMIN = 0; 42 private static final int REQUEST_OVERLAY_ACTIVITY = 1; 43 private static final long REMOVE_ADMIN_TIMEOUT = 5000; 44 45 private DevicePolicyManager mDevicePolicyManager; 46 private Button mAddDeviceAdminButton; 47 private boolean mAdminActivated; 48 private boolean mActivitiesFinishedInOrder; 49 private boolean mOverlayFinished; 50 private ComponentName mAdmin; 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 setContentView(R.layout.da_tapjacking_test_main); 56 setInfoResources(R.string.da_tapjacking_test, R.string.da_tapjacking_test_info, -1); 57 setPassFailButtonClickListeners(); 58 59 mAdmin = new ComponentName(this, EmptyDeviceAdminReceiver.class); 60 mDevicePolicyManager = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); 61 62 if (savedInstanceState != null) { 63 mAdminActivated = savedInstanceState.getBoolean(ADMIN_ACTIVATED_BUNDLE_KEY, false); 64 mActivitiesFinishedInOrder = savedInstanceState.getBoolean( 65 ACTIVITIES_FINISHED_IN_ORDER_KEY, false); 66 } else if (isActiveAdminAfterTimeout()) { 67 Log.e(TAG, "Could not remove active admin. Cannot proceed with test"); 68 finish(); 69 } 70 mAddDeviceAdminButton = findViewById(R.id.enable_admin_overlay_button); 71 mAddDeviceAdminButton.setOnClickListener(this); 72 } 73 74 private boolean isActiveAdminAfterTimeout() { 75 final long timeOut = SystemClock.uptimeMillis() + REMOVE_ADMIN_TIMEOUT; 76 while (mDevicePolicyManager.isAdminActive(mAdmin) 77 && SystemClock.uptimeMillis() < timeOut ) { 78 try { 79 Thread.sleep(1000); 80 } catch(InterruptedException exc) { 81 } 82 } 83 return mDevicePolicyManager.isAdminActive(mAdmin); 84 } 85 86 @Override 87 public void onClick(View v) { 88 if (v == mAddDeviceAdminButton) { 89 Intent securitySettingsIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 90 securitySettingsIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdmin); 91 startActivityForResult(securitySettingsIntent, REQUEST_ENABLE_ADMIN); 92 try { 93 Thread.sleep(2000); 94 } catch (InterruptedException exc) { 95 } 96 startActivityForResult(new Intent(this, OverlayingActivity.class), 97 REQUEST_OVERLAY_ACTIVITY); 98 } 99 } 100 101 @Override 102 public void onActivityResult(int requestCode, int resultCode, Intent data) { 103 if (requestCode == REQUEST_ENABLE_ADMIN) { 104 mActivitiesFinishedInOrder = mOverlayFinished; 105 if (resultCode == RESULT_OK) { 106 mAdminActivated = true; 107 Log.e(TAG, "Admin was activated. Restart the Test"); 108 } 109 } 110 else if (requestCode == REQUEST_OVERLAY_ACTIVITY) { 111 mOverlayFinished = true; 112 } 113 } 114 115 @Override 116 public void onResume() { 117 super.onResume(); 118 updateWidgets(); 119 } 120 121 @Override 122 public void onSaveInstanceState(Bundle icicle) { 123 icicle.putBoolean(ADMIN_ACTIVATED_BUNDLE_KEY, mAdminActivated); 124 icicle.putBoolean(ACTIVITIES_FINISHED_IN_ORDER_KEY, mActivitiesFinishedInOrder); 125 } 126 127 private void updateWidgets() { 128 mAddDeviceAdminButton.setEnabled(!mActivitiesFinishedInOrder && !mAdminActivated); 129 getPassButton().setEnabled(!mAdminActivated && mActivitiesFinishedInOrder); 130 } 131 } 132