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.PersistableBundle;
25import android.preference.PreferenceManager;
26import android.service.trust.TrustAgentService;
27import android.support.v4.content.LocalBroadcastManager;
28import android.util.Log;
29import android.widget.Toast;
30
31import java.util.List;
32
33public class SampleTrustAgent extends TrustAgentService
34        implements SharedPreferences.OnSharedPreferenceChangeListener {
35
36    /**
37     * If true, allows anyone to control this trust agent, e.g. using adb:
38     * <pre>
39     * $ adb shell am broadcast -a action.sample_trust_agent.grant_trust\
40     *  -e extra.message SampleTrust\
41     *  --el extra.duration 1000 --ez extra.init_by_user false --ez extra.dismiss_keyguard false
42     * </pre>
43     */
44    private static final boolean ALLOW_EXTERNAL_BROADCASTS = false;
45
46    LocalBroadcastManager mLocalBroadcastManager;
47
48    private static final String ACTION_GRANT_TRUST = "action.sample_trust_agent.grant_trust";
49    private static final String ACTION_REVOKE_TRUST = "action.sample_trust_agent.revoke_trust";
50
51    private static final String EXTRA_MESSAGE = "extra.message";
52    private static final String EXTRA_DURATION = "extra.duration";
53    private static final String EXTRA_INITIATED_BY_USER = "extra.init_by_user";
54    private static final String EXTRA_DISMISS_KEYGUARD = "extra.dismiss_keyguard";
55
56    private static final String PREFERENCE_REPORT_UNLOCK_ATTEMPTS
57            = "preference.report_unlock_attempts";
58    private static final String PREFERENCE_MANAGING_TRUST
59            = "preference.managing_trust";
60    private static final String PREFERENCE_REPORT_DEVICE_LOCKED = "preference.report_device_locked";
61
62    private static final String TAG = "SampleTrustAgent";
63
64    @Override
65    public void onCreate() {
66        super.onCreate();
67        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
68
69        IntentFilter filter = new IntentFilter();
70        filter.addAction(ACTION_GRANT_TRUST);
71        filter.addAction(ACTION_REVOKE_TRUST);
72        mLocalBroadcastManager.registerReceiver(mReceiver, filter);
73        if (ALLOW_EXTERNAL_BROADCASTS) {
74            registerReceiver(mReceiver, filter);
75        }
76
77        setManagingTrust(getIsManagingTrust(this));
78        PreferenceManager.getDefaultSharedPreferences(this)
79                .registerOnSharedPreferenceChangeListener(this);
80    }
81
82    @Override
83    public void onTrustTimeout() {
84        super.onTrustTimeout();
85        logAndShowToast("onTrustTimeout(): timeout expired");
86    }
87
88    @Override
89    public void onDeviceLocked() {
90        super.onDeviceLocked();
91        if (getReportDeviceLocked(this)) {
92            logAndShowToast("onDeviceLocked(): device is now locked");
93        }
94    }
95
96    @Override
97    public void onDeviceUnlocked() {
98        super.onDeviceUnlocked();
99        if (getReportDeviceLocked(this)) {
100            logAndShowToast("onDeviceUnlocked(): device is now unlocked");
101        }
102    }
103
104    @Override
105    public void onUnlockAttempt(boolean successful) {
106        if (getReportUnlockAttempts(this)) {
107            logAndShowToast("onUnlockAttempt(successful=" + successful + ")");
108        }
109    }
110
111    private void logAndShowToast(String text) {
112        Log.i(TAG, text);
113        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
114    }
115
116    @Override
117    public boolean onConfigure(List<PersistableBundle> options) {
118        if (options != null) {
119           for (int i = 0; i < options.size(); i++) {
120               Log.v(TAG, "Policy options received: " + options.get(i));
121           }
122        } else {
123            Log.w(TAG, "onConfigure() called with no options");
124        }
125        // TODO: Handle options
126        return true; // inform DPM that we support it
127    }
128
129    @Override
130    public void onDestroy() {
131        super.onDestroy();
132        mLocalBroadcastManager.unregisterReceiver(mReceiver);
133        if (ALLOW_EXTERNAL_BROADCASTS) {
134            unregisterReceiver(mReceiver);
135        }
136        PreferenceManager.getDefaultSharedPreferences(this)
137                .unregisterOnSharedPreferenceChangeListener(this);
138    }
139
140    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
141        @Override
142        public void onReceive(Context context, Intent intent) {
143            String action = intent.getAction();
144            if (ACTION_GRANT_TRUST.equals(action)) {
145                int flags = 0;
146                if (intent.getBooleanExtra(EXTRA_INITIATED_BY_USER, false)) {
147                    flags |= TrustAgentService.FLAG_GRANT_TRUST_INITIATED_BY_USER;
148                }
149                if (intent.getBooleanExtra(EXTRA_DISMISS_KEYGUARD, false)) {
150                    flags |= TrustAgentService.FLAG_GRANT_TRUST_DISMISS_KEYGUARD;
151                }
152
153                try {
154                    grantTrust(intent.getStringExtra(EXTRA_MESSAGE),
155                            intent.getLongExtra(EXTRA_DURATION, 0), flags);
156                } catch (IllegalStateException e) {
157                    logAndShowToast("IllegalStateException: " + e.getMessage());
158                }
159            } else if (ACTION_REVOKE_TRUST.equals(action)) {
160                revokeTrust();
161            }
162        }
163    };
164
165    public static void sendGrantTrust(Context context,
166            String message, long durationMs, boolean initiatedByUser) {
167        Intent intent = new Intent(ACTION_GRANT_TRUST);
168        intent.putExtra(EXTRA_MESSAGE, message);
169        intent.putExtra(EXTRA_DURATION, durationMs);
170        intent.putExtra(EXTRA_INITIATED_BY_USER, initiatedByUser);
171        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
172    }
173
174    public static void sendRevokeTrust(Context context) {
175        Intent intent = new Intent(ACTION_REVOKE_TRUST);
176        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
177    }
178
179    public static void setReportUnlockAttempts(Context context, boolean enabled) {
180        SharedPreferences sharedPreferences = PreferenceManager
181                .getDefaultSharedPreferences(context);
182        sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, enabled).apply();
183    }
184
185    public static boolean getReportUnlockAttempts(Context context) {
186        SharedPreferences sharedPreferences = PreferenceManager
187                .getDefaultSharedPreferences(context);
188        return sharedPreferences.getBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, false);
189    }
190
191    public static void setReportDeviceLocked(Context context, boolean enabled) {
192        SharedPreferences sharedPreferences = PreferenceManager
193                .getDefaultSharedPreferences(context);
194        sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_DEVICE_LOCKED, enabled).apply();
195    }
196
197    public static boolean getReportDeviceLocked(Context context) {
198        SharedPreferences sharedPreferences = PreferenceManager
199                .getDefaultSharedPreferences(context);
200        return sharedPreferences.getBoolean(PREFERENCE_REPORT_DEVICE_LOCKED, false);
201    }
202
203    public static void setIsManagingTrust(Context context, boolean enabled) {
204        SharedPreferences sharedPreferences = PreferenceManager
205                .getDefaultSharedPreferences(context);
206        sharedPreferences.edit().putBoolean(PREFERENCE_MANAGING_TRUST, enabled).apply();
207    }
208
209    public static boolean getIsManagingTrust(Context context) {
210        SharedPreferences sharedPreferences = PreferenceManager
211                .getDefaultSharedPreferences(context);
212        return sharedPreferences.getBoolean(PREFERENCE_MANAGING_TRUST, false);
213    }
214
215    @Override
216    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
217        if (PREFERENCE_MANAGING_TRUST.equals(key)) {
218            setManagingTrust(getIsManagingTrust(this));
219        }
220    }
221}
222