Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4f22777e authored by Jay Civelli's avatar Jay Civelli
Browse files

Make sure TrustAgent applies trust if granted before it is bound.

Change-Id: Ibe704d162930e4bb6135fabccb15263935ced009
parent f914a890
Loading
Loading
Loading
Loading
+40 −12
Original line number Diff line number Diff line
@@ -80,6 +80,11 @@ public class TrustAgentService extends Service {

    private ITrustAgentServiceCallback mCallback;

    private Runnable mPendingGrantTrustTask;

    // Lock used to access mPendingGrantTrustTask and mCallback.
    private final Object mLock = new Object();

    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
@@ -127,13 +132,25 @@ public class TrustAgentService extends Service {
     * @param initiatedByUser indicates that the user has explicitly initiated an action that proves
     *                        the user is about to use the device.
     */
    public final void grantTrust(CharSequence message, long durationMs, boolean initiatedByUser) {
    public final void grantTrust(
            final CharSequence message, final long durationMs, final boolean initiatedByUser) {
        synchronized (mLock) {
            if (mCallback != null) {
                try {
                    mCallback.grantTrust(message.toString(), durationMs, initiatedByUser);
                } catch (RemoteException e) {
                    onError("calling enableTrust()");
                }
            } else {
                // Remember trust has been granted so we can effectively grant it once the service
                // is bound.
                mPendingGrantTrustTask = new Runnable() {
                    @Override
                    public void run() {
                        grantTrust(message, durationMs, initiatedByUser);
                    }
                };
            }
        }
    }

@@ -141,6 +158,10 @@ public class TrustAgentService extends Service {
     * Call to revoke trust on the device.
     */
    public final void revokeTrust() {
        synchronized (mLock) {
            if (mPendingGrantTrustTask != null) {
                mPendingGrantTrustTask = null;
            }
            if (mCallback != null) {
                try {
                    mCallback.revokeTrust();
@@ -149,6 +170,7 @@ public class TrustAgentService extends Service {
                }
            }
        }
    }

    @Override
    public final IBinder onBind(Intent intent) {
@@ -164,7 +186,13 @@ public class TrustAgentService extends Service {
        }

        public void setCallback(ITrustAgentServiceCallback callback) {
            synchronized (mLock) {
                mCallback = callback;
                if (mPendingGrantTrustTask != null) {
                    mPendingGrantTrustTask.run();
                    mPendingGrantTrustTask = null;
                }
            }
        }
    }