blob: 00cb6dc7400ddfda2cb55710d99e40450a9ff72d [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) {
Nicholas Ambur78a22682020-03-23 15:42:21 -0700210 Slog.d(TAG, "onBootPhase: " + phase + " : " + isSafeMode());
211 if (PHASE_DEVICE_SPECIFIC_SERVICES_READY == phase) {
212 if (isSafeMode()) {
213 Slog.w(TAG, "not enabling SoundTriggerService in safe mode");
214 return;
215 }
216
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800217 initSoundTriggerHelper();
218 mLocalSoundTriggerService.setSoundTriggerHelper(mSoundTriggerHelper);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800219 } else if (PHASE_THIRD_PARTY_APPS_CAN_START == phase) {
220 mDbHelper = new SoundTriggerDbHelper(mContext);
221 }
222 }
223
224 @Override
225 public void onStartUser(int userHandle) {
226 }
227
228 @Override
229 public void onSwitchUser(int userHandle) {
230 }
231
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800232 private synchronized void initSoundTriggerHelper() {
233 if (mSoundTriggerHelper == null) {
234 mSoundTriggerHelper = new SoundTriggerHelper(mContext);
235 }
236 }
237
238 private synchronized boolean isInitialized() {
239 if (mSoundTriggerHelper == null ) {
240 Slog.e(TAG, "SoundTriggerHelper not initialized.");
241 return false;
242 }
243 return true;
244 }
245
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800246 class SoundTriggerServiceStub extends ISoundTriggerService.Stub {
247 @Override
248 public boolean onTransact(int code, Parcel data, Parcel reply, int flags)
249 throws RemoteException {
250 try {
251 return super.onTransact(code, data, reply, flags);
252 } catch (RuntimeException e) {
253 // The activity manager only throws security exceptions, so let's
254 // log all others.
255 if (!(e instanceof SecurityException)) {
256 Slog.wtf(TAG, "SoundTriggerService Crash", e);
257 }
258 throw e;
259 }
260 }
261
262 @Override
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800263 public int startRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback,
264 RecognitionConfig config) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800265 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
Arunesh Mishra2d1de782016-02-21 18:10:28 -0800266 if (!isInitialized()) return STATUS_ERROR;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800267 if (DEBUG) {
268 Slog.i(TAG, "startRecognition(): Uuid : " + parcelUuid);
269 }
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800270
Jason Hsu1363f582019-04-16 15:35:55 +0800271 sEventLogger.log(new SoundTriggerLogger.StringEvent("startRecognition(): Uuid : "
272 + parcelUuid));
273
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800274 GenericSoundModel model = getSoundModel(parcelUuid);
275 if (model == null) {
276 Slog.e(TAG, "Null model in database for id: " + parcelUuid);
Jason Hsu1363f582019-04-16 15:35:55 +0800277
278 sEventLogger.log(new SoundTriggerLogger.StringEvent(
279 "startRecognition(): Null model in database for id: " + parcelUuid));
280
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800281 return STATUS_ERROR;
282 }
283
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700284 int ret = mSoundTriggerHelper.startGenericRecognition(parcelUuid.getUuid(), model,
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800285 callback, config);
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700286 if (ret == STATUS_OK) {
287 mSoundModelStatTracker.onStart(parcelUuid.getUuid());
288 }
289 return ret;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800290 }
291
292 @Override
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800293 public int stopRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800294 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
295 if (DEBUG) {
296 Slog.i(TAG, "stopRecognition(): Uuid : " + parcelUuid);
297 }
Jason Hsu1363f582019-04-16 15:35:55 +0800298
299 sEventLogger.log(new SoundTriggerLogger.StringEvent("stopRecognition(): Uuid : "
300 + parcelUuid));
301
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800302 if (!isInitialized()) return STATUS_ERROR;
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700303
304 int ret = mSoundTriggerHelper.stopGenericRecognition(parcelUuid.getUuid(), callback);
305 if (ret == STATUS_OK) {
306 mSoundModelStatTracker.onStop(parcelUuid.getUuid());
307 }
308 return ret;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800309 }
310
311 @Override
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800312 public SoundTrigger.GenericSoundModel getSoundModel(ParcelUuid soundModelId) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800313 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
314 if (DEBUG) {
315 Slog.i(TAG, "getSoundModel(): id = " + soundModelId);
316 }
Jason Hsu1363f582019-04-16 15:35:55 +0800317
318 sEventLogger.log(new SoundTriggerLogger.StringEvent("getSoundModel(): id = "
319 + soundModelId));
320
Arunesh Mishra3fff7f52016-02-09 12:15:19 -0800321 SoundTrigger.GenericSoundModel model = mDbHelper.getGenericSoundModel(
322 soundModelId.getUuid());
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800323 return model;
324 }
325
326 @Override
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800327 public void updateSoundModel(SoundTrigger.GenericSoundModel soundModel) {
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800328 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
329 if (DEBUG) {
330 Slog.i(TAG, "updateSoundModel(): model = " + soundModel);
331 }
Jason Hsu1363f582019-04-16 15:35:55 +0800332
333 sEventLogger.log(new SoundTriggerLogger.StringEvent("updateSoundModel(): model = "
334 + soundModel));
335
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800336 mDbHelper.updateGenericSoundModel(soundModel);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800337 }
338
339 @Override
340 public void deleteSoundModel(ParcelUuid soundModelId) {
341 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
342 if (DEBUG) {
343 Slog.i(TAG, "deleteSoundModel(): id = " + soundModelId);
344 }
Jason Hsu1363f582019-04-16 15:35:55 +0800345
346 sEventLogger.log(new SoundTriggerLogger.StringEvent("deleteSoundModel(): id = "
347 + soundModelId));
348
Arunesh Mishra2d1de782016-02-21 18:10:28 -0800349 // Unload the model if it is loaded.
350 mSoundTriggerHelper.unloadGenericSoundModel(soundModelId.getUuid());
Arunesh Mishrac722ec412016-01-27 13:29:12 -0800351 mDbHelper.deleteGenericSoundModel(soundModelId.getUuid());
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700352
353 // Stop recognition if it is started.
354 mSoundModelStatTracker.onStop(soundModelId.getUuid());
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800355 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700356
357 @Override
358 public int loadGenericSoundModel(GenericSoundModel soundModel) {
359 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
360 if (!isInitialized()) return STATUS_ERROR;
361 if (soundModel == null || soundModel.uuid == null) {
362 Slog.e(TAG, "Invalid sound model");
Jason Hsu1363f582019-04-16 15:35:55 +0800363
364 sEventLogger.log(new SoundTriggerLogger.StringEvent(
365 "loadGenericSoundModel(): Invalid sound model"));
366
Chris Thorntonba08b792017-06-08 22:34:37 -0700367 return STATUS_ERROR;
368 }
369 if (DEBUG) {
370 Slog.i(TAG, "loadGenericSoundModel(): id = " + soundModel.uuid);
371 }
Jason Hsu1363f582019-04-16 15:35:55 +0800372
373 sEventLogger.log(new SoundTriggerLogger.StringEvent("loadGenericSoundModel(): id = "
374 + soundModel.uuid));
375
Chris Thorntonba08b792017-06-08 22:34:37 -0700376 synchronized (mLock) {
377 SoundModel oldModel = mLoadedModels.get(soundModel.uuid);
378 // If the model we're loading is actually different than what we had loaded, we
379 // should unload that other model now. We don't care about return codes since we
380 // don't know if the other model is loaded.
381 if (oldModel != null && !oldModel.equals(soundModel)) {
382 mSoundTriggerHelper.unloadGenericSoundModel(soundModel.uuid);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800383 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800384 mCallbacks.remove(soundModel.uuid);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800385 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700386 }
387 mLoadedModels.put(soundModel.uuid, soundModel);
388 }
389 return STATUS_OK;
390 }
391
392 @Override
393 public int loadKeyphraseSoundModel(KeyphraseSoundModel soundModel) {
394 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
395 if (!isInitialized()) return STATUS_ERROR;
396 if (soundModel == null || soundModel.uuid == null) {
397 Slog.e(TAG, "Invalid sound model");
Jason Hsu1363f582019-04-16 15:35:55 +0800398
399 sEventLogger.log(new SoundTriggerLogger.StringEvent(
400 "loadKeyphraseSoundModel(): Invalid sound model"));
401
Chris Thorntonba08b792017-06-08 22:34:37 -0700402 return STATUS_ERROR;
403 }
404 if (soundModel.keyphrases == null || soundModel.keyphrases.length != 1) {
405 Slog.e(TAG, "Only one keyphrase per model is currently supported.");
Jason Hsu1363f582019-04-16 15:35:55 +0800406
407 sEventLogger.log(new SoundTriggerLogger.StringEvent(
408 "loadKeyphraseSoundModel(): Only one keyphrase per model"
409 + " is currently supported."));
410
Chris Thorntonba08b792017-06-08 22:34:37 -0700411 return STATUS_ERROR;
412 }
413 if (DEBUG) {
414 Slog.i(TAG, "loadKeyphraseSoundModel(): id = " + soundModel.uuid);
415 }
Jason Hsu1363f582019-04-16 15:35:55 +0800416
417 sEventLogger.log(new SoundTriggerLogger.StringEvent("loadKeyphraseSoundModel(): id = "
418 + soundModel.uuid));
419
Chris Thorntonba08b792017-06-08 22:34:37 -0700420 synchronized (mLock) {
421 SoundModel oldModel = mLoadedModels.get(soundModel.uuid);
422 // If the model we're loading is actually different than what we had loaded, we
423 // should unload that other model now. We don't care about return codes since we
424 // don't know if the other model is loaded.
425 if (oldModel != null && !oldModel.equals(soundModel)) {
426 mSoundTriggerHelper.unloadKeyphraseSoundModel(soundModel.keyphrases[0].id);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800427 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800428 mCallbacks.remove(soundModel.uuid);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800429 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700430 }
431 mLoadedModels.put(soundModel.uuid, soundModel);
432 }
433 return STATUS_OK;
434 }
435
436 @Override
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800437 public int startRecognitionForService(ParcelUuid soundModelId, Bundle params,
438 ComponentName detectionService, SoundTrigger.RecognitionConfig config) {
Daulet Zhanguzine789eee2019-12-20 17:19:33 +0000439 Objects.requireNonNull(soundModelId);
440 Objects.requireNonNull(detectionService);
441 Objects.requireNonNull(config);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800442
Chris Thorntonba08b792017-06-08 22:34:37 -0700443 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
Philip P. Moltmann19402f52018-03-27 14:38:03 -0700444
Chris Thorntonba08b792017-06-08 22:34:37 -0700445 if (!isInitialized()) return STATUS_ERROR;
446 if (DEBUG) {
447 Slog.i(TAG, "startRecognition(): id = " + soundModelId);
448 }
449
Jason Hsu1363f582019-04-16 15:35:55 +0800450 sEventLogger.log(new SoundTriggerLogger.StringEvent(
451 "startRecognitionForService(): id = " + soundModelId));
452
Philip P. Moltmann19402f52018-03-27 14:38:03 -0700453 IRecognitionStatusCallback callback =
454 new RemoteSoundTriggerDetectionService(soundModelId.getUuid(), params,
455 detectionService, Binder.getCallingUserHandle(), config);
456
Chris Thorntonba08b792017-06-08 22:34:37 -0700457 synchronized (mLock) {
458 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
459 if (soundModel == null) {
460 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800461
462 sEventLogger.log(new SoundTriggerLogger.StringEvent(
463 "startRecognitionForService():" + soundModelId + " is not loaded"));
464
Chris Thorntonba08b792017-06-08 22:34:37 -0700465 return STATUS_ERROR;
466 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800467 IRecognitionStatusCallback existingCallback = null;
Chris Thorntonae5fb992017-12-07 18:26:31 -0800468 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800469 existingCallback = mCallbacks.get(soundModelId.getUuid());
Chris Thorntonae5fb992017-12-07 18:26:31 -0800470 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800471 if (existingCallback != null) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700472 Slog.e(TAG, soundModelId + " is already running");
Jason Hsu1363f582019-04-16 15:35:55 +0800473
474 sEventLogger.log(new SoundTriggerLogger.StringEvent(
475 "startRecognitionForService():"
476 + soundModelId + " is already running"));
477
Chris Thorntonba08b792017-06-08 22:34:37 -0700478 return STATUS_ERROR;
479 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700480 int ret;
481 switch (soundModel.type) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700482 case SoundModel.TYPE_GENERIC_SOUND:
483 ret = mSoundTriggerHelper.startGenericRecognition(soundModel.uuid,
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800484 (GenericSoundModel) soundModel, callback, config);
Chris Thorntonba08b792017-06-08 22:34:37 -0700485 break;
486 default:
487 Slog.e(TAG, "Unknown model type");
Jason Hsu1363f582019-04-16 15:35:55 +0800488
489 sEventLogger.log(new SoundTriggerLogger.StringEvent(
490 "startRecognitionForService(): Unknown model type"));
491
Chris Thorntonba08b792017-06-08 22:34:37 -0700492 return STATUS_ERROR;
493 }
494
495 if (ret != STATUS_OK) {
496 Slog.e(TAG, "Failed to start model: " + ret);
Jason Hsu1363f582019-04-16 15:35:55 +0800497
498 sEventLogger.log(new SoundTriggerLogger.StringEvent(
499 "startRecognitionForService(): Failed to start model:"));
500
Chris Thorntonba08b792017-06-08 22:34:37 -0700501 return ret;
502 }
Chris Thorntonae5fb992017-12-07 18:26:31 -0800503 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800504 mCallbacks.put(soundModelId.getUuid(), callback);
Chris Thorntonae5fb992017-12-07 18:26:31 -0800505 }
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700506
507 mSoundModelStatTracker.onStart(soundModelId.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700508 }
509 return STATUS_OK;
510 }
511
512 @Override
Philip P. Moltmann19402f52018-03-27 14:38:03 -0700513 public int stopRecognitionForService(ParcelUuid soundModelId) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700514 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
515 if (!isInitialized()) return STATUS_ERROR;
516 if (DEBUG) {
517 Slog.i(TAG, "stopRecognition(): id = " + soundModelId);
518 }
519
Jason Hsu1363f582019-04-16 15:35:55 +0800520 sEventLogger.log(new SoundTriggerLogger.StringEvent(
521 "stopRecognitionForService(): id = " + soundModelId));
522
Chris Thorntonba08b792017-06-08 22:34:37 -0700523 synchronized (mLock) {
524 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
525 if (soundModel == null) {
526 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800527
528 sEventLogger.log(new SoundTriggerLogger.StringEvent(
529 "stopRecognitionForService(): " + soundModelId
530 + " is not loaded"));
531
Chris Thorntonba08b792017-06-08 22:34:37 -0700532 return STATUS_ERROR;
533 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800534 IRecognitionStatusCallback callback = null;
Chris Thorntonae5fb992017-12-07 18:26:31 -0800535 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800536 callback = mCallbacks.get(soundModelId.getUuid());
Chris Thorntonae5fb992017-12-07 18:26:31 -0800537 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700538 if (callback == null) {
539 Slog.e(TAG, soundModelId + " is not running");
Jason Hsu1363f582019-04-16 15:35:55 +0800540
541 sEventLogger.log(new SoundTriggerLogger.StringEvent(
542 "stopRecognitionForService(): " + soundModelId
543 + " is not running"));
544
Chris Thorntonba08b792017-06-08 22:34:37 -0700545 return STATUS_ERROR;
546 }
547 int ret;
548 switch (soundModel.type) {
Chris Thorntonba08b792017-06-08 22:34:37 -0700549 case SoundModel.TYPE_GENERIC_SOUND:
550 ret = mSoundTriggerHelper.stopGenericRecognition(soundModel.uuid, callback);
551 break;
552 default:
553 Slog.e(TAG, "Unknown model type");
Jason Hsu1363f582019-04-16 15:35:55 +0800554
555 sEventLogger.log(new SoundTriggerLogger.StringEvent(
556 "stopRecognitionForService(): Unknown model type"));
557
Chris Thorntonba08b792017-06-08 22:34:37 -0700558 return STATUS_ERROR;
559 }
560
561 if (ret != STATUS_OK) {
562 Slog.e(TAG, "Failed to stop model: " + ret);
Jason Hsu1363f582019-04-16 15:35:55 +0800563
564 sEventLogger.log(new SoundTriggerLogger.StringEvent(
565 "stopRecognitionForService(): Failed to stop model: " + ret));
566
Chris Thorntonba08b792017-06-08 22:34:37 -0700567 return ret;
568 }
Chris Thorntonae5fb992017-12-07 18:26:31 -0800569 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800570 mCallbacks.remove(soundModelId.getUuid());
Chris Thorntonae5fb992017-12-07 18:26:31 -0800571 }
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -0700572
573 mSoundModelStatTracker.onStop(soundModelId.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700574 }
575 return STATUS_OK;
576 }
577
578 @Override
579 public int unloadSoundModel(ParcelUuid soundModelId) {
580 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
581 if (!isInitialized()) return STATUS_ERROR;
582 if (DEBUG) {
583 Slog.i(TAG, "unloadSoundModel(): id = " + soundModelId);
584 }
585
Jason Hsu1363f582019-04-16 15:35:55 +0800586 sEventLogger.log(new SoundTriggerLogger.StringEvent("unloadSoundModel(): id = "
587 + soundModelId));
588
Chris Thorntonba08b792017-06-08 22:34:37 -0700589 synchronized (mLock) {
590 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
591 if (soundModel == null) {
592 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800593
594 sEventLogger.log(new SoundTriggerLogger.StringEvent(
595 "unloadSoundModel(): " + soundModelId + " is not loaded"));
596
Chris Thorntonba08b792017-06-08 22:34:37 -0700597 return STATUS_ERROR;
598 }
599 int ret;
600 switch (soundModel.type) {
601 case SoundModel.TYPE_KEYPHRASE:
602 ret = mSoundTriggerHelper.unloadKeyphraseSoundModel(
603 ((KeyphraseSoundModel)soundModel).keyphrases[0].id);
604 break;
605 case SoundModel.TYPE_GENERIC_SOUND:
606 ret = mSoundTriggerHelper.unloadGenericSoundModel(soundModel.uuid);
607 break;
608 default:
609 Slog.e(TAG, "Unknown model type");
Jason Hsu1363f582019-04-16 15:35:55 +0800610
611 sEventLogger.log(new SoundTriggerLogger.StringEvent(
612 "unloadSoundModel(): Unknown model type"));
613
Chris Thorntonba08b792017-06-08 22:34:37 -0700614 return STATUS_ERROR;
615 }
616 if (ret != STATUS_OK) {
617 Slog.e(TAG, "Failed to unload model");
Jason Hsu1363f582019-04-16 15:35:55 +0800618
619 sEventLogger.log(new SoundTriggerLogger.StringEvent(
620 "unloadSoundModel(): Failed to unload model"));
621
Chris Thorntonba08b792017-06-08 22:34:37 -0700622 return ret;
623 }
624 mLoadedModels.remove(soundModelId.getUuid());
625 return STATUS_OK;
626 }
627 }
628
629 @Override
630 public boolean isRecognitionActive(ParcelUuid parcelUuid) {
631 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
632 if (!isInitialized()) return false;
Chris Thorntonae5fb992017-12-07 18:26:31 -0800633 synchronized (mCallbacksLock) {
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800634 IRecognitionStatusCallback callback = mCallbacks.get(parcelUuid.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700635 if (callback == null) {
636 return false;
637 }
Chris Thorntonba08b792017-06-08 22:34:37 -0700638 }
Chris Thorntonae5fb992017-12-07 18:26:31 -0800639 return mSoundTriggerHelper.isRecognitionRequested(parcelUuid.getUuid());
Chris Thorntonba08b792017-06-08 22:34:37 -0700640 }
Michael Dooley291751e2018-10-16 19:53:29 +0000641
642 @Override
mike dooleyb2ab04a2018-11-07 15:48:54 +0100643 public int getModelState(ParcelUuid soundModelId) {
Michael Dooley291751e2018-10-16 19:53:29 +0000644 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
mike dooleyb2ab04a2018-11-07 15:48:54 +0100645 int ret = STATUS_ERROR;
646 if (!isInitialized()) return ret;
Michael Dooley291751e2018-10-16 19:53:29 +0000647 if (DEBUG) {
648 Slog.i(TAG, "getModelState(): id = " + soundModelId);
649 }
650
Jason Hsu1363f582019-04-16 15:35:55 +0800651 sEventLogger.log(new SoundTriggerLogger.StringEvent("getModelState(): id = "
652 + soundModelId));
653
Michael Dooley291751e2018-10-16 19:53:29 +0000654 synchronized (mLock) {
655 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
656 if (soundModel == null) {
657 Slog.e(TAG, soundModelId + " is not loaded");
Jason Hsu1363f582019-04-16 15:35:55 +0800658
659 sEventLogger.log(new SoundTriggerLogger.StringEvent("getModelState(): "
660 + soundModelId + " is not loaded"));
661
mike dooleyb2ab04a2018-11-07 15:48:54 +0100662 return ret;
Michael Dooley291751e2018-10-16 19:53:29 +0000663 }
Michael Dooley291751e2018-10-16 19:53:29 +0000664 switch (soundModel.type) {
Michael Dooley291751e2018-10-16 19:53:29 +0000665 case SoundModel.TYPE_GENERIC_SOUND:
666 ret = mSoundTriggerHelper.getGenericModelState(soundModel.uuid);
667 break;
668 default:
mike dooleybaa22c72019-05-15 10:05:25 +0200669 // SoundModel.TYPE_KEYPHRASE is not supported to increase privacy.
670 Slog.e(TAG, "Unsupported model type, " + soundModel.type);
Jason Hsu1363f582019-04-16 15:35:55 +0800671 sEventLogger.log(new SoundTriggerLogger.StringEvent(
mike dooleybaa22c72019-05-15 10:05:25 +0200672 "getModelState(): Unsupported model type, "
673 + soundModel.type));
Michael Dooley291751e2018-10-16 19:53:29 +0000674 break;
675 }
Michael Dooley291751e2018-10-16 19:53:29 +0000676
677 return ret;
678 }
679 }
Nicholas Ambur1aa4b4b2019-08-22 12:13:29 -0700680
681 @Override
682 @Nullable
683 public ModuleProperties getModuleProperties() {
684 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
685 if (!isInitialized()) return null;
686 if (DEBUG) {
687 Slog.i(TAG, "getModuleProperties()");
688 }
689
690 synchronized (mLock) {
691 ModuleProperties properties = mSoundTriggerHelper.getModuleProperties();
692 sEventLogger.log(new SoundTriggerLogger.StringEvent(
693 "getModuleProperties(): " + properties.toString()));
694 return properties;
695 }
696 }
Nicholas Ambura0be6be2019-10-01 10:11:39 -0700697
698 @Override
699 public int setParameter(ParcelUuid soundModelId,
700 @ModelParams int modelParam, int value) {
701 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
702 if (!isInitialized()) return STATUS_NO_INIT;
703 if (DEBUG) {
704 Slog.d(TAG, "setParameter(): id=" + soundModelId
705 + ", param=" + modelParam
706 + ", value=" + value);
707 }
708
709 sEventLogger.log(new SoundTriggerLogger.StringEvent(
710 "setParameter(): id=" + soundModelId
711 + ", param=" + modelParam
712 + ", value=" + value));
713
714 synchronized (mLock) {
715 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
716 if (soundModel == null) {
717 Slog.e(TAG, soundModelId + " is not loaded. Loaded models: "
718 + mLoadedModels.toString());
719
720 sEventLogger.log(new SoundTriggerLogger.StringEvent("setParameter(): "
721 + soundModelId + " is not loaded"));
722
723 return STATUS_BAD_VALUE;
724 }
725
726 return mSoundTriggerHelper.setParameter(soundModel.uuid, modelParam, value);
727 }
728 }
729
730 @Override
731 public int getParameter(@NonNull ParcelUuid soundModelId,
732 @ModelParams int modelParam)
733 throws UnsupportedOperationException, IllegalArgumentException {
734 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
735 if (!isInitialized()) {
736 throw new UnsupportedOperationException("SoundTriggerHelper not initialized");
737 }
738 if (DEBUG) {
739 Slog.d(TAG, "getParameter(): id=" + soundModelId
740 + ", param=" + modelParam);
741 }
742
743 sEventLogger.log(new SoundTriggerLogger.StringEvent(
744 "getParameter(): id=" + soundModelId
745 + ", param=" + modelParam));
746
747 synchronized (mLock) {
748 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
749 if (soundModel == null) {
750 Slog.e(TAG, soundModelId + " is not loaded");
751
752 sEventLogger.log(new SoundTriggerLogger.StringEvent("getParameter(): "
753 + soundModelId + " is not loaded"));
754
755 throw new IllegalArgumentException("sound model is not loaded");
756 }
757
758 return mSoundTriggerHelper.getParameter(soundModel.uuid, modelParam);
759 }
760 }
761
762 @Override
763 @Nullable
764 public ModelParamRange queryParameter(@NonNull ParcelUuid soundModelId,
765 @ModelParams int modelParam) {
766 enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER);
767 if (!isInitialized()) return null;
768 if (DEBUG) {
769 Slog.d(TAG, "queryParameter(): id=" + soundModelId
770 + ", param=" + modelParam);
771 }
772
773 sEventLogger.log(new SoundTriggerLogger.StringEvent(
774 "queryParameter(): id=" + soundModelId
775 + ", param=" + modelParam));
776
777 synchronized (mLock) {
778 SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid());
779 if (soundModel == null) {
780 Slog.e(TAG, soundModelId + " is not loaded");
781
782 sEventLogger.log(new SoundTriggerLogger.StringEvent(
783 "queryParameter(): "
784 + soundModelId + " is not loaded"));
785
786 return null;
787 }
788
789 return mSoundTriggerHelper.queryParameter(soundModel.uuid, modelParam);
790 }
791 }
Arunesh Mishraa772e5f2016-01-25 10:33:11 -0800792 }
793
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -0800794 /**
795 * Counts the number of operations added in the last 24 hours.
796 */
797 private static class NumOps {
798 private final Object mLock = new Object();
799
800 @GuardedBy("mLock")
801 private int[] mNumOps = new int[24];
802 @GuardedBy("mLock")
803 private long mLastOpsHourSinceBoot;
804
805 /**
806 * Clear buckets of new hours that have elapsed since last operation.
807 *
808 * <p>I.e. when the last operation was triggered at 1:40 and the current operation was
809 * triggered at 4:03, the buckets "2, 3, and 4" are cleared.
810 *
811 * @param currentTime Current elapsed time since boot in ns
812 */
813 void clearOldOps(long currentTime) {
814 synchronized (mLock) {
815 long numHoursSinceBoot = TimeUnit.HOURS.convert(currentTime, TimeUnit.NANOSECONDS);
816
817 // Clear buckets of new hours that have elapsed since last operation
818 // I.e. when the last operation was triggered at 1:40 and the current
819 // operation was triggered at 4:03, the bucket "2, 3, and 4" is cleared
820 if (mLastOpsHourSinceBoot != 0) {
821 for (long hour = mLastOpsHourSinceBoot + 1; hour <= numHoursSinceBoot; hour++) {
822 mNumOps[(int) (hour % 24)] = 0;
823 }
824 }
825 }
826 }
827
828 /**
829 * Add a new operation.
830 *
831 * @param currentTime Current elapsed time since boot in ns
832 */
833 void addOp(long currentTime) {
834 synchronized (mLock) {
835 long numHoursSinceBoot = TimeUnit.HOURS.convert(currentTime, TimeUnit.NANOSECONDS);
836
837 mNumOps[(int) (numHoursSinceBoot % 24)]++;
838 mLastOpsHourSinceBoot = numHoursSinceBoot;
839 }
840 }
841
842 /**
843 * Get the total operations added in the last 24 hours.
844 *
845 * @return The total number of operations added in the last 24 hours
846 */
847 int getOpsAdded() {
848 synchronized (mLock) {
849 int totalOperationsInLastDay = 0;
850 for (int i = 0; i < 24; i++) {
851 totalOperationsInLastDay += mNumOps[i];
852 }
853
854 return totalOperationsInLastDay;
855 }
856 }
857 }
858
Philip P. Moltmanna5b44032018-05-04 13:59:45 -0700859 /**
860 * A single operation run in a {@link RemoteSoundTriggerDetectionService}.
861 *
862 * <p>Once the remote service is connected either setup + execute or setup + stop is executed.
863 */
864 private static class Operation {
865 private interface ExecuteOp {
866 void run(int opId, ISoundTriggerDetectionService service) throws RemoteException;
867 }
868
869 private final @Nullable Runnable mSetupOp;
870 private final @NonNull ExecuteOp mExecuteOp;
871 private final @Nullable Runnable mDropOp;
872
873 private Operation(@Nullable Runnable setupOp, @NonNull ExecuteOp executeOp,
874 @Nullable Runnable cancelOp) {
875 mSetupOp = setupOp;
876 mExecuteOp = executeOp;
877 mDropOp = cancelOp;
878 }
879
880 private void setup() {
881 if (mSetupOp != null) {
882 mSetupOp.run();
883 }
884 }
885
886 void run(int opId, @NonNull ISoundTriggerDetectionService service) throws RemoteException {
887 setup();
888 mExecuteOp.run(opId, service);
889 }
890
891 void drop() {
892 setup();
893
894 if (mDropOp != null) {
895 mDropOp.run();
896 }
897 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800898 }
899
900 /**
901 * Local end for a {@link SoundTriggerDetectionService}. Operations are queued up and executed
902 * when the service connects.
903 *
904 * <p>If operations take too long they are forcefully aborted.
905 *
906 * <p>This also limits the amount of operations in 24 hours.
907 */
908 private class RemoteSoundTriggerDetectionService
909 extends IRecognitionStatusCallback.Stub implements ServiceConnection {
910 private static final int MSG_STOP_ALL_PENDING_OPERATIONS = 1;
911
912 private final Object mRemoteServiceLock = new Object();
913
914 /** UUID of the model the service is started for */
915 private final @NonNull ParcelUuid mPuuid;
916 /** Params passed into the start method for the service */
917 private final @Nullable Bundle mParams;
918 /** Component name passed when starting the service */
919 private final @NonNull ComponentName mServiceName;
920 /** User that started the service */
921 private final @NonNull UserHandle mUser;
922 /** Configuration of the recognition the service is handling */
923 private final @NonNull RecognitionConfig mRecognitionConfig;
924 /** Wake lock keeping the remote service alive */
925 private final @NonNull PowerManager.WakeLock mRemoteServiceWakeLock;
926
927 private final @NonNull Handler mHandler;
928
929 /** Callbacks that are called by the service */
930 private final @NonNull ISoundTriggerDetectionServiceClient mClient;
931
932 /** Operations that are pending because the service is not yet connected */
933 @GuardedBy("mRemoteServiceLock")
934 private final ArrayList<Operation> mPendingOps = new ArrayList<>();
935 /** Operations that have been send to the service but have no yet finished */
936 @GuardedBy("mRemoteServiceLock")
937 private final ArraySet<Integer> mRunningOpIds = new ArraySet<>();
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -0800938 /** The number of operations executed in each of the last 24 hours */
939 private final NumOps mNumOps;
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800940
941 /** The service binder if connected */
942 @GuardedBy("mRemoteServiceLock")
943 private @Nullable ISoundTriggerDetectionService mService;
944 /** Whether the service has been bound */
945 @GuardedBy("mRemoteServiceLock")
946 private boolean mIsBound;
947 /** Whether the service has been destroyed */
948 @GuardedBy("mRemoteServiceLock")
949 private boolean mIsDestroyed;
950 /**
951 * Set once a final op is scheduled. No further ops can be added and the service is
952 * destroyed once the op finishes.
953 */
954 @GuardedBy("mRemoteServiceLock")
955 private boolean mDestroyOnceRunningOpsDone;
956
957 /** Total number of operations performed by this service */
958 @GuardedBy("mRemoteServiceLock")
959 private int mNumTotalOpsPerformed;
960
961 /**
962 * Create a new remote sound trigger detection service. This only binds to the service when
963 * operations are in flight. Each operation has a certain time it can run. Once no
964 * operations are allowed to run anymore, {@link #stopAllPendingOperations() all operations
965 * are aborted and stopped} and the service is disconnected.
966 *
967 * @param modelUuid The UUID of the model the recognition is for
968 * @param params The params passed to each method of the service
969 * @param serviceName The component name of the service
970 * @param user The user of the service
971 * @param config The configuration of the recognition
972 */
973 public RemoteSoundTriggerDetectionService(@NonNull UUID modelUuid,
974 @Nullable Bundle params, @NonNull ComponentName serviceName, @NonNull UserHandle user,
975 @NonNull RecognitionConfig config) {
976 mPuuid = new ParcelUuid(modelUuid);
977 mParams = params;
978 mServiceName = serviceName;
979 mUser = user;
980 mRecognitionConfig = config;
981 mHandler = new Handler(Looper.getMainLooper());
982
983 PowerManager pm = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE));
984 mRemoteServiceWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
985 "RemoteSoundTriggerDetectionService " + mServiceName.getPackageName() + ":"
986 + mServiceName.getClassName());
987
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -0800988 synchronized (mLock) {
989 NumOps numOps = mNumOpsPerPackage.get(mServiceName.getPackageName());
990 if (numOps == null) {
991 numOps = new NumOps();
992 mNumOpsPerPackage.put(mServiceName.getPackageName(), numOps);
993 }
994 mNumOps = numOps;
995 }
996
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -0800997 mClient = new ISoundTriggerDetectionServiceClient.Stub() {
998 @Override
999 public void onOpFinished(int opId) {
1000 long token = Binder.clearCallingIdentity();
1001 try {
1002 synchronized (mRemoteServiceLock) {
1003 mRunningOpIds.remove(opId);
1004
1005 if (mRunningOpIds.isEmpty() && mPendingOps.isEmpty()) {
1006 if (mDestroyOnceRunningOpsDone) {
1007 destroy();
1008 } else {
1009 disconnectLocked();
1010 }
1011 }
1012 }
1013 } finally {
1014 Binder.restoreCallingIdentity(token);
1015 }
1016 }
1017 };
1018 }
1019
1020 @Override
1021 public boolean pingBinder() {
1022 return !(mIsDestroyed || mDestroyOnceRunningOpsDone);
1023 }
1024
1025 /**
1026 * Disconnect from the service, but allow to re-connect when new operations are triggered.
1027 */
Andreas Gampe8ce7ed92018-09-05 16:53:00 -07001028 @GuardedBy("mRemoteServiceLock")
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001029 private void disconnectLocked() {
1030 if (mService != null) {
1031 try {
1032 mService.removeClient(mPuuid);
1033 } catch (Exception e) {
1034 Slog.e(TAG, mPuuid + ": Cannot remove client", e);
Jason Hsu1363f582019-04-16 15:35:55 +08001035
1036 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1037 + ": Cannot remove client"));
1038
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001039 }
1040
1041 mService = null;
1042 }
1043
1044 if (mIsBound) {
1045 mContext.unbindService(RemoteSoundTriggerDetectionService.this);
1046 mIsBound = false;
1047
1048 synchronized (mCallbacksLock) {
1049 mRemoteServiceWakeLock.release();
1050 }
1051 }
1052 }
1053
1054 /**
1055 * Disconnect, do not allow to reconnect to the service. All further operations will be
1056 * dropped.
1057 */
1058 private void destroy() {
1059 if (DEBUG) Slog.v(TAG, mPuuid + ": destroy");
1060
Jason Hsu1363f582019-04-16 15:35:55 +08001061 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid + ": destroy"));
1062
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001063 synchronized (mRemoteServiceLock) {
1064 disconnectLocked();
1065
1066 mIsDestroyed = true;
1067 }
1068
1069 // The callback is removed before the flag is set
1070 if (!mDestroyOnceRunningOpsDone) {
1071 synchronized (mCallbacksLock) {
1072 mCallbacks.remove(mPuuid.getUuid());
1073 }
1074 }
1075 }
1076
1077 /**
1078 * Stop all pending operations and then disconnect for the service.
1079 */
1080 private void stopAllPendingOperations() {
1081 synchronized (mRemoteServiceLock) {
1082 if (mIsDestroyed) {
1083 return;
1084 }
1085
1086 if (mService != null) {
1087 int numOps = mRunningOpIds.size();
1088 for (int i = 0; i < numOps; i++) {
1089 try {
1090 mService.onStopOperation(mPuuid, mRunningOpIds.valueAt(i));
1091 } catch (Exception e) {
1092 Slog.e(TAG, mPuuid + ": Could not stop operation "
1093 + mRunningOpIds.valueAt(i), e);
Jason Hsu1363f582019-04-16 15:35:55 +08001094
1095 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1096 + ": Could not stop operation " + mRunningOpIds.valueAt(i)));
1097
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001098 }
1099 }
1100
1101 mRunningOpIds.clear();
1102 }
1103
1104 disconnectLocked();
1105 }
1106 }
1107
1108 /**
1109 * Verify that the service has the expected properties and then bind to the service
1110 */
1111 private void bind() {
1112 long token = Binder.clearCallingIdentity();
1113 try {
1114 Intent i = new Intent();
1115 i.setComponent(mServiceName);
1116
1117 ResolveInfo ri = mContext.getPackageManager().resolveServiceAsUser(i,
1118 GET_SERVICES | GET_META_DATA | MATCH_DEBUG_TRIAGED_MISSING,
1119 mUser.getIdentifier());
1120
1121 if (ri == null) {
1122 Slog.w(TAG, mPuuid + ": " + mServiceName + " not found");
Jason Hsu1363f582019-04-16 15:35:55 +08001123
1124 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1125 + ": " + mServiceName + " not found"));
1126
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001127 return;
1128 }
1129
1130 if (!BIND_SOUND_TRIGGER_DETECTION_SERVICE
1131 .equals(ri.serviceInfo.permission)) {
1132 Slog.w(TAG, mPuuid + ": " + mServiceName + " does not require "
1133 + BIND_SOUND_TRIGGER_DETECTION_SERVICE);
Jason Hsu1363f582019-04-16 15:35:55 +08001134
1135 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1136 + ": " + mServiceName + " does not require "
1137 + BIND_SOUND_TRIGGER_DETECTION_SERVICE));
1138
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001139 return;
1140 }
1141
1142 mIsBound = mContext.bindServiceAsUser(i, this,
Nick Moukhine67b5e382020-03-16 17:51:37 +01001143 BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_INCLUDE_CAPABILITIES,
1144 mUser);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001145
1146 if (mIsBound) {
1147 mRemoteServiceWakeLock.acquire();
1148 } else {
1149 Slog.w(TAG, mPuuid + ": Could not bind to " + mServiceName);
Jason Hsu1363f582019-04-16 15:35:55 +08001150
1151 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1152 + ": Could not bind to " + mServiceName));
1153
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001154 }
1155 } finally {
1156 Binder.restoreCallingIdentity(token);
1157 }
1158 }
1159
1160 /**
1161 * Run an operation (i.e. send it do the service). If the service is not connected, this
1162 * binds the service and then runs the operation once connected.
1163 *
1164 * @param op The operation to run
1165 */
1166 private void runOrAddOperation(Operation op) {
1167 synchronized (mRemoteServiceLock) {
1168 if (mIsDestroyed || mDestroyOnceRunningOpsDone) {
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001169 Slog.w(TAG, mPuuid + ": Dropped operation as already destroyed or marked for "
1170 + "destruction");
1171
Jason Hsu1363f582019-04-16 15:35:55 +08001172 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1173 + ":Dropped operation as already destroyed or marked for destruction"));
1174
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001175 op.drop();
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001176 return;
1177 }
1178
1179 if (mService == null) {
1180 mPendingOps.add(op);
1181
1182 if (!mIsBound) {
1183 bind();
1184 }
1185 } else {
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001186 long currentTime = System.nanoTime();
1187 mNumOps.clearOldOps(currentTime);
1188
1189 // Drop operation if too many were executed in the last 24 hours.
1190 int opsAllowed = Settings.Global.getInt(mContext.getContentResolver(),
1191 MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY,
1192 Integer.MAX_VALUE);
1193
Philip P. Moltmann3d1683b2018-05-07 15:53:51 -07001194 // As we currently cannot dropping an op safely, disable throttling
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001195 int opsAdded = mNumOps.getOpsAdded();
Philip P. Moltmann3d1683b2018-05-07 15:53:51 -07001196 if (false && mNumOps.getOpsAdded() >= opsAllowed) {
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001197 try {
1198 if (DEBUG || opsAllowed + 10 > opsAdded) {
1199 Slog.w(TAG, mPuuid + ": Dropped operation as too many operations "
1200 + "were run in last 24 hours");
Jason Hsu1363f582019-04-16 15:35:55 +08001201
1202 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1203 + ": Dropped operation as too many operations "
1204 + "were run in last 24 hours"));
1205
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001206 }
1207
1208 op.drop();
1209 } catch (Exception e) {
1210 Slog.e(TAG, mPuuid + ": Could not drop operation", e);
Jason Hsu1363f582019-04-16 15:35:55 +08001211
1212 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1213 + ": Could not drop operation"));
1214
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001215 }
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001216 } else {
1217 mNumOps.addOp(currentTime);
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001218
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001219 // Find a free opID
1220 int opId = mNumTotalOpsPerformed;
1221 do {
1222 mNumTotalOpsPerformed++;
1223 } while (mRunningOpIds.contains(opId));
Philip P. Moltmann7e25b3d2018-03-09 20:22:58 -08001224
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001225 // Run OP
1226 try {
1227 if (DEBUG) Slog.v(TAG, mPuuid + ": runOp " + opId);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001228
Jason Hsu1363f582019-04-16 15:35:55 +08001229 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1230 + ": runOp " + opId));
1231
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001232 op.run(opId, mService);
1233 mRunningOpIds.add(opId);
1234 } catch (Exception e) {
1235 Slog.e(TAG, mPuuid + ": Could not run operation " + opId, e);
Jason Hsu1363f582019-04-16 15:35:55 +08001236
1237 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1238 + ": Could not run operation " + opId));
1239
Philip P. Moltmann09dd8c42018-04-11 09:23:19 -07001240 }
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001241 }
1242
1243 // Unbind from service if no operations are left (i.e. if the operation failed)
1244 if (mPendingOps.isEmpty() && mRunningOpIds.isEmpty()) {
1245 if (mDestroyOnceRunningOpsDone) {
1246 destroy();
1247 } else {
1248 disconnectLocked();
1249 }
1250 } else {
1251 mHandler.removeMessages(MSG_STOP_ALL_PENDING_OPERATIONS);
1252 mHandler.sendMessageDelayed(obtainMessage(
1253 RemoteSoundTriggerDetectionService::stopAllPendingOperations, this)
1254 .setWhat(MSG_STOP_ALL_PENDING_OPERATIONS),
1255 Settings.Global.getLong(mContext.getContentResolver(),
1256 SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT,
1257 Long.MAX_VALUE));
1258 }
1259 }
1260 }
1261 }
1262
1263 @Override
1264 public void onKeyphraseDetected(SoundTrigger.KeyphraseRecognitionEvent event) {
1265 Slog.w(TAG, mPuuid + "->" + mServiceName + ": IGNORED onKeyphraseDetected(" + event
1266 + ")");
Jason Hsu1363f582019-04-16 15:35:55 +08001267
1268 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid + "->" + mServiceName
1269 + ": IGNORED onKeyphraseDetected(" + event + ")"));
1270
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001271 }
1272
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001273 /**
1274 * Create an AudioRecord enough for starting and releasing the data buffered for the event.
1275 *
1276 * @param event The event that was received
1277 * @return The initialized AudioRecord
1278 */
1279 private @NonNull AudioRecord createAudioRecordForEvent(
1280 @NonNull SoundTrigger.GenericRecognitionEvent event) {
1281 AudioAttributes.Builder attributesBuilder = new AudioAttributes.Builder();
1282 attributesBuilder.setInternalCapturePreset(MediaRecorder.AudioSource.HOTWORD);
1283 AudioAttributes attributes = attributesBuilder.build();
1284
1285 // Use same AudioFormat processing as in RecognitionEvent.fromParcel
1286 AudioFormat originalFormat = event.getCaptureFormat();
1287 AudioFormat captureFormat = (new AudioFormat.Builder())
1288 .setChannelMask(originalFormat.getChannelMask())
1289 .setEncoding(originalFormat.getEncoding())
1290 .setSampleRate(originalFormat.getSampleRate())
1291 .build();
1292
1293 int bufferSize = AudioRecord.getMinBufferSize(
1294 captureFormat.getSampleRate() == AudioFormat.SAMPLE_RATE_UNSPECIFIED
1295 ? AudioFormat.SAMPLE_RATE_HZ_MAX
1296 : captureFormat.getSampleRate(),
1297 captureFormat.getChannelCount() == 2
1298 ? AudioFormat.CHANNEL_IN_STEREO
1299 : AudioFormat.CHANNEL_IN_MONO,
1300 captureFormat.getEncoding());
1301
Jason Hsu1363f582019-04-16 15:35:55 +08001302 sEventLogger.log(new SoundTriggerLogger.StringEvent("createAudioRecordForEvent"));
1303
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001304 return new AudioRecord(attributes, captureFormat, bufferSize,
1305 event.getCaptureSession());
1306 }
1307
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001308 @Override
1309 public void onGenericSoundTriggerDetected(SoundTrigger.GenericRecognitionEvent event) {
1310 if (DEBUG) Slog.v(TAG, mPuuid + ": Generic sound trigger event: " + event);
1311
Jason Hsu1363f582019-04-16 15:35:55 +08001312 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1313 + ": Generic sound trigger event: " + event));
1314
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001315 runOrAddOperation(new Operation(
1316 // always execute:
1317 () -> {
mike dooley9b20c1c2019-01-23 10:38:17 +01001318 if (!mRecognitionConfig.allowMultipleTriggers) {
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001319 // Unregister this remoteService once op is done
1320 synchronized (mCallbacksLock) {
1321 mCallbacks.remove(mPuuid.getUuid());
1322 }
1323 mDestroyOnceRunningOpsDone = true;
1324 }
1325 },
1326 // execute if not throttled:
1327 (opId, service) -> service.onGenericRecognitionEvent(mPuuid, opId, event),
1328 // execute if throttled:
1329 () -> {
1330 if (event.isCaptureAvailable()) {
1331 AudioRecord capturedData = createAudioRecordForEvent(event);
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001332
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001333 // Currently we need to start and release the audio record to reset
1334 // the DSP even if we don't want to process the event
1335 capturedData.startRecording();
1336 capturedData.release();
1337 }
1338 }));
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001339 }
1340
1341 @Override
1342 public void onError(int status) {
1343 if (DEBUG) Slog.v(TAG, mPuuid + ": onError: " + status);
1344
Jason Hsu1363f582019-04-16 15:35:55 +08001345 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1346 + ": onError: " + status));
1347
Philip P. Moltmanna5b44032018-05-04 13:59:45 -07001348 runOrAddOperation(
1349 new Operation(
1350 // always execute:
1351 () -> {
1352 // Unregister this remoteService once op is done
1353 synchronized (mCallbacksLock) {
1354 mCallbacks.remove(mPuuid.getUuid());
1355 }
1356 mDestroyOnceRunningOpsDone = true;
1357 },
1358 // execute if not throttled:
1359 (opId, service) -> service.onError(mPuuid, opId, status),
1360 // nothing to do if throttled
1361 null));
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001362 }
1363
1364 @Override
1365 public void onRecognitionPaused() {
1366 Slog.i(TAG, mPuuid + "->" + mServiceName + ": IGNORED onRecognitionPaused");
Jason Hsu1363f582019-04-16 15:35:55 +08001367
1368 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1369 + "->" + mServiceName + ": IGNORED onRecognitionPaused"));
1370
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001371 }
1372
1373 @Override
1374 public void onRecognitionResumed() {
1375 Slog.i(TAG, mPuuid + "->" + mServiceName + ": IGNORED onRecognitionResumed");
Jason Hsu1363f582019-04-16 15:35:55 +08001376
1377 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1378 + "->" + mServiceName + ": IGNORED onRecognitionResumed"));
1379
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001380 }
1381
1382 @Override
1383 public void onServiceConnected(ComponentName name, IBinder service) {
1384 if (DEBUG) Slog.v(TAG, mPuuid + ": onServiceConnected(" + service + ")");
1385
Jason Hsu1363f582019-04-16 15:35:55 +08001386 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1387 + ": onServiceConnected(" + service + ")"));
1388
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001389 synchronized (mRemoteServiceLock) {
1390 mService = ISoundTriggerDetectionService.Stub.asInterface(service);
1391
1392 try {
1393 mService.setClient(mPuuid, mParams, mClient);
1394 } catch (Exception e) {
1395 Slog.e(TAG, mPuuid + ": Could not init " + mServiceName, e);
1396 return;
1397 }
1398
1399 while (!mPendingOps.isEmpty()) {
1400 runOrAddOperation(mPendingOps.remove(0));
1401 }
1402 }
1403 }
1404
1405 @Override
1406 public void onServiceDisconnected(ComponentName name) {
1407 if (DEBUG) Slog.v(TAG, mPuuid + ": onServiceDisconnected");
1408
Jason Hsu1363f582019-04-16 15:35:55 +08001409 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1410 + ": onServiceDisconnected"));
1411
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001412 synchronized (mRemoteServiceLock) {
1413 mService = null;
1414 }
1415 }
1416
1417 @Override
1418 public void onBindingDied(ComponentName name) {
1419 if (DEBUG) Slog.v(TAG, mPuuid + ": onBindingDied");
1420
Jason Hsu1363f582019-04-16 15:35:55 +08001421 sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid
1422 + ": onBindingDied"));
1423
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001424 synchronized (mRemoteServiceLock) {
1425 destroy();
1426 }
1427 }
1428
1429 @Override
1430 public void onNullBinding(ComponentName name) {
1431 Slog.w(TAG, name + " for model " + mPuuid + " returned a null binding");
1432
Jason Hsu1363f582019-04-16 15:35:55 +08001433 sEventLogger.log(new SoundTriggerLogger.StringEvent(name + " for model "
1434 + mPuuid + " returned a null binding"));
1435
Philip P. Moltmann18e3eb82018-03-09 16:55:55 -08001436 synchronized (mRemoteServiceLock) {
1437 disconnectLocked();
1438 }
1439 }
1440 }
1441
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001442 public final class LocalSoundTriggerService extends SoundTriggerInternal {
1443 private final Context mContext;
1444 private SoundTriggerHelper mSoundTriggerHelper;
1445
1446 LocalSoundTriggerService(Context context) {
1447 mContext = context;
1448 }
1449
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001450 synchronized void setSoundTriggerHelper(SoundTriggerHelper helper) {
1451 mSoundTriggerHelper = helper;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001452 }
1453
1454 @Override
1455 public int startRecognition(int keyphraseId, KeyphraseSoundModel soundModel,
1456 IRecognitionStatusCallback listener, RecognitionConfig recognitionConfig) {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001457 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001458 return mSoundTriggerHelper.startKeyphraseRecognition(keyphraseId, soundModel, listener,
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001459 recognitionConfig);
1460 }
1461
1462 @Override
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001463 public synchronized int stopRecognition(int keyphraseId, IRecognitionStatusCallback listener) {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001464 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001465 return mSoundTriggerHelper.stopKeyphraseRecognition(keyphraseId, listener);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001466 }
1467
1468 @Override
Arunesh Mishra55a9b002016-02-01 14:06:37 -08001469 public ModuleProperties getModuleProperties() {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001470 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra55a9b002016-02-01 14:06:37 -08001471 return mSoundTriggerHelper.getModuleProperties();
1472 }
1473
1474 @Override
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001475 public int setParameter(int keyphraseId, @ModelParams int modelParam, int value) {
1476 if (!isInitialized()) throw new UnsupportedOperationException();
1477 return mSoundTriggerHelper.setKeyphraseParameter(keyphraseId, modelParam, value);
1478 }
1479
1480 @Override
1481 public int getParameter(int keyphraseId, @ModelParams int modelParam) {
1482 if (!isInitialized()) throw new UnsupportedOperationException();
1483 return mSoundTriggerHelper.getKeyphraseParameter(keyphraseId, modelParam);
1484 }
1485
1486 @Override
1487 @Nullable
1488 public ModelParamRange queryParameter(int keyphraseId, @ModelParams int modelParam) {
1489 if (!isInitialized()) throw new UnsupportedOperationException();
1490 return mSoundTriggerHelper.queryKeyphraseParameter(keyphraseId, modelParam);
1491 }
1492
1493 @Override
Arunesh Mishra2d1de782016-02-21 18:10:28 -08001494 public int unloadKeyphraseModel(int keyphraseId) {
Nicholas Amburf94db1c2019-12-08 19:04:06 -08001495 if (!isInitialized()) throw new UnsupportedOperationException();
Arunesh Mishra2d1de782016-02-21 18:10:28 -08001496 return mSoundTriggerHelper.unloadKeyphraseSoundModel(keyphraseId);
1497 }
1498
1499 @Override
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001500 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001501 if (!isInitialized()) return;
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001502 mSoundTriggerHelper.dump(fd, pw, args);
Jason Hsu1363f582019-04-16 15:35:55 +08001503 // log
1504 sEventLogger.dump(pw);
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -07001505
Nicholas Amburd3ec82f2020-01-03 17:44:19 -08001506 // enrolled models
1507 mDbHelper.dump(pw);
1508
Benjamin Schwartz9e7a0152019-06-06 17:39:22 -07001509 // stats
1510 mSoundModelStatTracker.dump(pw);
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001511 }
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001512
1513 private synchronized boolean isInitialized() {
1514 if (mSoundTriggerHelper == null ) {
1515 Slog.e(TAG, "SoundTriggerHelper not initialized.");
Jason Hsu1363f582019-04-16 15:35:55 +08001516
1517 sEventLogger.log(new SoundTriggerLogger.StringEvent(
1518 "SoundTriggerHelper not initialized."));
1519
Arunesh Mishra3fff7f52016-02-09 12:15:19 -08001520 return false;
1521 }
1522 return true;
1523 }
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001524 }
1525
1526 private void enforceCallingPermission(String permission) {
1527 if (mContext.checkCallingOrSelfPermission(permission)
1528 != PackageManager.PERMISSION_GRANTED) {
1529 throw new SecurityException("Caller does not hold the permission " + permission);
1530 }
1531 }
Jason Hsu1363f582019-04-16 15:35:55 +08001532
1533 //=================================================================
1534 // For logging
1535
1536 private static final SoundTriggerLogger sEventLogger = new SoundTriggerLogger(200,
1537 "SoundTrigger activity");
1538
Arunesh Mishraa772e5f2016-01-25 10:33:11 -08001539}