SampleTrustAgent.java revision 604e7558ef32098644b2f9456d7743a07ae789dc
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.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.SharedPreferences;
24import android.os.Bundle;
25import android.preference.PreferenceManager;
26import android.service.trust.TrustAgentService;
27import android.support.v4.content.LocalBroadcastManager;
28import android.util.Log;
29import android.widget.Toast;
30
31public class SampleTrustAgent extends TrustAgentService {
32
33    LocalBroadcastManager mLocalBroadcastManager;
34
35    private static final String ACTION_GRANT_TRUST = "action.sample_trust_agent.grant_trust";
36    private static final String ACTION_REVOKE_TRUST = "action.sample_trust_agent.revoke_trust";
37
38    private static final String EXTRA_MESSAGE = "extra.message";
39    private static final String EXTRA_DURATION = "extra.duration";
40    private static final String EXTRA_EXTRA = "extra.extra";
41
42    private static final String PREFERENCE_REPORT_UNLOCK_ATTEMPTS
43            = "preference.report_unlock_attempts";
44
45    private static final String TAG = "SampleTrustAgent";
46
47    @Override
48    public void onCreate() {
49        super.onCreate();
50        IntentFilter filter = new IntentFilter();
51        filter.addAction(ACTION_GRANT_TRUST);
52        filter.addAction(ACTION_REVOKE_TRUST);
53        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
54        mLocalBroadcastManager.registerReceiver(mReceiver, filter);
55    }
56
57    @Override
58    public void onUnlockAttempt(boolean successful) {
59        if (getReportUnlockAttempts(this)) {
60            Toast.makeText(this, "onUnlockAttempt(successful=" + successful + ")",
61                    Toast.LENGTH_SHORT).show();
62        }
63    }
64
65    @Override
66    public boolean onSetTrustAgentFeaturesEnabled(Bundle options) {
67        Log.v(TAG, "Policy options received: " + options.getStringArrayList(KEY_FEATURES));
68        // TODO: Handle options
69        return true; // inform DPM that we support it
70    }
71
72    @Override
73    public void onDestroy() {
74        super.onDestroy();
75        mLocalBroadcastManager.unregisterReceiver(mReceiver);
76    }
77
78    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
79        @Override
80        public void onReceive(Context context, Intent intent) {
81            String action = intent.getAction();
82            if (ACTION_GRANT_TRUST.equals(action)) {
83                grantTrust(intent.getStringExtra(EXTRA_MESSAGE),
84                        intent.getLongExtra(EXTRA_DURATION, 0),
85                        false /* initiatedByUser */);
86            } else if (ACTION_REVOKE_TRUST.equals(action)) {
87                revokeTrust();
88            }
89        }
90    };
91
92    public static void sendGrantTrust(Context context,
93            String message, long durationMs, Bundle extra) {
94        Intent intent = new Intent(ACTION_GRANT_TRUST);
95        intent.putExtra(EXTRA_MESSAGE, message);
96        intent.putExtra(EXTRA_DURATION, durationMs);
97        intent.putExtra(EXTRA_EXTRA, extra);
98        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
99    }
100
101    public static void sendRevokeTrust(Context context) {
102        Intent intent = new Intent(ACTION_REVOKE_TRUST);
103        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
104    }
105
106    public static void setReportUnlockAttempts(Context context, boolean enabled) {
107        SharedPreferences sharedPreferences = PreferenceManager
108                .getDefaultSharedPreferences(context);
109        sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, enabled).apply();
110    }
111
112    public static boolean getReportUnlockAttempts(Context context) {
113        SharedPreferences sharedPreferences = PreferenceManager
114                .getDefaultSharedPreferences(context);
115        return sharedPreferences.getBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, false);
116    }
117}
118