OSDN Git Service

Make sure ServiceConnection callback is called in main thread
authorTony Mak <tonymak@google.com>
Thu, 22 Dec 2016 11:02:45 +0000 (11:02 +0000)
committerTony Mak <tonymak@google.com>
Thu, 29 Dec 2016 14:07:46 +0000 (14:07 +0000)
Fix: 33568999

Test: cts-tradefed run cts --module CtsDevicePolicyManagerTestCases
--test com.android.cts.devicepolicy.DeviceOwnerPlusManagedProfileTest

Change-Id: I14c8b5b1f78192429e68a3057430406245a909c8

core/java/android/app/ContextImpl.java
core/java/android/app/admin/DevicePolicyManager.java
core/java/android/content/Context.java
core/java/android/content/ContextWrapper.java
test-runner/src/android/test/mock/MockContext.java

index 5f706dc..547c710 100644 (file)
@@ -1470,6 +1470,12 @@ class ContextImpl extends Context {
         return mMainThread.getApplicationThread();
     }
 
+    /** @hide */
+    @Override
+    public Handler getMainThreadHandler() {
+        return mMainThread.getHandler();
+    }
+
     private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, Handler
             handler, UserHandle user) {
         // Keep this in sync with DevicePolicyManager.bindDeviceAdminServiceAsUser.
index 6172884..6f73388 100644 (file)
@@ -7111,8 +7111,8 @@ public class DevicePolicyManager {
      * @param serviceIntent Identifies the service to connect to.  The Intent must specify either an
      *        explicit component name or a package name to match an
      *        {@link IntentFilter} published by a service.
-     * @param conn Receives information as the service is started and stopped. This must be a
-     *        valid {@link ServiceConnection} object; it must not be {@code null}.
+     * @param conn Receives information as the service is started and stopped in main thread. This
+     *        must be a valid {@link ServiceConnection} object; it must not be {@code null}.
      * @param flags Operation options for the binding operation. See
      *        {@link Context#bindService(Intent, ServiceConnection, int)}.
      * @param targetUser Which user to bind to. Must be one of the users returned by
@@ -7131,7 +7131,8 @@ public class DevicePolicyManager {
         throwIfParentInstance("bindDeviceAdminServiceAsUser");
         // Keep this in sync with ContextImpl.bindServiceCommon.
         try {
-            final IServiceConnection sd = mContext.getServiceDispatcher(conn, null, flags);
+            final IServiceConnection sd = mContext.getServiceDispatcher(
+                    conn, mContext.getMainThreadHandler(), flags);
             serviceIntent.prepareToLeaveProcess(mContext);
             return mService.bindDeviceAdminServiceAsUser(admin,
                     mContext.getIApplicationThread(), mContext.getActivityToken(), serviceIntent,
index f0f1d99..9dc60ab 100644 (file)
@@ -4403,4 +4403,11 @@ public abstract class Context {
     public IApplicationThread getIApplicationThread() {
         throw new RuntimeException("Not implemented. Must override in a subclass.");
     }
+
+    /**
+     * @hide
+     */
+    public Handler getMainThreadHandler() {
+        throw new RuntimeException("Not implemented. Must override in a subclass.");
+    }
 }
index 7533655..4b6076b 100644 (file)
@@ -885,4 +885,12 @@ public class ContextWrapper extends Context {
     public IApplicationThread getIApplicationThread() {
         return mBase.getIApplicationThread();
     }
+
+    /**
+     * @hide
+     */
+    @Override
+    public Handler getMainThreadHandler() {
+        return mBase.getMainThreadHandler();
+    }
 }
index 190fc35..bcc68b3 100644 (file)
@@ -780,4 +780,10 @@ public class MockContext extends Context {
     public IApplicationThread getIApplicationThread() {
         throw new UnsupportedOperationException();
     }
+
+    /** {@hide} */
+    @Override
+    public Handler getMainThreadHandler() {
+        throw new UnsupportedOperationException();
+    }
 }