Modularize VoiceInteraction/SoundTrigger

Rather than building VoiceInteraction monolithically, split out
SoundTrigger.

This allows for better build performance, more localized testing,
specific build flags, etc.

Split out the middleware in a static lib for now, but don't expose it
as such, since it will be refactored into SoundTrigger soon.

Fixes: 277374624
Test: Compiles (build changes only)
Change-Id: I631d073d81e578c70b033d0a43543ef89dda9194
diff --git a/services/voiceinteraction/Android.bp b/services/voiceinteraction/Android.bp
index 7332d2d..de8d144 100644
--- a/services/voiceinteraction/Android.bp
+++ b/services/voiceinteraction/Android.bp
@@ -9,11 +9,60 @@
 
 filegroup {
     name: "services.voiceinteraction-sources",
-    srcs: ["java/**/*.java"],
+    srcs: ["java/com/android/server/voiceinteraction/*.java"],
     path: "java",
     visibility: ["//frameworks/base/services"],
 }
 
+filegroup {
+    name: "services.soundtrigger_middleware-sources",
+    srcs: ["java/com/android/server/soundtrigger_middleware/*.java"],
+    path: "java",
+    visibility: ["//visibility:private"],
+}
+
+filegroup {
+    name: "services.soundtrigger_service-sources",
+    srcs: ["java/com/android/server/soundtrigger/*.java"],
+    path: "java",
+    visibility: ["//visibility:private"],
+}
+
+filegroup {
+    name: "services.soundtrigger-sources",
+    srcs: [
+        ":services.soundtrigger_service-sources",
+        ":services.soundtrigger_middleware-sources",
+    ],
+    path: "java",
+    visibility: ["//frameworks/base/services"],
+}
+
+java_library_static {
+    name: "services.soundtrigger_middleware",
+    defaults: ["platform_service_defaults"],
+    srcs: [":services.soundtrigger_middleware-sources"],
+    libs: [
+        "services.core",
+    ],
+    static_libs: [
+        "android.hardware.soundtrigger-V2.3-java",
+    ],
+    visibility: ["//visibility/base/services/tests/voiceinteraction"],
+}
+
+java_library_static {
+    name: "services.soundtrigger",
+    defaults: ["platform_service_defaults"],
+    srcs: [":services.soundtrigger_service-sources"],
+    libs: [
+        "services.core",
+    ],
+    static_libs: [
+        "services.soundtrigger_middleware",
+    ],
+}
+
 java_library_static {
     name: "services.voiceinteraction",
     defaults: ["platform_service_defaults"],
diff --git a/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerInternal.java b/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerInternal.java
deleted file mode 100644
index cc398d9..0000000
--- a/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerInternal.java
+++ /dev/null
@@ -1,156 +0,0 @@
-/**
- * Copyright (C) 2014 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.server.soundtrigger;
-
-import android.annotation.NonNull;
-import android.annotation.Nullable;
-import android.hardware.soundtrigger.IRecognitionStatusCallback;
-import android.hardware.soundtrigger.ModelParams;
-import android.hardware.soundtrigger.SoundTrigger;
-import android.hardware.soundtrigger.SoundTrigger.Keyphrase;
-import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel;
-import android.hardware.soundtrigger.SoundTrigger.ModelParamRange;
-import android.hardware.soundtrigger.SoundTrigger.ModuleProperties;
-import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig;
-import android.media.permission.Identity;
-import android.os.IBinder;
-
-import com.android.server.voiceinteraction.VoiceInteractionManagerService;
-
-import java.io.FileDescriptor;
-import java.io.PrintWriter;
-import java.util.List;
-
-/**
- * Provides a local service for managing voice-related recoginition models. This is primarily used
- * by the {@link VoiceInteractionManagerService}.
- */
-public interface SoundTriggerInternal {
-    /**
-     * Return codes for {@link Session#startRecognition(int, KeyphraseSoundModel,
-     *      IRecognitionStatusCallback, RecognitionConfig)},
-     * {@link Session#stopRecognition(int, IRecognitionStatusCallback)}
-     */
-    int STATUS_ERROR = SoundTrigger.STATUS_ERROR;
-    int STATUS_OK = SoundTrigger.STATUS_OK;
-
-    // Attach to a specific underlying STModule
-    Session attach(@NonNull IBinder client, ModuleProperties underlyingModule);
-
-    // Enumerate possible STModules to attach to
-    List<ModuleProperties> listModuleProperties(Identity originatorIdentity);
-
-    /**
-     * Dumps service-wide information.
-     */
-    void dump(FileDescriptor fd, PrintWriter pw, String[] args);
-
-    interface Session {
-        /**
-         * Starts recognition for the given keyphraseId.
-         *
-         * @param keyphraseId The identifier of the keyphrase for which
-         *                    the recognition is to be started.
-         * @param soundModel  The sound model to use for recognition.
-         * @param listener    The listener for the recognition events related to the given
-         *                    keyphrase.
-         * @param runInBatterySaverMode flag that indicates whether the recognition should continue
-         *                              after battery saver mode is enabled.
-         * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}.
-         */
-        int startRecognition(int keyphraseId, KeyphraseSoundModel soundModel,
-                IRecognitionStatusCallback listener, RecognitionConfig recognitionConfig,
-                boolean runInBatterySaverMode);
-
-        /**
-         * Stops recognition for the given {@link Keyphrase} if a recognition is
-         * currently active.
-         *
-         * @param keyphraseId The identifier of the keyphrase for which
-         *                    the recognition is to be stopped.
-         * @param listener    The listener for the recognition events related to the given
-         *                    keyphrase.
-         * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}.
-         */
-        int stopRecognition(int keyphraseId, IRecognitionStatusCallback listener);
-
-        ModuleProperties getModuleProperties();
-
-        /**
-         * Set a model specific {@link ModelParams} with the given value. This
-         * parameter will keep its value for the duration the model is loaded regardless of starting
-         * and
-         * stopping recognition. Once the model is unloaded, the value will be lost.
-         * {@link #queryParameter} should be checked first before calling this
-         * method.
-         *
-         * @param keyphraseId The identifier of the keyphrase for which
-         *                    to modify model parameters
-         * @param modelParam  {@link ModelParams}
-         * @param value       Value to set
-         * @return - {@link SoundTrigger#STATUS_OK} in case of success
-         * - {@link SoundTrigger#STATUS_NO_INIT} if the native service cannot be reached
-         * - {@link SoundTrigger#STATUS_BAD_VALUE} invalid input parameter
-         * - {@link SoundTrigger#STATUS_INVALID_OPERATION} if the call is out of sequence or
-         * if API is not supported by HAL
-         */
-        int setParameter(int keyphraseId, @ModelParams int modelParam, int value);
-
-        /**
-         * Get a model specific {@link ModelParams}. This parameter will keep its value
-         * for the duration the model is loaded regardless of starting and stopping recognition.
-         * Once the model is unloaded, the value will be lost. If the value is not set, a default
-         * value is returned. See ModelParams for parameter default values.
-         * {{@link #queryParameter}} should be checked first before calling this
-         * method.
-         *
-         * @param keyphraseId The identifier of the keyphrase for which
-         *                    to modify model parameters
-         * @param modelParam  {@link ModelParams}
-         * @return value of parameter
-         * @throws UnsupportedOperationException if hal or model do not support this API.
-         *                                       queryParameter should be checked first.
-         * @throws IllegalArgumentException      if invalid model handle or parameter is passed.
-         *                                       queryParameter should be checked first.
-         */
-        int getParameter(int keyphraseId, @ModelParams int modelParam);
-
-        /**
-         * Determine if parameter control is supported for the given model handle.
-         * This method should be checked prior to calling {@link #setParameter}
-         * or {@link #getParameter}.
-         *
-         * @param keyphraseId The identifier of the keyphrase for which
-         *                    to modify model parameters
-         * @param modelParam  {@link ModelParams}
-         * @return supported range of parameter, null if not supported
-         */
-        @Nullable
-        ModelParamRange queryParameter(int keyphraseId,
-                @ModelParams int modelParam);
-
-        /**
-         * Unloads (and stops if running) the given keyphraseId
-         */
-        int unloadKeyphraseModel(int keyphaseId);
-
-        /**
-         * Dumps session-wide information.
-         */
-        void dump(FileDescriptor fd, PrintWriter pw, String[] args);
-    }
-}
diff --git a/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerService.java b/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerService.java
index 04c1c04..523e087 100644
--- a/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerService.java
+++ b/services/voiceinteraction/java/com/android/server/soundtrigger/SoundTriggerService.java
@@ -86,6 +86,7 @@
 import com.android.internal.annotations.GuardedBy;
 import com.android.internal.app.ISoundTriggerService;
 import com.android.internal.app.ISoundTriggerSession;
+import com.android.server.SoundTriggerInternal;
 import com.android.server.SystemService;
 import com.android.server.utils.EventLogger;
 
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
index e1da2ca..4a581a0 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
@@ -99,11 +99,11 @@
 import com.android.internal.util.DumpUtils;
 import com.android.server.FgThread;
 import com.android.server.LocalServices;
+import com.android.server.SoundTriggerInternal;
 import com.android.server.SystemService;
 import com.android.server.UiThread;
 import com.android.server.pm.UserManagerInternal;
 import com.android.server.pm.permission.LegacyPermissionManagerInternal;
-import com.android.server.soundtrigger.SoundTriggerInternal;
 import com.android.server.utils.Slogf;
 import com.android.server.utils.TimingsTraceAndSlog;
 import com.android.server.wm.ActivityTaskManagerInternal;