SampleTrustAgentSettings.java revision 2bb8bfd1e85c8359412159fde57dd5722f5be58c
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.os.Bundle;
22import android.view.View;
23import android.widget.CheckBox;
24import android.widget.CompoundButton;
25
26public class SampleTrustAgentSettings extends Activity implements View.OnClickListener,
27        CompoundButton.OnCheckedChangeListener {
28
29    private static final int TRUST_DURATION_MS = 30 * 1000;
30
31    private CheckBox mReportUnlockAttempts;
32    private CheckBox mManagingTrust;
33
34    @Override
35    protected void onCreate(@Nullable Bundle savedInstanceState) {
36        super.onCreate(savedInstanceState);
37        setContentView(R.layout.sample_trust_agent_settings);
38
39        findViewById(R.id.enable_trust).setOnClickListener(this);
40        findViewById(R.id.revoke_trust).setOnClickListener(this);
41        findViewById(R.id.crash).setOnClickListener(this);
42
43        mReportUnlockAttempts = (CheckBox) findViewById(R.id.report_unlock_attempts);
44        mReportUnlockAttempts.setOnCheckedChangeListener(this);
45
46        mManagingTrust = (CheckBox) findViewById(R.id.managing_trust);
47        mManagingTrust.setOnCheckedChangeListener(this);
48    }
49
50    @Override
51    protected void onResume() {
52        super.onResume();
53        mReportUnlockAttempts.setChecked(SampleTrustAgent.getReportUnlockAttempts(this));
54        mManagingTrust.setChecked(SampleTrustAgent.getIsManagingTrust(this));
55    }
56
57    @Override
58    public void onClick(View v) {
59        int id = v.getId();
60        if (id == R.id.enable_trust) {
61            SampleTrustAgent.sendGrantTrust(this, "SampleTrustAgent", TRUST_DURATION_MS,
62                    false /* initiatedByUser */);
63        } else if (id == R.id.revoke_trust) {
64            SampleTrustAgent.sendRevokeTrust(this);
65        } else if (id == R.id.crash) {
66            throw new RuntimeException("crash");
67        }
68    }
69
70    @Override
71    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
72        if (buttonView == mReportUnlockAttempts) {
73            SampleTrustAgent.setReportUnlockAttempts(this, isChecked);
74        } else if (buttonView == mManagingTrust) {
75            SampleTrustAgent.setIsManagingTrust(this, isChecked);
76        }
77    }
78}
79