blob: 3196758996be720627b12697cf9f06a721475ef4 [file] [log] [blame]
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001/*
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
17package com.android.server.soundtrigger;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080018
19import static android.Manifest.permission.BIND_SOUND_TRIGGER_DETECTION_SERVICE;
20import static android.content.Context.BIND_AUTO_CREATE;
21import static android.content.Context.BIND_FOREGROUND_SERVICE;
Nick Moukhine67b5e382020-03-16 17:51:37 +010022import static android.content.Context.BIND_INCLUDE_CAPABILITIES;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080023import static android.content.pm.PackageManager.GET_META_DATA;
24import static android.content.pm.PackageManager.GET_SERVICES;
25import static android.content.pm.PackageManager.MATCH_DEBUG_TRIAGED_MISSING;
Nicholas Ambura0be6be2019-10-01 10:11:39 -070026import static android.hardware.soundtrigger.SoundTrigger.STATUS_BAD_VALUE;
Arunesh Mishra3fff7f52016-02-09 12:15:19 -080027import static android.hardware.soundtrigger.SoundTrigger.STATUS_ERROR;
Nicholas Ambura0be6be2019-10-01 10:11:39 -070028import static android.hardware.soundtrigger.SoundTrigger.STATUS_NO_INIT;
Chris Thorntonba08b792017-06-08 22:34:37 -070029import static android.hardware.soundtrigger.SoundTrigger.STATUS_OK;
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -080030import static android.provider.Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080031import static android.provider.Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080032
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080033import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage;
34
35import android.Manifest;
36import android.annotation.NonNull;
37import android.annotation.Nullable;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080038import android.content.ComponentName;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080039import android.content.Context;
Chris Thorntonba08b792017-06-08 22:34:37 -070040import android.content.Intent;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080041import android.content.ServiceConnection;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080042import android.content.pm.PackageManager;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080043import android.content.pm.ResolveInfo;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080044import android.hardware.soundtrigger.IRecognitionStatusCallback;
Nicholas Ambura0be6be2019-10-01 10:11:39 -070045import android.hardware.soundtrigger.ModelParams;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080046import android.hardware.soundtrigger.SoundTrigger;
Arunesh Mishrac722ec412016-01-27 13:29:12 -080047import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080048import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel;
Nicholas Ambura0be6be2019-10-01 10:11:39 -070049import android.hardware.soundtrigger.SoundTrigger.ModelParamRange;
Arunesh Mishra55a9b002016-02-01 14:06:37 -080050import android.hardware.soundtrigger.SoundTrigger.ModuleProperties;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080051import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080052import android.hardware.soundtrigger.SoundTrigger.SoundModel;
Philip P. Moltmanna5b44032018-05-04 13:59:45 -070053import android.media.AudioAttributes;
54import android.media.AudioFormat;
55import android.media.AudioRecord;
56import android.media.MediaRecorder;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080057import android.media.soundtrigger.ISoundTriggerDetectionService;
58import android.media.soundtrigger.ISoundTriggerDetectionServiceClient;
59import android.media.soundtrigger.SoundTriggerDetectionService;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080060import android.os.Binder;
Chris Thorntonba08b792017-06-08 22:34:37 -070061import android.os.Bundle;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080062import android.os.Handler;
63import android.os.IBinder;
64import android.os.Looper;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080065import android.os.Parcel;
66import android.os.ParcelUuid;
Chris Thorntonba08b792017-06-08 22:34:37 -070067import android.os.PowerManager;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080068import android.os.RemoteException;
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -070069import android.os.SystemClock;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080070import android.os.UserHandle;
71import android.provider.Settings;
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -080072import android.util.ArrayMap;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080073import android.util.ArraySet;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080074import android.util.Slog;
75
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080076import com.android.internal.annotations.GuardedBy;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080077import com.android.internal.app.ISoundTriggerService;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080078import com.android.server.SystemService;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080079
80import java.io.FileDescriptor;
81import java.io.PrintWriter;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -080082import java.util.ArrayList;
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -070083import java.util.Map;
Daulet Zhanguzine789eee2019-12-20 17:19:33 +000084import java.util.Objects;
Chris Thorntonba08b792017-06-08 22:34:37 -070085import java.util.TreeMap;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080086import java.util.UUID;
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -080087import java.util.concurrent.TimeUnit;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -080088
89/**
90 * A single SystemService to manage all sound/voice-based sound models on the DSP.
91 * This services provides apis to manage sound trigger-based sound models via
92 * the ISoundTriggerService interface. This class also publishes a local interface encapsulating
93 * the functionality provided by {@link SoundTriggerHelper} for use by
94 * {@link VoiceInteractionManagerService}.
95 *
96 * @hide
97 */
98public class SoundTriggerService extends SystemService {
Arunesh Mishra3fff7f52016-02-09 12:15:19 -080099 private static final String TAG = "SoundTriggerService";
100 private static final boolean DEBUG = true;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800101
102 final Context mContext;
Chris Thorntonba08b792017-06-08 22:34:37 -0700103 private Object mLock;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800104 private final SoundTriggerServiceStub mServiceStub;
105 private final LocalSoundTriggerService mLocalSoundTriggerService;
106 private SoundTriggerDbHelper mDbHelper;
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800107 private SoundTriggerHelper mSoundTriggerHelper;
Chris Thorntonba08b792017-06-08 22:34:37 -0700108 private final TreeMap<UUID, SoundModel> mLoadedModels;
Chris Thorntonae5fb992017-12-07 18:26:31 -0800109 private Object mCallbacksLock;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800110 private final TreeMap<UUID, IRecognitionStatusCallback> mCallbacks;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800111
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700112 class SoundModelStatTracker {
113 private class SoundModelStat {
114 SoundModelStat() {
115 mStartCount = 0;
116 mTotalTimeMsec = 0;
117 mLastStartTimestampMsec = 0;
118 mLastStopTimestampMsec = 0;
119 mIsStarted = false;
120 }
121 long mStartCount; // Number of times that given model started
122 long mTotalTimeMsec; // Total time (msec) that given model was running since boot
123 long mLastStartTimestampMsec; // SystemClock.elapsedRealtime model was last started
124 long mLastStopTimestampMsec; // SystemClock.elapsedRealtime model was last stopped
125 boolean mIsStarted; // true if model is currently running
126 }
127 private final TreeMap<UUID, SoundModelStat> mModelStats;
128
129 SoundModelStatTracker() {
130 mModelStats = new TreeMap<UUID, SoundModelStat>();
131 }
132
133 public synchronized void onStart(UUID id) {
134 SoundModelStat stat = mModelStats.get(id);
135 if (stat == null) {
136 stat = new SoundModelStat();
137 mModelStats.put(id, stat);
138 }
139
140 if (stat.mIsStarted) {
141 Slog.e(TAG, "error onStart(): Model " + id + " already started");
142 return;
143 }
144
145 stat.mStartCount++;
146 stat.mLastStartTimestampMsec = SystemClock.elapsedRealtime();
147 stat.mIsStarted = true;
148 }
149
150 public synchronized void onStop(UUID id) {
151 SoundModelStat stat = mModelStats.get(id);
152 if (stat == null) {
153 Slog.e(TAG, "error onStop(): Model " + id + " has no stats available");
154 return;
155 }
156
157 if (!stat.mIsStarted) {
158 Slog.e(TAG, "error onStop(): Model " + id + " already stopped");
159 return;
160 }
161
162 stat.mLastStopTimestampMsec = SystemClock.elapsedRealtime();
163 stat.mTotalTimeMsec += stat.mLastStopTimestampMsec - stat.mLastStartTimestampMsec;
164 stat.mIsStarted = false;
165 }
166
167 public synchronized void dump(PrintWriter pw) {
168 long curTime = SystemClock.elapsedRealtime();
169 pw.println("Model Stats:");
170 for (Map.Entry<UUID, SoundModelStat> entry : mModelStats.entrySet()) {
171 UUID uuid = entry.getKey();
172 SoundModelStat stat = entry.getValue();
173 long totalTimeMsec = stat.mTotalTimeMsec;
174 if (stat.mIsStarted) {
175 totalTimeMsec += curTime - stat.mLastStartTimestampMsec;
176 }
177 pw.println(uuid + ", total_time(msec)=" + totalTimeMsec
178 + ", total_count=" + stat.mStartCount
179 + ", last_start=" + stat.mLastStartTimestampMsec
180 + ", last_stop=" + stat.mLastStopTimestampMsec);
181 }
182 }
183 }
184
185 private final SoundModelStatTracker mSoundModelStatTracker;
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -0800186 /** Number of ops run by the {@link RemoteSoundTriggerDetectionService} per package name */
187 @GuardedBy("mLock")
188 private final ArrayMap<String, NumOps> mNumOpsPerPackage = new ArrayMap<>();
189
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800190 public SoundTriggerService(Context context) {
191 super(context);
192 mContext = context;
193 mServiceStub = new SoundTriggerServiceStub();
194 mLocalSoundTriggerService = new LocalSoundTriggerService(context);
Chris Thorntonba08b792017-06-08 22:34:37 -0700195 mLoadedModels = new TreeMap<UUID, SoundModel>();
Chris Thorntonae5fb992017-12-07 18:26:31 -0800196 mCallbacksLock = new Object();
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800197 mCallbacks = new TreeMap<>();
Chris Thorntonba08b792017-06-08 22:34:37 -0700198 mLock = new Object();
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700199 mSoundModelStatTracker = new SoundModelStatTracker();
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800200 }
201
202 @Override
203 public void onStart() {
204 publishBinderService(Context.SOUND_TRIGGER_SERVICE, mServiceStub);
205 publishLocalService(SoundTriggerInternal.class, mLocalSoundTriggerService);
206 }
207
208 @Override
209 public void onBootPhase(int phase) {
210 if (PHASE_SYSTEM_SERVICES_READY == phase) {
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800211 initSoundTriggerHelper();
212 mLocalSoundTriggerService.setSoundTriggerHelper(mSoundTriggerHelper);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800213 } else if (PHASE_THIRD_PARTY_APPS_CAN_START == phase) {
214 mDbHelper = new SoundTriggerDbHelper(mContext);
215 }
216 }
217
218 @Override
219 public void onStartUser(int userHandle) {
220 }
221
222 @Override
223 public void onSwitchUser(int userHandle) {
224 }
225
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800226 private synchronized void initSoundTriggerHelper() {
227 if (mSoundTriggerHelper == null) {
228 mSoundTriggerHelper = new SoundTriggerHelper(mContext);
229 }
230 }
231
232 private synchronized boolean isInitialized() {
233 if (mSoundTriggerHelper == null ) {
234 Slog.e(TAG, "SoundTriggerHelper not initialized.");
235 return false;
236 }
237 return true;
238 }
239
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800240 class SoundTriggerServiceStub extends ISoundTriggerService.Stub {
241 @Override
242 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
243 throws RemoteException {
244 try {
245 return super.onTransact(code, data, reply, flags);
246 } catch (RuntimeException e) {
247 // The activity manager only throws security exceptions, so let's
248 // log all others.
249 if (!(e instanceof SecurityException)) {
250 Slog.wtf(TAG, "SoundTriggerService Crash", e);
251 }
252 throw e;
253 }
254 }
255
256 @Override
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800257 public int startRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback,
258 RecognitionConfig config) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800259 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
Arunesh Mishra2d1de782016-02-21 18:10:28 -0800260 if (!isInitialized()) return STATUS_ERROR;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800261 if (DEBUG) {
262 Slog.i(TAG, "startRecognition(): Uuid : " + parcelUuid);
263 }
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800264
Jason Hsu1363f582019-04-16 15:35:55 +0800265 sEventLogger.log(new SoundTriggerLogger.StringEvent("startRecognition(): Uuid : "
266 + parcelUuid));
267
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800268 GenericSoundModel model = getSoundModel(parcelUuid);
269 if (model == null) {
270 Slog.e(TAG, "Null model in database for id: " + parcelUuid);
Jason Hsu1363f582019-04-16 15:35:55 +0800271
272 sEventLogger.log(new SoundTriggerLogger.StringEvent(
273 "startRecognition(): Null model in database for id: " + parcelUuid));
274
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800275 return STATUS_ERROR;
276 }
277
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700278 int ret = mSoundTriggerHelper.startGenericRecognition(parcelUuid.getUuid(), model,
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800279 callback, config);
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700280 if (ret == STATUS_OK) {
281 mSoundModelStatTracker.onStart(parcelUuid.getUuid());
282 }
283 return ret;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800284 }
285
286 @Override
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800287 public int stopRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800288 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
289 if (DEBUG) {
290 Slog.i(TAG, "stopRecognition(): Uuid : " + parcelUuid);
291 }
Jason Hsu1363f582019-04-16 15:35:55 +0800292
293 sEventLogger.log(new SoundTriggerLogger.StringEvent("stopRecognition(): Uuid : "
294 + parcelUuid));
295
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800296 if (!isInitialized()) return STATUS_ERROR;
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700297
298 int ret = mSoundTriggerHelper.stopGenericRecognition(parcelUuid.getUuid(), callback);
299 if (ret == STATUS_OK) {
300 mSoundModelStatTracker.onStop(parcelUuid.getUuid());
301 }
302 return ret;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800303 }
304
305 @Override
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800306 public SoundTrigger.GenericSoundModel getSoundModel(ParcelUuid soundModelId) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800307 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
308 if (DEBUG) {
309 Slog.i(TAG, "getSoundModel(): id = " + soundModelId);
310 }
Jason Hsu1363f582019-04-16 15:35:55 +0800311
312 sEventLogger.log(new SoundTriggerLogger.StringEvent("getSoundModel(): id = "
313 + soundModelId));
314
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800315 SoundTrigger.GenericSoundModel model = mDbHelper.getGenericSoundModel(
316 soundModelId.getUuid());
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800317 return model;
318 }
319
320 @Override
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800321 public void updateSoundModel(SoundTrigger.GenericSoundModel soundModel) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800322 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
323 if (DEBUG) {
324 Slog.i(TAG, "updateSoundModel(): model = " + soundModel);
325 }
Jason Hsu1363f582019-04-16 15:35:55 +0800326
327 sEventLogger.log(new SoundTriggerLogger.StringEvent("updateSoundModel(): model = "
328 + soundModel));
329
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800330 mDbHelper.updateGenericSoundModel(soundModel);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800331 }
332
333 @Override
334 public void deleteSoundModel(ParcelUuid soundModelId) {
335 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
336 if (DEBUG) {
337 Slog.i(TAG, "deleteSoundModel(): id = " + soundModelId);
338 }
Jason Hsu1363f582019-04-16 15:35:55 +0800339
340 sEventLogger.log(new SoundTriggerLogger.StringEvent("deleteSoundModel(): id = "
341 + soundModelId));
342
Arunesh Mishra2d1de782016-02-21 18:10:28 -0800343 // Unload the model if it is loaded.
344 mSoundTriggerHelper.unloadGenericSoundModel(soundModelId.getUuid());
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800345 mDbHelper.deleteGenericSoundModel(soundModelId.getUuid());
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700346
347 // Stop recognition if it is started.
348 mSoundModelStatTracker.onStop(soundModelId.getUuid());
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800349 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700350
351 @Override
352 public int loadGenericSoundModel(GenericSoundModel soundModel) {
353 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
354 if (!isInitialized()) return STATUS_ERROR;
355 if (soundModel == null || soundModel.uuid == null) {
356 Slog.e(TAG, "Invalid sound model");
Jason Hsu1363f582019-04-16 15:35:55 +0800357
358 sEventLogger.log(new SoundTriggerLogger.StringEvent(
359 "loadGenericSoundModel(): Invalid sound model"));
360
Chris Thorntonba08b792017-06-08 22:34:37 -0700361 return STATUS_ERROR;
362 }
363 if (DEBUG) {
364 Slog.i(TAG, "loadGenericSoundModel(): id = " + soundModel.uuid);
365 }
Jason Hsu1363f582019-04-16 15:35:55 +0800366
367 sEventLogger.log(new SoundTriggerLogger.StringEvent("loadGenericSoundModel(): id = "
368 + soundModel.uuid));
369
Chris Thorntonba08b792017-06-08 22:34:37 -0700370 synchronized (mLock) {
371 SoundModel oldModel = mLoadedModels.get(soundModel.uuid);
372 // If the model we're loading is actually different than what we had loaded, we
373 // should unload that other model now. We don't care about return codes since we
374 // don't know if the other model is loaded.
375 if (oldModel != null && !oldModel.equals(soundModel)) {
376 mSoundTriggerHelper.unloadGenericSoundModel(soundModel.uuid);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800377 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800378 mCallbacks.remove(soundModel.uuid);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800379 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700380 }
381 mLoadedModels.put(soundModel.uuid, soundModel);
382 }
383 return STATUS_OK;
384 }
385
386 @Override
387 public int loadKeyphraseSoundModel(KeyphraseSoundModel soundModel) {
388 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
389 if (!isInitialized()) return STATUS_ERROR;
390 if (soundModel == null || soundModel.uuid == null) {
391 Slog.e(TAG, "Invalid sound model");
Jason Hsu1363f582019-04-16 15:35:55 +0800392
393 sEventLogger.log(new SoundTriggerLogger.StringEvent(
394 "loadKeyphraseSoundModel(): Invalid sound model"));
395
Chris Thorntonba08b792017-06-08 22:34:37 -0700396 return STATUS_ERROR;
397 }
398 if (soundModel.keyphrases == null || soundModel.keyphrases.length != 1) {
399 Slog.e(TAG, "Only one keyphrase per model is currently supported.");
Jason Hsu1363f582019-04-16 15:35:55 +0800400
401 sEventLogger.log(new SoundTriggerLogger.StringEvent(
402 "loadKeyphraseSoundModel(): Only one keyphrase per model"
403 + " is currently supported."));
404
Chris Thorntonba08b792017-06-08 22:34:37 -0700405 return STATUS_ERROR;
406 }
407 if (DEBUG) {
408 Slog.i(TAG, "loadKeyphraseSoundModel(): id = " + soundModel.uuid);
409 }
Jason Hsu1363f582019-04-16 15:35:55 +0800410
411 sEventLogger.log(new SoundTriggerLogger.StringEvent("loadKeyphraseSoundModel(): id = "
412 + soundModel.uuid));
413
Chris Thorntonba08b792017-06-08 22:34:37 -0700414 synchronized (mLock) {
415 SoundModel oldModel = mLoadedModels.get(soundModel.uuid);
416 // If the model we're loading is actually different than what we had loaded, we
417 // should unload that other model now. We don't care about return codes since we
418 // don't know if the other model is loaded.
419 if (oldModel != null && !oldModel.equals(soundModel)) {
420 mSoundTriggerHelper.unloadKeyphraseSoundModel(soundModel.keyphrases[0].id);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800421 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800422 mCallbacks.remove(soundModel.uuid);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800423 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700424 }
425 mLoadedModels.put(soundModel.uuid, soundModel);
426 }
427 return STATUS_OK;
428 }
429
430 @Override
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800431 public int startRecognitionForService(ParcelUuid soundModelId, Bundle params,
432 ComponentName detectionService, SoundTrigger.RecognitionConfig config) {
Daulet Zhanguzine789eee2019-12-20 17:19:33 +0000433 Objects.requireNonNull(soundModelId);
434 Objects.requireNonNull(detectionService);
435 Objects.requireNonNull(config);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800436
Chris Thorntonba08b792017-06-08 22:34:37 -0700437 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
Philip P. Moltmann19402f52018-03-27 14:38:03 -0700438
Chris Thorntonba08b792017-06-08 22:34:37 -0700439 if (!isInitialized()) return STATUS_ERROR;
440 if (DEBUG) {
441 Slog.i(TAG, "startRecognition(): id = " + soundModelId);
442 }
443
Jason Hsu1363f582019-04-16 15:35:55 +0800444 sEventLogger.log(new SoundTriggerLogger.StringEvent(
445 "startRecognitionForService(): id = " + soundModelId));
446
Philip P. Moltmann19402f52018-03-27 14:38:03 -0700447 IRecognitionStatusCallback callback =
448 new RemoteSoundTriggerDetectionService(soundModelId.getUuid(), params,
449 detectionService, Binder.getCallingUserHandle(), config);
450
Chris Thorntonba08b792017-06-08 22:34:37 -0700451 synchronized (mLock) {
452 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
453 if (soundModel == null) {
454 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800455
456 sEventLogger.log(new SoundTriggerLogger.StringEvent(
457 "startRecognitionForService():" + soundModelId + " is not loaded"));
458
Chris Thorntonba08b792017-06-08 22:34:37 -0700459 return STATUS_ERROR;
460 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800461 IRecognitionStatusCallback existingCallback = null;
Chris Thorntonae5fb992017-12-07 18:26:31 -0800462 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800463 existingCallback = mCallbacks.get(soundModelId.getUuid());
Chris Thorntonae5fb992017-12-07 18:26:31 -0800464 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800465 if (existingCallback != null) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700466 Slog.e(TAG, soundModelId + " is already running");
Jason Hsu1363f582019-04-16 15:35:55 +0800467
468 sEventLogger.log(new SoundTriggerLogger.StringEvent(
469 "startRecognitionForService():"
470 + soundModelId + " is already running"));
471
Chris Thorntonba08b792017-06-08 22:34:37 -0700472 return STATUS_ERROR;
473 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700474 int ret;
475 switch (soundModel.type) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700476 case SoundModel.TYPE_GENERIC_SOUND:
477 ret = mSoundTriggerHelper.startGenericRecognition(soundModel.uuid,
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800478 (GenericSoundModel) soundModel, callback, config);
Chris Thorntonba08b792017-06-08 22:34:37 -0700479 break;
480 default:
481 Slog.e(TAG, "Unknown model type");
Jason Hsu1363f582019-04-16 15:35:55 +0800482
483 sEventLogger.log(new SoundTriggerLogger.StringEvent(
484 "startRecognitionForService(): Unknown model type"));
485
Chris Thorntonba08b792017-06-08 22:34:37 -0700486 return STATUS_ERROR;
487 }
488
489 if (ret != STATUS_OK) {
490 Slog.e(TAG, "Failed to start model: " + ret);
Jason Hsu1363f582019-04-16 15:35:55 +0800491
492 sEventLogger.log(new SoundTriggerLogger.StringEvent(
493 "startRecognitionForService(): Failed to start model:"));
494
Chris Thorntonba08b792017-06-08 22:34:37 -0700495 return ret;
496 }
Chris Thorntonae5fb992017-12-07 18:26:31 -0800497 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800498 mCallbacks.put(soundModelId.getUuid(), callback);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800499 }
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700500
501 mSoundModelStatTracker.onStart(soundModelId.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700502 }
503 return STATUS_OK;
504 }
505
506 @Override
Philip P. Moltmann19402f52018-03-27 14:38:03 -0700507 public int stopRecognitionForService(ParcelUuid soundModelId) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700508 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
509 if (!isInitialized()) return STATUS_ERROR;
510 if (DEBUG) {
511 Slog.i(TAG, "stopRecognition(): id = " + soundModelId);
512 }
513
Jason Hsu1363f582019-04-16 15:35:55 +0800514 sEventLogger.log(new SoundTriggerLogger.StringEvent(
515 "stopRecognitionForService(): id = " + soundModelId));
516
Chris Thorntonba08b792017-06-08 22:34:37 -0700517 synchronized (mLock) {
518 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
519 if (soundModel == null) {
520 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800521
522 sEventLogger.log(new SoundTriggerLogger.StringEvent(
523 "stopRecognitionForService(): " + soundModelId
524 + " is not loaded"));
525
Chris Thorntonba08b792017-06-08 22:34:37 -0700526 return STATUS_ERROR;
527 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800528 IRecognitionStatusCallback callback = null;
Chris Thorntonae5fb992017-12-07 18:26:31 -0800529 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800530 callback = mCallbacks.get(soundModelId.getUuid());
Chris Thorntonae5fb992017-12-07 18:26:31 -0800531 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700532 if (callback == null) {
533 Slog.e(TAG, soundModelId + " is not running");
Jason Hsu1363f582019-04-16 15:35:55 +0800534
535 sEventLogger.log(new SoundTriggerLogger.StringEvent(
536 "stopRecognitionForService(): " + soundModelId
537 + " is not running"));
538
Chris Thorntonba08b792017-06-08 22:34:37 -0700539 return STATUS_ERROR;
540 }
541 int ret;
542 switch (soundModel.type) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700543 case SoundModel.TYPE_GENERIC_SOUND:
544 ret = mSoundTriggerHelper.stopGenericRecognition(soundModel.uuid, callback);
545 break;
546 default:
547 Slog.e(TAG, "Unknown model type");
Jason Hsu1363f582019-04-16 15:35:55 +0800548
549 sEventLogger.log(new SoundTriggerLogger.StringEvent(
550 "stopRecognitionForService(): Unknown model type"));
551
Chris Thorntonba08b792017-06-08 22:34:37 -0700552 return STATUS_ERROR;
553 }
554
555 if (ret != STATUS_OK) {
556 Slog.e(TAG, "Failed to stop model: " + ret);
Jason Hsu1363f582019-04-16 15:35:55 +0800557
558 sEventLogger.log(new SoundTriggerLogger.StringEvent(
559 "stopRecognitionForService(): Failed to stop model: " + ret));
560
Chris Thorntonba08b792017-06-08 22:34:37 -0700561 return ret;
562 }
Chris Thorntonae5fb992017-12-07 18:26:31 -0800563 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800564 mCallbacks.remove(soundModelId.getUuid());
Chris Thorntonae5fb992017-12-07 18:26:31 -0800565 }
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700566
567 mSoundModelStatTracker.onStop(soundModelId.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700568 }
569 return STATUS_OK;
570 }
571
572 @Override
573 public int unloadSoundModel(ParcelUuid soundModelId) {
574 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
575 if (!isInitialized()) return STATUS_ERROR;
576 if (DEBUG) {
577 Slog.i(TAG, "unloadSoundModel(): id = " + soundModelId);
578 }
579
Jason Hsu1363f582019-04-16 15:35:55 +0800580 sEventLogger.log(new SoundTriggerLogger.StringEvent("unloadSoundModel(): id = "
581 + soundModelId));
582
Chris Thorntonba08b792017-06-08 22:34:37 -0700583 synchronized (mLock) {
584 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
585 if (soundModel == null) {
586 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800587
588 sEventLogger.log(new SoundTriggerLogger.StringEvent(
589 "unloadSoundModel(): " + soundModelId + " is not loaded"));
590
Chris Thorntonba08b792017-06-08 22:34:37 -0700591 return STATUS_ERROR;
592 }
593 int ret;
594 switch (soundModel.type) {
595 case SoundModel.TYPE_KEYPHRASE:
596 ret = mSoundTriggerHelper.unloadKeyphraseSoundModel(
597 ((KeyphraseSoundModel)soundModel).keyphrases[0].id);
598 break;
599 case SoundModel.TYPE_GENERIC_SOUND:
600 ret = mSoundTriggerHelper.unloadGenericSoundModel(soundModel.uuid);
601 break;
602 default:
603 Slog.e(TAG, "Unknown model type");
Jason Hsu1363f582019-04-16 15:35:55 +0800604
605 sEventLogger.log(new SoundTriggerLogger.StringEvent(
606 "unloadSoundModel(): Unknown model type"));
607
Chris Thorntonba08b792017-06-08 22:34:37 -0700608 return STATUS_ERROR;
609 }
610 if (ret != STATUS_OK) {
611 Slog.e(TAG, "Failed to unload model");
Jason Hsu1363f582019-04-16 15:35:55 +0800612
613 sEventLogger.log(new SoundTriggerLogger.StringEvent(
614 "unloadSoundModel(): Failed to unload model"));
615
Chris Thorntonba08b792017-06-08 22:34:37 -0700616 return ret;
617 }
618 mLoadedModels.remove(soundModelId.getUuid());
619 return STATUS_OK;
620 }
621 }
622
623 @Override
624 public boolean isRecognitionActive(ParcelUuid parcelUuid) {
625 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
626 if (!isInitialized()) return false;
Chris Thorntonae5fb992017-12-07 18:26:31 -0800627 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800628 IRecognitionStatusCallback callback = mCallbacks.get(parcelUuid.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700629 if (callback == null) {
630 return false;
631 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700632 }
Chris Thorntonae5fb992017-12-07 18:26:31 -0800633 return mSoundTriggerHelper.isRecognitionRequested(parcelUuid.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700634 }
Michael Dooley291751e2018-10-16 19:53:29 +0000635
636 @Override
mike dooleyb2ab04a2018-11-07 15:48:54 +0100637 public int getModelState(ParcelUuid soundModelId) {
Michael Dooley291751e2018-10-16 19:53:29 +0000638 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
mike dooleyb2ab04a2018-11-07 15:48:54 +0100639 int ret = STATUS_ERROR;
640 if (!isInitialized()) return ret;
Michael Dooley291751e2018-10-16 19:53:29 +0000641 if (DEBUG) {
642 Slog.i(TAG, "getModelState(): id = " + soundModelId);
643 }
644
Jason Hsu1363f582019-04-16 15:35:55 +0800645 sEventLogger.log(new SoundTriggerLogger.StringEvent("getModelState(): id = "
646 + soundModelId));
647
Michael Dooley291751e2018-10-16 19:53:29 +0000648 synchronized (mLock) {
649 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
650 if (soundModel == null) {
651 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800652
653 sEventLogger.log(new SoundTriggerLogger.StringEvent("getModelState(): "
654 + soundModelId + " is not loaded"));
655
mike dooleyb2ab04a2018-11-07 15:48:54 +0100656 return ret;
Michael Dooley291751e2018-10-16 19:53:29 +0000657 }
Michael Dooley291751e2018-10-16 19:53:29 +0000658 switch (soundModel.type) {
Michael Dooley291751e2018-10-16 19:53:29 +0000659 case SoundModel.TYPE_GENERIC_SOUND:
660 ret = mSoundTriggerHelper.getGenericModelState(soundModel.uuid);
661 break;
662 default:
mike dooleybaa22c72019-05-15 10:05:25 +0200663 // SoundModel.TYPE_KEYPHRASE is not supported to increase privacy.
664 Slog.e(TAG, "Unsupported model type, " + soundModel.type);
Jason Hsu1363f582019-04-16 15:35:55 +0800665 sEventLogger.log(new SoundTriggerLogger.StringEvent(
mike dooleybaa22c72019-05-15 10:05:25 +0200666 "getModelState(): Unsupported model type, "
667 + soundModel.type));
Michael Dooley291751e2018-10-16 19:53:29 +0000668 break;
669 }
Michael Dooley291751e2018-10-16 19:53:29 +0000670
671 return ret;
672 }
673 }
Nicholas Ambur1aa4b4b2019-08-22 12:13:29 -0700674
675 @Override
676 @Nullable
677 public ModuleProperties getModuleProperties() {
678 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
679 if (!isInitialized()) return null;
680 if (DEBUG) {
681 Slog.i(TAG, "getModuleProperties()");
682 }
683
684 synchronized (mLock) {
685 ModuleProperties properties = mSoundTriggerHelper.getModuleProperties();
686 sEventLogger.log(new SoundTriggerLogger.StringEvent(
687 "getModuleProperties(): " + properties.toString()));
688 return properties;
689 }
690 }
Nicholas Ambura0be6be2019-10-01 10:11:39 -0700691
692 @Override
693 public int setParameter(ParcelUuid soundModelId,
694 @ModelParams int modelParam, int value) {
695 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
696 if (!isInitialized()) return STATUS_NO_INIT;
697 if (DEBUG) {
698 Slog.d(TAG, "setParameter(): id=" + soundModelId
699 + ", param=" + modelParam
700 + ", value=" + value);
701 }
702
703 sEventLogger.log(new SoundTriggerLogger.StringEvent(
704 "setParameter(): id=" + soundModelId
705 + ", param=" + modelParam
706 + ", value=" + value));
707
708 synchronized (mLock) {
709 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
710 if (soundModel == null) {
711 Slog.e(TAG, soundModelId + " is not loaded. Loaded models: "
712 + mLoadedModels.toString());
713
714 sEventLogger.log(new SoundTriggerLogger.StringEvent("setParameter(): "
715 + soundModelId + " is not loaded"));
716
717 return STATUS_BAD_VALUE;
718 }
719
720 return mSoundTriggerHelper.setParameter(soundModel.uuid, modelParam, value);
721 }
722 }
723
724 @Override
725 public int getParameter(@NonNull ParcelUuid soundModelId,
726 @ModelParams int modelParam)
727 throws UnsupportedOperationException, IllegalArgumentException {
728 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
729 if (!isInitialized()) {
730 throw new UnsupportedOperationException("SoundTriggerHelper not initialized");
731 }
732 if (DEBUG) {
733 Slog.d(TAG, "getParameter(): id=" + soundModelId
734 + ", param=" + modelParam);
735 }
736
737 sEventLogger.log(new SoundTriggerLogger.StringEvent(
738 "getParameter(): id=" + soundModelId
739 + ", param=" + modelParam));
740
741 synchronized (mLock) {
742 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
743 if (soundModel == null) {
744 Slog.e(TAG, soundModelId + " is not loaded");
745
746 sEventLogger.log(new SoundTriggerLogger.StringEvent("getParameter(): "
747 + soundModelId + " is not loaded"));
748
749 throw new IllegalArgumentException("sound model is not loaded");
750 }
751
752 return mSoundTriggerHelper.getParameter(soundModel.uuid, modelParam);
753 }
754 }
755
756 @Override
757 @Nullable
758 public ModelParamRange queryParameter(@NonNull ParcelUuid soundModelId,
759 @ModelParams int modelParam) {
760 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
761 if (!isInitialized()) return null;
762 if (DEBUG) {
763 Slog.d(TAG, "queryParameter(): id=" + soundModelId
764 + ", param=" + modelParam);
765 }
766
767 sEventLogger.log(new SoundTriggerLogger.StringEvent(
768 "queryParameter(): id=" + soundModelId
769 + ", param=" + modelParam));
770
771 synchronized (mLock) {
772 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
773 if (soundModel == null) {
774 Slog.e(TAG, soundModelId + " is not loaded");
775
776 sEventLogger.log(new SoundTriggerLogger.StringEvent(
777 "queryParameter(): "
778 + soundModelId + " is not loaded"));
779
780 return null;
781 }
782
783 return mSoundTriggerHelper.queryParameter(soundModel.uuid, modelParam);
784 }
785 }
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800786 }
787
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -0800788 /**
789 * Counts the number of operations added in the last 24 hours.
790 */
791 private static class NumOps {
792 private final Object mLock = new Object();
793
794 @GuardedBy("mLock")
795 private int[] mNumOps = new int[24];
796 @GuardedBy("mLock")
797 private long mLastOpsHourSinceBoot;
798
799 /**
800 * Clear buckets of new hours that have elapsed since last operation.
801 *
802 * <p>I.e. when the last operation was triggered at 1:40 and the current operation was
803 * triggered at 4:03, the buckets "2, 3, and 4" are cleared.
804 *
805 * @param currentTime Current elapsed time since boot in ns
806 */
807 void clearOldOps(long currentTime) {
808 synchronized (mLock) {
809 long numHoursSinceBoot = TimeUnit.HOURS.convert(currentTime, TimeUnit.NANOSECONDS);
810
811 // Clear buckets of new hours that have elapsed since last operation
812 // I.e. when the last operation was triggered at 1:40 and the current
813 // operation was triggered at 4:03, the bucket "2, 3, and 4" is cleared
814 if (mLastOpsHourSinceBoot != 0) {
815 for (long hour = mLastOpsHourSinceBoot + 1; hour <= numHoursSinceBoot; hour++) {
816 mNumOps[(int) (hour % 24)] = 0;
817 }
818 }
819 }
820 }
821
822 /**
823 * Add a new operation.
824 *
825 * @param currentTime Current elapsed time since boot in ns
826 */
827 void addOp(long currentTime) {
828 synchronized (mLock) {
829 long numHoursSinceBoot = TimeUnit.HOURS.convert(currentTime, TimeUnit.NANOSECONDS);
830
831 mNumOps[(int) (numHoursSinceBoot % 24)]++;
832 mLastOpsHourSinceBoot = numHoursSinceBoot;
833 }
834 }
835
836 /**
837 * Get the total operations added in the last 24 hours.
838 *
839 * @return The total number of operations added in the last 24 hours
840 */
841 int getOpsAdded() {
842 synchronized (mLock) {
843 int totalOperationsInLastDay = 0;
844 for (int i = 0; i < 24; i++) {
845 totalOperationsInLastDay += mNumOps[i];
846 }
847
848 return totalOperationsInLastDay;
849 }
850 }
851 }
852
Philip P. Moltmanna5b44032018-05-04 13:59:45 -0700853 /**
854 * A single operation run in a {@link RemoteSoundTriggerDetectionService}.
855 *
856 * <p>Once the remote service is connected either setup + execute or setup + stop is executed.
857 */
858 private static class Operation {
859 private interface ExecuteOp {
860 void run(int opId, ISoundTriggerDetectionService service) throws RemoteException;
861 }
862
863 private final @Nullable Runnable mSetupOp;
864 private final @NonNull ExecuteOp mExecuteOp;
865 private final @Nullable Runnable mDropOp;
866
867 private Operation(@Nullable Runnable setupOp, @NonNull ExecuteOp executeOp,
868 @Nullable Runnable cancelOp) {
869 mSetupOp = setupOp;
870 mExecuteOp = executeOp;
871 mDropOp = cancelOp;
872 }
873
874 private void setup() {
875 if (mSetupOp != null) {
876 mSetupOp.run();
877 }
878 }
879
880 void run(int opId, @NonNull ISoundTriggerDetectionService service) throws RemoteException {
881 setup();
882 mExecuteOp.run(opId, service);
883 }
884
885 void drop() {
886 setup();
887
888 if (mDropOp != null) {
889 mDropOp.run();
890 }
891 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800892 }
893
894 /**
895 * Local end for a {@link SoundTriggerDetectionService}. Operations are queued up and executed
896 * when the service connects.
897 *
898 * <p>If operations take too long they are forcefully aborted.
899 *
900 * <p>This also limits the amount of operations in 24 hours.
901 */
902 private class RemoteSoundTriggerDetectionService
903 extends IRecognitionStatusCallback.Stub implements ServiceConnection {
904 private static final int MSG_STOP_ALL_PENDING_OPERATIONS = 1;
905
906 private final Object mRemoteServiceLock = new Object();
907
908 /** UUID of the model the service is started for */
909 private final @NonNull ParcelUuid mPuuid;
910 /** Params passed into the start method for the service */
911 private final @Nullable Bundle mParams;
912 /** Component name passed when starting the service */
913 private final @NonNull ComponentName mServiceName;
914 /** User that started the service */
915 private final @NonNull UserHandle mUser;
916 /** Configuration of the recognition the service is handling */
917 private final @NonNull RecognitionConfig mRecognitionConfig;
918 /** Wake lock keeping the remote service alive */
919 private final @NonNull PowerManager.WakeLock mRemoteServiceWakeLock;
920
921 private final @NonNull Handler mHandler;
922
923 /** Callbacks that are called by the service */
924 private final @NonNull ISoundTriggerDetectionServiceClient mClient;
925
926 /** Operations that are pending because the service is not yet connected */
927 @GuardedBy("mRemoteServiceLock")
928 private final ArrayList<Operation> mPendingOps = new ArrayList<>();
929 /** Operations that have been send to the service but have no yet finished */
930 @GuardedBy("mRemoteServiceLock")
931 private final ArraySet<Integer> mRunningOpIds = new ArraySet<>();
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -0800932 /** The number of operations executed in each of the last 24 hours */
933 private final NumOps mNumOps;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800934
935 /** The service binder if connected */
936 @GuardedBy("mRemoteServiceLock")
937 private @Nullable ISoundTriggerDetectionService mService;
938 /** Whether the service has been bound */
939 @GuardedBy("mRemoteServiceLock")
940 private boolean mIsBound;
941 /** Whether the service has been destroyed */
942 @GuardedBy("mRemoteServiceLock")
943 private boolean mIsDestroyed;
944 /**
945 * Set once a final op is scheduled. No further ops can be added and the service is
946 * destroyed once the op finishes.
947 */
948 @GuardedBy("mRemoteServiceLock")
949 private boolean mDestroyOnceRunningOpsDone;
950
951 /** Total number of operations performed by this service */
952 @GuardedBy("mRemoteServiceLock")
953 private int mNumTotalOpsPerformed;
954
955 /**
956 * Create a new remote sound trigger detection service. This only binds to the service when
957 * operations are in flight. Each operation has a certain time it can run. Once no
958 * operations are allowed to run anymore, {@link #stopAllPendingOperations() all operations
959 * are aborted and stopped} and the service is disconnected.
960 *
961 * @param modelUuid The UUID of the model the recognition is for
962 * @param params The params passed to each method of the service
963 * @param serviceName The component name of the service
964 * @param user The user of the service
965 * @param config The configuration of the recognition
966 */
967 public RemoteSoundTriggerDetectionService(@NonNull UUID modelUuid,
968 @Nullable Bundle params, @NonNull ComponentName serviceName, @NonNull UserHandle user,
969 @NonNull RecognitionConfig config) {
970 mPuuid = new ParcelUuid(modelUuid);
971 mParams = params;
972 mServiceName = serviceName;
973 mUser = user;
974 mRecognitionConfig = config;
975 mHandler = new Handler(Looper.getMainLooper());
976
977 PowerManager pm = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE));
978 mRemoteServiceWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
979 "RemoteSoundTriggerDetectionService " + mServiceName.getPackageName() + ":"
980 + mServiceName.getClassName());
981
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -0800982 synchronized (mLock) {
983 NumOps numOps = mNumOpsPerPackage.get(mServiceName.getPackageName());
984 if (numOps == null) {
985 numOps = new NumOps();
986 mNumOpsPerPackage.put(mServiceName.getPackageName(), numOps);
987 }
988 mNumOps = numOps;
989 }
990
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800991 mClient = new ISoundTriggerDetectionServiceClient.Stub() {
992 @Override
993 public void onOpFinished(int opId) {
994 long token = Binder.clearCallingIdentity();
995 try {
996 synchronized (mRemoteServiceLock) {
997 mRunningOpIds.remove(opId);
998
999 if (mRunningOpIds.isEmpty() && mPendingOps.isEmpty()) {
1000 if (mDestroyOnceRunningOpsDone) {
1001 destroy();
1002 } else {
1003 disconnectLocked();
1004 }
1005 }
1006 }
1007 } finally {
1008 Binder.restoreCallingIdentity(token);
1009 }
1010 }
1011 };
1012 }
1013
1014 @Override
1015 public boolean pingBinder() {
1016 return !(mIsDestroyed || mDestroyOnceRunningOpsDone);
1017 }
1018
1019 /**
1020 * Disconnect from the service, but allow to re-connect when new operations are triggered.
1021 */
Andreas Gampe8ce7ed92018-09-05 16:53:00 -07001022 @GuardedBy("mRemoteServiceLock")
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001023 private void disconnectLocked() {
1024 if (mService != null) {
1025 try {
1026 mService.removeClient(mPuuid);
1027 } catch (Exception e) {
1028 Slog.e(TAG, mPuuid + ": Cannot remove client", e);
Jason Hsu1363f582019-04-16 15:35:55 +08001029
1030 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1031 + ": Cannot remove client"));
1032
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001033 }
1034
1035 mService = null;
1036 }
1037
1038 if (mIsBound) {
1039 mContext.unbindService(RemoteSoundTriggerDetectionService.this);
1040 mIsBound = false;
1041
1042 synchronized (mCallbacksLock) {
1043 mRemoteServiceWakeLock.release();
1044 }
1045 }
1046 }
1047
1048 /**
1049 * Disconnect, do not allow to reconnect to the service. All further operations will be
1050 * dropped.
1051 */
1052 private void destroy() {
1053 if (DEBUG) Slog.v(TAG, mPuuid + ": destroy");
1054
Jason Hsu1363f582019-04-16 15:35:55 +08001055 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid + ": destroy"));
1056
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001057 synchronized (mRemoteServiceLock) {
1058 disconnectLocked();
1059
1060 mIsDestroyed = true;
1061 }
1062
1063 // The callback is removed before the flag is set
1064 if (!mDestroyOnceRunningOpsDone) {
1065 synchronized (mCallbacksLock) {
1066 mCallbacks.remove(mPuuid.getUuid());
1067 }
1068 }
1069 }
1070
1071 /**
1072 * Stop all pending operations and then disconnect for the service.
1073 */
1074 private void stopAllPendingOperations() {
1075 synchronized (mRemoteServiceLock) {
1076 if (mIsDestroyed) {
1077 return;
1078 }
1079
1080 if (mService != null) {
1081 int numOps = mRunningOpIds.size();
1082 for (int i = 0; i < numOps; i++) {
1083 try {
1084 mService.onStopOperation(mPuuid, mRunningOpIds.valueAt(i));
1085 } catch (Exception e) {
1086 Slog.e(TAG, mPuuid + ": Could not stop operation "
1087 + mRunningOpIds.valueAt(i), e);
Jason Hsu1363f582019-04-16 15:35:55 +08001088
1089 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1090 + ": Could not stop operation " + mRunningOpIds.valueAt(i)));
1091
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001092 }
1093 }
1094
1095 mRunningOpIds.clear();
1096 }
1097
1098 disconnectLocked();
1099 }
1100 }
1101
1102 /**
1103 * Verify that the service has the expected properties and then bind to the service
1104 */
1105 private void bind() {
1106 long token = Binder.clearCallingIdentity();
1107 try {
1108 Intent i = new Intent();
1109 i.setComponent(mServiceName);
1110
1111 ResolveInfo ri = mContext.getPackageManager().resolveServiceAsUser(i,
1112 GET_SERVICES | GET_META_DATA | MATCH_DEBUG_TRIAGED_MISSING,
1113 mUser.getIdentifier());
1114
1115 if (ri == null) {
1116 Slog.w(TAG, mPuuid + ": " + mServiceName + " not found");
Jason Hsu1363f582019-04-16 15:35:55 +08001117
1118 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1119 + ": " + mServiceName + " not found"));
1120
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001121 return;
1122 }
1123
1124 if (!BIND_SOUND_TRIGGER_DETECTION_SERVICE
1125 .equals(ri.serviceInfo.permission)) {
1126 Slog.w(TAG, mPuuid + ": " + mServiceName + " does not require "
1127 + BIND_SOUND_TRIGGER_DETECTION_SERVICE);
Jason Hsu1363f582019-04-16 15:35:55 +08001128
1129 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1130 + ": " + mServiceName + " does not require "
1131 + BIND_SOUND_TRIGGER_DETECTION_SERVICE));
1132
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001133 return;
1134 }
1135
1136 mIsBound = mContext.bindServiceAsUser(i, this,
Nick Moukhine67b5e382020-03-16 17:51:37 +01001137 BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_INCLUDE_CAPABILITIES,
1138 mUser);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001139
1140 if (mIsBound) {
1141 mRemoteServiceWakeLock.acquire();
1142 } else {
1143 Slog.w(TAG, mPuuid + ": Could not bind to " + mServiceName);
Jason Hsu1363f582019-04-16 15:35:55 +08001144
1145 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1146 + ": Could not bind to " + mServiceName));
1147
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001148 }
1149 } finally {
1150 Binder.restoreCallingIdentity(token);
1151 }
1152 }
1153
1154 /**
1155 * Run an operation (i.e. send it do the service). If the service is not connected, this
1156 * binds the service and then runs the operation once connected.
1157 *
1158 * @param op The operation to run
1159 */
1160 private void runOrAddOperation(Operation op) {
1161 synchronized (mRemoteServiceLock) {
1162 if (mIsDestroyed || mDestroyOnceRunningOpsDone) {
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001163 Slog.w(TAG, mPuuid + ": Dropped operation as already destroyed or marked for "
1164 + "destruction");
1165
Jason Hsu1363f582019-04-16 15:35:55 +08001166 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1167 + ":Dropped operation as already destroyed or marked for destruction"));
1168
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001169 op.drop();
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001170 return;
1171 }
1172
1173 if (mService == null) {
1174 mPendingOps.add(op);
1175
1176 if (!mIsBound) {
1177 bind();
1178 }
1179 } else {
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001180 long currentTime = System.nanoTime();
1181 mNumOps.clearOldOps(currentTime);
1182
1183 // Drop operation if too many were executed in the last 24 hours.
1184 int opsAllowed = Settings.Global.getInt(mContext.getContentResolver(),
1185 MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
1186 Integer.MAX_VALUE);
1187
Philip P. Moltmann3d1683b2018-05-07 15:53:51 -07001188 // As we currently cannot dropping an op safely, disable throttling
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001189 int opsAdded = mNumOps.getOpsAdded();
Philip P. Moltmann3d1683b2018-05-07 15:53:51 -07001190 if (false && mNumOps.getOpsAdded() >= opsAllowed) {
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001191 try {
1192 if (DEBUG || opsAllowed + 10 > opsAdded) {
1193 Slog.w(TAG, mPuuid + ": Dropped operation as too many operations "
1194 + "were run in last 24 hours");
Jason Hsu1363f582019-04-16 15:35:55 +08001195
1196 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1197 + ": Dropped operation as too many operations "
1198 + "were run in last 24 hours"));
1199
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001200 }
1201
1202 op.drop();
1203 } catch (Exception e) {
1204 Slog.e(TAG, mPuuid + ": Could not drop operation", e);
Jason Hsu1363f582019-04-16 15:35:55 +08001205
1206 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1207 + ": Could not drop operation"));
1208
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001209 }
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001210 } else {
1211 mNumOps.addOp(currentTime);
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001212
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001213 // Find a free opID
1214 int opId = mNumTotalOpsPerformed;
1215 do {
1216 mNumTotalOpsPerformed++;
1217 } while (mRunningOpIds.contains(opId));
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001218
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001219 // Run OP
1220 try {
1221 if (DEBUG) Slog.v(TAG, mPuuid + ": runOp " + opId);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001222
Jason Hsu1363f582019-04-16 15:35:55 +08001223 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1224 + ": runOp " + opId));
1225
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001226 op.run(opId, mService);
1227 mRunningOpIds.add(opId);
1228 } catch (Exception e) {
1229 Slog.e(TAG, mPuuid + ": Could not run operation " + opId, e);
Jason Hsu1363f582019-04-16 15:35:55 +08001230
1231 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1232 + ": Could not run operation " + opId));
1233
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001234 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001235 }
1236
1237 // Unbind from service if no operations are left (i.e. if the operation failed)
1238 if (mPendingOps.isEmpty() && mRunningOpIds.isEmpty()) {
1239 if (mDestroyOnceRunningOpsDone) {
1240 destroy();
1241 } else {
1242 disconnectLocked();
1243 }
1244 } else {
1245 mHandler.removeMessages(MSG_STOP_ALL_PENDING_OPERATIONS);
1246 mHandler.sendMessageDelayed(obtainMessage(
1247 RemoteSoundTriggerDetectionService::stopAllPendingOperations, this)
1248 .setWhat(MSG_STOP_ALL_PENDING_OPERATIONS),
1249 Settings.Global.getLong(mContext.getContentResolver(),
1250 SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT,
1251 Long.MAX_VALUE));
1252 }
1253 }
1254 }
1255 }
1256
1257 @Override
1258 public void onKeyphraseDetected(SoundTrigger.KeyphraseRecognitionEvent event) {
1259 Slog.w(TAG, mPuuid + "->" + mServiceName + ": IGNORED onKeyphraseDetected(" + event
1260 + ")");
Jason Hsu1363f582019-04-16 15:35:55 +08001261
1262 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid + "->" + mServiceName
1263 + ": IGNORED onKeyphraseDetected(" + event + ")"));
1264
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001265 }
1266
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001267 /**
1268 * Create an AudioRecord enough for starting and releasing the data buffered for the event.
1269 *
1270 * @param event The event that was received
1271 * @return The initialized AudioRecord
1272 */
1273 private @NonNull AudioRecord createAudioRecordForEvent(
1274 @NonNull SoundTrigger.GenericRecognitionEvent event) {
1275 AudioAttributes.Builder attributesBuilder = new AudioAttributes.Builder();
1276 attributesBuilder.setInternalCapturePreset(MediaRecorder.AudioSource.HOTWORD);
1277 AudioAttributes attributes = attributesBuilder.build();
1278
1279 // Use same AudioFormat processing as in RecognitionEvent.fromParcel
1280 AudioFormat originalFormat = event.getCaptureFormat();
1281 AudioFormat captureFormat = (new AudioFormat.Builder())
1282 .setChannelMask(originalFormat.getChannelMask())
1283 .setEncoding(originalFormat.getEncoding())
1284 .setSampleRate(originalFormat.getSampleRate())
1285 .build();
1286
1287 int bufferSize = AudioRecord.getMinBufferSize(
1288 captureFormat.getSampleRate() == AudioFormat.SAMPLE_RATE_UNSPECIFIED
1289 ? AudioFormat.SAMPLE_RATE_HZ_MAX
1290 : captureFormat.getSampleRate(),
1291 captureFormat.getChannelCount() == 2
1292 ? AudioFormat.CHANNEL_IN_STEREO
1293 : AudioFormat.CHANNEL_IN_MONO,
1294 captureFormat.getEncoding());
1295
Jason Hsu1363f582019-04-16 15:35:55 +08001296 sEventLogger.log(new SoundTriggerLogger.StringEvent("createAudioRecordForEvent"));
1297
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001298 return new AudioRecord(attributes, captureFormat, bufferSize,
1299 event.getCaptureSession());
1300 }
1301
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001302 @Override
1303 public void onGenericSoundTriggerDetected(SoundTrigger.GenericRecognitionEvent event) {
1304 if (DEBUG) Slog.v(TAG, mPuuid + ": Generic sound trigger event: " + event);
1305
Jason Hsu1363f582019-04-16 15:35:55 +08001306 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1307 + ": Generic sound trigger event: " + event));
1308
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001309 runOrAddOperation(new Operation(
1310 // always execute:
1311 () -> {
mike dooley9b20c1c2019-01-23 10:38:17 +01001312 if (!mRecognitionConfig.allowMultipleTriggers) {
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001313 // Unregister this remoteService once op is done
1314 synchronized (mCallbacksLock) {
1315 mCallbacks.remove(mPuuid.getUuid());
1316 }
1317 mDestroyOnceRunningOpsDone = true;
1318 }
1319 },
1320 // execute if not throttled:
1321 (opId, service) -> service.onGenericRecognitionEvent(mPuuid, opId, event),
1322 // execute if throttled:
1323 () -> {
1324 if (event.isCaptureAvailable()) {
1325 AudioRecord capturedData = createAudioRecordForEvent(event);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001326
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001327 // Currently we need to start and release the audio record to reset
1328 // the DSP even if we don't want to process the event
1329 capturedData.startRecording();
1330 capturedData.release();
1331 }
1332 }));
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001333 }
1334
1335 @Override
1336 public void onError(int status) {
1337 if (DEBUG) Slog.v(TAG, mPuuid + ": onError: " + status);
1338
Jason Hsu1363f582019-04-16 15:35:55 +08001339 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1340 + ": onError: " + status));
1341
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001342 runOrAddOperation(
1343 new Operation(
1344 // always execute:
1345 () -> {
1346 // Unregister this remoteService once op is done
1347 synchronized (mCallbacksLock) {
1348 mCallbacks.remove(mPuuid.getUuid());
1349 }
1350 mDestroyOnceRunningOpsDone = true;
1351 },
1352 // execute if not throttled:
1353 (opId, service) -> service.onError(mPuuid, opId, status),
1354 // nothing to do if throttled
1355 null));
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001356 }
1357
1358 @Override
1359 public void onRecognitionPaused() {
1360 Slog.i(TAG, mPuuid + "->" + mServiceName + ": IGNORED onRecognitionPaused");
Jason Hsu1363f582019-04-16 15:35:55 +08001361
1362 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1363 + "->" + mServiceName + ": IGNORED onRecognitionPaused"));
1364
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001365 }
1366
1367 @Override
1368 public void onRecognitionResumed() {
1369 Slog.i(TAG, mPuuid + "->" + mServiceName + ": IGNORED onRecognitionResumed");
Jason Hsu1363f582019-04-16 15:35:55 +08001370
1371 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1372 + "->" + mServiceName + ": IGNORED onRecognitionResumed"));
1373
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001374 }
1375
1376 @Override
1377 public void onServiceConnected(ComponentName name, IBinder service) {
1378 if (DEBUG) Slog.v(TAG, mPuuid + ": onServiceConnected(" + service + ")");
1379
Jason Hsu1363f582019-04-16 15:35:55 +08001380 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1381 + ": onServiceConnected(" + service + ")"));
1382
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001383 synchronized (mRemoteServiceLock) {
1384 mService = ISoundTriggerDetectionService.Stub.asInterface(service);
1385
1386 try {
1387 mService.setClient(mPuuid, mParams, mClient);
1388 } catch (Exception e) {
1389 Slog.e(TAG, mPuuid + ": Could not init " + mServiceName, e);
1390 return;
1391 }
1392
1393 while (!mPendingOps.isEmpty()) {
1394 runOrAddOperation(mPendingOps.remove(0));
1395 }
1396 }
1397 }
1398
1399 @Override
1400 public void onServiceDisconnected(ComponentName name) {
1401 if (DEBUG) Slog.v(TAG, mPuuid + ": onServiceDisconnected");
1402
Jason Hsu1363f582019-04-16 15:35:55 +08001403 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1404 + ": onServiceDisconnected"));
1405
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001406 synchronized (mRemoteServiceLock) {
1407 mService = null;
1408 }
1409 }
1410
1411 @Override
1412 public void onBindingDied(ComponentName name) {
1413 if (DEBUG) Slog.v(TAG, mPuuid + ": onBindingDied");
1414
Jason Hsu1363f582019-04-16 15:35:55 +08001415 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1416 + ": onBindingDied"));
1417
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001418 synchronized (mRemoteServiceLock) {
1419 destroy();
1420 }
1421 }
1422
1423 @Override
1424 public void onNullBinding(ComponentName name) {
1425 Slog.w(TAG, name + " for model " + mPuuid + " returned a null binding");
1426
Jason Hsu1363f582019-04-16 15:35:55 +08001427 sEventLogger.log(new SoundTriggerLogger.StringEvent(name + " for model "
1428 + mPuuid + " returned a null binding"));
1429
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001430 synchronized (mRemoteServiceLock) {
1431 disconnectLocked();
1432 }
1433 }
1434 }
1435
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001436 public final class LocalSoundTriggerService extends SoundTriggerInternal {
1437 private final Context mContext;
1438 private SoundTriggerHelper mSoundTriggerHelper;
1439
1440 LocalSoundTriggerService(Context context) {
1441 mContext = context;
1442 }
1443
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001444 synchronized void setSoundTriggerHelper(SoundTriggerHelper helper) {
1445 mSoundTriggerHelper = helper;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001446 }
1447
1448 @Override
1449 public int startRecognition(int keyphraseId, KeyphraseSoundModel soundModel,
1450 IRecognitionStatusCallback listener, RecognitionConfig recognitionConfig) {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001451 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001452 return mSoundTriggerHelper.startKeyphraseRecognition(keyphraseId, soundModel, listener,
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001453 recognitionConfig);
1454 }
1455
1456 @Override
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001457 public synchronized int stopRecognition(int keyphraseId, IRecognitionStatusCallback listener) {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001458 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001459 return mSoundTriggerHelper.stopKeyphraseRecognition(keyphraseId, listener);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001460 }
1461
1462 @Override
Arunesh Mishra55a9b002016-02-01 14:06:37 -08001463 public ModuleProperties getModuleProperties() {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001464 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra55a9b002016-02-01 14:06:37 -08001465 return mSoundTriggerHelper.getModuleProperties();
1466 }
1467
1468 @Override
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001469 public int setParameter(int keyphraseId, @ModelParams int modelParam, int value) {
1470 if (!isInitialized()) throw new UnsupportedOperationException();
1471 return mSoundTriggerHelper.setKeyphraseParameter(keyphraseId, modelParam, value);
1472 }
1473
1474 @Override
1475 public int getParameter(int keyphraseId, @ModelParams int modelParam) {
1476 if (!isInitialized()) throw new UnsupportedOperationException();
1477 return mSoundTriggerHelper.getKeyphraseParameter(keyphraseId, modelParam);
1478 }
1479
1480 @Override
1481 @Nullable
1482 public ModelParamRange queryParameter(int keyphraseId, @ModelParams int modelParam) {
1483 if (!isInitialized()) throw new UnsupportedOperationException();
1484 return mSoundTriggerHelper.queryKeyphraseParameter(keyphraseId, modelParam);
1485 }
1486
1487 @Override
Arunesh Mishra2d1de782016-02-21 18:10:28 -08001488 public int unloadKeyphraseModel(int keyphraseId) {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001489 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra2d1de782016-02-21 18:10:28 -08001490 return mSoundTriggerHelper.unloadKeyphraseSoundModel(keyphraseId);
1491 }
1492
1493 @Override
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001494 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001495 if (!isInitialized()) return;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001496 mSoundTriggerHelper.dump(fd, pw, args);
Jason Hsu1363f582019-04-16 15:35:55 +08001497 // log
1498 sEventLogger.dump(pw);
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -07001499
Nicholas Amburd3ec82f2020-01-03 17:44:19 -08001500 // enrolled models
1501 mDbHelper.dump(pw);
1502
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -07001503 // stats
1504 mSoundModelStatTracker.dump(pw);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001505 }
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001506
1507 private synchronized boolean isInitialized() {
1508 if (mSoundTriggerHelper == null ) {
1509 Slog.e(TAG, "SoundTriggerHelper not initialized.");
Jason Hsu1363f582019-04-16 15:35:55 +08001510
1511 sEventLogger.log(new SoundTriggerLogger.StringEvent(
1512 "SoundTriggerHelper not initialized."));
1513
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001514 return false;
1515 }
1516 return true;
1517 }
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001518 }
1519
1520 private void enforceCallingPermission(String permission) {
1521 if (mContext.checkCallingOrSelfPermission(permission)
1522 != PackageManager.PERMISSION_GRANTED) {
1523 throw new SecurityException("Caller does not hold the permission " + permission);
1524 }
1525 }
Jason Hsu1363f582019-04-16 15:35:55 +08001526
1527 //=================================================================
1528 // For logging
1529
1530 private static final SoundTriggerLogger sEventLogger = new SoundTriggerLogger(200,
1531 "SoundTrigger activity");
1532
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001533}