diff options
author | Adrian Roos <roosa@google.com> | 2014-03-27 14:58:41 +0100 |
---|---|---|
committer | Adrian Roos <roosa@google.com> | 2014-03-31 19:27:52 +0000 |
commit | a3dafcfb26117e3a2efa3983bd7ba79ae6831680 (patch) | |
tree | 99102272450b6d7b16afbc56a9d19221ba925eef /packages/Keyguard/test/SampleTrustAgent/src/com | |
parent | 46842d946d1777c22f05e6bb96933c1b5cbd00d4 (diff) | |
download | frameworks_base-a3dafcfb26117e3a2efa3983bd7ba79ae6831680.zip frameworks_base-a3dafcfb26117e3a2efa3983bd7ba79ae6831680.tar.gz frameworks_base-a3dafcfb26117e3a2efa3983bd7ba79ae6831680.tar.bz2 |
Add a SampleTrustAgent
Adds a simple app that shows how to build a trust agent. For
lack of a better place to put this, this stays in Keyguard's tests
for now.
Bug: 13723878
Change-Id: I9ebad253d3d89c846fe8afaad6babce9e7b80b5e
Diffstat (limited to 'packages/Keyguard/test/SampleTrustAgent/src/com')
2 files changed, 176 insertions, 0 deletions
diff --git a/packages/Keyguard/test/SampleTrustAgent/src/com/android/trustagent/test/SampleTrustAgent.java b/packages/Keyguard/test/SampleTrustAgent/src/com/android/trustagent/test/SampleTrustAgent.java new file mode 100644 index 0000000..25406d6 --- /dev/null +++ b/packages/Keyguard/test/SampleTrustAgent/src/com/android/trustagent/test/SampleTrustAgent.java @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.trustagent.test; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.service.trust.TrustAgentService; +import android.support.v4.content.LocalBroadcastManager; +import android.widget.Toast; + +public class SampleTrustAgent extends TrustAgentService { + + LocalBroadcastManager mLocalBroadcastManager; + + private static final String ACTION_ENABLE_TRUST = "action.sample_trust_agent.enable_trust"; + private static final String ACTION_REVOKE_TRUST = "action.sample_trust_agent.revoke_trust"; + + private static final String EXTRA_MESSAGE = "extra.message"; + private static final String EXTRA_DURATION = "extra.duration"; + private static final String EXTRA_EXTRA = "extra.extra"; + + private static final String PREFERENCE_REPORT_UNLOCK_ATTEMPTS + = "preference.report_unlock_attempts"; + + @Override + public void onCreate() { + super.onCreate(); + IntentFilter filter = new IntentFilter(); + filter.addAction(ACTION_ENABLE_TRUST); + filter.addAction(ACTION_REVOKE_TRUST); + mLocalBroadcastManager = LocalBroadcastManager.getInstance(this); + mLocalBroadcastManager.registerReceiver(mReceiver, filter); + } + + @Override + protected void onUnlockAttempt(boolean successful) { + if (getReportUnlockAttempts(this)) { + Toast.makeText(this, "onUnlockAttempt(successful=" + successful + ")", + Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onDestroy() { + super.onDestroy(); + mLocalBroadcastManager.unregisterReceiver(mReceiver); + } + + private BroadcastReceiver mReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + String action = intent.getAction(); + if (ACTION_ENABLE_TRUST.equals(action)) { + enableTrust(intent.getStringExtra(EXTRA_MESSAGE), + intent.getLongExtra(EXTRA_DURATION, 0), + false /* initiatedByUser */); + } else if (ACTION_REVOKE_TRUST.equals(action)) { + revokeTrust(); + } + } + }; + + public static void sendEnableTrust(Context context, + String message, long durationMs, Bundle extra) { + Intent intent = new Intent(ACTION_ENABLE_TRUST); + intent.putExtra(EXTRA_MESSAGE, message); + intent.putExtra(EXTRA_DURATION, durationMs); + intent.putExtra(EXTRA_EXTRA, extra); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); + } + + public static void sendRevokeTrust(Context context) { + Intent intent = new Intent(ACTION_REVOKE_TRUST); + LocalBroadcastManager.getInstance(context).sendBroadcast(intent); + } + + public static void setReportUnlockAttempts(Context context, boolean enabled) { + SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(context); + sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, enabled).apply(); + } + + public static boolean getReportUnlockAttempts(Context context) { + SharedPreferences sharedPreferences = PreferenceManager + .getDefaultSharedPreferences(context); + return sharedPreferences.getBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, false); + } +} diff --git a/packages/Keyguard/test/SampleTrustAgent/src/com/android/trustagent/test/SampleTrustAgentSettings.java b/packages/Keyguard/test/SampleTrustAgent/src/com/android/trustagent/test/SampleTrustAgentSettings.java new file mode 100644 index 0000000..0a6f675 --- /dev/null +++ b/packages/Keyguard/test/SampleTrustAgent/src/com/android/trustagent/test/SampleTrustAgentSettings.java @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.trustagent.test; + +import android.annotation.Nullable; +import android.app.Activity; +import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.view.View; +import android.widget.CheckBox; +import android.widget.CompoundButton; + +public class SampleTrustAgentSettings extends Activity implements View.OnClickListener, + CompoundButton.OnCheckedChangeListener { + + private static final int TRUST_DURATION_MS = 30 * 1000; + + private CheckBox mReportUnlockAttempts; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.sample_trust_agent_settings); + + findViewById(R.id.enable_trust).setOnClickListener(this); + findViewById(R.id.revoke_trust).setOnClickListener(this); + + mReportUnlockAttempts = (CheckBox) findViewById(R.id.report_unlock_attempts); + mReportUnlockAttempts.setOnCheckedChangeListener(this); + } + + @Override + protected void onResume() { + super.onResume(); + mReportUnlockAttempts.setChecked(SampleTrustAgent.getReportUnlockAttempts(this)); + } + + @Override + public void onClick(View v) { + int id = v.getId(); + if (id == R.id.enable_trust) { + SampleTrustAgent.sendEnableTrust(this, "SampleTrustAgent", TRUST_DURATION_MS, + null /* extra */); + } else if (id == R.id.revoke_trust) { + SampleTrustAgent.sendRevokeTrust(this); + } + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (buttonView.getId() == R.id.report_unlock_attempts) { + SampleTrustAgent.setReportUnlockAttempts(this, isChecked); + } + } +} |