| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.trust; |
| 18 |
|
| 19 |
import android.Manifest; |
| 20 |
import android.annotation.RequiresPermission; |
| 21 |
import android.annotation.SystemService; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.hardware.biometrics.BiometricSourceType; |
| 25 |
import android.os.Bundle; |
| 26 |
import android.os.Handler; |
| 27 |
import android.os.IBinder; |
| 28 |
import android.os.Looper; |
| 29 |
import android.os.Message; |
| 30 |
import android.os.RemoteException; |
| 31 |
import android.util.ArrayMap; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
@SystemService(Context.TRUST_SERVICE) |
| 41 |
public class TrustManager { |
| 42 |
|
| 43 |
private static final int MSG_TRUST_CHANGED = 1; |
| 44 |
private static final int MSG_TRUST_MANAGED_CHANGED = 2; |
| 45 |
private static final int MSG_TRUST_ERROR = 3; |
| 46 |
|
| 47 |
private static final String TAG = "TrustManager"; |
| 48 |
private static final String DATA_FLAGS = "initiatedByUser"; |
| 49 |
private static final String DATA_NEWLY_UNLOCKED = "newlyUnlocked"; |
| 50 |
private static final String DATA_MESSAGE = "message"; |
| 51 |
private static final String DATA_GRANTED_MESSAGES = "grantedMessages"; |
| 52 |
|
| 53 |
private final ITrustManager mService; |
| 54 |
private final ArrayMap<TrustListener, ITrustListener> mTrustListeners; |
| 55 |
|
| 56 |
public TrustManager(IBinder b) { |
| 57 |
mService = ITrustManager.Stub.asInterface(b); |
| 58 |
mTrustListeners = new ArrayMap<TrustListener, ITrustListener>(); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@RequiresPermission(Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE) |
| 69 |
public void setDeviceLockedForUser(int userId, boolean locked) { |
| 70 |
try { |
| 71 |
mService.setDeviceLockedForUser(userId, locked); |
| 72 |
} catch (RemoteException e) { |
| 73 |
throw e.rethrowFromSystemServer(); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
@UnsupportedAppUsage |
| 85 |
public void reportUnlockAttempt(boolean successful, int userId) { |
| 86 |
try { |
| 87 |
mService.reportUnlockAttempt(successful, userId); |
| 88 |
} catch (RemoteException e) { |
| 89 |
throw e.rethrowFromSystemServer(); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public void reportUserRequestedUnlock(int userId, boolean dismissKeyguard) { |
| 101 |
try { |
| 102 |
mService.reportUserRequestedUnlock(userId, dismissKeyguard); |
| 103 |
} catch (RemoteException e) { |
| 104 |
throw e.rethrowFromSystemServer(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public void reportUserMayRequestUnlock(int userId) { |
| 114 |
try { |
| 115 |
mService.reportUserMayRequestUnlock(userId); |
| 116 |
} catch (RemoteException e) { |
| 117 |
throw e.rethrowFromSystemServer(); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public void reportUnlockLockout(int timeoutMs, int userId) { |
| 134 |
try { |
| 135 |
mService.reportUnlockLockout(timeoutMs, userId); |
| 136 |
} catch (RemoteException e) { |
| 137 |
throw e.rethrowFromSystemServer(); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public void reportEnabledTrustAgentsChanged(int userId) { |
| 147 |
try { |
| 148 |
mService.reportEnabledTrustAgentsChanged(userId); |
| 149 |
} catch (RemoteException e) { |
| 150 |
throw e.rethrowFromSystemServer(); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public void reportKeyguardShowingChanged() { |
| 160 |
try { |
| 161 |
mService.reportKeyguardShowingChanged(); |
| 162 |
} catch (RemoteException e) { |
| 163 |
throw e.rethrowFromSystemServer(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public void registerTrustListener(final TrustListener trustListener) { |
| 173 |
try { |
| 174 |
ITrustListener.Stub iTrustListener = new ITrustListener.Stub() { |
| 175 |
@Override |
| 176 |
public void onTrustChanged(boolean enabled, boolean newlyUnlocked, int userId, |
| 177 |
int flags, List<String> trustGrantedMessages) { |
| 178 |
Message m = mHandler.obtainMessage(MSG_TRUST_CHANGED, (enabled ? 1 : 0), userId, |
| 179 |
trustListener); |
| 180 |
if (flags != 0) { |
| 181 |
m.getData().putInt(DATA_FLAGS, flags); |
| 182 |
} |
| 183 |
m.getData().putInt(DATA_NEWLY_UNLOCKED, newlyUnlocked ? 1 : 0); |
| 184 |
m.getData().putCharSequenceArrayList( |
| 185 |
DATA_GRANTED_MESSAGES, (ArrayList) trustGrantedMessages); |
| 186 |
m.sendToTarget(); |
| 187 |
} |
| 188 |
|
| 189 |
@Override |
| 190 |
public void onTrustManagedChanged(boolean managed, int userId) { |
| 191 |
mHandler.obtainMessage(MSG_TRUST_MANAGED_CHANGED, (managed ? 1 : 0), userId, |
| 192 |
trustListener).sendToTarget(); |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public void onTrustError(CharSequence message) { |
| 197 |
Message m = mHandler.obtainMessage(MSG_TRUST_ERROR, trustListener); |
| 198 |
m.getData().putCharSequence(DATA_MESSAGE, message); |
| 199 |
m.sendToTarget(); |
| 200 |
} |
| 201 |
}; |
| 202 |
mService.registerTrustListener(iTrustListener); |
| 203 |
mTrustListeners.put(trustListener, iTrustListener); |
| 204 |
} catch (RemoteException e) { |
| 205 |
throw e.rethrowFromSystemServer(); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
public void unregisterTrustListener(final TrustListener trustListener) { |
| 215 |
ITrustListener iTrustListener = mTrustListeners.remove(trustListener); |
| 216 |
if (iTrustListener != null) { |
| 217 |
try { |
| 218 |
mService.unregisterTrustListener(iTrustListener); |
| 219 |
} catch (RemoteException e) { |
| 220 |
throw e.rethrowFromSystemServer(); |
| 221 |
} |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
@RequiresPermission(android.Manifest.permission.TRUST_LISTENER) |
| 230 |
public boolean isTrustUsuallyManaged(int userId) { |
| 231 |
try { |
| 232 |
return mService.isTrustUsuallyManaged(userId); |
| 233 |
} catch (RemoteException e) { |
| 234 |
throw e.rethrowFromSystemServer(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
@RequiresPermission(Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE) |
| 246 |
public void unlockedByBiometricForUser(int userId, BiometricSourceType source) { |
| 247 |
try { |
| 248 |
mService.unlockedByBiometricForUser(userId, source); |
| 249 |
} catch (RemoteException e) { |
| 250 |
throw e.rethrowFromSystemServer(); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
@RequiresPermission(Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE) |
| 258 |
public void clearAllBiometricRecognized(BiometricSourceType source, int unlockedUser) { |
| 259 |
try { |
| 260 |
mService.clearAllBiometricRecognized(source, unlockedUser); |
| 261 |
} catch (RemoteException e) { |
| 262 |
throw e.rethrowFromSystemServer(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 267 |
@Override |
| 268 |
public void handleMessage(Message msg) { |
| 269 |
switch(msg.what) { |
| 270 |
case MSG_TRUST_CHANGED: |
| 271 |
Bundle data = msg.peekData(); |
| 272 |
int flags = data != null ? data.getInt(DATA_FLAGS) : 0; |
| 273 |
boolean enabled = msg.arg1 != 0; |
| 274 |
int newlyUnlockedInt = |
| 275 |
data != null ? data.getInt(DATA_NEWLY_UNLOCKED) : 0; |
| 276 |
boolean newlyUnlocked = newlyUnlockedInt != 0; |
| 277 |
((TrustListener) msg.obj).onTrustChanged(enabled, newlyUnlocked, msg.arg2, |
| 278 |
flags, msg.getData().getStringArrayList(DATA_GRANTED_MESSAGES)); |
| 279 |
break; |
| 280 |
case MSG_TRUST_MANAGED_CHANGED: |
| 281 |
((TrustListener)msg.obj).onTrustManagedChanged(msg.arg1 != 0, msg.arg2); |
| 282 |
break; |
| 283 |
case MSG_TRUST_ERROR: |
| 284 |
final CharSequence message = msg.peekData().getCharSequence(DATA_MESSAGE); |
| 285 |
((TrustListener) msg.obj).onTrustError(message); |
| 286 |
} |
| 287 |
} |
| 288 |
}; |
| 289 |
|
| 290 |
public interface TrustListener { |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
void onTrustChanged(boolean enabled, boolean newlyUnlocked, int userId, int flags, |
| 305 |
List<String> trustGrantedMessages); |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
void onTrustManagedChanged(boolean enabled, int userId); |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
void onTrustError(CharSequence message); |
| 319 |
} |
| 320 |
} |