Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 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 | |
| 17 | package com.android.server.trust; |
| 18 | |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 19 | import android.annotation.TargetApi; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 20 | import android.app.AlarmManager; |
| 21 | import android.app.PendingIntent; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 22 | import android.app.admin.DevicePolicyManager; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 27 | import android.content.IntentFilter; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 28 | import android.content.ServiceConnection; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 30 | import android.os.Binder; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 31 | import android.os.Build; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.IBinder; |
| 34 | import android.os.Message; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 35 | import android.os.PatternMatcher; |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 36 | import android.os.PersistableBundle; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 37 | import android.os.RemoteException; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 38 | import android.os.SystemClock; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 39 | import android.os.UserHandle; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 40 | import android.service.trust.ITrustAgentService; |
| 41 | import android.service.trust.ITrustAgentServiceCallback; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 42 | import android.service.trust.TrustAgentService; |
| 43 | import android.util.Log; |
| 44 | import android.util.Slog; |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 45 | |
Adrian Roos | a43fd03 | 2015-03-09 19:10:15 +0100 | [diff] [blame] | 46 | import java.util.Collections; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 47 | import java.util.List; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * A wrapper around a TrustAgentService interface. Coordinates communication between |
| 51 | * TrustManager and the actual TrustAgent. |
| 52 | */ |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 53 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 54 | public class TrustAgentWrapper { |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 55 | private static final String EXTRA_COMPONENT_NAME = "componentName"; |
| 56 | private static final String TRUST_EXPIRED_ACTION = "android.server.trust.TRUST_EXPIRED_ACTION"; |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 57 | private static final String PERMISSION = android.Manifest.permission.PROVIDE_TRUST_AGENT; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 58 | private static final boolean DEBUG = TrustManagerService.DEBUG; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 59 | private static final String TAG = "TrustAgentWrapper"; |
| 60 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 61 | private static final int MSG_GRANT_TRUST = 1; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 62 | private static final int MSG_REVOKE_TRUST = 2; |
| 63 | private static final int MSG_TRUST_TIMEOUT = 3; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 64 | private static final int MSG_RESTART_TIMEOUT = 4; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 65 | private static final int MSG_SET_TRUST_AGENT_FEATURES_COMPLETED = 5; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 66 | private static final int MSG_MANAGING_TRUST = 6; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 67 | private static final int MSG_ADD_ESCROW_TOKEN = 7; |
| 68 | private static final int MSG_REMOVE_ESCROW_TOKEN = 8; |
| 69 | private static final int MSG_ESCROW_TOKEN_STATE = 9; |
| 70 | private static final int MSG_UNLOCK_USER = 10; |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 71 | private static final int MSG_SHOW_KEYGUARD_ERROR_MESSAGE = 11; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 72 | |
| 73 | /** |
| 74 | * Time in uptime millis that we wait for the service connection, both when starting |
| 75 | * and when the service disconnects. |
| 76 | */ |
| 77 | private static final long RESTART_TIMEOUT_MILLIS = 5 * 60000; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 78 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 79 | /** |
| 80 | * Long extra for {@link #MSG_GRANT_TRUST} |
| 81 | */ |
| 82 | private static final String DATA_DURATION = "duration"; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 83 | private static final String DATA_ESCROW_TOKEN = "escrow_token"; |
| 84 | private static final String DATA_HANDLE = "handle"; |
| 85 | private static final String DATA_USER_ID = "user_id"; |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 86 | private static final String DATA_MESSAGE = "message"; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 87 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 88 | private final TrustManagerService mTrustManagerService; |
| 89 | private final int mUserId; |
| 90 | private final Context mContext; |
| 91 | private final ComponentName mName; |
| 92 | |
| 93 | private ITrustAgentService mTrustAgentService; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 94 | private boolean mBound; |
| 95 | private long mScheduledRestartUptimeMillis; |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 96 | private long mMaximumTimeToLock; // from DevicePolicyManager |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 97 | private boolean mPendingSuccessfulUnlock = false; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 98 | |
| 99 | // Trust state |
| 100 | private boolean mTrusted; |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 101 | private CharSequence mMessage; |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 102 | private boolean mTrustDisabledByDpm; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 103 | private boolean mManagingTrust; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 104 | private IBinder mSetTrustAgentFeaturesToken; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 105 | private AlarmManager mAlarmManager; |
| 106 | private final Intent mAlarmIntent; |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 107 | private PendingIntent mAlarmPendingIntent; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 108 | |
| 109 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 110 | @Override |
| 111 | public void onReceive(Context context, Intent intent) { |
| 112 | ComponentName component = intent.getParcelableExtra(EXTRA_COMPONENT_NAME); |
| 113 | if (TRUST_EXPIRED_ACTION.equals(intent.getAction()) |
| 114 | && mName.equals(component)) { |
| 115 | mHandler.removeMessages(MSG_TRUST_TIMEOUT); |
| 116 | mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT); |
| 117 | } |
| 118 | } |
| 119 | }; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 120 | |
| 121 | private final Handler mHandler = new Handler() { |
| 122 | @Override |
| 123 | public void handleMessage(Message msg) { |
| 124 | switch (msg.what) { |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 125 | case MSG_GRANT_TRUST: |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 126 | if (!isConnected()) { |
| 127 | Log.w(TAG, "Agent is not connected, cannot grant trust: " |
| 128 | + mName.flattenToShortString()); |
| 129 | return; |
| 130 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 131 | mTrusted = true; |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 132 | mMessage = (CharSequence) msg.obj; |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 133 | int flags = msg.arg1; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 134 | long durationMs = msg.getData().getLong(DATA_DURATION); |
| 135 | if (durationMs > 0) { |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 136 | final long duration; |
| 137 | if (mMaximumTimeToLock != 0) { |
| 138 | // Enforce DevicePolicyManager timeout. This is here as a safeguard to |
| 139 | // ensure trust agents are evaluating trust state at least as often as |
| 140 | // the policy dictates. Admins that want more guarantees should be using |
| 141 | // DevicePolicyManager#KEYGUARD_DISABLE_TRUST_AGENTS. |
| 142 | duration = Math.min(durationMs, mMaximumTimeToLock); |
| 143 | if (DEBUG) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 144 | Slog.d(TAG, "DPM lock timeout in effect. Timeout adjusted from " |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 145 | + durationMs + " to " + duration); |
| 146 | } |
| 147 | } else { |
| 148 | duration = durationMs; |
| 149 | } |
| 150 | long expiration = SystemClock.elapsedRealtime() + duration; |
| 151 | mAlarmPendingIntent = PendingIntent.getBroadcast(mContext, 0, mAlarmIntent, |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 152 | PendingIntent.FLAG_CANCEL_CURRENT); |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 153 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, expiration, |
| 154 | mAlarmPendingIntent); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 155 | } |
| 156 | mTrustManagerService.mArchive.logGrantTrust(mUserId, mName, |
| 157 | (mMessage != null ? mMessage.toString() : null), |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 158 | durationMs, flags); |
| 159 | mTrustManagerService.updateTrust(mUserId, flags); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 160 | break; |
| 161 | case MSG_TRUST_TIMEOUT: |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 162 | if (DEBUG) Slog.d(TAG, "Trust timed out : " + mName.flattenToShortString()); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 163 | mTrustManagerService.mArchive.logTrustTimeout(mUserId, mName); |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 164 | onTrustTimeout(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 165 | // Fall through. |
| 166 | case MSG_REVOKE_TRUST: |
| 167 | mTrusted = false; |
| 168 | mMessage = null; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 169 | mHandler.removeMessages(MSG_TRUST_TIMEOUT); |
| 170 | if (msg.what == MSG_REVOKE_TRUST) { |
| 171 | mTrustManagerService.mArchive.logRevokeTrust(mUserId, mName); |
| 172 | } |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 173 | mTrustManagerService.updateTrust(mUserId, 0); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 174 | break; |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 175 | case MSG_RESTART_TIMEOUT: |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 176 | Slog.w(TAG, "Connection attempt to agent " + mName.flattenToShortString() |
| 177 | + " timed out, rebinding"); |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 178 | destroy(); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 179 | mTrustManagerService.resetAgent(mName, mUserId); |
| 180 | break; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 181 | case MSG_SET_TRUST_AGENT_FEATURES_COMPLETED: |
| 182 | IBinder token = (IBinder) msg.obj; |
| 183 | boolean result = msg.arg1 != 0; |
| 184 | if (mSetTrustAgentFeaturesToken == token) { |
| 185 | mSetTrustAgentFeaturesToken = null; |
| 186 | if (mTrustDisabledByDpm && result) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 187 | if (DEBUG) Slog.d(TAG, "Re-enabling agent because it acknowledged " |
| 188 | + "enabled features: " + mName.flattenToShortString()); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 189 | mTrustDisabledByDpm = false; |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 190 | mTrustManagerService.updateTrust(mUserId, 0); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 191 | } |
| 192 | } else { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 193 | if (DEBUG) Slog.w(TAG, "Ignoring MSG_SET_TRUST_AGENT_FEATURES_COMPLETED " |
| 194 | + "with obsolete token: " + mName.flattenToShortString()); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 195 | } |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 196 | break; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 197 | case MSG_MANAGING_TRUST: |
| 198 | mManagingTrust = msg.arg1 != 0; |
| 199 | if (!mManagingTrust) { |
| 200 | mTrusted = false; |
| 201 | mMessage = null; |
| 202 | } |
| 203 | mTrustManagerService.mArchive.logManagingTrust(mUserId, mName, mManagingTrust); |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 204 | mTrustManagerService.updateTrust(mUserId, 0); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 205 | break; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 206 | case MSG_ADD_ESCROW_TOKEN: { |
| 207 | byte[] eToken = msg.getData().getByteArray(DATA_ESCROW_TOKEN); |
| 208 | int userId = msg.getData().getInt(DATA_USER_ID); |
| 209 | long handle = mTrustManagerService.addEscrowToken(eToken, userId); |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 210 | boolean resultDeliverred = false; |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 211 | try { |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 212 | if (mTrustAgentService != null) { |
| 213 | mTrustAgentService.onEscrowTokenAdded( |
| 214 | eToken, handle, UserHandle.of(userId)); |
| 215 | resultDeliverred = true; |
| 216 | } |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 217 | } catch (RemoteException e) { |
| 218 | onError(e); |
| 219 | } |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 220 | |
| 221 | if (!resultDeliverred) { |
| 222 | mTrustManagerService.removeEscrowToken(handle, userId); |
| 223 | } |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | case MSG_ESCROW_TOKEN_STATE: { |
| 227 | long handle = msg.getData().getLong(DATA_HANDLE); |
| 228 | int userId = msg.getData().getInt(DATA_USER_ID); |
| 229 | boolean active = mTrustManagerService.isEscrowTokenActive(handle, userId); |
| 230 | try { |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 231 | if (mTrustAgentService != null) { |
| 232 | mTrustAgentService.onTokenStateReceived(handle, |
| 233 | active ? TrustAgentService.TOKEN_STATE_ACTIVE |
| 234 | : TrustAgentService.TOKEN_STATE_INACTIVE); |
| 235 | } |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 236 | } catch (RemoteException e) { |
| 237 | onError(e); |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | case MSG_REMOVE_ESCROW_TOKEN: { |
| 242 | long handle = msg.getData().getLong(DATA_HANDLE); |
| 243 | int userId = msg.getData().getInt(DATA_USER_ID); |
| 244 | boolean success = mTrustManagerService.removeEscrowToken(handle, userId); |
| 245 | try { |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 246 | if (mTrustAgentService != null) { |
| 247 | mTrustAgentService.onEscrowTokenRemoved(handle, success); |
| 248 | } |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 249 | } catch (RemoteException e) { |
| 250 | onError(e); |
| 251 | } |
| 252 | break; |
| 253 | } |
| 254 | case MSG_UNLOCK_USER: { |
| 255 | long handle = msg.getData().getLong(DATA_HANDLE); |
| 256 | int userId = msg.getData().getInt(DATA_USER_ID); |
| 257 | byte[] eToken = msg.getData().getByteArray(DATA_ESCROW_TOKEN); |
| 258 | mTrustManagerService.unlockUserWithToken(handle, eToken, userId); |
| 259 | break; |
| 260 | } |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 261 | case MSG_SHOW_KEYGUARD_ERROR_MESSAGE: { |
| 262 | CharSequence message = msg.getData().getCharSequence(DATA_MESSAGE); |
| 263 | mTrustManagerService.showKeyguardErrorMessage(message); |
| 264 | break; |
| 265 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | }; |
| 269 | |
| 270 | private ITrustAgentServiceCallback mCallback = new ITrustAgentServiceCallback.Stub() { |
| 271 | |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 272 | @Override |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 273 | public void grantTrust(CharSequence userMessage, long durationMs, int flags) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 274 | if (DEBUG) Slog.d(TAG, "enableTrust(" + userMessage + ", durationMs = " + durationMs |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 275 | + ", flags = " + flags + ")"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 276 | |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 277 | Message msg = mHandler.obtainMessage( |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 278 | MSG_GRANT_TRUST, flags, 0, userMessage); |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 279 | msg.getData().putLong(DATA_DURATION, durationMs); |
| 280 | msg.sendToTarget(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 283 | @Override |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 284 | public void revokeTrust() { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 285 | if (DEBUG) Slog.d(TAG, "revokeTrust()"); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 286 | mHandler.sendEmptyMessage(MSG_REVOKE_TRUST); |
| 287 | } |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 288 | |
| 289 | @Override |
| 290 | public void setManagingTrust(boolean managingTrust) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 291 | if (DEBUG) Slog.d(TAG, "managingTrust()"); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 292 | mHandler.obtainMessage(MSG_MANAGING_TRUST, managingTrust ? 1 : 0, 0).sendToTarget(); |
| 293 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 294 | |
| 295 | @Override |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 296 | public void onConfigureCompleted(boolean result, IBinder token) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 297 | if (DEBUG) Slog.d(TAG, "onSetTrustAgentFeaturesEnabledCompleted(result=" + result); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 298 | mHandler.obtainMessage(MSG_SET_TRUST_AGENT_FEATURES_COMPLETED, |
| 299 | result ? 1 : 0, 0, token).sendToTarget(); |
| 300 | } |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 301 | |
| 302 | @Override |
| 303 | public void addEscrowToken(byte[] token, int userId) { |
| 304 | if (mContext.getResources() |
| 305 | .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) { |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 306 | throw new SecurityException("Escrow token API is not allowed."); |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (DEBUG) Slog.d(TAG, "adding escrow token for user " + userId); |
| 310 | Message msg = mHandler.obtainMessage(MSG_ADD_ESCROW_TOKEN); |
| 311 | msg.getData().putByteArray(DATA_ESCROW_TOKEN, token); |
| 312 | msg.getData().putInt(DATA_USER_ID, userId); |
| 313 | msg.sendToTarget(); |
| 314 | } |
| 315 | |
| 316 | @Override |
| 317 | public void isEscrowTokenActive(long handle, int userId) { |
| 318 | if (mContext.getResources() |
| 319 | .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) { |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 320 | throw new SecurityException("Escrow token API is not allowed."); |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | if (DEBUG) Slog.d(TAG, "checking the state of escrow token on user " + userId); |
| 324 | Message msg = mHandler.obtainMessage(MSG_ESCROW_TOKEN_STATE); |
| 325 | msg.getData().putLong(DATA_HANDLE, handle); |
| 326 | msg.getData().putInt(DATA_USER_ID, userId); |
| 327 | msg.sendToTarget(); |
| 328 | } |
| 329 | |
| 330 | @Override |
| 331 | public void removeEscrowToken(long handle, int userId) { |
| 332 | if (mContext.getResources() |
| 333 | .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) { |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 334 | throw new SecurityException("Escrow token API is not allowed."); |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | if (DEBUG) Slog.d(TAG, "removing escrow token on user " + userId); |
| 338 | Message msg = mHandler.obtainMessage(MSG_REMOVE_ESCROW_TOKEN); |
| 339 | msg.getData().putLong(DATA_HANDLE, handle); |
| 340 | msg.getData().putInt(DATA_USER_ID, userId); |
| 341 | msg.sendToTarget(); |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void unlockUserWithToken(long handle, byte[] token, int userId) { |
| 346 | if (mContext.getResources() |
| 347 | .getBoolean(com.android.internal.R.bool.config_allowEscrowTokenForTrustAgent)) { |
Lingjun Li | 44196d3 | 2017-02-17 18:32:23 -0800 | [diff] [blame] | 348 | throw new SecurityException("Escrow token API is not allowed."); |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (DEBUG) Slog.d(TAG, "unlocking user " + userId); |
| 352 | Message msg = mHandler.obtainMessage(MSG_UNLOCK_USER); |
| 353 | msg.getData().putInt(DATA_USER_ID, userId); |
| 354 | msg.getData().putLong(DATA_HANDLE, handle); |
| 355 | msg.getData().putByteArray(DATA_ESCROW_TOKEN, token); |
| 356 | msg.sendToTarget(); |
| 357 | } |
Lucas Dupin | ef88654 | 2018-01-03 16:03:07 -0800 | [diff] [blame] | 358 | |
| 359 | @Override |
| 360 | public void showKeyguardErrorMessage(CharSequence message) { |
| 361 | if (DEBUG) Slog.d(TAG, "Showing keyguard error message: " + message); |
| 362 | Message msg = mHandler.obtainMessage(MSG_SHOW_KEYGUARD_ERROR_MESSAGE); |
| 363 | msg.getData().putCharSequence(DATA_MESSAGE, message); |
| 364 | msg.sendToTarget(); |
| 365 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 366 | }; |
| 367 | |
| 368 | private final ServiceConnection mConnection = new ServiceConnection() { |
| 369 | @Override |
| 370 | public void onServiceConnected(ComponentName name, IBinder service) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 371 | if (DEBUG) Slog.d(TAG, "TrustAgent started : " + name.flattenToString()); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 372 | mHandler.removeMessages(MSG_RESTART_TIMEOUT); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 373 | mTrustAgentService = ITrustAgentService.Stub.asInterface(service); |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 374 | mTrustManagerService.mArchive.logAgentConnected(mUserId, name); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 375 | setCallback(mCallback); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 376 | updateDevicePolicyFeatures(); |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 377 | |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 378 | if (mPendingSuccessfulUnlock) { |
| 379 | onUnlockAttempt(true); |
| 380 | mPendingSuccessfulUnlock = false; |
| 381 | } |
| 382 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 383 | if (mTrustManagerService.isDeviceLockedInner(mUserId)) { |
| 384 | onDeviceLocked(); |
| 385 | } else { |
| 386 | onDeviceUnlocked(); |
| 387 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | @Override |
| 391 | public void onServiceDisconnected(ComponentName name) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 392 | if (DEBUG) Slog.d(TAG, "TrustAgent disconnected : " + name.flattenToShortString()); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 393 | mTrustAgentService = null; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 394 | mManagingTrust = false; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 395 | mSetTrustAgentFeaturesToken = null; |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 396 | mTrustManagerService.mArchive.logAgentDied(mUserId, name); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 397 | mHandler.sendEmptyMessage(MSG_REVOKE_TRUST); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 398 | if (mBound) { |
| 399 | scheduleRestart(); |
| 400 | } |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 401 | // mTrustDisabledByDpm maintains state |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 402 | } |
| 403 | }; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 404 | |
| 405 | public TrustAgentWrapper(Context context, TrustManagerService trustManagerService, |
| 406 | Intent intent, UserHandle user) { |
| 407 | mContext = context; |
| 408 | mTrustManagerService = trustManagerService; |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 409 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 410 | mUserId = user.getIdentifier(); |
| 411 | mName = intent.getComponent(); |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 412 | |
| 413 | mAlarmIntent = new Intent(TRUST_EXPIRED_ACTION).putExtra(EXTRA_COMPONENT_NAME, mName); |
| 414 | mAlarmIntent.setData(Uri.parse(mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME))); |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 415 | mAlarmIntent.setPackage(context.getPackageName()); |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 416 | |
| 417 | final IntentFilter alarmFilter = new IntentFilter(TRUST_EXPIRED_ACTION); |
| 418 | alarmFilter.addDataScheme(mAlarmIntent.getScheme()); |
| 419 | final String pathUri = mAlarmIntent.toUri(Intent.URI_INTENT_SCHEME); |
| 420 | alarmFilter.addDataPath(pathUri, PatternMatcher.PATTERN_LITERAL); |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 421 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 422 | // Schedules a restart for when connecting times out. If the connection succeeds, |
| 423 | // the restart is canceled in mCallback's onConnected. |
| 424 | scheduleRestart(); |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 425 | mBound = context.bindServiceAsUser(intent, mConnection, |
| 426 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, user); |
Adrian Roos | 1221b06 | 2015-03-26 12:29:51 -0700 | [diff] [blame] | 427 | if (mBound) { |
| 428 | mContext.registerReceiver(mBroadcastReceiver, alarmFilter, PERMISSION, null); |
| 429 | } else { |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 430 | Log.e(TAG, "Can't bind to TrustAgent " + mName.flattenToShortString()); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
| 434 | private void onError(Exception e) { |
Lingjun Li | 93a145f | 2017-01-23 17:13:35 -0800 | [diff] [blame] | 435 | Slog.w(TAG , "Exception ", e); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Jim Miller | d4efaac | 2014-08-14 18:02:45 -0700 | [diff] [blame] | 438 | private void onTrustTimeout() { |
| 439 | try { |
| 440 | if (mTrustAgentService != null) mTrustAgentService.onTrustTimeout(); |
| 441 | } catch (RemoteException e) { |
| 442 | onError(e); |
| 443 | } |
| 444 | } |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 445 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 446 | /** |
| 447 | * @see android.service.trust.TrustAgentService#onUnlockAttempt(boolean) |
| 448 | */ |
| 449 | public void onUnlockAttempt(boolean successful) { |
| 450 | try { |
Adrian Roos | 517b3a4 | 2016-03-03 14:58:33 -0800 | [diff] [blame] | 451 | if (mTrustAgentService != null) { |
| 452 | mTrustAgentService.onUnlockAttempt(successful); |
| 453 | } else { |
| 454 | mPendingSuccessfulUnlock = successful; |
| 455 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 456 | } catch (RemoteException e) { |
| 457 | onError(e); |
| 458 | } |
| 459 | } |
| 460 | |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 461 | /** |
Zachary Iqbal | 327323d | 2017-01-12 14:41:13 -0800 | [diff] [blame] | 462 | * @see android.service.trust.TrustAgentService#onUnlockLockout(int) |
| 463 | */ |
| 464 | public void onUnlockLockout(int timeoutMs) { |
| 465 | try { |
| 466 | if (mTrustAgentService != null) { |
| 467 | mTrustAgentService.onUnlockLockout(timeoutMs); |
| 468 | } |
| 469 | } catch (RemoteException e) { |
| 470 | onError(e); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /** |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 475 | * @see android.service.trust.TrustAgentService#onDeviceLocked() |
| 476 | */ |
| 477 | public void onDeviceLocked() { |
| 478 | try { |
| 479 | if (mTrustAgentService != null) mTrustAgentService.onDeviceLocked(); |
| 480 | } catch (RemoteException e) { |
| 481 | onError(e); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * @see android.service.trust.TrustAgentService#onDeviceUnlocked() |
| 487 | */ |
| 488 | public void onDeviceUnlocked() { |
| 489 | try { |
| 490 | if (mTrustAgentService != null) mTrustAgentService.onDeviceUnlocked(); |
| 491 | } catch (RemoteException e) { |
| 492 | onError(e); |
| 493 | } |
| 494 | } |
| 495 | |
Ram Periathiruvadi | 32d5355 | 2019-02-19 13:25:46 -0800 | [diff] [blame] | 496 | /** |
| 497 | * @see android.service.trust.TrustAgentService#onTokenStateReceived() |
| 498 | * |
| 499 | */ |
| 500 | public void onEscrowTokenActivated(long handle, int userId) { |
| 501 | if (DEBUG) Slog.d(TAG, "onEscrowTokenActivated: " + handle + " user: " + userId); |
| 502 | if (mTrustAgentService != null) { |
| 503 | try { |
| 504 | mTrustAgentService.onTokenStateReceived(handle, |
| 505 | TrustAgentService.TOKEN_STATE_ACTIVE); |
| 506 | } catch (RemoteException e) { |
| 507 | onError(e); |
| 508 | } |
| 509 | } |
| 510 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 511 | private void setCallback(ITrustAgentServiceCallback callback) { |
| 512 | try { |
| 513 | if (mTrustAgentService != null) { |
| 514 | mTrustAgentService.setCallback(callback); |
| 515 | } |
| 516 | } catch (RemoteException e) { |
| 517 | onError(e); |
| 518 | } |
| 519 | } |
| 520 | |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 521 | boolean updateDevicePolicyFeatures() { |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 522 | boolean trustDisabled = false; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 523 | if (DEBUG) Slog.d(TAG, "updateDevicePolicyFeatures(" + mName + ")"); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 524 | try { |
| 525 | if (mTrustAgentService != null) { |
| 526 | DevicePolicyManager dpm = |
| 527 | (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 528 | |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 529 | if ((dpm.getKeyguardDisabledFeatures(null, mUserId) |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 530 | & DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS) != 0) { |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 531 | List<PersistableBundle> config = dpm.getTrustAgentConfiguration( |
| 532 | null, mName, mUserId); |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 533 | trustDisabled = true; |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 534 | if (DEBUG) Slog.d(TAG, "Detected trust agents disabled. Config = " + config); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 535 | if (config != null && config.size() > 0) { |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 536 | if (DEBUG) { |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 537 | Slog.d(TAG, "TrustAgent " + mName.flattenToShortString() |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 538 | + " disabled until it acknowledges "+ config); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 539 | } |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 540 | mSetTrustAgentFeaturesToken = new Binder(); |
Jim Miller | e303bf4 | 2014-08-26 17:12:29 -0700 | [diff] [blame] | 541 | mTrustAgentService.onConfigure(config, mSetTrustAgentFeaturesToken); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 542 | } |
Adrian Roos | a43fd03 | 2015-03-09 19:10:15 +0100 | [diff] [blame] | 543 | } else { |
| 544 | mTrustAgentService.onConfigure(Collections.EMPTY_LIST, null); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 545 | } |
Pavel Grafov | 2893998 | 2017-10-03 15:11:52 +0100 | [diff] [blame] | 546 | final long maxTimeToLock = dpm.getMaximumTimeToLock(null, mUserId); |
Jim Miller | 76b9b8b | 2014-08-22 17:04:57 -0700 | [diff] [blame] | 547 | if (maxTimeToLock != mMaximumTimeToLock) { |
| 548 | // If the timeout changes, cancel the alarm and send a timeout event to have |
| 549 | // the agent re-evaluate trust. |
| 550 | mMaximumTimeToLock = maxTimeToLock; |
| 551 | if (mAlarmPendingIntent != null) { |
| 552 | mAlarmManager.cancel(mAlarmPendingIntent); |
| 553 | mAlarmPendingIntent = null; |
| 554 | mHandler.sendEmptyMessage(MSG_TRUST_TIMEOUT); |
| 555 | } |
| 556 | } |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 557 | } |
| 558 | } catch (RemoteException e) { |
| 559 | onError(e); |
| 560 | } |
| 561 | if (mTrustDisabledByDpm != trustDisabled) { |
| 562 | mTrustDisabledByDpm = trustDisabled; |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 563 | mTrustManagerService.updateTrust(mUserId, 0); |
Jim Miller | 604e755 | 2014-07-18 19:00:02 -0700 | [diff] [blame] | 564 | } |
| 565 | return trustDisabled; |
| 566 | } |
| 567 | |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 568 | public boolean isTrusted() { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 569 | return mTrusted && mManagingTrust && !mTrustDisabledByDpm; |
| 570 | } |
| 571 | |
| 572 | public boolean isManagingTrust() { |
| 573 | return mManagingTrust && !mTrustDisabledByDpm; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 574 | } |
| 575 | |
Adrian Roos | 7e03dfc | 2014-05-16 16:06:28 +0200 | [diff] [blame] | 576 | public CharSequence getMessage() { |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 577 | return mMessage; |
| 578 | } |
| 579 | |
Adrian Roos | fc29e0b | 2014-11-11 12:55:44 +0100 | [diff] [blame] | 580 | public void destroy() { |
| 581 | mHandler.removeMessages(MSG_RESTART_TIMEOUT); |
| 582 | |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 583 | if (!mBound) { |
| 584 | return; |
| 585 | } |
Adrian Roos | 5d63978 | 2016-07-21 11:43:02 -0700 | [diff] [blame] | 586 | if (DEBUG) Slog.d(TAG, "TrustAgent unbound : " + mName.flattenToShortString()); |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 587 | mTrustManagerService.mArchive.logAgentStopped(mUserId, mName); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 588 | mContext.unbindService(mConnection); |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 589 | mBound = false; |
Adrian Roos | 1221b06 | 2015-03-26 12:29:51 -0700 | [diff] [blame] | 590 | mContext.unregisterReceiver(mBroadcastReceiver); |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 591 | mTrustAgentService = null; |
Adrian Roos | 8f21158 | 2014-07-29 15:09:57 +0200 | [diff] [blame] | 592 | mSetTrustAgentFeaturesToken = null; |
Adrian Roos | 7d59b4f | 2014-05-27 20:01:31 +0200 | [diff] [blame] | 593 | mHandler.sendEmptyMessage(MSG_REVOKE_TRUST); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 594 | } |
Adrian Roos | 7a4f3d4 | 2014-05-02 12:12:20 +0200 | [diff] [blame] | 595 | |
| 596 | public boolean isConnected() { |
| 597 | return mTrustAgentService != null; |
| 598 | } |
Adrian Roos | c5f95ce | 2014-07-24 16:00:46 +0200 | [diff] [blame] | 599 | |
| 600 | public boolean isBound() { |
| 601 | return mBound; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * If not connected, returns the time at which the agent is restarted. |
| 606 | * |
| 607 | * @return restart time in uptime millis. |
| 608 | */ |
| 609 | public long getScheduledRestartUptimeMillis() { |
| 610 | return mScheduledRestartUptimeMillis; |
| 611 | } |
| 612 | |
| 613 | private void scheduleRestart() { |
| 614 | mHandler.removeMessages(MSG_RESTART_TIMEOUT); |
| 615 | mScheduledRestartUptimeMillis = SystemClock.uptimeMillis() + RESTART_TIMEOUT_MILLIS; |
| 616 | mHandler.sendEmptyMessageAtTime(MSG_RESTART_TIMEOUT, mScheduledRestartUptimeMillis); |
| 617 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 618 | } |