samsung: doze: use ExecutorService for listener registration
authorezio84 <brabus84@gmail.com>
Thu, 14 Jun 2018 13:00:19 +0000 (15:00 +0200)
committerBruno Martins <bgcngm@gmail.com>
Wed, 13 Jan 2021 14:23:47 +0000 (15:23 +0100)
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

doze/src/org/lineageos/settings/doze/SamsungDozeService.java

index 9a56f81813ad2d4af479d93590fc2548485c732d..101f670b017feca260483883668864b85059d7a8 100644 (file)
@@ -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);
+            });
         }
     }