1/*
2 * Copyright (C) 2014 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
17package com.android.trustagent.test;
18
19import android.annotation.Nullable;
20import android.app.Activity;
21import android.app.KeyguardManager;
22import android.os.Bundle;
23import android.view.View;
24import android.widget.CheckBox;
25import android.widget.CompoundButton;
26import android.widget.TextView;
27
28public class SampleTrustAgentSettings extends Activity implements View.OnClickListener,
29        CompoundButton.OnCheckedChangeListener {
30
31    private static final int TRUST_DURATION_MS = 30 * 1000;
32
33    private CheckBox mReportUnlockAttempts;
34    private CheckBox mReportDeviceLocked;
35    private CheckBox mManagingTrust;
36    private TextView mCheckDeviceLockedResult;
37
38    private KeyguardManager mKeyguardManager;
39
40
41    @Override
42    protected void onCreate(@Nullable Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        mKeyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
46
47        setContentView(R.layout.sample_trust_agent_settings);
48
49        findViewById(R.id.enable_trust).setOnClickListener(this);
50        findViewById(R.id.revoke_trust).setOnClickListener(this);
51        findViewById(R.id.crash).setOnClickListener(this);
52        findViewById(R.id.check_device_locked).setOnClickListener(this);
53
54        mReportUnlockAttempts = (CheckBox) findViewById(R.id.report_unlock_attempts);
55        mReportUnlockAttempts.setOnCheckedChangeListener(this);
56
57        mReportDeviceLocked = (CheckBox) findViewById(R.id.report_device_locked);
58        mReportDeviceLocked.setOnCheckedChangeListener(this);
59
60        mManagingTrust = (CheckBox) findViewById(R.id.managing_trust);
61        mManagingTrust.setOnCheckedChangeListener(this);
62
63        mCheckDeviceLockedResult = (TextView) findViewById(R.id.check_device_locked_result);
64    }
65
66    @Override
67    protected void onResume() {
68        super.onResume();
69        mReportUnlockAttempts.setChecked(SampleTrustAgent.getReportUnlockAttempts(this));
70        mManagingTrust.setChecked(SampleTrustAgent.getIsManagingTrust(this));
71        updateTrustedState();
72    }
73
74    @Override
75    public void onClick(View v) {
76        int id = v.getId();
77        if (id == R.id.enable_trust) {
78            SampleTrustAgent.sendGrantTrust(this, "SampleTrustAgent", TRUST_DURATION_MS,
79                    false /* initiatedByUser */);
80        } else if (id == R.id.revoke_trust) {
81            SampleTrustAgent.sendRevokeTrust(this);
82        } else if (id == R.id.crash) {
83            throw new RuntimeException("crash");
84        } else if (id == R.id.check_device_locked) {
85            updateTrustedState();
86        }
87    }
88
89    @Override
90    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
91        if (buttonView == mReportUnlockAttempts) {
92            SampleTrustAgent.setReportUnlockAttempts(this, isChecked);
93        } else if (buttonView == mManagingTrust) {
94            SampleTrustAgent.setIsManagingTrust(this, isChecked);
95        } else if (buttonView == mReportDeviceLocked) {
96            SampleTrustAgent.setReportDeviceLocked(this, isChecked);
97        }
98    }
99
100    private void updateTrustedState() {
101        mCheckDeviceLockedResult.setText(Boolean.toString(
102                mKeyguardManager.isDeviceLocked()));
103    }
104}
105