summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/java/android/service/dreams/DreamService.java24
-rw-r--r--services/tests/dreamservicetests/src/com/android/server/dreams/TestDreamEnvironment.java2
2 files changed, 14 insertions, 12 deletions
diff --git a/core/java/android/service/dreams/DreamService.java b/core/java/android/service/dreams/DreamService.java
index 31143a559060..81cecf1b3642 100644
--- a/core/java/android/service/dreams/DreamService.java
+++ b/core/java/android/service/dreams/DreamService.java
@@ -341,7 +341,7 @@ public class DreamService extends Service implements Window.Callback {
*/
public interface WakefulHandler {
/** Posts a {@link Runnable} to be ran on the underlying {@link Handler}. */
- void post(Runnable r);
+ void postIfNeeded(Runnable r);
/**
* Returns the underlying {@link Handler}. Should only be used for passing the handler into
@@ -386,8 +386,10 @@ public class DreamService extends Service implements Window.Callback {
}
@Override
- public void post(Runnable r) {
- if (mWakeLock != null) {
+ public void postIfNeeded(Runnable r) {
+ if (mHandler.getLooper().isCurrentThread()) {
+ r.run();
+ } else if (mWakeLock != null) {
mHandler.post(mWakeLock.wrap(r));
} else {
mHandler.post(r);
@@ -995,17 +997,17 @@ public class DreamService extends Service implements Window.Callback {
}
}
- private void post(Runnable runnable) {
+ private void postIfNeeded(Runnable runnable) {
// The handler is based on the populated context is not ready at construction time.
// therefore we fetch on demand.
- mInjector.getWakefulHandler().post(runnable);
+ mInjector.getWakefulHandler().postIfNeeded(runnable);
}
/**
* Updates doze state. Note that this must be called on the mHandler.
*/
private void updateDoze() {
- post(() -> {
+ postIfNeeded(() -> {
if (mDreamToken == null) {
Slog.w(mTag, "Updating doze without a dream token.");
return;
@@ -1047,7 +1049,7 @@ public class DreamService extends Service implements Window.Callback {
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
public void stopDozing() {
- post(() -> {
+ postIfNeeded(() -> {
if (mDreamToken == null) {
return;
}
@@ -1290,7 +1292,7 @@ public class DreamService extends Service implements Window.Callback {
final long token = Binder.clearCallingIdentity();
try {
// Simply finish dream when exit is requested.
- post(() -> finishInternal());
+ postIfNeeded(() -> finishInternal());
} finally {
Binder.restoreCallingIdentity(token);
}
@@ -1396,7 +1398,7 @@ public class DreamService extends Service implements Window.Callback {
* </p>
*/
public final void finish() {
- post(this::finishInternal);
+ postIfNeeded(this::finishInternal);
}
private void finishInternal() {
@@ -1458,7 +1460,7 @@ public class DreamService extends Service implements Window.Callback {
* </p>
*/
public final void wakeUp() {
- post(()-> wakeUp(false));
+ postIfNeeded(()-> wakeUp(false));
}
/**
@@ -1964,7 +1966,7 @@ public class DreamService extends Service implements Window.Callback {
return;
}
- service.post(() -> consumer.accept(service));
+ service.postIfNeeded(() -> consumer.accept(service));
}
@Override
diff --git a/services/tests/dreamservicetests/src/com/android/server/dreams/TestDreamEnvironment.java b/services/tests/dreamservicetests/src/com/android/server/dreams/TestDreamEnvironment.java
index a16d463cae71..03d4ab8e4982 100644
--- a/services/tests/dreamservicetests/src/com/android/server/dreams/TestDreamEnvironment.java
+++ b/services/tests/dreamservicetests/src/com/android/server/dreams/TestDreamEnvironment.java
@@ -201,7 +201,7 @@ public class TestDreamEnvironment {
doAnswer(invocation -> {
((Runnable) invocation.getArgument(0)).run();
return null;
- }).when(mWakefulHandler).post(any());
+ }).when(mWakefulHandler).postIfNeeded(any());
}
@Override
public void init(Context context) {