blob: 88ba874083b588a080460562a2172c5ffb20e253 [file] [log] [blame]
Adrian Roos82142c22014-03-27 14:56:59 +01001/*
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 android.app.trust;
18
Clara Bayarri00a9b892016-01-13 16:17:09 +000019import android.Manifest;
Adrian Roosb5e47222015-08-14 15:53:06 -070020import android.annotation.IntDef;
Clara Bayarri00a9b892016-01-13 16:17:09 +000021import android.annotation.RequiresPermission;
Adrian Roos82142c22014-03-27 14:56:59 +010022import android.os.Handler;
23import android.os.IBinder;
24import android.os.Looper;
25import android.os.Message;
26import android.os.RemoteException;
Adrian Roosb5e47222015-08-14 15:53:06 -070027import android.os.UserHandle;
Adrian Roos82142c22014-03-27 14:56:59 +010028import android.util.ArrayMap;
29import android.util.Log;
Adrian Roosb5e47222015-08-14 15:53:06 -070030import android.util.SparseIntArray;
31
32import java.lang.annotation.Retention;
33import java.lang.annotation.RetentionPolicy;
Adrian Roos82142c22014-03-27 14:56:59 +010034
35/**
36 * See {@link com.android.server.trust.TrustManagerService}
37 * @hide
38 */
39public class TrustManager {
40
41 private static final int MSG_TRUST_CHANGED = 1;
Adrian Roos7861c662014-07-25 15:37:28 +020042 private static final int MSG_TRUST_MANAGED_CHANGED = 2;
Adrian Roos82142c22014-03-27 14:56:59 +010043
44 private static final String TAG = "TrustManager";
Adrian Roos94e15a52015-04-16 12:23:18 -070045 private static final String DATA_FLAGS = "initiatedByUser";
Adrian Roos82142c22014-03-27 14:56:59 +010046
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 Bayarri56878a92015-10-29 15:43:55 +000056 * 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 Bayarri00a9b892016-01-13 16:17:09 +000059 * Requires the {@link android.Manifest.permission#ACCESS_KEYGUARD_SECURE_STORAGE} permission.
60 *
Clara Bayarri56878a92015-10-29 15:43:55 +000061 * @param userId The id for the user to be locked/unlocked.
62 * @param locked The value for that user's locked state.
63 */
Clara Bayarri00a9b892016-01-13 16:17:09 +000064 @RequiresPermission(Manifest.permission.ACCESS_KEYGUARD_SECURE_STORAGE)
Clara Bayarri56878a92015-10-29 15:43:55 +000065 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 Roos82142c22014-03-27 14:56:59 +010074 * 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 Roos481a6df2014-11-20 19:48:56 +0100102 * 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 Roos82142c22014-03-27 14:56:59 +0100115 * 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 Roos94e15a52015-04-16 12:23:18 -0700123 public void onTrustChanged(boolean enabled, int userId, int flags) {
Adrian Roos3c9a3502014-08-06 19:09:45 +0200124 Message m = mHandler.obtainMessage(MSG_TRUST_CHANGED, (enabled ? 1 : 0), userId,
125 trustListener);
Adrian Roos94e15a52015-04-16 12:23:18 -0700126 if (flags != 0) {
127 m.getData().putInt(DATA_FLAGS, flags);
Adrian Roos3c9a3502014-08-06 19:09:45 +0200128 }
129 m.sendToTarget();
Adrian Roos82142c22014-03-27 14:56:59 +0100130 }
Adrian Roos7861c662014-07-25 15:37:28 +0200131
132 @Override
Adrian Roos3c9a3502014-08-06 19:09:45 +0200133 public void onTrustManagedChanged(boolean managed, int userId) {
Adrian Roos7861c662014-07-25 15:37:28 +0200134 mHandler.obtainMessage(MSG_TRUST_MANAGED_CHANGED, (managed ? 1 : 0), userId,
135 trustListener).sendToTarget();
136 }
Adrian Roos82142c22014-03-27 14:56:59 +0100137 };
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 Roos94e15a52015-04-16 12:23:18 -0700170 int flags = msg.peekData() != null ? msg.peekData().getInt(DATA_FLAGS) : 0;
171 ((TrustListener)msg.obj).onTrustChanged(msg.arg1 != 0, msg.arg2, flags);
Adrian Roos82142c22014-03-27 14:56:59 +0100172 break;
Adrian Roos7861c662014-07-25 15:37:28 +0200173 case MSG_TRUST_MANAGED_CHANGED:
174 ((TrustListener)msg.obj).onTrustManagedChanged(msg.arg1 != 0, msg.arg2);
Adrian Roos82142c22014-03-27 14:56:59 +0100175 }
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 Roos94e15a52015-04-16 12:23:18 -0700185 * @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 Roos82142c22014-03-27 14:56:59 +0100188 */
Adrian Roos94e15a52015-04-16 12:23:18 -0700189 void onTrustChanged(boolean enabled, int userId, int flags);
Adrian Roos7861c662014-07-25 15:37:28 +0200190
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 Roos82142c22014-03-27 14:56:59 +0100197 }
198}