1a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos/* 2a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * Copyright (C) 2014 The Android Open Source Project 3a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * 4a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * Licensed under the Apache License, Version 2.0 (the "License"); 5a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * you may not use this file except in compliance with the License. 6a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * You may obtain a copy of the License at 7a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * 8a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * http://www.apache.org/licenses/LICENSE-2.0 9a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * 10a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * Unless required by applicable law or agreed to in writing, software 11a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * distributed under the License is distributed on an "AS IS" BASIS, 12a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * See the License for the specific language governing permissions and 14a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * limitations under the License 15a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos */ 16a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 17a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roospackage com.android.trustagent.test; 18a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 19a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.annotation.Nullable; 20a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.app.Activity; 212ace72544ad237bb2327d9241af459c239619161Adrian Roosimport android.app.KeyguardManager; 22a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.os.Bundle; 23a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.view.View; 24a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.widget.CheckBox; 25a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.widget.CompoundButton; 262ace72544ad237bb2327d9241af459c239619161Adrian Roosimport android.widget.TextView; 27a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 28a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roospublic class SampleTrustAgentSettings extends Activity implements View.OnClickListener, 29a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos CompoundButton.OnCheckedChangeListener { 30a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 31a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos private static final int TRUST_DURATION_MS = 30 * 1000; 32a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 33a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos private CheckBox mReportUnlockAttempts; 34481a6df99fea124bc4354da34ff668750cdc9041Adrian Roos private CheckBox mReportDeviceLocked; 357861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos private CheckBox mManagingTrust; 3650bfeec868157106e8b60abf8964cb24462af182Adrian Roos private TextView mCheckDeviceLockedResult; 372ace72544ad237bb2327d9241af459c239619161Adrian Roos 382ace72544ad237bb2327d9241af459c239619161Adrian Roos private KeyguardManager mKeyguardManager; 392ace72544ad237bb2327d9241af459c239619161Adrian Roos 40a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 41a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos @Override 42a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos protected void onCreate(@Nullable Bundle savedInstanceState) { 43a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos super.onCreate(savedInstanceState); 442ace72544ad237bb2327d9241af459c239619161Adrian Roos 452ace72544ad237bb2327d9241af459c239619161Adrian Roos mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE); 462ace72544ad237bb2327d9241af459c239619161Adrian Roos 47a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos setContentView(R.layout.sample_trust_agent_settings); 48a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 49a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos findViewById(R.id.enable_trust).setOnClickListener(this); 50a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos findViewById(R.id.revoke_trust).setOnClickListener(this); 51c5f95cea2639b698594a85acbde6a5519941d7b1Adrian Roos findViewById(R.id.crash).setOnClickListener(this); 5250bfeec868157106e8b60abf8964cb24462af182Adrian Roos findViewById(R.id.check_device_locked).setOnClickListener(this); 53a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 54a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos mReportUnlockAttempts = (CheckBox) findViewById(R.id.report_unlock_attempts); 55a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos mReportUnlockAttempts.setOnCheckedChangeListener(this); 567861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos 57481a6df99fea124bc4354da34ff668750cdc9041Adrian Roos mReportDeviceLocked = (CheckBox) findViewById(R.id.report_device_locked); 58481a6df99fea124bc4354da34ff668750cdc9041Adrian Roos mReportDeviceLocked.setOnCheckedChangeListener(this); 59481a6df99fea124bc4354da34ff668750cdc9041Adrian Roos 607861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos mManagingTrust = (CheckBox) findViewById(R.id.managing_trust); 617861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos mManagingTrust.setOnCheckedChangeListener(this); 622ace72544ad237bb2327d9241af459c239619161Adrian Roos 6350bfeec868157106e8b60abf8964cb24462af182Adrian Roos mCheckDeviceLockedResult = (TextView) findViewById(R.id.check_device_locked_result); 64a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos } 65a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 66a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos @Override 67a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos protected void onResume() { 68a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos super.onResume(); 69a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos mReportUnlockAttempts.setChecked(SampleTrustAgent.getReportUnlockAttempts(this)); 707861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos mManagingTrust.setChecked(SampleTrustAgent.getIsManagingTrust(this)); 712ace72544ad237bb2327d9241af459c239619161Adrian Roos updateTrustedState(); 72a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos } 73a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 74a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos @Override 75a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos public void onClick(View v) { 76a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos int id = v.getId(); 77a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos if (id == R.id.enable_trust) { 787e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos SampleTrustAgent.sendGrantTrust(this, "SampleTrustAgent", TRUST_DURATION_MS, 792bb8bfd1e85c8359412159fde57dd5722f5be58cAdrian Roos false /* initiatedByUser */); 80a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos } else if (id == R.id.revoke_trust) { 81a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos SampleTrustAgent.sendRevokeTrust(this); 82c5f95cea2639b698594a85acbde6a5519941d7b1Adrian Roos } else if (id == R.id.crash) { 83c5f95cea2639b698594a85acbde6a5519941d7b1Adrian Roos throw new RuntimeException("crash"); 8450bfeec868157106e8b60abf8964cb24462af182Adrian Roos } else if (id == R.id.check_device_locked) { 852ace72544ad237bb2327d9241af459c239619161Adrian Roos updateTrustedState(); 86a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos } 87a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos } 88a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos 89a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos @Override 90a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 917861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos if (buttonView == mReportUnlockAttempts) { 92a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos SampleTrustAgent.setReportUnlockAttempts(this, isChecked); 937861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos } else if (buttonView == mManagingTrust) { 947861c663fd64af33ec2a4c5ad653c806dc8bd994Adrian Roos SampleTrustAgent.setIsManagingTrust(this, isChecked); 95481a6df99fea124bc4354da34ff668750cdc9041Adrian Roos } else if (buttonView == mReportDeviceLocked) { 96481a6df99fea124bc4354da34ff668750cdc9041Adrian Roos SampleTrustAgent.setReportDeviceLocked(this, isChecked); 97a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos } 98a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos } 992ace72544ad237bb2327d9241af459c239619161Adrian Roos 1002ace72544ad237bb2327d9241af459c239619161Adrian Roos private void updateTrustedState() { 10150bfeec868157106e8b60abf8964cb24462af182Adrian Roos mCheckDeviceLockedResult.setText(Boolean.toString( 10250bfeec868157106e8b60abf8964cb24462af182Adrian Roos mKeyguardManager.isDeviceLocked())); 1032ace72544ad237bb2327d9241af459c239619161Adrian Roos } 104a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos} 105