From cc3bed76b05d92e60d1e76e39df2d2f0850666bc Mon Sep 17 00:00:00 2001 From: ezio84 Date: Thu, 14 Jun 2018 15:00:19 +0200 Subject: [PATCH] samsung: doze: use ExecutorService for listener registration Replicate what Google did for SystemUI in this commit. https://github.com/aosp-mirror/platform_frameworks_base/commit/fabc743bcf6e6623e530545c4b31285ea642f087 Registering a sensor seems to be an expensive operation, and we do it on each screen-on event, so moving it to an asynchronous task looks like a good idea anyway. By moving all non-essential binder calls of the main thread or to the next frame, we bring this down to 5ms, such that window animation and Keyguard animation starts about at the same time. The interesting part about the ExecutorService: "Memory consistency effects: Actions in a thread prior to the submission of a Runnable or Callable task to an ExecutorService happen-before any actions taken by that task, which in turn happen-before the result is retrieved via Future.get()." (from https://developer.android.com/reference/java/util/concurrent/ExecutorService) Change-Id: I4f37bb9a7dc9d7775d587d4ebd4b6619f3b77e81 --- .../settings/doze/SamsungDozeService.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/doze/src/org/lineageos/settings/doze/SamsungDozeService.java b/doze/src/org/lineageos/settings/doze/SamsungDozeService.java index 9a56f818..101f670b 100644 --- a/doze/src/org/lineageos/settings/doze/SamsungDozeService.java +++ b/doze/src/org/lineageos/settings/doze/SamsungDozeService.java @@ -30,6 +30,10 @@ import android.os.PowerManager; import android.os.UserHandle; import android.util.Log; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; + public class SamsungDozeService extends Service { private static final String TAG = "SamsungDozeService"; private static final boolean DEBUG = false; @@ -39,6 +43,7 @@ public class SamsungDozeService extends Service { private static final int POCKET_DELTA_NS = 1000 * 1000 * 1000; private Context mContext; + private ExecutorService mExecutorService; private SamsungProximitySensor mSensor; private PowerManager mPowerManager; @@ -55,6 +60,11 @@ public class SamsungDozeService extends Service { public SamsungProximitySensor(Context context) { mSensorManager = context.getSystemService(SensorManager.class); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY); + mExecutorService = Executors.newSingleThreadExecutor(); + } + + private Future submit(Runnable runnable) { + return mExecutorService.submit(runnable); } @Override @@ -92,11 +102,16 @@ public class SamsungDozeService extends Service { } public void enable() { - mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL); + submit(() -> { + mSensorManager.registerListener(this, mSensor, + SensorManager.SENSOR_DELAY_NORMAL); + }); } public void disable() { - mSensorManager.unregisterListener(this, mSensor); + submit(() -> { + mSensorManager.unregisterListener(this, mSensor); + }); } }