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

Commit 05dc89bf authored by Jay Civelli's avatar Jay Civelli Committed by Android (Google) Code Review
Browse files

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

parents 8a30b31a 4f22777e
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;
                }
            }
        }
    }