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