diff options
| author | Jeff Sharkey <jsharkey@android.com> | 2017-12-12 17:26:23 -0700 |
|---|---|---|
| committer | Jeff Sharkey <jsharkey@android.com> | 2017-12-13 13:59:04 -0700 |
| commit | 8439ac08b1b8ef362cd649dbefdea4ac140051e2 (patch) | |
| tree | f7dfbb91bb60ff3c2e8768aa9975ca45b4563e59 /core/java/android/content/Context.java | |
| parent | ff38f236b55b51a9f8e03b909f4791ccca329c48 (diff) | |
Start accepting Executors instead of Handlers.
After discussion in API council, our new best-practices are to have
developers provide an Executor to dispatch callbacks/listeners on,
instead of the previous guidance of using a Handler.
Define Context.getMainExecutor() to easily obtain an Executor
associated with the main thread of application. This allows new
APIs to require a @NonNull Executor. Also define a new
@CallbackExecutor auto-doc annotation that explains background and
points developers at new Context method above.
Test: cts-tradefed run commandAndExit cts-dev -m CtsContentTestCases -t android.content.cts.ContextTest#testMainLooper
Test: cts-tradefed run commandAndExit cts-dev -m CtsContentTestCases -t android.content.cts.ContextTest#testMainExecutor
Bug: 70348426
Change-Id: I536892bcd80fcfba1bb1ddf67648c08a26d7ddd2
Diffstat (limited to 'core/java/android/content/Context.java')
| -rw-r--r-- | core/java/android/content/Context.java | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index a47433093988..458ba05015ba 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -51,6 +51,7 @@ import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.os.Handler; +import android.os.HandlerExecutor; import android.os.IBinder; import android.os.Looper; import android.os.StatFs; @@ -75,6 +76,7 @@ import java.io.IOException; import java.io.InputStream; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.concurrent.Executor; /** * Interface to global information about an application environment. This is @@ -462,6 +464,16 @@ public abstract class Context { public abstract Looper getMainLooper(); /** + * Return an {@link Executor} that will run enqueued tasks on the main + * thread associated with this context. This is the thread used to dispatch + * calls to application components (activities, services, etc). + */ + public Executor getMainExecutor() { + // This is pretty inefficient, which is why ContextImpl overrides it + return new HandlerExecutor(new Handler(getMainLooper())); + } + + /** * Return the context of the single, global Application object of the * current process. This generally should only be used if you need a * Context whose lifecycle is separate from the current context, that is |
