Merge "Register new AIDL SensorManager"
diff --git a/services/core/jni/Android.bp b/services/core/jni/Android.bp
index 301fca7..93a4ebf 100644
--- a/services/core/jni/Android.bp
+++ b/services/core/jni/Android.bp
@@ -132,6 +132,7 @@
         "libschedulerservicehidl",
         "libsensorservice",
         "libsensorservicehidl",
+        "libsensorserviceaidl",
         "libgui",
         "libtimestats_atoms_proto",
         "libusbhost",
@@ -180,6 +181,7 @@
         "android.hidl.token@1.0-utils",
         "android.frameworks.schedulerservice@1.0",
         "android.frameworks.sensorservice@1.0",
+        "android.frameworks.sensorservice-V1-ndk",
         "android.frameworks.stats@1.0",
         "android.frameworks.stats-V1-ndk",
         "android.system.suspend.control-V1-cpp",
diff --git a/services/core/jni/com_android_server_SystemServer.cpp b/services/core/jni/com_android_server_SystemServer.cpp
index b171a07..be18f64 100644
--- a/services/core/jni/com_android_server_SystemServer.cpp
+++ b/services/core/jni/com_android_server_SystemServer.cpp
@@ -14,35 +14,31 @@
  * limitations under the License.
  */
 
-#include <dlfcn.h>
-#include <pthread.h>
-
-#include <chrono>
-#include <thread>
-
-#include <jni.h>
-#include <nativehelper/JNIHelp.h>
-
+#include <android-base/properties.h>
 #include <android/binder_manager.h>
 #include <android/binder_stability.h>
 #include <android/hidl/manager/1.2/IServiceManager.h>
 #include <binder/IServiceManager.h>
+#include <bionic/malloc.h>
+#include <bionic/reserved_signals.h>
+#include <dlfcn.h>
 #include <hidl/HidlTransportSupport.h>
 #include <incremental_service.h>
-
+#include <jni.h>
 #include <memtrackproxy/MemtrackProxy.h>
+#include <nativehelper/JNIHelp.h>
+#include <pthread.h>
 #include <schedulerservice/SchedulingPolicyService.h>
+#include <sensorserviceaidl/SensorManagerAidl.h>
 #include <sensorservicehidl/SensorManager.h>
 #include <stats/StatsAidl.h>
 #include <stats/StatsHal.h>
-
-#include <bionic/malloc.h>
-#include <bionic/reserved_signals.h>
-
-#include <android-base/properties.h>
+#include <utils/AndroidThreads.h>
 #include <utils/Log.h>
 #include <utils/misc.h>
-#include <utils/AndroidThreads.h>
+
+#include <chrono>
+#include <thread>
 
 using namespace std::chrono_literals;
 
@@ -57,7 +53,9 @@
     const std::string instance = std::string() + IStats::descriptor + "/default";
     const binder_exception_t err =
             AServiceManager_addService(statsService->asBinder().get(), instance.c_str());
-    LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register AIDL %s: %d", instance.c_str(), err);
+    if (err != EX_NONE) {
+        ALOGW("Cannot register AIDL %s: %d", instance.c_str(), err);
+    }
 }
 
 static void startStatsHidlService() {
@@ -69,6 +67,42 @@
     ALOGW_IF(err != android::OK, "Cannot register HIDL %s: %d", IStats::descriptor, err);
 }
 
+static void startSensorManagerAidlService(JNIEnv* env) {
+    using ::aidl::android::frameworks::sensorservice::ISensorManager;
+    using ::android::frameworks::sensorservice::implementation::SensorManagerAidl;
+
+    JavaVM* vm;
+    LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Cannot get Java VM");
+
+    std::shared_ptr<SensorManagerAidl> sensorService =
+            ndk::SharedRefBase::make<SensorManagerAidl>(vm);
+    const std::string instance = std::string() + ISensorManager::descriptor + "/default";
+    const binder_exception_t err =
+            AServiceManager_addService(sensorService->asBinder().get(), instance.c_str());
+    LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register AIDL %s: %d", instance.c_str(), err);
+}
+
+static void startSensorManagerHidlService(JNIEnv* env) {
+    using ::android::frameworks::sensorservice::V1_0::ISensorManager;
+    using ::android::frameworks::sensorservice::V1_0::implementation::SensorManager;
+    using ::android::hardware::configureRpcThreadpool;
+    using ::android::hidl::manager::V1_0::IServiceManager;
+
+    JavaVM* vm;
+    LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Cannot get Java VM");
+
+    android::sp<ISensorManager> sensorService = new SensorManager(vm);
+    if (IServiceManager::Transport::HWBINDER ==
+        android::hardware::defaultServiceManager1_2()->getTransport(ISensorManager::descriptor,
+                                                                    "default")) {
+        android::status_t err = sensorService->registerAsService();
+        LOG_ALWAYS_FATAL_IF(err != android::OK, "Cannot register %s: %d",
+                            ISensorManager::descriptor, err);
+    } else {
+        ALOGW("%s is deprecated. Skipping registration.", ISensorManager::descriptor);
+    }
+}
+
 } // namespace
 
 namespace android {
@@ -78,6 +112,12 @@
     startStatsAidlService();
 }
 
+static void android_server_SystemServer_startISensorManagerService(JNIEnv* env,
+                                                                   jobject /* clazz */) {
+    startSensorManagerHidlService(env);
+    startSensorManagerAidlService(env);
+}
+
 static void android_server_SystemServer_startMemtrackProxyService(JNIEnv* env,
                                                                   jobject /* clazz */) {
     using aidl::android::hardware::memtrack::MemtrackProxy;
@@ -93,35 +133,19 @@
     LOG_ALWAYS_FATAL_IF(err != EX_NONE, "Cannot register %s: %d", memtrackProxyService, err);
 }
 
-static void android_server_SystemServer_startHidlServices(JNIEnv* env, jobject /* clazz */) {
+static void android_server_SystemServer_startHidlServices(JNIEnv* /* env */, jobject /* clazz */) {
     using ::android::frameworks::schedulerservice::V1_0::ISchedulingPolicyService;
     using ::android::frameworks::schedulerservice::V1_0::implementation::SchedulingPolicyService;
-    using ::android::frameworks::sensorservice::V1_0::ISensorManager;
-    using ::android::frameworks::sensorservice::V1_0::implementation::SensorManager;
     using ::android::hardware::configureRpcThreadpool;
     using ::android::hidl::manager::V1_0::IServiceManager;
 
-    status_t err;
-
     configureRpcThreadpool(5, false /* callerWillJoin */);
 
-    JavaVM *vm;
-    LOG_ALWAYS_FATAL_IF(env->GetJavaVM(&vm) != JNI_OK, "Cannot get Java VM");
-
-    sp<ISensorManager> sensorService = new SensorManager(vm);
-    if (IServiceManager::Transport::HWBINDER ==
-        hardware::defaultServiceManager1_2()->getTransport(ISensorManager::descriptor, "default")) {
-        err = sensorService->registerAsService();
-        LOG_ALWAYS_FATAL_IF(err != OK, "Cannot register %s: %d", ISensorManager::descriptor, err);
-    } else {
-        ALOGW("%s is deprecated. Skipping registration.", ISensorManager::descriptor);
-    }
-
     sp<ISchedulingPolicyService> schedulingService = new SchedulingPolicyService();
     if (IServiceManager::Transport::HWBINDER ==
         hardware::defaultServiceManager1_2()->getTransport(ISchedulingPolicyService::descriptor,
                                                            "default")) {
-        err = schedulingService->registerAsService("default");
+        status_t err = schedulingService->registerAsService("default");
         LOG_ALWAYS_FATAL_IF(err != OK, "Cannot register %s: %d",
                             ISchedulingPolicyService::descriptor, err);
     } else {
@@ -156,6 +180,8 @@
 static const JNINativeMethod gMethods[] = {
         /* name, signature, funcPtr */
         {"startIStatsService", "()V", (void*)android_server_SystemServer_startIStatsService},
+        {"startISensorManagerService", "()V",
+         (void*)android_server_SystemServer_startISensorManagerService},
         {"startMemtrackProxyService", "()V",
          (void*)android_server_SystemServer_startMemtrackProxyService},
         {"startHidlServices", "()V", (void*)android_server_SystemServer_startHidlServices},
diff --git a/services/java/com/android/server/SystemServer.java b/services/java/com/android/server/SystemServer.java
index c2a6944..a953bcb 100644
--- a/services/java/com/android/server/SystemServer.java
+++ b/services/java/com/android/server/SystemServer.java
@@ -466,6 +466,7 @@
     private final long mRuntimeStartUptime;
 
     private static final String START_HIDL_SERVICES = "StartHidlServices";
+    private static final String START_SENSOR_MANAGER_SERVICE = "StartISensorManagerService";
     private static final String START_BLOB_STORE_SERVICE = "startBlobStoreManagerService";
 
     private static final String SYSPROP_START_COUNT = "sys.system_server.start_count";
@@ -484,6 +485,9 @@
     /** Start the IStats services. This is a blocking call and can take time. */
     private static native void startIStatsService();
 
+    /** Start the ISensorManager service. This is a blocking call and can take time. */
+    private static native void startISensorManagerService();
+
     /**
      * Start the memtrack proxy service.
      */
@@ -1597,11 +1601,18 @@
             wm.onInitReady();
             t.traceEnd();
 
-            // Start receiving calls from HIDL services. Start in in a separate thread
+            // Start receiving calls from SensorManager services. Start in a separate thread
             // because it need to connect to SensorManager. This has to start
             // after PHASE_WAIT_FOR_SENSOR_SERVICE is done.
             SystemServerInitThreadPool.submit(() -> {
                 TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog();
+                traceLog.traceBegin(START_SENSOR_MANAGER_SERVICE);
+                startISensorManagerService();
+                traceLog.traceEnd();
+            }, START_SENSOR_MANAGER_SERVICE);
+
+            SystemServerInitThreadPool.submit(() -> {
+                TimingsTraceAndSlog traceLog = TimingsTraceAndSlog.newAsyncLog();
                 traceLog.traceBegin(START_HIDL_SERVICES);
                 startHidlServices();
                 traceLog.traceEnd();