Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 17 | package com.android.server.soundtrigger; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 18 | |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 19 | import android.content.BroadcastReceiver; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.IntentFilter; |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 23 | import android.hardware.soundtrigger.IRecognitionStatusCallback; |
| 24 | import android.hardware.soundtrigger.SoundTrigger; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 25 | import android.hardware.soundtrigger.SoundTrigger.GenericRecognitionEvent; |
| 26 | import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel; |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 27 | import android.hardware.soundtrigger.SoundTrigger.Keyphrase; |
Sandeep Siddhartha | 6817337 | 2014-07-28 13:25:30 -0700 | [diff] [blame] | 28 | import android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionEvent; |
| 29 | import android.hardware.soundtrigger.SoundTrigger.KeyphraseRecognitionExtra; |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 30 | import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 31 | import android.hardware.soundtrigger.SoundTrigger.ModuleProperties; |
Sandeep Siddhartha | 8ecaf5f | 2014-07-10 19:38:18 -0700 | [diff] [blame] | 32 | import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 33 | import android.hardware.soundtrigger.SoundTrigger.RecognitionEvent; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 34 | import android.hardware.soundtrigger.SoundTrigger.SoundModel; |
Eric Laurent | d3b8223 | 2014-07-30 08:57:39 -0700 | [diff] [blame] | 35 | import android.hardware.soundtrigger.SoundTrigger.SoundModelEvent; |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 36 | import android.hardware.soundtrigger.SoundTriggerModule; |
Hall Liu | 5fb337f | 2017-11-22 17:38:15 -0800 | [diff] [blame] | 37 | import android.os.Binder; |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 38 | import android.os.DeadObjectException; |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 39 | import android.os.PowerManager; |
Makoto Onuki | 2eccd02 | 2017-11-01 13:44:23 -0700 | [diff] [blame] | 40 | import android.os.PowerManager.ServiceType; |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 42 | import android.telephony.PhoneStateListener; |
| 43 | import android.telephony.TelephonyManager; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 44 | import android.util.Slog; |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 45 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 46 | import com.android.internal.logging.MetricsLogger; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 47 | |
Sandeep Siddhartha | 6b8556d | 2014-08-06 19:47:25 -0700 | [diff] [blame] | 48 | import java.io.FileDescriptor; |
| 49 | import java.io.PrintWriter; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 50 | import java.util.ArrayList; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 51 | import java.util.HashMap; |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 52 | import java.util.Iterator; |
| 53 | import java.util.Map; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 54 | import java.util.UUID; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 57 | * Helper for {@link SoundTrigger} APIs. Supports two types of models: |
| 58 | * (i) A voice model which is exported via the {@link VoiceInteractionService}. There can only be |
| 59 | * a single voice model running on the DSP at any given time. |
| 60 | * |
| 61 | * (ii) Generic sound-trigger models: Supports multiple of these. |
| 62 | * |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 63 | * Currently this just acts as an abstraction over all SoundTrigger API calls. |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 64 | * @hide |
| 65 | */ |
| 66 | public class SoundTriggerHelper implements SoundTrigger.StatusListener { |
| 67 | static final String TAG = "SoundTriggerHelper"; |
Sandeep Siddhartha | 8cf8f71 | 2014-09-15 12:51:08 -0700 | [diff] [blame] | 68 | static final boolean DBG = false; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 69 | |
| 70 | /** |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 71 | * Return codes for {@link #startRecognition(int, KeyphraseSoundModel, |
| 72 | * IRecognitionStatusCallback, RecognitionConfig)}, |
| 73 | * {@link #stopRecognition(int, IRecognitionStatusCallback)} |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 74 | */ |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 75 | public static final int STATUS_ERROR = SoundTrigger.STATUS_ERROR; |
| 76 | public static final int STATUS_OK = SoundTrigger.STATUS_OK; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 77 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 78 | private static final int INVALID_VALUE = Integer.MIN_VALUE; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 79 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 80 | /** The {@link ModuleProperties} for the system, or null if none exists. */ |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 81 | final ModuleProperties mModuleProperties; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 82 | |
| 83 | /** The properties for the DSP module */ |
Eric Laurent | 2475e38 | 2014-09-09 15:52:25 -0700 | [diff] [blame] | 84 | private SoundTriggerModule mModule; |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 85 | private final Object mLock = new Object(); |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 86 | private final Context mContext; |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 87 | private final TelephonyManager mTelephonyManager; |
| 88 | private final PhoneStateListener mPhoneStateListener; |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 89 | private final PowerManager mPowerManager; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 90 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 91 | // The SoundTriggerManager layer handles multiple recognition models of type generic and |
| 92 | // keyphrase. We store the ModelData here in a hashmap. |
| 93 | private final HashMap<UUID, ModelData> mModelDataMap; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 94 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 95 | // An index of keyphrase sound models so that we can reach them easily. We support indexing |
| 96 | // keyphrase sound models with a keyphrase ID. Sound model with the same keyphrase ID will |
| 97 | // replace an existing model, thus there is a 1:1 mapping from keyphrase ID to a voice |
| 98 | // sound model. |
| 99 | private HashMap<Integer, UUID> mKeyphraseUuidMap; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 100 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 101 | private boolean mCallActive = false; |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 102 | private boolean mIsPowerSaveMode = false; |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 103 | // Indicates if the native sound trigger service is disabled or not. |
| 104 | // This is an indirect indication of the microphone being open in some other application. |
| 105 | private boolean mServiceDisabled = false; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 106 | |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 107 | // Whether ANY recognition (keyphrase or generic) has been requested. |
| 108 | private boolean mRecognitionRequested = false; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 109 | |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 110 | private PowerSaveModeListener mPowerSaveModeListener; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 111 | |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 112 | SoundTriggerHelper(Context context) { |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 113 | ArrayList <ModuleProperties> modules = new ArrayList<>(); |
| 114 | int status = SoundTrigger.listModules(modules); |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 115 | mContext = context; |
| 116 | mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); |
| 117 | mPowerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 118 | mModelDataMap = new HashMap<UUID, ModelData>(); |
| 119 | mKeyphraseUuidMap = new HashMap<Integer, UUID>(); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 120 | mPhoneStateListener = new MyCallStateListener(); |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 121 | if (status != SoundTrigger.STATUS_OK || modules.size() == 0) { |
Sandeep Siddhartha | a433af6 | 2014-07-16 15:36:44 -0700 | [diff] [blame] | 122 | Slog.w(TAG, "listModules status=" + status + ", # of modules=" + modules.size()); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 123 | mModuleProperties = null; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 124 | mModule = null; |
| 125 | } else { |
| 126 | // TODO: Figure out how to determine which module corresponds to the DSP hardware. |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 127 | mModuleProperties = modules.get(0); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 132 | * Starts recognition for the given generic sound model ID. This is a wrapper around {@link |
| 133 | * startRecognition()}. |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 134 | * |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 135 | * @param modelId UUID of the sound model. |
| 136 | * @param soundModel The generic sound model to use for recognition. |
| 137 | * @param callback Callack for the recognition events related to the given keyphrase. |
| 138 | * @param recognitionConfig Instance of RecognitionConfig containing the parameters for the |
| 139 | * recognition. |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 140 | * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}. |
| 141 | */ |
| 142 | int startGenericRecognition(UUID modelId, GenericSoundModel soundModel, |
| 143 | IRecognitionStatusCallback callback, RecognitionConfig recognitionConfig) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 144 | MetricsLogger.count(mContext, "sth_start_recognition", 1); |
| 145 | if (modelId == null || soundModel == null || callback == null || |
| 146 | recognitionConfig == null) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 147 | Slog.w(TAG, "Passed in bad data to startGenericRecognition()."); |
| 148 | return STATUS_ERROR; |
| 149 | } |
| 150 | |
| 151 | synchronized (mLock) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 152 | ModelData modelData = getOrCreateGenericModelDataLocked(modelId); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 153 | if (modelData == null) { |
| 154 | Slog.w(TAG, "Irrecoverable error occurred, check UUID / sound model data."); |
| 155 | return STATUS_ERROR; |
| 156 | } |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 157 | return startRecognition(soundModel, modelData, callback, recognitionConfig, |
| 158 | INVALID_VALUE /* keyphraseId */); |
| 159 | } |
| 160 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 161 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 162 | /** |
| 163 | * Starts recognition for the given keyphraseId. |
| 164 | * |
| 165 | * @param keyphraseId The identifier of the keyphrase for which |
| 166 | * the recognition is to be started. |
| 167 | * @param soundModel The sound model to use for recognition. |
| 168 | * @param callback The callback for the recognition events related to the given keyphrase. |
| 169 | * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}. |
| 170 | */ |
| 171 | int startKeyphraseRecognition(int keyphraseId, KeyphraseSoundModel soundModel, |
| 172 | IRecognitionStatusCallback callback, RecognitionConfig recognitionConfig) { |
| 173 | synchronized (mLock) { |
| 174 | MetricsLogger.count(mContext, "sth_start_recognition", 1); |
| 175 | if (soundModel == null || callback == null || recognitionConfig == null) { |
| 176 | return STATUS_ERROR; |
| 177 | } |
| 178 | |
| 179 | if (DBG) { |
| 180 | Slog.d(TAG, "startKeyphraseRecognition for keyphraseId=" + keyphraseId |
| 181 | + " soundModel=" + soundModel + ", callback=" + callback.asBinder() |
| 182 | + ", recognitionConfig=" + recognitionConfig); |
| 183 | Slog.d(TAG, "moduleProperties=" + mModuleProperties); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 184 | dumpModelStateLocked(); |
| 185 | } |
| 186 | |
| 187 | ModelData model = getKeyphraseModelDataLocked(keyphraseId); |
| 188 | if (model != null && !model.isKeyphraseModel()) { |
| 189 | Slog.e(TAG, "Generic model with same UUID exists."); |
| 190 | return STATUS_ERROR; |
| 191 | } |
| 192 | |
| 193 | // Process existing model first. |
Chris Thornton | 41e04a4 | 2016-04-18 16:51:08 -0700 | [diff] [blame] | 194 | if (model != null && !model.getModelId().equals(soundModel.uuid)) { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 195 | // The existing model has a different UUID, should be replaced. |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 196 | int status = cleanUpExistingKeyphraseModelLocked(model); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 197 | if (status != STATUS_OK) { |
| 198 | return status; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 199 | } |
Chris Thornton | 41e04a4 | 2016-04-18 16:51:08 -0700 | [diff] [blame] | 200 | removeKeyphraseModelLocked(keyphraseId); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 201 | model = null; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 202 | } |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 203 | |
| 204 | // We need to create a new one: either no previous models existed for given keyphrase id |
| 205 | // or the existing model had a different UUID and was cleaned up. |
| 206 | if (model == null) { |
| 207 | model = createKeyphraseModelDataLocked(soundModel.uuid, keyphraseId); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 208 | } |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 209 | |
| 210 | return startRecognition(soundModel, model, callback, recognitionConfig, |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 211 | keyphraseId); |
| 212 | } |
| 213 | } |
| 214 | |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 215 | private int cleanUpExistingKeyphraseModelLocked(ModelData modelData) { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 216 | // Stop and clean up a previous ModelData if one exists. This usually is used when the |
| 217 | // previous model has a different UUID for the same keyphrase ID. |
| 218 | int status = tryStopAndUnloadLocked(modelData, true /* stop */, true /* unload */); |
| 219 | if (status != STATUS_OK) { |
| 220 | Slog.w(TAG, "Unable to stop or unload previous model: " + |
| 221 | modelData.toString()); |
| 222 | } |
| 223 | return status; |
| 224 | } |
| 225 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 226 | /** |
| 227 | * Starts recognition for the given sound model. A single routine for both keyphrase and |
| 228 | * generic sound models. |
| 229 | * |
| 230 | * @param soundModel The sound model to use for recognition. |
| 231 | * @param modelData Instance of {@link #ModelData} for the given model. |
| 232 | * @param callback Callback for the recognition events related to the given keyphrase. |
| 233 | * @param recognitionConfig Instance of {@link RecognitionConfig} containing the parameters |
| 234 | * @param keyphraseId Keyphrase ID for keyphrase models only. Pass in INVALID_VALUE for other |
| 235 | * models. |
| 236 | * for the recognition. |
| 237 | * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}. |
| 238 | */ |
| 239 | int startRecognition(SoundModel soundModel, ModelData modelData, |
| 240 | IRecognitionStatusCallback callback, RecognitionConfig recognitionConfig, |
| 241 | int keyphraseId) { |
| 242 | synchronized (mLock) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 243 | if (mModuleProperties == null) { |
| 244 | Slog.w(TAG, "Attempting startRecognition without the capability"); |
| 245 | return STATUS_ERROR; |
| 246 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 247 | if (mModule == null) { |
| 248 | mModule = SoundTrigger.attachModule(mModuleProperties.id, this, null); |
| 249 | if (mModule == null) { |
| 250 | Slog.w(TAG, "startRecognition cannot attach to sound trigger module"); |
| 251 | return STATUS_ERROR; |
| 252 | } |
| 253 | } |
| 254 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 255 | // If the existing SoundModel is different (for the same UUID for Generic and same |
| 256 | // keyphrase ID for voice), ensure that it is unloaded and stopped before proceeding. |
| 257 | // This works for both keyphrase and generic models. This logic also ensures that a |
| 258 | // previously loaded (or started) model is appropriately stopped. Since this is a |
| 259 | // generalization of the previous logic with a single keyphrase model, we should have |
| 260 | // no regression with the previous version of this code as was given in the |
| 261 | // startKeyphrase() routine. |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 262 | if (modelData.getSoundModel() != null) { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 263 | boolean stopModel = false; // Stop the model after checking that it is started. |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 264 | boolean unloadModel = false; |
| 265 | if (modelData.getSoundModel().equals(soundModel) && modelData.isModelStarted()) { |
| 266 | // The model has not changed, but the previous model is "started". |
| 267 | // Stop the previously running model. |
| 268 | stopModel = true; |
| 269 | unloadModel = false; // No need to unload if the model hasn't changed. |
| 270 | } else if (!modelData.getSoundModel().equals(soundModel)) { |
| 271 | // We have a different model for this UUID. Stop and unload if needed. This |
| 272 | // helps maintain the singleton restriction for keyphrase sound models. |
| 273 | stopModel = modelData.isModelStarted(); |
| 274 | unloadModel = modelData.isModelLoaded(); |
| 275 | } |
| 276 | if (stopModel || unloadModel) { |
| 277 | int status = tryStopAndUnloadLocked(modelData, stopModel, unloadModel); |
| 278 | if (status != STATUS_OK) { |
| 279 | Slog.w(TAG, "Unable to stop or unload previous model: " + |
| 280 | modelData.toString()); |
| 281 | return status; |
| 282 | } |
| 283 | } |
| 284 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 285 | |
| 286 | IRecognitionStatusCallback oldCallback = modelData.getCallback(); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 287 | if (oldCallback != null && oldCallback.asBinder() != callback.asBinder()) { |
| 288 | Slog.w(TAG, "Canceling previous recognition for model id: " + |
| 289 | modelData.getModelId()); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 290 | try { |
| 291 | oldCallback.onError(STATUS_ERROR); |
| 292 | } catch (RemoteException e) { |
| 293 | Slog.w(TAG, "RemoteException in onDetectionStopped", e); |
| 294 | } |
| 295 | modelData.clearCallback(); |
| 296 | } |
| 297 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 298 | // Load the model if it is not loaded. |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 299 | if (!modelData.isModelLoaded()) { |
Chris Thornton | a533b99 | 2017-03-24 17:30:29 -0700 | [diff] [blame] | 300 | // Before we try and load this model, we should first make sure that any other |
| 301 | // models that don't have an active recognition/dead callback are unloaded. Since |
| 302 | // there is a finite limit on the number of models that the hardware may be able to |
| 303 | // have loaded, we want to make sure there's room for our model. |
| 304 | stopAndUnloadDeadModelsLocked(); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 305 | int[] handle = new int[] { INVALID_VALUE }; |
| 306 | int status = mModule.loadSoundModel(soundModel, handle); |
| 307 | if (status != SoundTrigger.STATUS_OK) { |
| 308 | Slog.w(TAG, "loadSoundModel call failed with " + status); |
| 309 | return status; |
| 310 | } |
| 311 | if (handle[0] == INVALID_VALUE) { |
| 312 | Slog.w(TAG, "loadSoundModel call returned invalid sound model handle"); |
| 313 | return STATUS_ERROR; |
| 314 | } |
| 315 | modelData.setHandle(handle[0]); |
Arunesh Mishra | 933da81 | 2016-02-18 15:31:54 -0800 | [diff] [blame] | 316 | modelData.setLoaded(); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 317 | Slog.d(TAG, "Sound model loaded with handle:" + handle[0]); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 318 | } |
| 319 | modelData.setCallback(callback); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 320 | modelData.setRequested(true); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 321 | modelData.setRecognitionConfig(recognitionConfig); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 322 | modelData.setSoundModel(soundModel); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 323 | |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 324 | int status = startRecognitionLocked(modelData, |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 325 | false /* Don't notify for synchronous calls */); |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 326 | |
| 327 | // Initialize power save, call active state monitoring logic. |
| 328 | if (status == STATUS_OK && !mRecognitionRequested) { |
| 329 | initializeTelephonyAndPowerStateListeners(); |
| 330 | mRecognitionRequested = true; |
| 331 | } |
| 332 | |
| 333 | return status; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 338 | * Stops recognition for the given generic sound model. This is a wrapper for {@link |
| 339 | * #stopRecognition}. |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 340 | * |
| 341 | * @param modelId The identifier of the generic sound model for which |
| 342 | * the recognition is to be stopped. |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 343 | * @param callback The callback for the recognition events related to the given sound model. |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 344 | * |
| 345 | * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}. |
| 346 | */ |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 347 | int stopGenericRecognition(UUID modelId, IRecognitionStatusCallback callback) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 348 | synchronized (mLock) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 349 | MetricsLogger.count(mContext, "sth_stop_recognition", 1); |
| 350 | if (callback == null || modelId == null) { |
| 351 | Slog.e(TAG, "Null callbackreceived for stopGenericRecognition() for modelid:" + |
| 352 | modelId); |
| 353 | return STATUS_ERROR; |
| 354 | } |
| 355 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 356 | ModelData modelData = mModelDataMap.get(modelId); |
| 357 | if (modelData == null || !modelData.isGenericModel()) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 358 | Slog.w(TAG, "Attempting stopRecognition on invalid model with id:" + modelId); |
| 359 | return STATUS_ERROR; |
| 360 | } |
| 361 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 362 | int status = stopRecognition(modelData, callback); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 363 | if (status != SoundTrigger.STATUS_OK) { |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 364 | Slog.w(TAG, "stopGenericRecognition failed: " + status); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 365 | } |
| 366 | return status; |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | /** |
Sandeep Siddhartha | 0558972 | 2014-07-17 16:21:54 -0700 | [diff] [blame] | 371 | * Stops recognition for the given {@link Keyphrase} if a recognition is |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 372 | * currently active. This is a wrapper for {@link #stopRecognition()}. |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 373 | * |
Sandeep Siddhartha | 8ecaf5f | 2014-07-10 19:38:18 -0700 | [diff] [blame] | 374 | * @param keyphraseId The identifier of the keyphrase for which |
| 375 | * the recognition is to be stopped. |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 376 | * @param callback The callback for the recognition events related to the given keyphrase. |
Sandeep Siddhartha | 8ecaf5f | 2014-07-10 19:38:18 -0700 | [diff] [blame] | 377 | * |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 378 | * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}. |
| 379 | */ |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 380 | int stopKeyphraseRecognition(int keyphraseId, IRecognitionStatusCallback callback) { |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 381 | synchronized (mLock) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 382 | MetricsLogger.count(mContext, "sth_stop_recognition", 1); |
| 383 | if (callback == null) { |
| 384 | Slog.e(TAG, "Null callback received for stopKeyphraseRecognition() for keyphraseId:" + |
| 385 | keyphraseId); |
| 386 | return STATUS_ERROR; |
| 387 | } |
| 388 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 389 | ModelData modelData = getKeyphraseModelDataLocked(keyphraseId); |
| 390 | if (modelData == null || !modelData.isKeyphraseModel()) { |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 391 | Slog.e(TAG, "No model exists for given keyphrase Id " + keyphraseId); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 392 | return STATUS_ERROR; |
| 393 | } |
| 394 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 395 | if (DBG) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 396 | Slog.d(TAG, "stopRecognition for keyphraseId=" + keyphraseId + ", callback =" + |
| 397 | callback.asBinder()); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 398 | Slog.d(TAG, "current callback=" + (modelData == null ? "null" : |
| 399 | modelData.getCallback().asBinder())); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 400 | } |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 401 | int status = stopRecognition(modelData, callback); |
Sandeep Siddhartha | 8ecaf5f | 2014-07-10 19:38:18 -0700 | [diff] [blame] | 402 | if (status != SoundTrigger.STATUS_OK) { |
Sandeep Siddhartha | 2c0273e | 2014-08-01 11:32:03 -0700 | [diff] [blame] | 403 | return status; |
Sandeep Siddhartha | 8ecaf5f | 2014-07-10 19:38:18 -0700 | [diff] [blame] | 404 | } |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 405 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 406 | return status; |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 410 | /** |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 411 | * Stops recognition for the given ModelData instance. |
| 412 | * |
| 413 | * @param modelData Instance of {@link #ModelData} sound model. |
| 414 | * @param callback The callback for the recognition events related to the given keyphrase. |
| 415 | * @return One of {@link #STATUS_ERROR} or {@link #STATUS_OK}. |
| 416 | */ |
| 417 | private int stopRecognition(ModelData modelData, IRecognitionStatusCallback callback) { |
| 418 | synchronized (mLock) { |
| 419 | if (callback == null) { |
| 420 | return STATUS_ERROR; |
| 421 | } |
| 422 | if (mModuleProperties == null || mModule == null) { |
| 423 | Slog.w(TAG, "Attempting stopRecognition without the capability"); |
| 424 | return STATUS_ERROR; |
| 425 | } |
| 426 | |
| 427 | IRecognitionStatusCallback currentCallback = modelData.getCallback(); |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 428 | if (modelData == null || currentCallback == null || |
| 429 | (!modelData.isRequested() && !modelData.isModelStarted())) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 430 | // startGenericRecognition hasn't been called or it failed. |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 431 | Slog.w(TAG, "Attempting stopRecognition without a successful startRecognition"); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 432 | return STATUS_ERROR; |
| 433 | } |
| 434 | |
| 435 | if (currentCallback.asBinder() != callback.asBinder()) { |
| 436 | // We don't allow a different listener to stop the recognition than the one |
| 437 | // that started it. |
| 438 | Slog.w(TAG, "Attempting stopRecognition for another recognition"); |
| 439 | return STATUS_ERROR; |
| 440 | } |
| 441 | |
| 442 | // Request stop recognition via the update() method. |
| 443 | modelData.setRequested(false); |
| 444 | int status = updateRecognitionLocked(modelData, isRecognitionAllowed(), |
| 445 | false /* don't notify for synchronous calls */); |
| 446 | if (status != SoundTrigger.STATUS_OK) { |
| 447 | return status; |
| 448 | } |
| 449 | |
| 450 | // We leave the sound model loaded but not started, this helps us when we start back. |
| 451 | // Also clear the internal state once the recognition has been stopped. |
| 452 | modelData.setLoaded(); |
| 453 | modelData.clearCallback(); |
| 454 | modelData.setRecognitionConfig(null); |
| 455 | |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 456 | if (!computeRecognitionRequestedLocked()) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 457 | internalClearGlobalStateLocked(); |
| 458 | } |
| 459 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 460 | return status; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | // Stop a previously started model if it was started. Optionally, unload if the previous model |
| 465 | // is stale and is about to be replaced. |
| 466 | // Needs to be called with the mLock held. |
| 467 | private int tryStopAndUnloadLocked(ModelData modelData, boolean stopModel, |
| 468 | boolean unloadModel) { |
| 469 | int status = STATUS_OK; |
| 470 | if (modelData.isModelNotLoaded()) { |
| 471 | return status; |
| 472 | } |
| 473 | if (stopModel && modelData.isModelStarted()) { |
| 474 | status = stopRecognitionLocked(modelData, |
| 475 | false /* don't notify for synchronous calls */); |
| 476 | if (status != SoundTrigger.STATUS_OK) { |
| 477 | Slog.w(TAG, "stopRecognition failed: " + status); |
| 478 | return status; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | if (unloadModel && modelData.isModelLoaded()) { |
| 483 | Slog.d(TAG, "Unloading previously loaded stale model."); |
| 484 | status = mModule.unloadSoundModel(modelData.getHandle()); |
| 485 | MetricsLogger.count(mContext, "sth_unloading_stale_model", 1); |
| 486 | if (status != SoundTrigger.STATUS_OK) { |
| 487 | Slog.w(TAG, "unloadSoundModel call failed with " + status); |
| 488 | } else { |
| 489 | // Clear the ModelData state if successful. |
| 490 | modelData.clearState(); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | return status; |
| 494 | } |
| 495 | |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 496 | public ModuleProperties getModuleProperties() { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 497 | return mModuleProperties; |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 498 | } |
| 499 | |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 500 | int unloadKeyphraseSoundModel(int keyphraseId) { |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 501 | synchronized (mLock) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 502 | MetricsLogger.count(mContext, "sth_unload_keyphrase_sound_model", 1); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 503 | ModelData modelData = getKeyphraseModelDataLocked(keyphraseId); |
| 504 | if (mModule == null || modelData == null || modelData.getHandle() == INVALID_VALUE || |
| 505 | !modelData.isKeyphraseModel()) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 506 | return STATUS_ERROR; |
| 507 | } |
| 508 | |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 509 | // Stop recognition if it's the current one. |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 510 | modelData.setRequested(false); |
| 511 | int status = updateRecognitionLocked(modelData, isRecognitionAllowed(), |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 512 | false /* don't notify */); |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 513 | if (status != SoundTrigger.STATUS_OK) { |
| 514 | Slog.w(TAG, "Stop recognition failed for keyphrase ID:" + status); |
| 515 | } |
| 516 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 517 | status = mModule.unloadSoundModel(modelData.getHandle()); |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 518 | if (status != SoundTrigger.STATUS_OK) { |
| 519 | Slog.w(TAG, "unloadKeyphraseSoundModel call failed with " + status); |
| 520 | } |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 521 | |
| 522 | // Remove it from existence. |
| 523 | removeKeyphraseModelLocked(keyphraseId); |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 524 | return status; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | int unloadGenericSoundModel(UUID modelId) { |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 529 | synchronized (mLock) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 530 | MetricsLogger.count(mContext, "sth_unload_generic_sound_model", 1); |
| 531 | if (modelId == null || mModule == null) { |
| 532 | return STATUS_ERROR; |
| 533 | } |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 534 | ModelData modelData = mModelDataMap.get(modelId); |
| 535 | if (modelData == null || !modelData.isGenericModel()) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 536 | Slog.w(TAG, "Unload error: Attempting unload invalid generic model with id:" + |
| 537 | modelId); |
Ryan Bavetta | 79655eb | 2016-03-07 14:34:51 -0800 | [diff] [blame] | 538 | return STATUS_ERROR; |
| 539 | } |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 540 | if (!modelData.isModelLoaded()) { |
| 541 | // Nothing to do here. |
| 542 | Slog.i(TAG, "Unload: Given generic model is not loaded:" + modelId); |
| 543 | return STATUS_OK; |
| 544 | } |
| 545 | if (modelData.isModelStarted()) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 546 | int status = stopRecognitionLocked(modelData, |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 547 | false /* don't notify for synchronous calls */); |
| 548 | if (status != SoundTrigger.STATUS_OK) { |
| 549 | Slog.w(TAG, "stopGenericRecognition failed: " + status); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | int status = mModule.unloadSoundModel(modelData.getHandle()); |
| 554 | if (status != SoundTrigger.STATUS_OK) { |
| 555 | Slog.w(TAG, "unloadGenericSoundModel() call failed with " + status); |
| 556 | Slog.w(TAG, "unloadGenericSoundModel() force-marking model as unloaded."); |
| 557 | } |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 558 | |
| 559 | // Remove it from existence. |
| 560 | mModelDataMap.remove(modelId); |
| 561 | if (DBG) dumpModelStateLocked(); |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 562 | return status; |
| 563 | } |
| 564 | } |
| 565 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 566 | boolean isRecognitionRequested(UUID modelId) { |
| 567 | synchronized (mLock) { |
| 568 | ModelData modelData = mModelDataMap.get(modelId); |
| 569 | return modelData != null && modelData.isRequested(); |
| 570 | } |
| 571 | } |
| 572 | |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 573 | int getGenericModelState(UUID modelId) { |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 574 | synchronized (mLock) { |
| 575 | MetricsLogger.count(mContext, "sth_get_generic_model_state", 1); |
| 576 | if (modelId == null || mModule == null) { |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 577 | return STATUS_ERROR; |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 578 | } |
| 579 | ModelData modelData = mModelDataMap.get(modelId); |
| 580 | if (modelData == null || !modelData.isGenericModel()) { |
| 581 | Slog.w(TAG, "GetGenericModelState error: Invalid generic model id:" + |
| 582 | modelId); |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 583 | return STATUS_ERROR; |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 584 | } |
| 585 | if (!modelData.isModelLoaded()) { |
| 586 | Slog.i(TAG, "GetGenericModelState: Given generic model is not loaded:" + modelId); |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 587 | return STATUS_ERROR; |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 588 | } |
| 589 | if (!modelData.isModelStarted()) { |
| 590 | Slog.i(TAG, "GetGenericModelState: Given generic model is not started:" + modelId); |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 591 | return STATUS_ERROR; |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 592 | } |
| 593 | |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 594 | return mModule.getModelState(modelData.getHandle()); |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 598 | int getKeyphraseModelState(UUID modelId) { |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 599 | Slog.w(TAG, "GetKeyphraseModelState error: Not implemented"); |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 600 | return STATUS_ERROR; |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 603 | //---- SoundTrigger.StatusListener methods |
| 604 | @Override |
| 605 | public void onRecognition(RecognitionEvent event) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 606 | if (event == null) { |
| 607 | Slog.w(TAG, "Null recognition event!"); |
| 608 | return; |
| 609 | } |
| 610 | |
| 611 | if (!(event instanceof KeyphraseRecognitionEvent) && |
| 612 | !(event instanceof GenericRecognitionEvent)) { |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 613 | Slog.w(TAG, "Invalid recognition event type (not one of generic or keyphrase)!"); |
Sandeep Siddhartha | 8ecaf5f | 2014-07-10 19:38:18 -0700 | [diff] [blame] | 614 | return; |
| 615 | } |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 616 | |
Sandeep Siddhartha | 6817337 | 2014-07-28 13:25:30 -0700 | [diff] [blame] | 617 | if (DBG) Slog.d(TAG, "onRecognition: " + event); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 618 | synchronized (mLock) { |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 619 | switch (event.status) { |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 620 | case SoundTrigger.RECOGNITION_STATUS_ABORT: |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 621 | onRecognitionAbortLocked(event); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 622 | break; |
| 623 | case SoundTrigger.RECOGNITION_STATUS_FAILURE: |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 624 | // Fire failures to all listeners since it's not tied to a keyphrase. |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 625 | onRecognitionFailureLocked(); |
| 626 | break; |
| 627 | case SoundTrigger.RECOGNITION_STATUS_SUCCESS: |
mike dooley | 23c6256 | 2018-12-20 16:05:24 +0100 | [diff] [blame] | 628 | case SoundTrigger.RECOGNITION_STATUS_GET_STATE_RESPONSE: |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 629 | if (isKeyphraseRecognitionEvent(event)) { |
| 630 | onKeyphraseRecognitionSuccessLocked((KeyphraseRecognitionEvent) event); |
| 631 | } else { |
| 632 | onGenericRecognitionSuccessLocked((GenericRecognitionEvent) event); |
| 633 | } |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 634 | break; |
| 635 | } |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 636 | } |
| 637 | } |
| 638 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 639 | private boolean isKeyphraseRecognitionEvent(RecognitionEvent event) { |
Arunesh Mishra | f47f173 | 2016-02-18 16:16:12 -0800 | [diff] [blame] | 640 | return event instanceof KeyphraseRecognitionEvent; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | private void onGenericRecognitionSuccessLocked(GenericRecognitionEvent event) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 644 | MetricsLogger.count(mContext, "sth_generic_recognition_event", 1); |
mike dooley | 23c6256 | 2018-12-20 16:05:24 +0100 | [diff] [blame] | 645 | if (event.status != SoundTrigger.RECOGNITION_STATUS_SUCCESS |
| 646 | && event.status != SoundTrigger.RECOGNITION_STATUS_GET_STATE_RESPONSE) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 647 | return; |
| 648 | } |
Ryan Bavetta | 79655eb | 2016-03-07 14:34:51 -0800 | [diff] [blame] | 649 | ModelData model = getModelDataForLocked(event.soundModelHandle); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 650 | if (model == null || !model.isGenericModel()) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 651 | Slog.w(TAG, "Generic recognition event: Model does not exist for handle: " + |
| 652 | event.soundModelHandle); |
| 653 | return; |
| 654 | } |
| 655 | |
| 656 | IRecognitionStatusCallback callback = model.getCallback(); |
| 657 | if (callback == null) { |
| 658 | Slog.w(TAG, "Generic recognition event: Null callback for model handle: " + |
| 659 | event.soundModelHandle); |
| 660 | return; |
| 661 | } |
| 662 | |
mike dooley | da48092 | 2019-01-04 09:39:47 +0100 | [diff] [blame] | 663 | model.setStopped(); |
| 664 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 665 | try { |
Arunesh Mishra | f47f173 | 2016-02-18 16:16:12 -0800 | [diff] [blame] | 666 | callback.onGenericSoundTriggerDetected((GenericRecognitionEvent) event); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 667 | } catch (DeadObjectException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 668 | forceStopAndUnloadModelLocked(model, e); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 669 | return; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 670 | } catch (RemoteException e) { |
Arunesh Mishra | f47f173 | 2016-02-18 16:16:12 -0800 | [diff] [blame] | 671 | Slog.w(TAG, "RemoteException in onGenericSoundTriggerDetected", e); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 674 | RecognitionConfig config = model.getRecognitionConfig(); |
| 675 | if (config == null) { |
| 676 | Slog.w(TAG, "Generic recognition event: Null RecognitionConfig for model handle: " + |
| 677 | event.soundModelHandle); |
| 678 | return; |
| 679 | } |
| 680 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 681 | model.setRequested(config.allowMultipleTriggers); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 682 | // TODO: Remove this block if the lower layer supports multiple triggers. |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 683 | if (model.isRequested()) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 684 | updateRecognitionLocked(model, isRecognitionAllowed() /* isAllowed */, |
| 685 | true /* notify */); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 686 | } |
| 687 | } |
| 688 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 689 | @Override |
Eric Laurent | d3b8223 | 2014-07-30 08:57:39 -0700 | [diff] [blame] | 690 | public void onSoundModelUpdate(SoundModelEvent event) { |
| 691 | if (event == null) { |
| 692 | Slog.w(TAG, "Invalid sound model event!"); |
| 693 | return; |
| 694 | } |
Eric Laurent | d3b8223 | 2014-07-30 08:57:39 -0700 | [diff] [blame] | 695 | if (DBG) Slog.d(TAG, "onSoundModelUpdate: " + event); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 696 | synchronized (mLock) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 697 | MetricsLogger.count(mContext, "sth_sound_model_updated", 1); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 698 | onSoundModelUpdatedLocked(event); |
| 699 | } |
Eric Laurent | d3b8223 | 2014-07-30 08:57:39 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 702 | @Override |
Eric Laurent | d3b8223 | 2014-07-30 08:57:39 -0700 | [diff] [blame] | 703 | public void onServiceStateChange(int state) { |
| 704 | if (DBG) Slog.d(TAG, "onServiceStateChange, state: " + state); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 705 | synchronized (mLock) { |
| 706 | onServiceStateChangedLocked(SoundTrigger.SERVICE_STATE_DISABLED == state); |
| 707 | } |
Eric Laurent | d3b8223 | 2014-07-30 08:57:39 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 710 | @Override |
| 711 | public void onServiceDied() { |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 712 | Slog.e(TAG, "onServiceDied!!"); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 713 | MetricsLogger.count(mContext, "sth_service_died", 1); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 714 | synchronized (mLock) { |
| 715 | onServiceDiedLocked(); |
Sandeep Siddhartha | 6817337 | 2014-07-28 13:25:30 -0700 | [diff] [blame] | 716 | } |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 717 | } |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 718 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 719 | private void onCallStateChangedLocked(boolean callActive) { |
| 720 | if (mCallActive == callActive) { |
| 721 | // We consider multiple call states as being active |
| 722 | // so we check if something really changed or not here. |
| 723 | return; |
| 724 | } |
| 725 | mCallActive = callActive; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 726 | updateAllRecognitionsLocked(true /* notify */); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 727 | } |
| 728 | |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 729 | private void onPowerSaveModeChangedLocked(boolean isPowerSaveMode) { |
| 730 | if (mIsPowerSaveMode == isPowerSaveMode) { |
| 731 | return; |
| 732 | } |
| 733 | mIsPowerSaveMode = isPowerSaveMode; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 734 | updateAllRecognitionsLocked(true /* notify */); |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 737 | private void onSoundModelUpdatedLocked(SoundModelEvent event) { |
| 738 | // TODO: Handle sound model update here. |
| 739 | } |
| 740 | |
| 741 | private void onServiceStateChangedLocked(boolean disabled) { |
| 742 | if (disabled == mServiceDisabled) { |
| 743 | return; |
| 744 | } |
| 745 | mServiceDisabled = disabled; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 746 | updateAllRecognitionsLocked(true /* notify */); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 747 | } |
| 748 | |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 749 | private void onRecognitionAbortLocked(RecognitionEvent event) { |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 750 | Slog.w(TAG, "Recognition aborted"); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 751 | MetricsLogger.count(mContext, "sth_recognition_aborted", 1); |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 752 | ModelData modelData = getModelDataForLocked(event.soundModelHandle); |
Chris Thornton | d0a83b8 | 2016-07-19 18:56:15 -0700 | [diff] [blame] | 753 | if (modelData != null && modelData.isModelStarted()) { |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 754 | modelData.setStopped(); |
Chris Thornton | d0a83b8 | 2016-07-19 18:56:15 -0700 | [diff] [blame] | 755 | try { |
| 756 | modelData.getCallback().onRecognitionPaused(); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 757 | } catch (DeadObjectException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 758 | forceStopAndUnloadModelLocked(modelData, e); |
Chris Thornton | d0a83b8 | 2016-07-19 18:56:15 -0700 | [diff] [blame] | 759 | } catch (RemoteException e) { |
| 760 | Slog.w(TAG, "RemoteException in onRecognitionPaused", e); |
| 761 | } |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 762 | } |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | private void onRecognitionFailureLocked() { |
| 766 | Slog.w(TAG, "Recognition failure"); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 767 | MetricsLogger.count(mContext, "sth_recognition_failure_event", 1); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 768 | try { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 769 | sendErrorCallbacksToAllLocked(STATUS_ERROR); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 770 | } finally { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 771 | internalClearModelStateLocked(); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 772 | internalClearGlobalStateLocked(); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 773 | } |
| 774 | } |
| 775 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 776 | private int getKeyphraseIdFromEvent(KeyphraseRecognitionEvent event) { |
| 777 | if (event == null) { |
| 778 | Slog.w(TAG, "Null RecognitionEvent received."); |
| 779 | return INVALID_VALUE; |
| 780 | } |
| 781 | KeyphraseRecognitionExtra[] keyphraseExtras = |
| 782 | ((KeyphraseRecognitionEvent) event).keyphraseExtras; |
| 783 | if (keyphraseExtras == null || keyphraseExtras.length == 0) { |
| 784 | Slog.w(TAG, "Invalid keyphrase recognition event!"); |
| 785 | return INVALID_VALUE; |
| 786 | } |
| 787 | // TODO: Handle more than one keyphrase extras. |
| 788 | return keyphraseExtras[0].id; |
| 789 | } |
| 790 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 791 | private void onKeyphraseRecognitionSuccessLocked(KeyphraseRecognitionEvent event) { |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 792 | Slog.i(TAG, "Recognition success"); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 793 | MetricsLogger.count(mContext, "sth_keyphrase_recognition_event", 1); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 794 | int keyphraseId = getKeyphraseIdFromEvent(event); |
| 795 | ModelData modelData = getKeyphraseModelDataLocked(keyphraseId); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 796 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 797 | if (modelData == null || !modelData.isKeyphraseModel()) { |
| 798 | Slog.e(TAG, "Keyphase model data does not exist for ID:" + keyphraseId); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 799 | return; |
| 800 | } |
| 801 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 802 | if (modelData.getCallback() == null) { |
| 803 | Slog.w(TAG, "Received onRecognition event without callback for keyphrase model."); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 804 | return; |
| 805 | } |
mike dooley | 23c6256 | 2018-12-20 16:05:24 +0100 | [diff] [blame] | 806 | |
mike dooley | da48092 | 2019-01-04 09:39:47 +0100 | [diff] [blame] | 807 | modelData.setStopped(); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 808 | |
| 809 | try { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 810 | modelData.getCallback().onKeyphraseDetected((KeyphraseRecognitionEvent) event); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 811 | } catch (DeadObjectException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 812 | forceStopAndUnloadModelLocked(modelData, e); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 813 | return; |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 814 | } catch (RemoteException e) { |
Arunesh Mishra | f47f173 | 2016-02-18 16:16:12 -0800 | [diff] [blame] | 815 | Slog.w(TAG, "RemoteException in onKeyphraseDetected", e); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 816 | } |
| 817 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 818 | RecognitionConfig config = modelData.getRecognitionConfig(); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 819 | if (config != null) { |
| 820 | // Whether we should continue by starting this again. |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 821 | modelData.setRequested(config.allowMultipleTriggers); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 822 | } |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 823 | // TODO: Remove this block if the lower layer supports multiple triggers. |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 824 | if (modelData.isRequested()) { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 825 | updateRecognitionLocked(modelData, isRecognitionAllowed(), true /* notify */); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | private void updateAllRecognitionsLocked(boolean notify) { |
| 830 | boolean isAllowed = isRecognitionAllowed(); |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 831 | // updateRecognitionLocked can possibly update the list of models |
| 832 | ArrayList<ModelData> modelDatas = new ArrayList<ModelData>(mModelDataMap.values()); |
| 833 | for (ModelData modelData : modelDatas) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 834 | updateRecognitionLocked(modelData, isAllowed, notify); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | private int updateRecognitionLocked(ModelData model, boolean isAllowed, |
| 839 | boolean notify) { |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 840 | boolean start = model.isRequested() && isAllowed; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 841 | if (start == model.isModelStarted()) { |
| 842 | // No-op. |
| 843 | return STATUS_OK; |
| 844 | } |
| 845 | if (start) { |
| 846 | return startRecognitionLocked(model, notify); |
| 847 | } else { |
| 848 | return stopRecognitionLocked(model, notify); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | |
| 852 | private void onServiceDiedLocked() { |
| 853 | try { |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 854 | MetricsLogger.count(mContext, "sth_service_died", 1); |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 855 | sendErrorCallbacksToAllLocked(SoundTrigger.STATUS_DEAD_OBJECT); |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 856 | } finally { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 857 | internalClearModelStateLocked(); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 858 | internalClearGlobalStateLocked(); |
Eric Laurent | 2475e38 | 2014-09-09 15:52:25 -0700 | [diff] [blame] | 859 | if (mModule != null) { |
| 860 | mModule.detach(); |
| 861 | mModule = null; |
| 862 | } |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 863 | } |
| 864 | } |
| 865 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 866 | // internalClearGlobalStateLocked() cleans up the telephony and power save listeners. |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 867 | private void internalClearGlobalStateLocked() { |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 868 | // Unregister from call state changes. |
mike dooley | cabbb11 | 2018-04-17 17:19:04 +0200 | [diff] [blame] | 869 | long token = Binder.clearCallingIdentity(); |
| 870 | try { |
| 871 | mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_NONE); |
| 872 | } finally { |
| 873 | Binder.restoreCallingIdentity(token); |
| 874 | } |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 875 | |
| 876 | // Unregister from power save mode changes. |
| 877 | if (mPowerSaveModeListener != null) { |
| 878 | mContext.unregisterReceiver(mPowerSaveModeListener); |
| 879 | mPowerSaveModeListener = null; |
| 880 | } |
| 881 | } |
| 882 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 883 | // Clears state for all models (generic and keyphrase). |
| 884 | private void internalClearModelStateLocked() { |
| 885 | for (ModelData modelData : mModelDataMap.values()) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 886 | modelData.clearState(); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 890 | class MyCallStateListener extends PhoneStateListener { |
| 891 | @Override |
| 892 | public void onCallStateChanged(int state, String arg1) { |
| 893 | if (DBG) Slog.d(TAG, "onCallStateChanged: " + state); |
| 894 | synchronized (mLock) { |
rago | f96269a | 2019-03-26 10:34:04 -0700 | [diff] [blame] | 895 | onCallStateChangedLocked(TelephonyManager.CALL_STATE_OFFHOOK == state); |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
| 900 | class PowerSaveModeListener extends BroadcastReceiver { |
| 901 | @Override |
| 902 | public void onReceive(Context context, Intent intent) { |
| 903 | if (!PowerManager.ACTION_POWER_SAVE_MODE_CHANGED.equals(intent.getAction())) { |
| 904 | return; |
| 905 | } |
jackqdyulei | 455e90a | 2017-02-09 15:29:16 -0800 | [diff] [blame] | 906 | boolean active = mPowerManager.getPowerSaveState(ServiceType.SOUND) |
| 907 | .batterySaverEnabled; |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 908 | if (DBG) Slog.d(TAG, "onPowerSaveModeChanged: " + active); |
| 909 | synchronized (mLock) { |
| 910 | onPowerSaveModeChangedLocked(active); |
| 911 | } |
| 912 | } |
Sandeep Siddhartha | cb4e81c | 2014-08-05 15:03:46 -0700 | [diff] [blame] | 913 | } |
Sandeep Siddhartha | 6b8556d | 2014-08-06 19:47:25 -0700 | [diff] [blame] | 914 | |
| 915 | void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 916 | synchronized (mLock) { |
| 917 | pw.print(" module properties="); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 918 | pw.println(mModuleProperties == null ? "null" : mModuleProperties); |
Sandeep Siddhartha | 6b8556d | 2014-08-06 19:47:25 -0700 | [diff] [blame] | 919 | |
Sandeep Siddhartha | 6b8556d | 2014-08-06 19:47:25 -0700 | [diff] [blame] | 920 | pw.print(" call active="); pw.println(mCallActive); |
Sandeep Siddhartha | efe0f9c | 2014-08-22 17:45:04 -0700 | [diff] [blame] | 921 | pw.print(" power save mode active="); pw.println(mIsPowerSaveMode); |
Sandeep Siddhartha | 6b8556d | 2014-08-06 19:47:25 -0700 | [diff] [blame] | 922 | pw.print(" service disabled="); pw.println(mServiceDisabled); |
| 923 | } |
| 924 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 925 | |
| 926 | private void initializeTelephonyAndPowerStateListeners() { |
Hall Liu | 5fb337f | 2017-11-22 17:38:15 -0800 | [diff] [blame] | 927 | long token = Binder.clearCallingIdentity(); |
| 928 | try { |
| 929 | // Get the current call state synchronously for the first recognition. |
rago | f96269a | 2019-03-26 10:34:04 -0700 | [diff] [blame] | 930 | mCallActive = mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_OFFHOOK; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 931 | |
Hall Liu | 5fb337f | 2017-11-22 17:38:15 -0800 | [diff] [blame] | 932 | // Register for call state changes when the first call to start recognition occurs. |
| 933 | mTelephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 934 | |
Hall Liu | 5fb337f | 2017-11-22 17:38:15 -0800 | [diff] [blame] | 935 | // Register for power saver mode changes when the first call to start recognition |
| 936 | // occurs. |
| 937 | if (mPowerSaveModeListener == null) { |
| 938 | mPowerSaveModeListener = new PowerSaveModeListener(); |
| 939 | mContext.registerReceiver(mPowerSaveModeListener, |
| 940 | new IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)); |
| 941 | } |
| 942 | mIsPowerSaveMode = mPowerManager.getPowerSaveState(ServiceType.SOUND) |
| 943 | .batterySaverEnabled; |
| 944 | } finally { |
| 945 | Binder.restoreCallingIdentity(token); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 946 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 949 | // Sends an error callback to all models with a valid registered callback. |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 950 | private void sendErrorCallbacksToAllLocked(int errorCode) { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 951 | for (ModelData modelData : mModelDataMap.values()) { |
| 952 | IRecognitionStatusCallback callback = modelData.getCallback(); |
| 953 | if (callback != null) { |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 954 | try { |
| 955 | callback.onError(errorCode); |
| 956 | } catch (RemoteException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 957 | Slog.w(TAG, "RemoteException sendErrorCallbacksToAllLocked for model handle " + |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 958 | modelData.getHandle(), e); |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
Chris Thornton | a533b99 | 2017-03-24 17:30:29 -0700 | [diff] [blame] | 964 | /** |
| 965 | * Stops and unloads a sound model, and removes any reference to the model if successful. |
| 966 | * |
| 967 | * @param modelData The model data to remove. |
| 968 | * @param exception Optional exception to print in logcat. May be null. |
| 969 | */ |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 970 | private void forceStopAndUnloadModelLocked(ModelData modelData, Exception exception) { |
| 971 | forceStopAndUnloadModelLocked(modelData, exception, null /* modelDataIterator */); |
Chris Thornton | a533b99 | 2017-03-24 17:30:29 -0700 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /** |
| 975 | * Stops and unloads a sound model, and removes any reference to the model if successful. |
| 976 | * |
| 977 | * @param modelData The model data to remove. |
| 978 | * @param exception Optional exception to print in logcat. May be null. |
| 979 | * @param modelDataIterator If this function is to be used while iterating over the |
| 980 | * mModelDataMap, you can provide the iterator for the current model data to be used to |
| 981 | * remove the modelData from the map. This avoids generating a |
| 982 | * ConcurrentModificationException, since this function will try and remove the model |
| 983 | * data from the mModelDataMap when it can successfully unload the model. |
| 984 | */ |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 985 | private void forceStopAndUnloadModelLocked(ModelData modelData, Exception exception, |
Chris Thornton | a533b99 | 2017-03-24 17:30:29 -0700 | [diff] [blame] | 986 | Iterator modelDataIterator) { |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 987 | if (exception != null) { |
| 988 | Slog.e(TAG, "forceStopAndUnloadModel", exception); |
| 989 | } |
| 990 | if (modelData.isModelStarted()) { |
| 991 | Slog.d(TAG, "Stopping previously started dangling model " + modelData.getHandle()); |
| 992 | if (mModule.stopRecognition(modelData.getHandle()) != STATUS_OK) { |
| 993 | modelData.setStopped(); |
| 994 | modelData.setRequested(false); |
| 995 | } else { |
| 996 | Slog.e(TAG, "Failed to stop model " + modelData.getHandle()); |
| 997 | } |
| 998 | } |
| 999 | if (modelData.isModelLoaded()) { |
| 1000 | Slog.d(TAG, "Unloading previously loaded dangling model " + modelData.getHandle()); |
| 1001 | if (mModule.unloadSoundModel(modelData.getHandle()) == STATUS_OK) { |
| 1002 | // Remove the model data from existence. |
Chris Thornton | a533b99 | 2017-03-24 17:30:29 -0700 | [diff] [blame] | 1003 | if (modelDataIterator != null) { |
| 1004 | modelDataIterator.remove(); |
| 1005 | } else { |
| 1006 | mModelDataMap.remove(modelData.getModelId()); |
| 1007 | } |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 1008 | Iterator it = mKeyphraseUuidMap.entrySet().iterator(); |
| 1009 | while (it.hasNext()) { |
| 1010 | Map.Entry pair = (Map.Entry) it.next(); |
| 1011 | if (pair.getValue().equals(modelData.getModelId())) { |
| 1012 | it.remove(); |
| 1013 | } |
| 1014 | } |
| 1015 | modelData.clearState(); |
| 1016 | } else { |
| 1017 | Slog.e(TAG, "Failed to unload model " + modelData.getHandle()); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
Chris Thornton | a533b99 | 2017-03-24 17:30:29 -0700 | [diff] [blame] | 1022 | private void stopAndUnloadDeadModelsLocked() { |
| 1023 | Iterator it = mModelDataMap.entrySet().iterator(); |
| 1024 | while (it.hasNext()) { |
| 1025 | ModelData modelData = (ModelData) ((Map.Entry) it.next()).getValue(); |
| 1026 | if (!modelData.isModelLoaded()) { |
| 1027 | continue; |
| 1028 | } |
| 1029 | if (modelData.getCallback() == null |
| 1030 | || (modelData.getCallback().asBinder() != null |
| 1031 | && !modelData.getCallback().asBinder().pingBinder())) { |
| 1032 | // No one is listening on this model, so we might as well evict it. |
| 1033 | Slog.w(TAG, "Removing model " + modelData.getHandle() + " that has no clients"); |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 1034 | forceStopAndUnloadModelLocked(modelData, null /* exception */, it); |
Chris Thornton | a533b99 | 2017-03-24 17:30:29 -0700 | [diff] [blame] | 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
Ryan Bavetta | 79655eb | 2016-03-07 14:34:51 -0800 | [diff] [blame] | 1039 | private ModelData getOrCreateGenericModelDataLocked(UUID modelId) { |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1040 | ModelData modelData = mModelDataMap.get(modelId); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1041 | if (modelData == null) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1042 | modelData = ModelData.createGenericModelData(modelId); |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1043 | mModelDataMap.put(modelId, modelData); |
| 1044 | } else if (!modelData.isGenericModel()) { |
| 1045 | Slog.e(TAG, "UUID already used for non-generic model."); |
| 1046 | return null; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1047 | } |
| 1048 | return modelData; |
| 1049 | } |
| 1050 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1051 | private void removeKeyphraseModelLocked(int keyphraseId) { |
| 1052 | UUID uuid = mKeyphraseUuidMap.get(keyphraseId); |
| 1053 | if (uuid == null) { |
| 1054 | return; |
| 1055 | } |
| 1056 | mModelDataMap.remove(uuid); |
| 1057 | mKeyphraseUuidMap.remove(keyphraseId); |
| 1058 | } |
| 1059 | |
| 1060 | private ModelData getKeyphraseModelDataLocked(int keyphraseId) { |
| 1061 | UUID uuid = mKeyphraseUuidMap.get(keyphraseId); |
| 1062 | if (uuid == null) { |
| 1063 | return null; |
| 1064 | } |
| 1065 | return mModelDataMap.get(uuid); |
| 1066 | } |
| 1067 | |
| 1068 | // Use this to create a new ModelData entry for a keyphrase Id. It will overwrite existing |
| 1069 | // mapping if one exists. |
| 1070 | private ModelData createKeyphraseModelDataLocked(UUID modelId, int keyphraseId) { |
| 1071 | mKeyphraseUuidMap.remove(keyphraseId); |
| 1072 | mModelDataMap.remove(modelId); |
| 1073 | mKeyphraseUuidMap.put(keyphraseId, modelId); |
| 1074 | ModelData modelData = ModelData.createKeyphraseModelData(modelId); |
| 1075 | mModelDataMap.put(modelId, modelData); |
| 1076 | return modelData; |
| 1077 | } |
| 1078 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1079 | // Instead of maintaining a second hashmap of modelHandle -> ModelData, we just |
| 1080 | // iterate through to find the right object (since we don't expect 100s of models |
| 1081 | // to be stored). |
Ryan Bavetta | 79655eb | 2016-03-07 14:34:51 -0800 | [diff] [blame] | 1082 | private ModelData getModelDataForLocked(int modelHandle) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1083 | // Fetch ModelData object corresponding to the model handle. |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1084 | for (ModelData model : mModelDataMap.values()) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1085 | if (model.getHandle() == modelHandle) { |
| 1086 | return model; |
| 1087 | } |
| 1088 | } |
| 1089 | return null; |
| 1090 | } |
| 1091 | |
| 1092 | // Whether we are allowed to run any recognition at all. The conditions that let us run |
| 1093 | // a recognition include: no active phone call or not being in a power save mode. Also, |
| 1094 | // the native service should be enabled. |
| 1095 | private boolean isRecognitionAllowed() { |
| 1096 | return !mCallActive && !mServiceDisabled && !mIsPowerSaveMode; |
| 1097 | } |
| 1098 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1099 | // A single routine that implements the start recognition logic for both generic and keyphrase |
| 1100 | // models. |
| 1101 | private int startRecognitionLocked(ModelData modelData, boolean notify) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1102 | IRecognitionStatusCallback callback = modelData.getCallback(); |
| 1103 | int handle = modelData.getHandle(); |
| 1104 | RecognitionConfig config = modelData.getRecognitionConfig(); |
| 1105 | if (callback == null || handle == INVALID_VALUE || config == null) { |
| 1106 | // Nothing to do here. |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1107 | Slog.w(TAG, "startRecognition: Bad data passed in."); |
| 1108 | MetricsLogger.count(mContext, "sth_start_recognition_error", 1); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1109 | return STATUS_ERROR; |
| 1110 | } |
| 1111 | |
| 1112 | if (!isRecognitionAllowed()) { |
| 1113 | // Nothing to do here. |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1114 | Slog.w(TAG, "startRecognition requested but not allowed."); |
| 1115 | MetricsLogger.count(mContext, "sth_start_recognition_not_allowed", 1); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1116 | return STATUS_OK; |
| 1117 | } |
| 1118 | |
| 1119 | int status = mModule.startRecognition(handle, config); |
| 1120 | if (status != SoundTrigger.STATUS_OK) { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1121 | Slog.w(TAG, "startRecognition failed with " + status); |
| 1122 | MetricsLogger.count(mContext, "sth_start_recognition_error", 1); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1123 | // Notify of error if needed. |
| 1124 | if (notify) { |
| 1125 | try { |
| 1126 | callback.onError(status); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 1127 | } catch (DeadObjectException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 1128 | forceStopAndUnloadModelLocked(modelData, e); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1129 | } catch (RemoteException e) { |
| 1130 | Slog.w(TAG, "RemoteException in onError", e); |
| 1131 | } |
| 1132 | } |
| 1133 | } else { |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 1134 | Slog.i(TAG, "startRecognition successful."); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1135 | MetricsLogger.count(mContext, "sth_start_recognition_success", 1); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1136 | modelData.setStarted(); |
| 1137 | // Notify of resume if needed. |
| 1138 | if (notify) { |
| 1139 | try { |
| 1140 | callback.onRecognitionResumed(); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 1141 | } catch (DeadObjectException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 1142 | forceStopAndUnloadModelLocked(modelData, e); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1143 | } catch (RemoteException e) { |
| 1144 | Slog.w(TAG, "RemoteException in onRecognitionResumed", e); |
| 1145 | } |
| 1146 | } |
| 1147 | } |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1148 | if (DBG) { |
| 1149 | Slog.d(TAG, "Model being started :" + modelData.toString()); |
| 1150 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1151 | return status; |
| 1152 | } |
| 1153 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1154 | private int stopRecognitionLocked(ModelData modelData, boolean notify) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1155 | IRecognitionStatusCallback callback = modelData.getCallback(); |
| 1156 | |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 1157 | // Stop recognition. |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1158 | int status = STATUS_OK; |
| 1159 | |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 1160 | status = mModule.stopRecognition(modelData.getHandle()); |
| 1161 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1162 | if (status != SoundTrigger.STATUS_OK) { |
| 1163 | Slog.w(TAG, "stopRecognition call failed with " + status); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1164 | MetricsLogger.count(mContext, "sth_stop_recognition_error", 1); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1165 | if (notify) { |
| 1166 | try { |
| 1167 | callback.onError(status); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 1168 | } catch (DeadObjectException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 1169 | forceStopAndUnloadModelLocked(modelData, e); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1170 | } catch (RemoteException e) { |
| 1171 | Slog.w(TAG, "RemoteException in onError", e); |
| 1172 | } |
| 1173 | } |
| 1174 | } else { |
| 1175 | modelData.setStopped(); |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1176 | MetricsLogger.count(mContext, "sth_stop_recognition_success", 1); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1177 | // Notify of pause if needed. |
| 1178 | if (notify) { |
| 1179 | try { |
| 1180 | callback.onRecognitionPaused(); |
Chris Thornton | 2411a2c | 2017-03-22 13:52:50 -0700 | [diff] [blame] | 1181 | } catch (DeadObjectException e) { |
Chris Thornton | 23a5740 | 2017-06-10 17:31:57 -0700 | [diff] [blame] | 1182 | forceStopAndUnloadModelLocked(modelData, e); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1183 | } catch (RemoteException e) { |
| 1184 | Slog.w(TAG, "RemoteException in onRecognitionPaused", e); |
| 1185 | } |
| 1186 | } |
| 1187 | } |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1188 | if (DBG) { |
| 1189 | Slog.d(TAG, "Model being stopped :" + modelData.toString()); |
| 1190 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1191 | return status; |
| 1192 | } |
| 1193 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1194 | private void dumpModelStateLocked() { |
| 1195 | for (UUID modelId : mModelDataMap.keySet()) { |
| 1196 | ModelData modelData = mModelDataMap.get(modelId); |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 1197 | Slog.i(TAG, "Model :" + modelData.toString()); |
| 1198 | } |
| 1199 | } |
| 1200 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1201 | // Computes whether we have any recognition running at all (voice or generic). Sets |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 1202 | // the mRecognitionRequested variable with the result. |
| 1203 | private boolean computeRecognitionRequestedLocked() { |
Ryan Bavetta | 79655eb | 2016-03-07 14:34:51 -0800 | [diff] [blame] | 1204 | if (mModuleProperties == null || mModule == null) { |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 1205 | mRecognitionRequested = false; |
| 1206 | return mRecognitionRequested; |
Ryan Bavetta | 79655eb | 2016-03-07 14:34:51 -0800 | [diff] [blame] | 1207 | } |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1208 | for (ModelData modelData : mModelDataMap.values()) { |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 1209 | if (modelData.isRequested()) { |
| 1210 | mRecognitionRequested = true; |
| 1211 | return mRecognitionRequested; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1212 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1213 | } |
Eric Laurent | 80565fb | 2019-07-18 08:22:38 -0700 | [diff] [blame] | 1214 | mRecognitionRequested = false; |
| 1215 | return mRecognitionRequested; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | // This class encapsulates the callbacks, state, handles and any other information that |
| 1219 | // represents a model. |
| 1220 | private static class ModelData { |
| 1221 | // Model not loaded (and hence not started). |
| 1222 | static final int MODEL_NOTLOADED = 0; |
| 1223 | |
| 1224 | // Loaded implies model was successfully loaded. Model not started yet. |
| 1225 | static final int MODEL_LOADED = 1; |
| 1226 | |
| 1227 | // Started implies model was successfully loaded and start was called. |
| 1228 | static final int MODEL_STARTED = 2; |
| 1229 | |
| 1230 | // One of MODEL_NOTLOADED, MODEL_LOADED, MODEL_STARTED (which implies loaded). |
| 1231 | private int mModelState; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1232 | private UUID mModelId; |
| 1233 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1234 | // mRequested captures the explicit intent that a start was requested for this model. We |
| 1235 | // continue to capture and retain this state even after the model gets started, so that we |
| 1236 | // know when a model gets stopped due to "other" reasons, that we should start it again. |
| 1237 | // This was the intended behavior of the "mRequested" variable in the previous version of |
| 1238 | // this code that we are replicating here. |
| 1239 | // |
| 1240 | // The "other" reasons include power save, abort being called from the lower layer (due |
| 1241 | // to concurrent capture not being supported) and phone call state. Once we recover from |
| 1242 | // these transient disruptions, we would start such models again where mRequested == true. |
| 1243 | // Thus, mRequested gets reset only when there is an explicit intent to stop the model |
| 1244 | // coming from the SoundTriggerService layer that uses this class (and thus eventually |
| 1245 | // from the app that manages this model). |
| 1246 | private boolean mRequested = false; |
| 1247 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1248 | // One of SoundModel.TYPE_GENERIC or SoundModel.TYPE_KEYPHRASE. Initially set |
| 1249 | // to SoundModel.TYPE_UNKNOWN; |
| 1250 | private int mModelType = SoundModel.TYPE_UNKNOWN; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1251 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1252 | private IRecognitionStatusCallback mCallback = null; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1253 | private RecognitionConfig mRecognitionConfig = null; |
| 1254 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1255 | // Model handle is an integer used by the HAL as an identifier for sound |
| 1256 | // models. |
| 1257 | private int mModelHandle = INVALID_VALUE; |
| 1258 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1259 | // The SoundModel instance, one of KeyphraseSoundModel or GenericSoundModel. |
| 1260 | private SoundModel mSoundModel = null; |
| 1261 | |
| 1262 | private ModelData(UUID modelId, int modelType) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1263 | mModelId = modelId; |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1264 | // Private constructor, since we require modelType to be one of TYPE_GENERIC, |
| 1265 | // TYPE_KEYPHRASE or TYPE_UNKNOWN. |
| 1266 | mModelType = modelType; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1267 | } |
| 1268 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1269 | static ModelData createKeyphraseModelData(UUID modelId) { |
| 1270 | return new ModelData(modelId, SoundModel.TYPE_KEYPHRASE); |
| 1271 | } |
| 1272 | |
| 1273 | static ModelData createGenericModelData(UUID modelId) { |
| 1274 | return new ModelData(modelId, SoundModel.TYPE_GENERIC_SOUND); |
| 1275 | } |
| 1276 | |
| 1277 | // Note that most of the functionality in this Java class will not work for |
| 1278 | // SoundModel.TYPE_UNKNOWN nevertheless we have it since lower layers support it. |
| 1279 | static ModelData createModelDataOfUnknownType(UUID modelId) { |
| 1280 | return new ModelData(modelId, SoundModel.TYPE_UNKNOWN); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | synchronized void setCallback(IRecognitionStatusCallback callback) { |
| 1284 | mCallback = callback; |
| 1285 | } |
| 1286 | |
| 1287 | synchronized IRecognitionStatusCallback getCallback() { |
| 1288 | return mCallback; |
| 1289 | } |
| 1290 | |
| 1291 | synchronized boolean isModelLoaded() { |
Arunesh Mishra | 933da81 | 2016-02-18 15:31:54 -0800 | [diff] [blame] | 1292 | return (mModelState == MODEL_LOADED || mModelState == MODEL_STARTED); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1295 | synchronized boolean isModelNotLoaded() { |
| 1296 | return mModelState == MODEL_NOTLOADED; |
| 1297 | } |
| 1298 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1299 | synchronized void setStarted() { |
| 1300 | mModelState = MODEL_STARTED; |
| 1301 | } |
| 1302 | |
| 1303 | synchronized void setStopped() { |
| 1304 | mModelState = MODEL_LOADED; |
| 1305 | } |
| 1306 | |
Arunesh Mishra | 933da81 | 2016-02-18 15:31:54 -0800 | [diff] [blame] | 1307 | synchronized void setLoaded() { |
| 1308 | mModelState = MODEL_LOADED; |
| 1309 | } |
| 1310 | |
| 1311 | synchronized boolean isModelStarted() { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1312 | return mModelState == MODEL_STARTED; |
| 1313 | } |
| 1314 | |
| 1315 | synchronized void clearState() { |
| 1316 | mModelState = MODEL_NOTLOADED; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1317 | mModelHandle = INVALID_VALUE; |
Chris Thornton | 41e04a4 | 2016-04-18 16:51:08 -0700 | [diff] [blame] | 1318 | mRecognitionConfig = null; |
| 1319 | mRequested = false; |
| 1320 | mCallback = null; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | synchronized void clearCallback() { |
| 1324 | mCallback = null; |
| 1325 | } |
| 1326 | |
| 1327 | synchronized void setHandle(int handle) { |
| 1328 | mModelHandle = handle; |
| 1329 | } |
| 1330 | |
| 1331 | synchronized void setRecognitionConfig(RecognitionConfig config) { |
| 1332 | mRecognitionConfig = config; |
| 1333 | } |
| 1334 | |
| 1335 | synchronized int getHandle() { |
| 1336 | return mModelHandle; |
| 1337 | } |
| 1338 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1339 | synchronized UUID getModelId() { |
| 1340 | return mModelId; |
| 1341 | } |
| 1342 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1343 | synchronized RecognitionConfig getRecognitionConfig() { |
| 1344 | return mRecognitionConfig; |
| 1345 | } |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 1346 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1347 | // Whether a start recognition was requested. |
Chris Thornton | 37f9776 | 2016-04-29 16:23:45 -0700 | [diff] [blame] | 1348 | synchronized boolean isRequested() { |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1349 | return mRequested; |
| 1350 | } |
| 1351 | |
| 1352 | synchronized void setRequested(boolean requested) { |
| 1353 | mRequested = requested; |
| 1354 | } |
| 1355 | |
| 1356 | synchronized void setSoundModel(SoundModel soundModel) { |
| 1357 | mSoundModel = soundModel; |
| 1358 | } |
| 1359 | |
| 1360 | synchronized SoundModel getSoundModel() { |
| 1361 | return mSoundModel; |
| 1362 | } |
| 1363 | |
| 1364 | synchronized int getModelType() { |
| 1365 | return mModelType; |
| 1366 | } |
| 1367 | |
| 1368 | synchronized boolean isKeyphraseModel() { |
| 1369 | return mModelType == SoundModel.TYPE_KEYPHRASE; |
| 1370 | } |
| 1371 | |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1372 | synchronized boolean isGenericModel() { |
| 1373 | return mModelType == SoundModel.TYPE_GENERIC_SOUND; |
| 1374 | } |
| 1375 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1376 | synchronized String stateToString() { |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 1377 | switch(mModelState) { |
| 1378 | case MODEL_NOTLOADED: return "NOT_LOADED"; |
| 1379 | case MODEL_LOADED: return "LOADED"; |
| 1380 | case MODEL_STARTED: return "STARTED"; |
| 1381 | } |
| 1382 | return "Unknown state"; |
| 1383 | } |
| 1384 | |
Arunesh Mishra | 78ab7cf | 2016-03-03 22:52:36 -0800 | [diff] [blame] | 1385 | synchronized String requestedToString() { |
| 1386 | return "Requested: " + (mRequested ? "Yes" : "No"); |
| 1387 | } |
| 1388 | |
| 1389 | synchronized String callbackToString() { |
| 1390 | return "Callback: " + (mCallback != null ? mCallback.asBinder() : "null"); |
| 1391 | } |
| 1392 | |
| 1393 | synchronized String uuidToString() { |
| 1394 | return "UUID: " + mModelId; |
| 1395 | } |
| 1396 | |
| 1397 | synchronized public String toString() { |
| 1398 | return "Handle: " + mModelHandle + "\n" + |
| 1399 | "ModelState: " + stateToString() + "\n" + |
| 1400 | requestedToString() + "\n" + |
| 1401 | callbackToString() + "\n" + |
Arunesh Mishra | fac2515 | 2016-04-10 20:35:16 -0700 | [diff] [blame] | 1402 | uuidToString() + "\n" + modelTypeToString(); |
| 1403 | } |
| 1404 | |
| 1405 | synchronized String modelTypeToString() { |
| 1406 | String type = null; |
| 1407 | switch (mModelType) { |
| 1408 | case SoundModel.TYPE_GENERIC_SOUND: type = "Generic"; break; |
| 1409 | case SoundModel.TYPE_UNKNOWN: type = "Unknown"; break; |
| 1410 | case SoundModel.TYPE_KEYPHRASE: type = "Keyphrase"; break; |
| 1411 | } |
| 1412 | return "Model type: " + type + "\n"; |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 1413 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1414 | } |
Sandeep | d701820 | 2014-07-10 15:15:39 -0700 | [diff] [blame] | 1415 | } |