TrustAgentWrapper.java revision 7a4f3d448b17b4bea190c906d7ecc7f8bec9ff80
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.server.trust; 18 19import android.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.ServiceConnection; 23import android.os.Handler; 24import android.os.IBinder; 25import android.os.Message; 26import android.os.RemoteException; 27import android.os.UserHandle; 28import android.util.Log; 29import android.util.Slog; 30import android.service.trust.ITrustAgentService; 31import android.service.trust.ITrustAgentServiceCallback; 32 33/** 34 * A wrapper around a TrustAgentService interface. Coordinates communication between 35 * TrustManager and the actual TrustAgent. 36 */ 37public class TrustAgentWrapper { 38 private static final boolean DEBUG = false; 39 private static final String TAG = "TrustAgentWrapper"; 40 41 private static final int MSG_GRANT_TRUST = 1; 42 private static final int MSG_REVOKE_TRUST = 2; 43 private static final int MSG_TRUST_TIMEOUT = 3; 44 45 /** 46 * Long extra for {@link #MSG_GRANT_TRUST} 47 */ 48 private static final String DATA_DURATION = "duration"; 49 50 private final TrustManagerService mTrustManagerService; 51 private final int mUserId; 52 private final Context mContext; 53 private final ComponentName mName; 54 55 private ITrustAgentService mTrustAgentService; 56 57 // Trust state 58 private boolean mTrusted; 59 private CharSequence mMessage; 60 61 private final Handler mHandler = new Handler() { 62 @Override 63 public void handleMessage(Message msg) { 64 switch (msg.what) { 65 case MSG_GRANT_TRUST: 66 mTrusted = true; 67 mMessage = (CharSequence) msg.obj; 68 boolean initiatedByUser = msg.arg1 != 0; 69 // TODO: Handle initiatedByUser. 70 long durationMs = msg.getData().getLong(DATA_DURATION); 71 if (durationMs > 0) { 72 mHandler.removeMessages(MSG_TRUST_TIMEOUT); 73 mHandler.sendEmptyMessageDelayed(MSG_TRUST_TIMEOUT, durationMs); 74 } 75 mTrustManagerService.mArchive.logGrantTrust(mUserId, mName, 76 (mMessage != null ? mMessage.toString() : null), 77 durationMs, initiatedByUser); 78 mTrustManagerService.updateTrust(mUserId); 79 break; 80 case MSG_TRUST_TIMEOUT: 81 if (DEBUG) Slog.v(TAG, "Trust timed out : " + mName.flattenToShortString()); 82 mTrustManagerService.mArchive.logTrustTimeout(mUserId, mName); 83 // Fall through. 84 case MSG_REVOKE_TRUST: 85 mTrusted = false; 86 mMessage = null; 87 mHandler.removeMessages(MSG_TRUST_TIMEOUT); 88 if (msg.what == MSG_REVOKE_TRUST) { 89 mTrustManagerService.mArchive.logRevokeTrust(mUserId, mName); 90 } 91 mTrustManagerService.updateTrust(mUserId); 92 break; 93 } 94 } 95 }; 96 97 private ITrustAgentServiceCallback mCallback = new ITrustAgentServiceCallback.Stub() { 98 99 @Override 100 public void grantTrust(CharSequence userMessage, long durationMs, boolean initiatedByUser) { 101 if (DEBUG) Slog.v(TAG, "enableTrust(" + userMessage + ", durationMs = " + durationMs 102 + ", initiatedByUser = " + initiatedByUser + ")"); 103 104 Message msg = mHandler.obtainMessage( 105 MSG_GRANT_TRUST, initiatedByUser ? 1 : 0, 0, userMessage); 106 msg.getData().putLong(DATA_DURATION, durationMs); 107 msg.sendToTarget(); 108 } 109 110 @Override 111 public void revokeTrust() { 112 if (DEBUG) Slog.v(TAG, "revokeTrust()"); 113 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST); 114 } 115 }; 116 117 private final ServiceConnection mConnection = new ServiceConnection() { 118 @Override 119 public void onServiceConnected(ComponentName name, IBinder service) { 120 if (DEBUG) Log.v(TAG, "TrustAgent started : " + name.flattenToString()); 121 mTrustAgentService = ITrustAgentService.Stub.asInterface(service); 122 setCallback(mCallback); 123 } 124 125 @Override 126 public void onServiceDisconnected(ComponentName name) { 127 if (DEBUG) Log.v(TAG, "TrustAgent disconnected : " + name.flattenToShortString()); 128 mTrustAgentService = null; 129 mTrustManagerService.mArchive.logAgentDied(mUserId, name); 130 mHandler.sendEmptyMessage(MSG_REVOKE_TRUST); 131 } 132 }; 133 134 135 public TrustAgentWrapper(Context context, TrustManagerService trustManagerService, 136 Intent intent, UserHandle user) { 137 mContext = context; 138 mTrustManagerService = trustManagerService; 139 mUserId = user.getIdentifier(); 140 mName = intent.getComponent(); 141 if (!context.bindServiceAsUser(intent, mConnection, Context.BIND_AUTO_CREATE, user)) { 142 if (DEBUG) Log.v(TAG, "can't bind to TrustAgent " + mName.flattenToShortString()); 143 // TODO: retry somehow? 144 } 145 } 146 147 private void onError(Exception e) { 148 Slog.w(TAG , "Remote Exception", e); 149 } 150 151 /** 152 * @see android.service.trust.TrustAgentService#onUnlockAttempt(boolean) 153 */ 154 public void onUnlockAttempt(boolean successful) { 155 try { 156 if (mTrustAgentService != null) mTrustAgentService.onUnlockAttempt(successful); 157 } catch (RemoteException e) { 158 onError(e); 159 } 160 } 161 162 private void setCallback(ITrustAgentServiceCallback callback) { 163 try { 164 if (mTrustAgentService != null) { 165 mTrustAgentService.setCallback(callback); 166 } 167 } catch (RemoteException e) { 168 onError(e); 169 } 170 } 171 172 public boolean isTrusted() { 173 return mTrusted; 174 } 175 176 public CharSequence getMessage() { 177 return mMessage; 178 } 179 180 public void unbind() { 181 if (DEBUG) Log.v(TAG, "TrustAgent unbound : " + mName.flattenToShortString()); 182 mContext.unbindService(mConnection); 183 } 184 185 public boolean isConnected() { 186 return mTrustAgentService != null; 187 } 188} 189