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