From 79bacbbbdb4a2aa4cf6d143c2d2c30d7dd31ace5 Mon Sep 17 00:00:00 2001 From: Adrian Roos Date: Wed, 26 Oct 2016 11:00:12 -0700 Subject: [PATCH] AmbientDisplay: Fixed threading Change-Id: Iee8d2b12145f3f1ab98099631539f6421e99f910 --- .../src/com/android/systemui/DejankUtils.java | 13 +++----- .../android/systemui/doze/DozeSensors.java | 13 ++++---- .../android/systemui/doze/DozeService.java | 4 +++ .../src/com/android/systemui/util/Assert.java | 31 +++++++++++++++++++ 4 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 packages/SystemUI/src/com/android/systemui/util/Assert.java diff --git a/packages/SystemUI/src/com/android/systemui/DejankUtils.java b/packages/SystemUI/src/com/android/systemui/DejankUtils.java index f8ce1d3aa58b8..eba1d0f9f9c68 100644 --- a/packages/SystemUI/src/com/android/systemui/DejankUtils.java +++ b/packages/SystemUI/src/com/android/systemui/DejankUtils.java @@ -16,8 +16,9 @@ package com.android.systemui; +import com.android.systemui.util.Assert; + import android.os.Handler; -import android.os.Looper; import android.view.Choreographer; import java.util.ArrayList; @@ -50,7 +51,7 @@ public class DejankUtils { *

Needs to be called from the main thread. */ public static void postAfterTraversal(Runnable r) { - throwIfNotCalledOnMainThread(); + Assert.isMainThread(); sPendingRunnables.add(r); postAnimationCallback(); } @@ -61,7 +62,7 @@ public class DejankUtils { *

Needs to be called from the main thread. */ public static void removeCallbacks(Runnable r) { - throwIfNotCalledOnMainThread(); + Assert.isMainThread(); sPendingRunnables.remove(r); sHandler.removeCallbacks(r); } @@ -70,10 +71,4 @@ public class DejankUtils { sChoreographer.postCallback(Choreographer.CALLBACK_ANIMATION, sAnimationCallbackRunnable, null); } - - private static void throwIfNotCalledOnMainThread() { - if (!Looper.getMainLooper().isCurrentThread()) { - throw new IllegalStateException("should be called from the main thread."); - } - } } diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java b/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java index fca156293eef0..fd1ac8e743079 100644 --- a/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java +++ b/packages/SystemUI/src/com/android/systemui/doze/DozeSensors.java @@ -208,14 +208,15 @@ public class DozeSensors { @Override @AnyThread public void onTrigger(TriggerEvent event) { - mWakeLock.acquire(); - try { - if (DEBUG) Log.d(TAG, "onTrigger: " + triggerEventToString(event)); + mHandler.post(mWakeLock.wrap(() -> { + if (DEBUG) + Log.d(TAG, "onTrigger: " + triggerEventToString(event)); boolean sensorPerformsProxCheck = false; if (mSensor.getType() == Sensor.TYPE_PICK_UP_GESTURE) { int subType = (int) event.values[0]; MetricsLogger.action( - mContext, MetricsProto.MetricsEvent.ACTION_AMBIENT_GESTURE, subType); + mContext, MetricsProto.MetricsEvent.ACTION_AMBIENT_GESTURE, + subType); sensorPerformsProxCheck = mDozeParameters.getPickupSubtypePerformsProxCheck(subType); } @@ -223,9 +224,7 @@ public class DozeSensors { mRegistered = false; mCallback.onSensorPulse(mPulseReason, sensorPerformsProxCheck); updateListener(); // reregister, this sensor only fires once - } finally { - mWakeLock.release(); - } + })); } public void registerSettingsObserver(ContentObserver settingsObserver) { diff --git a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java index 6ddf3699df017..8789845cb56b3 100644 --- a/packages/SystemUI/src/com/android/systemui/doze/DozeService.java +++ b/packages/SystemUI/src/com/android/systemui/doze/DozeService.java @@ -35,8 +35,11 @@ import android.util.Log; import android.view.Display; import com.android.internal.hardware.AmbientDisplayConfiguration; +import com.android.internal.util.Preconditions; +import com.android.systemui.DejankUtils; import com.android.systemui.SystemUIApplication; import com.android.systemui.statusbar.phone.DozeParameters; +import com.android.systemui.util.Assert; import java.io.FileDescriptor; import java.io.PrintWriter; @@ -188,6 +191,7 @@ public class DozeService extends DreamService implements DozeSensors.Callback { } private void requestPulse(final int reason, boolean performedProxCheck) { + Assert.isMainThread(); if (mHost != null && mDreaming && !mPulsing) { // Let the host know we want to pulse. Wait for it to be ready, then // turn the screen on. When finished, turn the screen off again. diff --git a/packages/SystemUI/src/com/android/systemui/util/Assert.java b/packages/SystemUI/src/com/android/systemui/util/Assert.java new file mode 100644 index 0000000000000..af447f3f74ecf --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/util/Assert.java @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +package com.android.systemui.util; + +import android.os.Looper; + +/** + * Helper providing common assertions. + */ +public class Assert { + + public static void isMainThread() { + if (!Looper.getMainLooper().isCurrentThread()) { + throw new IllegalStateException("should be called from the main thread."); + } + } +}