SampleTrustAgent.java revision 7e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1
1a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos/*
2a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * Copyright (C) 2014 The Android Open Source Project
3a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos *
4a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * Licensed under the Apache License, Version 2.0 (the "License");
5a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * you may not use this file except in compliance with the License.
6a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * You may obtain a copy of the License at
7a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos *
8a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos *      http://www.apache.org/licenses/LICENSE-2.0
9a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos *
10a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * Unless required by applicable law or agreed to in writing, software
11a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * distributed under the License is distributed on an "AS IS" BASIS,
12a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * See the License for the specific language governing permissions and
14a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos * limitations under the License
15a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos */
16a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
17a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roospackage com.android.trustagent.test;
18a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
19a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.content.BroadcastReceiver;
20a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.content.Context;
21a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.content.Intent;
22a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.content.IntentFilter;
23a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.content.SharedPreferences;
24a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.os.Bundle;
25a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.preference.PreferenceManager;
26a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.service.trust.TrustAgentService;
27a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.support.v4.content.LocalBroadcastManager;
28a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roosimport android.widget.Toast;
29a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
30a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roospublic class SampleTrustAgent extends TrustAgentService {
31a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
32a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    LocalBroadcastManager mLocalBroadcastManager;
33a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
347e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos    private static final String ACTION_GRANT_TRUST = "action.sample_trust_agent.grant_trust";
35a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    private static final String ACTION_REVOKE_TRUST = "action.sample_trust_agent.revoke_trust";
36a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
37a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    private static final String EXTRA_MESSAGE = "extra.message";
38a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    private static final String EXTRA_DURATION = "extra.duration";
39a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    private static final String EXTRA_EXTRA = "extra.extra";
40a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
41a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    private static final String PREFERENCE_REPORT_UNLOCK_ATTEMPTS
42a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos            = "preference.report_unlock_attempts";
43a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
44a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    @Override
45a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    public void onCreate() {
46a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        super.onCreate();
47a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        IntentFilter filter = new IntentFilter();
487e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos        filter.addAction(ACTION_GRANT_TRUST);
49a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        filter.addAction(ACTION_REVOKE_TRUST);
50a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
51a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        mLocalBroadcastManager.registerReceiver(mReceiver, filter);
52a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    }
53a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
54a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    @Override
557e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos    public void onUnlockAttempt(boolean successful) {
56a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        if (getReportUnlockAttempts(this)) {
57a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos            Toast.makeText(this, "onUnlockAttempt(successful=" + successful + ")",
58a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos                    Toast.LENGTH_SHORT).show();
59a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        }
60a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    }
61a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
62a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    @Override
63a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    public void onDestroy() {
64a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        super.onDestroy();
65a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        mLocalBroadcastManager.unregisterReceiver(mReceiver);
66a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    }
67a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
68a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
69a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        @Override
70a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        public void onReceive(Context context, Intent intent) {
71a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos            String action = intent.getAction();
727e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos            if (ACTION_GRANT_TRUST.equals(action)) {
737e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos                grantTrust(intent.getStringExtra(EXTRA_MESSAGE),
74a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos                        intent.getLongExtra(EXTRA_DURATION, 0),
75a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos                        false /* initiatedByUser */);
76a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos            } else if (ACTION_REVOKE_TRUST.equals(action)) {
77a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos                revokeTrust();
78a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos            }
79a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        }
80a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    };
81a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
827e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos    public static void sendGrantTrust(Context context,
83a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos            String message, long durationMs, Bundle extra) {
847e03dfcb796ef1a6000a5fd5fda03c9e15ea62e1Adrian Roos        Intent intent = new Intent(ACTION_GRANT_TRUST);
85a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        intent.putExtra(EXTRA_MESSAGE, message);
86a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        intent.putExtra(EXTRA_DURATION, durationMs);
87a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        intent.putExtra(EXTRA_EXTRA, extra);
88a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
89a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    }
90a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
91a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    public static void sendRevokeTrust(Context context) {
92a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        Intent intent = new Intent(ACTION_REVOKE_TRUST);
93a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        LocalBroadcastManager.getInstance(context).sendBroadcast(intent);
94a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    }
95a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
96a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    public static void setReportUnlockAttempts(Context context, boolean enabled) {
97a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        SharedPreferences sharedPreferences = PreferenceManager
98a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos                .getDefaultSharedPreferences(context);
99a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        sharedPreferences.edit().putBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, enabled).apply();
100a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    }
101a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos
102a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    public static boolean getReportUnlockAttempts(Context context) {
103a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        SharedPreferences sharedPreferences = PreferenceManager
104a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos                .getDefaultSharedPreferences(context);
105a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos        return sharedPreferences.getBoolean(PREFERENCE_REPORT_UNLOCK_ATTEMPTS, false);
106a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos    }
107a3dafcfb26117e3a2efa3983bd7ba79ae6831680Adrian Roos}
108