blob: 010931387671009f8b2f87f917d5326f3bfa2d60 [file] [log] [blame]
Adrian Roos82142c22014-03-27 14:56:59 +01001/*
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.server.trust;
18
Jim Millerd4efaac2014-08-14 18:02:45 -070019import android.app.AlarmManager;
20import android.app.PendingIntent;
Jim Miller604e7552014-07-18 19:00:02 -070021import android.app.admin.DevicePolicyManager;
Jim Millerd4efaac2014-08-14 18:02:45 -070022import android.content.BroadcastReceiver;
Adrian Roos82142c22014-03-27 14:56:59 +010023import android.content.ComponentName;
24import android.content.Context;
25import android.content.Intent;
Jim Millerd4efaac2014-08-14 18:02:45 -070026import android.content.IntentFilter;
Adrian Roos82142c22014-03-27 14:56:59 +010027import android.content.ServiceConnection;
Jim Millerd4efaac2014-08-14 18:02:45 -070028import android.net.Uri;
Adrian Roos8f211582014-07-29 15:09:57 +020029import android.os.Binder;
Adrian Roos82142c22014-03-27 14:56:59 +010030import android.os.Handler;
31import android.os.IBinder;
32import android.os.Message;
Jim Millerd4efaac2014-08-14 18:02:45 -070033import android.os.PatternMatcher;
Jim Millere303bf42014-08-26 17:12:29 -070034import android.os.PersistableBundle;
Adrian Roos82142c22014-03-27 14:56:59 +010035import android.os.RemoteException;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020036import android.os.SystemClock;
Adrian Roos82142c22014-03-27 14:56:59 +010037import android.os.UserHandle;
38import android.util.Log;
39import android.util.Slog;
40import android.service.trust.ITrustAgentService;
41import android.service.trust.ITrustAgentServiceCallback;
Jim Miller604e7552014-07-18 19:00:02 -070042
Adrian Roosa43fd032015-03-09 19:10:15 +010043import java.util.Collections;
Jim Miller604e7552014-07-18 19:00:02 -070044import java.util.List;
Adrian Roos82142c22014-03-27 14:56:59 +010045
46/**
47 * A wrapper around a TrustAgentService interface. Coordinates communication between
48 * TrustManager and the actual TrustAgent.
49 */
50public class TrustAgentWrapper {
Jim Millerd4efaac2014-08-14 18:02:45 -070051 private static final String EXTRA_COMPONENT_NAME = "componentName";
52 private static final String TRUST_EXPIRED_ACTION = "android.server.trust.TRUST_EXPIRED_ACTION";
Jim Miller76b9b8b2014-08-22 17:04:57 -070053 private static final String PERMISSION = android.Manifest.permission.PROVIDE_TRUST_AGENT;
Adrian Roos82142c22014-03-27 14:56:59 +010054 private static final boolean DEBUG = false;
55 private static final String TAG = "TrustAgentWrapper";
56
Adrian Roos7a4f3d42014-05-02 12:12:20 +020057 private static final int MSG_GRANT_TRUST = 1;
Adrian Roos82142c22014-03-27 14:56:59 +010058 private static final int MSG_REVOKE_TRUST = 2;
59 private static final int MSG_TRUST_TIMEOUT = 3;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020060 private static final int MSG_RESTART_TIMEOUT = 4;
Adrian Roos8f211582014-07-29 15:09:57 +020061 private static final int MSG_SET_TRUST_AGENT_FEATURES_COMPLETED = 5;
Adrian Roos7861c662014-07-25 15:37:28 +020062 private static final int MSG_MANAGING_TRUST = 6;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020063
64 /**
65 * Time in uptime millis that we wait for the service connection, both when starting
66 * and when the service disconnects.
67 */
68 private static final long RESTART_TIMEOUT_MILLIS = 5 * 60000;
Adrian Roos82142c22014-03-27 14:56:59 +010069
Adrian Roos7a4f3d42014-05-02 12:12:20 +020070 /**
71 * Long extra for {@link #MSG_GRANT_TRUST}
72 */
73 private static final String DATA_DURATION = "duration";
74
Adrian Roos82142c22014-03-27 14:56:59 +010075 private final TrustManagerService mTrustManagerService;
76 private final int mUserId;
77 private final Context mContext;
78 private final ComponentName mName;
79
80 private ITrustAgentService mTrustAgentService;
Adrian Roosc5f95ce2014-07-24 16:00:46 +020081 private boolean mBound;
82 private long mScheduledRestartUptimeMillis;
Jim Miller76b9b8b2014-08-22 17:04:57 -070083 private long mMaximumTimeToLock; // from DevicePolicyManager
Adrian Roos82142c22014-03-27 14:56:59 +010084
85 // Trust state
86 private boolean mTrusted;
Adrian Roos7e03dfc2014-05-16 16:06:28 +020087 private CharSequence mMessage;
Jim Miller604e7552014-07-18 19:00:02 -070088 private boolean mTrustDisabledByDpm;
Adrian Roos7861c662014-07-25 15:37:28 +020089 private boolean mManagingTrust;
Adrian Roos8f211582014-07-29 15:09:57 +020090 private IBinder mSetTrustAgentFeaturesToken;
Jim Millerd4efaac2014-08-14 18:02:45 -070091 private AlarmManager mAlarmManager;
92 private final Intent mAlarmIntent;
Jim Miller76b9b8b2014-08-22 17:04:57 -070093 private PendingIntent mAlarmPendingIntent;
Jim Millerd4efaac2014-08-14 18:02:45 -070094
95 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
96 @Override
97 public void onReceive(Context context, Intent intent) {
98 ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT_NAME);
99 if (TRUST_EXPIRED_ACTION.equals(intent.getAction())
100 && mName.equals(component)) {
101 mHandler.removeMessages(MSG_TRUST_TIMEOUT);
102 mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT);
103 }
104 }
105 };
Adrian Roos82142c22014-03-27 14:56:59 +0100106
107 private final Handler mHandler = new Handler() {
108 @Override
109 public void handleMessage(Message msg) {
110 switch (msg.what) {
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200111 case MSG_GRANT_TRUST:
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200112 if (!isConnected()) {
113 Log.w(TAG, "Agent is not connected, cannot grant trust: "
114 + mName.flattenToShortString());
115 return;
116 }
Adrian Roos82142c22014-03-27 14:56:59 +0100117 mTrusted = true;
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200118 mMessage = (CharSequence) msg.obj;
Adrian Roos82142c22014-03-27 14:56:59 +0100119 boolean initiatedByUser = msg.arg1 != 0;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200120 long durationMs = msg.getData().getLong(DATA_DURATION);
121 if (durationMs > 0) {
Jim Miller76b9b8b2014-08-22 17:04:57 -0700122 final long duration;
123 if (mMaximumTimeToLock != 0) {
124 // Enforce DevicePolicyManager timeout. This is here as a safeguard to
125 // ensure trust agents are evaluating trust state at least as often as
126 // the policy dictates. Admins that want more guarantees should be using
127 // DevicePolicyManager#KEYGUARD_DISABLE_TRUST_AGENTS.
128 duration = Math.min(durationMs, mMaximumTimeToLock);
129 if (DEBUG) {
130 Log.v(TAG, "DPM lock timeout in effect. Timeout adjusted from "
131 + durationMs + " to " + duration);
132 }
133 } else {
134 duration = durationMs;
135 }
136 long expiration = SystemClock.elapsedRealtime() + duration;
137 mAlarmPendingIntent = PendingIntent.getBroadcast(mContext, 0, mAlarmIntent,
Jim Millerd4efaac2014-08-14 18:02:45 -0700138 PendingIntent.FLAG_CANCEL_CURRENT);
Jim Miller76b9b8b2014-08-22 17:04:57 -0700139 mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, expiration,
140 mAlarmPendingIntent);
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200141 }
142 mTrustManagerService.mArchive.logGrantTrust(mUserId, mName,
143 (mMessage != null ? mMessage.toString() : null),
144 durationMs, initiatedByUser);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200145 mTrustManagerService.updateTrust(mUserId, initiatedByUser);
Adrian Roos82142c22014-03-27 14:56:59 +0100146 break;
147 case MSG_TRUST_TIMEOUT:
148 if (DEBUG) Slog.v(TAG, "Trust timed out : " + mName.flattenToShortString());
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200149 mTrustManagerService.mArchive.logTrustTimeout(mUserId, mName);
Jim Millerd4efaac2014-08-14 18:02:45 -0700150 onTrustTimeout();
Adrian Roos82142c22014-03-27 14:56:59 +0100151 // Fall through.
152 case MSG_REVOKE_TRUST:
153 mTrusted = false;
154 mMessage = null;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200155 mHandler.removeMessages(MSG_TRUST_TIMEOUT);
156 if (msg.what == MSG_REVOKE_TRUST) {
157 mTrustManagerService.mArchive.logRevokeTrust(mUserId, mName);
158 }
Adrian Roos3c9a3502014-08-06 19:09:45 +0200159 mTrustManagerService.updateTrust(mUserId, false);
Adrian Roos82142c22014-03-27 14:56:59 +0100160 break;
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200161 case MSG_RESTART_TIMEOUT:
Adrian Roosfc29e0b2014-11-11 12:55:44 +0100162 destroy();
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200163 mTrustManagerService.resetAgent(mName, mUserId);
164 break;
Adrian Roos8f211582014-07-29 15:09:57 +0200165 case MSG_SET_TRUST_AGENT_FEATURES_COMPLETED:
166 IBinder token = (IBinder) msg.obj;
167 boolean result = msg.arg1 != 0;
168 if (mSetTrustAgentFeaturesToken == token) {
169 mSetTrustAgentFeaturesToken = null;
170 if (mTrustDisabledByDpm && result) {
171 if (DEBUG) Log.v(TAG, "Re-enabling agent because it acknowledged "
172 + "enabled features: " + mName);
173 mTrustDisabledByDpm = false;
Adrian Roos3c9a3502014-08-06 19:09:45 +0200174 mTrustManagerService.updateTrust(mUserId, false);
Adrian Roos8f211582014-07-29 15:09:57 +0200175 }
176 } else {
177 if (DEBUG) Log.w(TAG, "Ignoring MSG_SET_TRUST_AGENT_FEATURES_COMPLETED "
178 + "with obsolete token: " + mName);
179 }
Jim Miller604e7552014-07-18 19:00:02 -0700180 break;
Adrian Roos7861c662014-07-25 15:37:28 +0200181 case MSG_MANAGING_TRUST:
182 mManagingTrust = msg.arg1 != 0;
183 if (!mManagingTrust) {
184 mTrusted = false;
185 mMessage = null;
186 }
187 mTrustManagerService.mArchive.logManagingTrust(mUserId, mName, mManagingTrust);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200188 mTrustManagerService.updateTrust(mUserId, false);
Adrian Roos7861c662014-07-25 15:37:28 +0200189 break;
Adrian Roos82142c22014-03-27 14:56:59 +0100190 }
191 }
192 };
193
194 private ITrustAgentServiceCallback mCallback = new ITrustAgentServiceCallback.Stub() {
195
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200196 @Override
197 public void grantTrust(CharSequence userMessage, long durationMs, boolean initiatedByUser) {
Adrian Roos82142c22014-03-27 14:56:59 +0100198 if (DEBUG) Slog.v(TAG, "enableTrust(" + userMessage + ", durationMs = " + durationMs
199 + ", initiatedByUser = " + initiatedByUser + ")");
200
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200201 Message msg = mHandler.obtainMessage(
202 MSG_GRANT_TRUST, initiatedByUser ? 1 : 0, 0, userMessage);
203 msg.getData().putLong(DATA_DURATION, durationMs);
204 msg.sendToTarget();
Adrian Roos82142c22014-03-27 14:56:59 +0100205 }
206
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200207 @Override
Adrian Roos82142c22014-03-27 14:56:59 +0100208 public void revokeTrust() {
209 if (DEBUG) Slog.v(TAG, "revokeTrust()");
210 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
211 }
Adrian Roos7861c662014-07-25 15:37:28 +0200212
213 @Override
214 public void setManagingTrust(boolean managingTrust) {
215 if (DEBUG) Slog.v(TAG, "managingTrust()");
216 mHandler.obtainMessage(MSG_MANAGING_TRUST, managingTrust ? 1 : 0, 0).sendToTarget();
217 }
Adrian Roos8f211582014-07-29 15:09:57 +0200218
219 @Override
Jim Millere303bf42014-08-26 17:12:29 -0700220 public void onConfigureCompleted(boolean result, IBinder token) {
Adrian Roos8f211582014-07-29 15:09:57 +0200221 if (DEBUG) Slog.v(TAG, "onSetTrustAgentFeaturesEnabledCompleted(result=" + result);
222 mHandler.obtainMessage(MSG_SET_TRUST_AGENT_FEATURES_COMPLETED,
223 result ? 1 : 0, 0, token).sendToTarget();
224 }
Adrian Roos82142c22014-03-27 14:56:59 +0100225 };
226
227 private final ServiceConnection mConnection = new ServiceConnection() {
228 @Override
229 public void onServiceConnected(ComponentName name, IBinder service) {
230 if (DEBUG) Log.v(TAG, "TrustAgent started : " + name.flattenToString());
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200231 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
Adrian Roos82142c22014-03-27 14:56:59 +0100232 mTrustAgentService = ITrustAgentService.Stub.asInterface(service);
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200233 mTrustManagerService.mArchive.logAgentConnected(mUserId, name);
Adrian Roos82142c22014-03-27 14:56:59 +0100234 setCallback(mCallback);
Adrian Roos8f211582014-07-29 15:09:57 +0200235 updateDevicePolicyFeatures();
Adrian Roos481a6df2014-11-20 19:48:56 +0100236
237 if (mTrustManagerService.isDeviceLockedInner(mUserId)) {
238 onDeviceLocked();
239 } else {
240 onDeviceUnlocked();
241 }
Adrian Roos82142c22014-03-27 14:56:59 +0100242 }
243
244 @Override
245 public void onServiceDisconnected(ComponentName name) {
246 if (DEBUG) Log.v(TAG, "TrustAgent disconnected : " + name.flattenToShortString());
247 mTrustAgentService = null;
Adrian Roos7861c662014-07-25 15:37:28 +0200248 mManagingTrust = false;
Adrian Roos8f211582014-07-29 15:09:57 +0200249 mSetTrustAgentFeaturesToken = null;
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200250 mTrustManagerService.mArchive.logAgentDied(mUserId, name);
Adrian Roos82142c22014-03-27 14:56:59 +0100251 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200252 if (mBound) {
253 scheduleRestart();
254 }
Jim Miller604e7552014-07-18 19:00:02 -0700255 // mTrustDisabledByDpm maintains state
Jim Miller604e7552014-07-18 19:00:02 -0700256 }
257 };
Adrian Roos82142c22014-03-27 14:56:59 +0100258
259 public TrustAgentWrapper(Context context, TrustManagerService trustManagerService,
260 Intent intent, UserHandle user) {
261 mContext = context;
262 mTrustManagerService = trustManagerService;
Jim Millerd4efaac2014-08-14 18:02:45 -0700263 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
Adrian Roos82142c22014-03-27 14:56:59 +0100264 mUserId = user.getIdentifier();
265 mName = intent.getComponent();
Jim Millerd4efaac2014-08-14 18:02:45 -0700266
267 mAlarmIntent = new Intent(TRUST_EXPIRED_ACTION).putExtra(EXTRA_COMPONENT_NAME, mName);
268 mAlarmIntent.setData(Uri.parse(mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME)));
Jim Miller76b9b8b2014-08-22 17:04:57 -0700269 mAlarmIntent.setPackage(context.getPackageName());
Jim Millerd4efaac2014-08-14 18:02:45 -0700270
271 final IntentFilter alarmFilter = new IntentFilter(TRUST_EXPIRED_ACTION);
272 alarmFilter.addDataScheme(mAlarmIntent.getScheme());
273 final String pathUri = mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME);
274 alarmFilter.addDataPath(pathUri, PatternMatcher.PATTERN_LITERAL);
Jim Miller76b9b8b2014-08-22 17:04:57 -0700275 mContext.registerReceiver(mBroadcastReceiver, alarmFilter, PERMISSION, null);
Jim Millerd4efaac2014-08-14 18:02:45 -0700276
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200277 // Schedules a restart for when connecting times out. If the connection succeeds,
278 // the restart is canceled in mCallback's onConnected.
279 scheduleRestart();
280 mBound = context.bindServiceAsUser(intent, mConnection, Context.BIND_AUTO_CREATE, user);
281 if (!mBound) {
282 Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString());
Adrian Roos82142c22014-03-27 14:56:59 +0100283 }
284 }
285
286 private void onError(Exception e) {
287 Slog.w(TAG , "Remote Exception", e);
288 }
289
Jim Millerd4efaac2014-08-14 18:02:45 -0700290 private void onTrustTimeout() {
291 try {
292 if (mTrustAgentService != null) mTrustAgentService.onTrustTimeout();
293 } catch (RemoteException e) {
294 onError(e);
295 }
296 }
Adrian Roos481a6df2014-11-20 19:48:56 +0100297
Adrian Roos82142c22014-03-27 14:56:59 +0100298 /**
299 * @see android.service.trust.TrustAgentService#onUnlockAttempt(boolean)
300 */
301 public void onUnlockAttempt(boolean successful) {
302 try {
303 if (mTrustAgentService != null) mTrustAgentService.onUnlockAttempt(successful);
304 } catch (RemoteException e) {
305 onError(e);
306 }
307 }
308
Adrian Roos481a6df2014-11-20 19:48:56 +0100309 /**
310 * @see android.service.trust.TrustAgentService#onDeviceLocked()
311 */
312 public void onDeviceLocked() {
313 try {
314 if (mTrustAgentService != null) mTrustAgentService.onDeviceLocked();
315 } catch (RemoteException e) {
316 onError(e);
317 }
318 }
319
320 /**
321 * @see android.service.trust.TrustAgentService#onDeviceUnlocked()
322 */
323 public void onDeviceUnlocked() {
324 try {
325 if (mTrustAgentService != null) mTrustAgentService.onDeviceUnlocked();
326 } catch (RemoteException e) {
327 onError(e);
328 }
329 }
330
Adrian Roos82142c22014-03-27 14:56:59 +0100331 private void setCallback(ITrustAgentServiceCallback callback) {
332 try {
333 if (mTrustAgentService != null) {
334 mTrustAgentService.setCallback(callback);
335 }
336 } catch (RemoteException e) {
337 onError(e);
338 }
339 }
340
Adrian Roos8f211582014-07-29 15:09:57 +0200341 boolean updateDevicePolicyFeatures() {
Jim Miller604e7552014-07-18 19:00:02 -0700342 boolean trustDisabled = false;
Adrian Roos8f211582014-07-29 15:09:57 +0200343 if (DEBUG) Slog.v(TAG, "updateDevicePolicyFeatures(" + mName + ")");
Jim Miller604e7552014-07-18 19:00:02 -0700344 try {
345 if (mTrustAgentService != null) {
346 DevicePolicyManager dpm =
347 (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
Adrian Roos8f211582014-07-29 15:09:57 +0200348
Jim Millere303bf42014-08-26 17:12:29 -0700349 if ((dpm.getKeyguardDisabledFeatures(null, mUserId)
Adrian Roos8f211582014-07-29 15:09:57 +0200350 & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0) {
Jim Millere303bf42014-08-26 17:12:29 -0700351 List<PersistableBundle> config = dpm.getTrustAgentConfiguration(
352 null, mName, mUserId);
Adrian Roos8f211582014-07-29 15:09:57 +0200353 trustDisabled = true;
Jim Millere303bf42014-08-26 17:12:29 -0700354 if (DEBUG) Slog.v(TAG, "Detected trust agents disabled. Config = " + config);
355 if (config != null && config.size() > 0) {
Adrian Roos8f211582014-07-29 15:09:57 +0200356 if (DEBUG) {
357 Slog.v(TAG, "TrustAgent " + mName.flattenToShortString()
Jim Millere303bf42014-08-26 17:12:29 -0700358 + " disabled until it acknowledges "+ config);
Jim Miller604e7552014-07-18 19:00:02 -0700359 }
Adrian Roos8f211582014-07-29 15:09:57 +0200360 mSetTrustAgentFeaturesToken = new Binder();
Jim Millere303bf42014-08-26 17:12:29 -0700361 mTrustAgentService.onConfigure(config, mSetTrustAgentFeaturesToken);
Jim Miller604e7552014-07-18 19:00:02 -0700362 }
Adrian Roosa43fd032015-03-09 19:10:15 +0100363 } else {
364 mTrustAgentService.onConfigure(Collections.EMPTY_LIST, null);
Jim Miller604e7552014-07-18 19:00:02 -0700365 }
Jim Miller76b9b8b2014-08-22 17:04:57 -0700366 final long maxTimeToLock = dpm.getMaximumTimeToLock(null);
367 if (maxTimeToLock != mMaximumTimeToLock) {
368 // If the timeout changes, cancel the alarm and send a timeout event to have
369 // the agent re-evaluate trust.
370 mMaximumTimeToLock = maxTimeToLock;
371 if (mAlarmPendingIntent != null) {
372 mAlarmManager.cancel(mAlarmPendingIntent);
373 mAlarmPendingIntent = null;
374 mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT);
375 }
376 }
Jim Miller604e7552014-07-18 19:00:02 -0700377 }
378 } catch (RemoteException e) {
379 onError(e);
380 }
381 if (mTrustDisabledByDpm != trustDisabled) {
382 mTrustDisabledByDpm = trustDisabled;
Adrian Roos3c9a3502014-08-06 19:09:45 +0200383 mTrustManagerService.updateTrust(mUserId, false);
Jim Miller604e7552014-07-18 19:00:02 -0700384 }
385 return trustDisabled;
386 }
387
Adrian Roos82142c22014-03-27 14:56:59 +0100388 public boolean isTrusted() {
Adrian Roos7861c662014-07-25 15:37:28 +0200389 return mTrusted && mManagingTrust && !mTrustDisabledByDpm;
390 }
391
392 public boolean isManagingTrust() {
393 return mManagingTrust && !mTrustDisabledByDpm;
Adrian Roos82142c22014-03-27 14:56:59 +0100394 }
395
Adrian Roos7e03dfc2014-05-16 16:06:28 +0200396 public CharSequence getMessage() {
Adrian Roos82142c22014-03-27 14:56:59 +0100397 return mMessage;
398 }
399
Adrian Roosfc29e0b2014-11-11 12:55:44 +0100400 public void destroy() {
Adrian Roosdfdbad32015-03-05 14:41:04 +0100401 mContext.unregisterReceiver(mBroadcastReceiver);
Adrian Roosfc29e0b2014-11-11 12:55:44 +0100402 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
403
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200404 if (!mBound) {
405 return;
406 }
Adrian Roos82142c22014-03-27 14:56:59 +0100407 if (DEBUG) Log.v(TAG, "TrustAgent unbound : " + mName.flattenToShortString());
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200408 mTrustManagerService.mArchive.logAgentStopped(mUserId, mName);
Adrian Roos82142c22014-03-27 14:56:59 +0100409 mContext.unbindService(mConnection);
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200410 mBound = false;
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200411 mTrustAgentService = null;
Adrian Roos8f211582014-07-29 15:09:57 +0200412 mSetTrustAgentFeaturesToken = null;
Adrian Roos7d59b4f2014-05-27 20:01:31 +0200413 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST);
Adrian Roos82142c22014-03-27 14:56:59 +0100414 }
Adrian Roos7a4f3d42014-05-02 12:12:20 +0200415
416 public boolean isConnected() {
417 return mTrustAgentService != null;
418 }
Adrian Roosc5f95ce2014-07-24 16:00:46 +0200419
420 public boolean isBound() {
421 return mBound;
422 }
423
424 /**
425 * If not connected, returns the time at which the agent is restarted.
426 *
427 * @return restart time in uptime millis.
428 */
429 public long getScheduledRestartUptimeMillis() {
430 return mScheduledRestartUptimeMillis;
431 }
432
433 private void scheduleRestart() {
434 mHandler.removeMessages(MSG_RESTART_TIMEOUT);
435 mScheduledRestartUptimeMillis = SystemClock.uptimeMillis() + RESTART_TIMEOUT_MILLIS;
436 mHandler.sendEmptyMessageAtTime(MSG_RESTART_TIMEOUT, mScheduledRestartUptimeMillis);
437 }
Adrian Roos82142c22014-03-27 14:56:59 +0100438}