SampleTrustAgent.java revision 7861c663fd64af33ec2a4c5ad653c806dc8bd994
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        implements SharedPreferences.OnSharedPreferenceChangeListener {
33
34    LocalBroadcastManager mLocalBroadcastManager;
35
36    private static final String ACTION_GRANT_TRUST = "action.sample_trust_agent.grant_trust";
37    private static final String ACTION_REVOKE_TRUST = "action.sample_trust_agent.revoke_trust";
38
39    private static final String EXTRA_MESSAGE = "extra.message";
40    private static final String EXTRA_DURATION = "extra.duration";
41    private static final String EXTRA_EXTRA = "extra.extra";
42
43    private static final String PREFERENCE_REPORT_UNLOCK_ATTEMPTS
44            = "preference.report_unlock_attempts";
45    private static final String PREFERENCE_MANAGING_TRUST
46            = "preference.managing_trust";
47
48    private static final String TAG = "SampleTrustAgent";
49
50    @Override
51    public void onCreate() {
52        super.onCreate();
53        IntentFilter filter = new IntentFilter();
54        filter.addAction(ACTION_GRANT_TRUST);
55        filter.addAction(ACTION_REVOKE_TRUST);
56        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
57        mLocalBroadcastManager.registerReceiver(mReceiver, filter);
58        setManagingTrust(getIsManagingTrust(this));
59        PreferenceManager.getDefaultSharedPreferences(this)
60                .registerOnSharedPreferenceChangeListener(this);
61    }
62
63    @Override
64    public void onUnlockAttempt(boolean successful) {
65        if (getReportUnlockAttempts(this)) {
66            Toast.makeText(this, "onUnlockAttempt(successful=" + successful + ")",
67                    Toast.LENGTH_SHORT).show();
68        }
69    }
70
71    @Override
72    public boolean onSetTrustAgentFeaturesEnabled(Bundle options) {
73        Log.v(TAG, "Policy options received: " + options.getStringArrayList(KEY_FEATURES));
74        // TODO: Handle options
75        return true; // inform DPM that we support it
76    }
77
78    @Override
79    public void onDestroy() {
80        super.onDestroy();
81        mLocalBroadcastManager.unregisterReceiver(mReceiver);
82        PreferenceManager.getDefaultSharedPreferences(this)
83                .unregisterOnSharedPreferenceChangeListener(this);
84    }
85
86    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
87        @Override
88        public void onReceive(Context context, Intent intent) {
89            String action = intent.getAction();
90            if (ACTION_GRANT_TRUST.equals(action)) {
91                try {
92                    grantTrust(intent.getStringExtra(EXTRA_MESSAGE),
93                            intent.getLongExtra(EXTRA_DURATION, 0),
94                            false /* initiatedByUser */);
95                } catch (IllegalStateException e) {
96                    Toast.makeText(context,
97                            "IllegalStateException: " + e.getMessage(), Toast.LENGTH_SHORT).show();
98                }
99            } else if (ACTION_REVOKE_TRUST.equals(action)) {
100                revokeTrust();
101            }
102        }
103    };
104
105    public static void sendGrantTrust(Context context,
106            String message, long durationMs, Bundle extra) {
107        Intent intent = new Intent(ACTION_GRANT_TRUST);
108        intent.putExtra(EXTRA_MESSAGE, message);
109        intent.putExtra(EXTRA_DURATION, durationMs);
110        intent.putExtra(EXTRA_EXTRA, extra);
111        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
112    }
113
114    public static void sendRevokeTrust(Context context) {
115        Intent intent = new Intent(ACTION_REVOKE_TRUST);
116        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
117    }
118
119    public static void setReportUnlockAttempts(Context context, boolean enabled) {
120        SharedPreferences sharedPreferences = PreferenceManager
121                .getDefaultSharedPreferences(context);
122        sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, enabled).apply();
123    }
124
125    public static boolean getReportUnlockAttempts(Context context) {
126        SharedPreferences sharedPreferences = PreferenceManager
127                .getDefaultSharedPreferences(context);
128        return sharedPreferences.getBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, false);
129    }
130
131    public static void setIsManagingTrust(Context context, boolean enabled) {
132        SharedPreferences sharedPreferences = PreferenceManager
133                .getDefaultSharedPreferences(context);
134        sharedPreferences.edit().putBoolean(PREFERENCE_MANAGING_TRUST, enabled).apply();
135    }
136
137    public static boolean getIsManagingTrust(Context context) {
138        SharedPreferences sharedPreferences = PreferenceManager
139                .getDefaultSharedPreferences(context);
140        return sharedPreferences.getBoolean(PREFERENCE_MANAGING_TRUST, false);
141    }
142
143    @Override
144    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
145        if (PREFERENCE_MANAGING_TRUST.equals(key)) {
146            setManagingTrust(getIsManagingTrust(this));
147        }
148    }
149}
150