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 | |
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')
7 files changed, 323 insertions, 0 deletions
diff --git a/packages/Keyguard/test/SampleTrustAgent/Android.mk b/packages/Keyguard/test/SampleTrustAgent/Android.mk new file mode 100644 index 0000000..7551fdf --- /dev/null +++ b/packages/Keyguard/test/SampleTrustAgent/Android.mk @@ -0,0 +1,33 @@ +# 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. +# + +LOCAL_PATH:= $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES := $(call all-java-files-under, src) + +LOCAL_PACKAGE_NAME := SampleTrustAgent + +# Remove these to verify permission checks are working correctly +#LOCAL_CERTIFICATE := platform +#LOCAL_PRIVILEGED_MODULE := true + +LOCAL_MODULE_TAGS := tests + +# LOCAL_PROGUARD_FLAG_FILES := proguard.flags + +LOCAL_STATIC_JAVA_LIBRARIES := android-support-v4 + +include $(BUILD_PACKAGE) diff --git a/packages/Keyguard/test/SampleTrustAgent/AndroidManifest.xml b/packages/Keyguard/test/SampleTrustAgent/AndroidManifest.xml new file mode 100644 index 0000000..1511911 --- /dev/null +++ b/packages/Keyguard/test/SampleTrustAgent/AndroidManifest.xml @@ -0,0 +1,41 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ 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 + --> +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.trustagent.test"> + <uses-sdk android:minSdkVersion="10" android:targetSdkVersion="17"/> + <uses-permission android:name="android.permission.CONTROL_KEYGUARD" /> + <application android:label="@string/app_name"> + <service + android:name=".SampleTrustAgent" + android:label="@string/app_name" + android:exported="true"> + <intent-filter> + <action android:name="android.service.trust.TrustAgentService" /> + <category android:name="android.intent.category.DEFAULT" /> + </intent-filter> + <meta-data android:name="android.service.trust.trustagent" + android:resource="@xml/sample_trust_agent"/> + </service> + + <activity + android:name=".SampleTrustAgentSettings" + android:label="@string/app_name" + android:exported="true" + android:launchMode="singleInstance" > + </activity> + </application> +</manifest> diff --git a/packages/Keyguard/test/SampleTrustAgent/res/layout/sample_trust_agent_settings.xml b/packages/Keyguard/test/SampleTrustAgent/res/layout/sample_trust_agent_settings.xml new file mode 100644 index 0000000..01b107b --- /dev/null +++ b/packages/Keyguard/test/SampleTrustAgent/res/layout/sample_trust_agent_settings.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> + +<!-- + ~ 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 + --> + +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:orientation="vertical" + android:layout_width="match_parent" + android:layout_height="match_parent"> + <Button android:id="@+id/enable_trust" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Grant trust for 30 seconds" /> + <Button android:id="@+id/revoke_trust" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Revoke trust" /> + <CheckBox android:id="@+id/report_unlock_attempts" + android:layout_width="match_parent" + android:layout_height="wrap_content" + android:text="Report unlock attempts" /> +</LinearLayout>
\ No newline at end of file diff --git a/packages/Keyguard/test/SampleTrustAgent/res/values/strings.xml b/packages/Keyguard/test/SampleTrustAgent/res/values/strings.xml new file mode 100644 index 0000000..0c6b502 --- /dev/null +++ b/packages/Keyguard/test/SampleTrustAgent/res/values/strings.xml @@ -0,0 +1,20 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ 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 + --> + +<resources> + <string name="app_name">Sample Trust Agent</string> +</resources>
\ No newline at end of file diff --git a/packages/Keyguard/test/SampleTrustAgent/res/xml/sample_trust_agent.xml b/packages/Keyguard/test/SampleTrustAgent/res/xml/sample_trust_agent.xml new file mode 100644 index 0000000..b48e011 --- /dev/null +++ b/packages/Keyguard/test/SampleTrustAgent/res/xml/sample_trust_agent.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- + ~ 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 + --> +<trust_agent xmlns:android="http://schemas.android.com/apk/res/android" + android:settingsActivity=".SampleTrustAgentSettings" /> 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); + } + } +} |