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 android.app.trust; |
| 18 | |
| 19 | import android.os.Handler; |
| 20 | import android.os.IBinder; |
| 21 | import android.os.Looper; |
| 22 | import android.os.Message; |
| 23 | import android.os.RemoteException; |
| 24 | import android.util.ArrayMap; |
| 25 | import android.util.Log; |
| 26 | |
| 27 | /** |
| 28 | * See {@link com.android.server.trust.TrustManagerService} |
| 29 | * @hide |
| 30 | */ |
| 31 | public class TrustManager { |
| 32 | |
| 33 | private static final int MSG_TRUST_CHANGED = 1; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 34 | private static final int MSG_TRUST_MANAGED_CHANGED = 2; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 35 | |
| 36 | private static final String TAG = "TrustManager"; |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 37 | private static final String DATA_FLAGS = "initiatedByUser"; |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 38 | |
| 39 | private final ITrustManager mService; |
| 40 | private final ArrayMap<TrustListener, ITrustListener> mTrustListeners; |
| 41 | |
| 42 | public TrustManager(IBinder b) { |
| 43 | mService = ITrustManager.Stub.asInterface(b); |
| 44 | mTrustListeners = new ArrayMap<TrustListener, ITrustListener>(); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Reports that user {@param userId} has tried to unlock the device. |
| 49 | * |
| 50 | * @param successful if true, the unlock attempt was successful. |
| 51 | * |
| 52 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 53 | */ |
| 54 | public void reportUnlockAttempt(boolean successful, int userId) { |
| 55 | try { |
| 56 | mService.reportUnlockAttempt(successful, userId); |
| 57 | } catch (RemoteException e) { |
| 58 | onError(e); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Reports that the list of enabled trust agents changed for user {@param userId}. |
| 64 | * |
| 65 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 66 | */ |
| 67 | public void reportEnabledTrustAgentsChanged(int userId) { |
| 68 | try { |
| 69 | mService.reportEnabledTrustAgentsChanged(userId); |
| 70 | } catch (RemoteException e) { |
| 71 | onError(e); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
Adrian Roos | 2c12cfa | 2014-06-25 23:28:53 +0200 | [diff] [blame] | 76 | * Reports that trust is disabled until credentials have been entered for user {@param userId}. |
| 77 | * |
| 78 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 79 | * |
| 80 | * @param userId either an explicit user id or {@link android.os.UserHandle#USER_ALL} |
| 81 | */ |
| 82 | public void reportRequireCredentialEntry(int userId) { |
| 83 | try { |
| 84 | mService.reportRequireCredentialEntry(userId); |
| 85 | } catch (RemoteException e) { |
| 86 | onError(e); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /** |
Adrian Roos | 481a6df | 2014-11-20 19:48:56 +0100 | [diff] [blame] | 91 | * Reports that the visibility of the keyguard has changed. |
| 92 | * |
| 93 | * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission. |
| 94 | */ |
| 95 | public void reportKeyguardShowingChanged() { |
| 96 | try { |
| 97 | mService.reportKeyguardShowingChanged(); |
| 98 | } catch (RemoteException e) { |
| 99 | onError(e); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 104 | * Registers a listener for trust events. |
| 105 | * |
| 106 | * Requires the {@link android.Manifest.permission#TRUST_LISTENER} permission. |
| 107 | */ |
| 108 | public void registerTrustListener(final TrustListener trustListener) { |
| 109 | try { |
| 110 | ITrustListener.Stub iTrustListener = new ITrustListener.Stub() { |
| 111 | @Override |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 112 | public void onTrustChanged(boolean enabled, int userId, int flags) { |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 113 | Message m = mHandler.obtainMessage(MSG_TRUST_CHANGED, (enabled ? 1 : 0), userId, |
| 114 | trustListener); |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 115 | if (flags != 0) { |
| 116 | m.getData().putInt(DATA_FLAGS, flags); |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 117 | } |
| 118 | m.sendToTarget(); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 119 | } |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 120 | |
| 121 | @Override |
Adrian Roos | 3c9a350 | 2014-08-06 19:09:45 +0200 | [diff] [blame] | 122 | public void onTrustManagedChanged(boolean managed, int userId) { |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 123 | mHandler.obtainMessage(MSG_TRUST_MANAGED_CHANGED, (managed ? 1 : 0), userId, |
| 124 | trustListener).sendToTarget(); |
| 125 | } |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 126 | }; |
| 127 | mService.registerTrustListener(iTrustListener); |
| 128 | mTrustListeners.put(trustListener, iTrustListener); |
| 129 | } catch (RemoteException e) { |
| 130 | onError(e); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Unregisters a listener for trust events. |
| 136 | * |
| 137 | * Requires the {@link android.Manifest.permission#TRUST_LISTENER} permission. |
| 138 | */ |
| 139 | public void unregisterTrustListener(final TrustListener trustListener) { |
| 140 | ITrustListener iTrustListener = mTrustListeners.remove(trustListener); |
| 141 | if (iTrustListener != null) { |
| 142 | try { |
| 143 | mService.unregisterTrustListener(iTrustListener); |
| 144 | } catch (RemoteException e) { |
| 145 | onError(e); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private void onError(Exception e) { |
| 151 | Log.e(TAG, "Error while calling TrustManagerService", e); |
| 152 | } |
| 153 | |
| 154 | private final Handler mHandler = new Handler(Looper.getMainLooper()) { |
| 155 | @Override |
| 156 | public void handleMessage(Message msg) { |
| 157 | switch(msg.what) { |
| 158 | case MSG_TRUST_CHANGED: |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 159 | int flags = msg.peekData() != null ? msg.peekData().getInt(DATA_FLAGS) : 0; |
| 160 | ((TrustListener)msg.obj).onTrustChanged(msg.arg1 != 0, msg.arg2, flags); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 161 | break; |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 162 | case MSG_TRUST_MANAGED_CHANGED: |
| 163 | ((TrustListener)msg.obj).onTrustManagedChanged(msg.arg1 != 0, msg.arg2); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | public interface TrustListener { |
| 169 | |
| 170 | /** |
| 171 | * Reports that the trust state has changed. |
| 172 | * @param enabled if true, the system believes the environment to be trusted. |
| 173 | * @param userId the user, for which the trust changed. |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 174 | * @param flags flags specified by the trust agent when granting trust. See |
| 175 | * {@link android.service.trust.TrustAgentService#grantTrust(CharSequence, long, int) |
| 176 | * TrustAgentService.grantTrust(CharSequence, long, int)}. |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 177 | */ |
Adrian Roos | 94e15a5 | 2015-04-16 12:23:18 -0700 | [diff] [blame] | 178 | void onTrustChanged(boolean enabled, int userId, int flags); |
Adrian Roos | 7861c66 | 2014-07-25 15:37:28 +0200 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * Reports that whether trust is managed has changed |
| 182 | * @param enabled if true, at least one trust agent is managing trust. |
| 183 | * @param userId the user, for which the state changed. |
| 184 | */ |
| 185 | void onTrustManagedChanged(boolean enabled, int userId); |
Adrian Roos | 82142c2 | 2014-03-27 14:56:59 +0100 | [diff] [blame] | 186 | } |
| 187 | } |