Move sound trigger calls to VoiceInteractionManagerService

- This ensures that any data being loaded on the DSP comes from the framework

Change-Id: Ie15f0994850ba8f298ca07c49fe0b89e066d9e2b
diff --git a/core/java/android/hardware/soundtrigger/SoundTrigger.java b/core/java/android/hardware/soundtrigger/SoundTrigger.java
index 1f48a92..7b0a678 100644
--- a/core/java/android/hardware/soundtrigger/SoundTrigger.java
+++ b/core/java/android/hardware/soundtrigger/SoundTrigger.java
@@ -17,8 +17,11 @@
 package android.hardware.soundtrigger;
 
 import android.os.Handler;
+import android.os.Parcel;
+import android.os.Parcelable;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.UUID;
 
 /**
@@ -43,7 +46,7 @@
      * ID used to target any API call to this paricular module. Module
      * properties are returned by listModules() method.
      ****************************************************************************/
-    public static class ModuleProperties {
+    public static class ModuleProperties implements Parcelable {
         /** Unique module ID provided by the native service */
         public final int id;
 
@@ -102,6 +105,70 @@
             this.supportsConcurrentCapture = supportsConcurrentCapture;
             this.powerConsumptionMw = powerConsumptionMw;
         }
+
+        public static final Parcelable.Creator<ModuleProperties> CREATOR
+                = new Parcelable.Creator<ModuleProperties>() {
+            public ModuleProperties createFromParcel(Parcel in) {
+                return ModuleProperties.fromParcel(in);
+            }
+
+            public ModuleProperties[] newArray(int size) {
+                return new ModuleProperties[size];
+            }
+        };
+
+        private static ModuleProperties fromParcel(Parcel in) {
+            int id = in.readInt();
+            String implementor = in.readString();
+            String description = in.readString();
+            String uuid = in.readString();
+            int version = in.readInt();
+            int maxSoundModels = in.readInt();
+            int maxKeyphrases = in.readInt();
+            int maxUsers = in.readInt();
+            int recognitionModes = in.readInt();
+            boolean supportsCaptureTransition = in.readByte() == 1;
+            int maxBufferMs = in.readInt();
+            boolean supportsConcurrentCapture = in.readByte() == 1;
+            int powerConsumptionMw = in.readInt();
+            return new ModuleProperties(id, implementor, description, uuid, version,
+                    maxSoundModels, maxKeyphrases, maxUsers, recognitionModes,
+                    supportsCaptureTransition, maxBufferMs, supportsConcurrentCapture,
+                    powerConsumptionMw);
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeInt(id);
+            dest.writeString(implementor);
+            dest.writeString(description);
+            dest.writeString(uuid.toString());
+            dest.writeInt(version);
+            dest.writeInt(maxSoundModels);
+            dest.writeInt(maxKeyphrases);
+            dest.writeInt(maxUsers);
+            dest.writeInt(recognitionModes);
+            dest.writeByte((byte) (supportsCaptureTransition ? 1 : 0));
+            dest.writeInt(maxBufferMs);
+            dest.writeByte((byte) (supportsConcurrentCapture ? 1 : 0));
+            dest.writeInt(powerConsumptionMw);
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public String toString() {
+            return "ModuleProperties [id=" + id + ", implementor=" + implementor + ", description="
+                    + description + ", uuid=" + uuid + ", version=" + version + ", maxSoundModels="
+                    + maxSoundModels + ", maxKeyphrases=" + maxKeyphrases + ", maxUsers="
+                    + maxUsers + ", recognitionModes=" + recognitionModes
+                    + ", supportsCaptureTransition=" + supportsCaptureTransition + ", maxBufferMs="
+                    + maxBufferMs + ", supportsConcurrentCapture=" + supportsConcurrentCapture
+                    + ", powerConsumptionMw=" + powerConsumptionMw + "]";
+        }
     }
 
     /*****************************************************************************
@@ -137,7 +204,7 @@
      * A Keyphrase describes a key phrase that can be detected by a
      * {@link KeyphraseSoundModel}
      ****************************************************************************/
-    public static class Keyphrase {
+    public static class Keyphrase implements Parcelable {
         /** Unique identifier for this keyphrase */
         public final int id;
 
@@ -161,6 +228,96 @@
             this.text = text;
             this.users = users;
         }
+
+        public static final Parcelable.Creator<Keyphrase> CREATOR
+                = new Parcelable.Creator<Keyphrase>() {
+            public Keyphrase createFromParcel(Parcel in) {
+                return Keyphrase.fromParcel(in);
+            }
+
+            public Keyphrase[] newArray(int size) {
+                return new Keyphrase[size];
+            }
+        };
+
+        private static Keyphrase fromParcel(Parcel in) {
+            int id = in.readInt();
+            int recognitionModes = in.readInt();
+            String locale = in.readString();
+            String text = in.readString();
+            int[] users = null;
+            int numUsers = in.readInt();
+            if (numUsers > 0) {
+                users = new int[numUsers];
+                in.readIntArray(users);
+            }
+            return new Keyphrase(id, recognitionModes, locale, text, users);
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeInt(id);
+            dest.writeInt(recognitionModes);
+            dest.writeString(locale);
+            dest.writeString(text);
+            if (users != null) {
+                dest.writeInt(users.length);
+                dest.writeIntArray(users);
+            } else {
+                dest.writeInt(0);
+            }
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + ((text == null) ? 0 : text.hashCode());
+            result = prime * result + id;
+            result = prime * result + ((locale == null) ? 0 : locale.hashCode());
+            result = prime * result + recognitionModes;
+            result = prime * result + Arrays.hashCode(users);
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (obj == null)
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            Keyphrase other = (Keyphrase) obj;
+            if (text == null) {
+                if (other.text != null)
+                    return false;
+            } else if (!text.equals(other.text))
+                return false;
+            if (id != other.id)
+                return false;
+            if (locale == null) {
+                if (other.locale != null)
+                    return false;
+            } else if (!locale.equals(other.locale))
+                return false;
+            if (recognitionModes != other.recognitionModes)
+                return false;
+            if (!Arrays.equals(users, other.users))
+                return false;
+            return true;
+        }
+
+        @Override
+        public String toString() {
+            return "Keyphrase [id=" + id + ", recognitionModes=" + recognitionModes + ", locale="
+                    + locale + ", text=" + text + ", users=" + Arrays.toString(users) + "]";
+        }
     }
 
     /*****************************************************************************
@@ -168,7 +325,7 @@
      * It contains data needed by the hardware to detect a certain number of key phrases
      * and the list of corresponding {@link Keyphrase} descriptors.
      ****************************************************************************/
-    public static class KeyphraseSoundModel extends SoundModel {
+    public static class KeyphraseSoundModel extends SoundModel implements Parcelable {
         /** Key phrases in this sound model */
         public final Keyphrase[] keyphrases; // keyword phrases in model
 
@@ -176,6 +333,46 @@
             super(id, TYPE_KEYPHRASE, data);
             this.keyphrases = keyphrases;
         }
+
+        public static final Parcelable.Creator<KeyphraseSoundModel> CREATOR
+                = new Parcelable.Creator<KeyphraseSoundModel>() {
+            public KeyphraseSoundModel createFromParcel(Parcel in) {
+                return KeyphraseSoundModel.fromParcel(in);
+            }
+
+            public KeyphraseSoundModel[] newArray(int size) {
+                return new KeyphraseSoundModel[size];
+            }
+        };
+
+        private static KeyphraseSoundModel fromParcel(Parcel in) {
+            UUID uuid = UUID.fromString(in.readString());
+            byte[] data = null;
+            int dataLength = in.readInt();
+            if (dataLength > 0) {
+                data = new byte[dataLength];
+                in.readByteArray(data);
+            }
+            Keyphrase[] keyphrases = in.createTypedArray(Keyphrase.CREATOR);
+            return new KeyphraseSoundModel(uuid, data, keyphrases);
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeString(uuid.toString());
+            if (data != null) {
+                dest.writeInt(data.length);
+                dest.writeByteArray(data);
+            } else {
+                dest.writeInt(0);
+            }
+            dest.writeTypedArray(keyphrases, 0);
+        }
     }
 
     /**
@@ -239,7 +436,7 @@
      *  {@link SoundTriggerModule#startRecognition(int, RecognitionConfig)} to configure the
      *  recognition request.
      */
-    public static class RecognitionConfig {
+    public static class RecognitionConfig implements Parcelable {
         /** True if the DSP should capture the trigger sound and make it available for further
          * capture. */
         public final boolean captureRequested;
@@ -256,6 +453,47 @@
             this.keyphrases = keyphrases;
             this.data = data;
         }
+
+        public static final Parcelable.Creator<RecognitionConfig> CREATOR
+                = new Parcelable.Creator<RecognitionConfig>() {
+            public RecognitionConfig createFromParcel(Parcel in) {
+                return RecognitionConfig.fromParcel(in);
+            }
+
+            public RecognitionConfig[] newArray(int size) {
+                return new RecognitionConfig[size];
+            }
+        };
+
+        private static RecognitionConfig fromParcel(Parcel in) {
+            boolean captureRequested = in.readByte() == 1;
+            KeyphraseRecognitionExtra[] keyphrases =
+                    in.createTypedArray(KeyphraseRecognitionExtra.CREATOR);
+            byte[] data = null;
+            int dataLength = in.readInt();
+            if (dataLength > 0) {
+                data = new byte[dataLength];
+                in.readByteArray(data);
+            }
+            return new RecognitionConfig(captureRequested, keyphrases, data);
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeByte((byte) (captureRequested ? 1 : 0));
+            dest.writeTypedArray(keyphrases, 0);
+            if (data != null) {
+                dest.writeInt(data.length);
+                dest.writeByteArray(data);
+            } else {
+                dest.writeInt(0);
+            }
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
     }
 
     /**
@@ -266,7 +504,7 @@
      * should trigger a recognition.
      * - The user ID is derived from the system ID {@link android.os.UserHandle#getIdentifier()}.
      */
-    public static class ConfidenceLevel {
+    public static class ConfidenceLevel implements Parcelable {
         public final int userId;
         public final int confidenceLevel;
 
@@ -274,14 +512,42 @@
             this.userId = userId;
             this.confidenceLevel = confidenceLevel;
         }
+
+        public static final Parcelable.Creator<ConfidenceLevel> CREATOR
+                = new Parcelable.Creator<ConfidenceLevel>() {
+            public ConfidenceLevel createFromParcel(Parcel in) {
+                return ConfidenceLevel.fromParcel(in);
+            }
+
+            public ConfidenceLevel[] newArray(int size) {
+                return new ConfidenceLevel[size];
+            }
+        };
+
+        private static ConfidenceLevel fromParcel(Parcel in) {
+            int userId = in.readInt();
+            int confidenceLevel = in.readInt();
+            return new ConfidenceLevel(userId, confidenceLevel);
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeInt(userId);
+            dest.writeInt(confidenceLevel);
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
     }
 
     /**
      *  Additional data conveyed by a {@link KeyphraseRecognitionEvent}
      *  for a key phrase detection.
      */
-    public static class KeyphraseRecognitionExtra {
-        /** The keyphrse ID */
+    public static class KeyphraseRecognitionExtra implements Parcelable {
+        /** The keyphrase ID */
         public final int id;
 
         /** Recognition modes matched for this event */
@@ -297,6 +563,36 @@
             this.recognitionModes = recognitionModes;
             this.confidenceLevels = confidenceLevels;
         }
+
+        public static final Parcelable.Creator<KeyphraseRecognitionExtra> CREATOR
+                = new Parcelable.Creator<KeyphraseRecognitionExtra>() {
+            public KeyphraseRecognitionExtra createFromParcel(Parcel in) {
+                return KeyphraseRecognitionExtra.fromParcel(in);
+            }
+
+            public KeyphraseRecognitionExtra[] newArray(int size) {
+                return new KeyphraseRecognitionExtra[size];
+            }
+        };
+
+        private static KeyphraseRecognitionExtra fromParcel(Parcel in) {
+            int id = in.readInt();
+            int recognitionModes = in.readInt();
+            ConfidenceLevel[] confidenceLevels = in.createTypedArray(ConfidenceLevel.CREATOR);
+            return new KeyphraseRecognitionExtra(id, recognitionModes, confidenceLevels);
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeInt(id);
+            dest.writeInt(recognitionModes);
+            dest.writeTypedArray(confidenceLevels, 0);
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
     }
 
     /**