Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.soundtrigger; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 18 | |
| 19 | import static android.Manifest.permission.BIND_SOUND_TRIGGER_DETECTION_SERVICE; |
| 20 | import static android.content.Context.BIND_AUTO_CREATE; |
| 21 | import static android.content.Context.BIND_FOREGROUND_SERVICE; |
Nick Moukhine | 67b5e38 | 2020-03-16 17:51:37 +0100 | [diff] [blame^] | 22 | import static android.content.Context.BIND_INCLUDE_CAPABILITIES; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 23 | import static android.content.pm.PackageManager.GET_META_DATA; |
| 24 | import static android.content.pm.PackageManager.GET_SERVICES; |
| 25 | import static android.content.pm.PackageManager.MATCH_DEBUG_TRIAGED_MISSING; |
Nicholas Ambur | a0be6be | 2019-10-01 10:11:39 -0700 | [diff] [blame] | 26 | import static android.hardware.soundtrigger.SoundTrigger.STATUS_BAD_VALUE; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 27 | import static android.hardware.soundtrigger.SoundTrigger.STATUS_ERROR; |
Nicholas Ambur | a0be6be | 2019-10-01 10:11:39 -0700 | [diff] [blame] | 28 | import static android.hardware.soundtrigger.SoundTrigger.STATUS_NO_INIT; |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 29 | import static android.hardware.soundtrigger.SoundTrigger.STATUS_OK; |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 30 | import static android.provider.Settings.Global.MAX_SOUND_TRIGGER_DETECTION_SERVICE_OPS_PER_DAY; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 31 | import static android.provider.Settings.Global.SOUND_TRIGGER_DETECTION_SERVICE_OP_TIMEOUT; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 32 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 33 | import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 34 | |
| 35 | import android.Manifest; |
| 36 | import android.annotation.NonNull; |
| 37 | import android.annotation.Nullable; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 38 | import android.content.ComponentName; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 39 | import android.content.Context; |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 40 | import android.content.Intent; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 41 | import android.content.ServiceConnection; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 42 | import android.content.pm.PackageManager; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 43 | import android.content.pm.ResolveInfo; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 44 | import android.hardware.soundtrigger.IRecognitionStatusCallback; |
Nicholas Ambur | a0be6be | 2019-10-01 10:11:39 -0700 | [diff] [blame] | 45 | import android.hardware.soundtrigger.ModelParams; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 46 | import android.hardware.soundtrigger.SoundTrigger; |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 47 | import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 48 | import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel; |
Nicholas Ambur | a0be6be | 2019-10-01 10:11:39 -0700 | [diff] [blame] | 49 | import android.hardware.soundtrigger.SoundTrigger.ModelParamRange; |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 50 | import android.hardware.soundtrigger.SoundTrigger.ModuleProperties; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 51 | import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 52 | import android.hardware.soundtrigger.SoundTrigger.SoundModel; |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 53 | import android.media.AudioAttributes; |
| 54 | import android.media.AudioFormat; |
| 55 | import android.media.AudioRecord; |
| 56 | import android.media.MediaRecorder; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 57 | import android.media.soundtrigger.ISoundTriggerDetectionService; |
| 58 | import android.media.soundtrigger.ISoundTriggerDetectionServiceClient; |
| 59 | import android.media.soundtrigger.SoundTriggerDetectionService; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 60 | import android.os.Binder; |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 61 | import android.os.Bundle; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 62 | import android.os.Handler; |
| 63 | import android.os.IBinder; |
| 64 | import android.os.Looper; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 65 | import android.os.Parcel; |
| 66 | import android.os.ParcelUuid; |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 67 | import android.os.PowerManager; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 68 | import android.os.RemoteException; |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 69 | import android.os.SystemClock; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 70 | import android.os.UserHandle; |
| 71 | import android.provider.Settings; |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 72 | import android.util.ArrayMap; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 73 | import android.util.ArraySet; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 74 | import android.util.Slog; |
| 75 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 76 | import com.android.internal.annotations.GuardedBy; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 77 | import com.android.internal.app.ISoundTriggerService; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 78 | import com.android.server.SystemService; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 79 | |
| 80 | import java.io.FileDescriptor; |
| 81 | import java.io.PrintWriter; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 82 | import java.util.ArrayList; |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 83 | import java.util.Map; |
Daulet Zhanguzin | e789eee | 2019-12-20 17:19:33 +0000 | [diff] [blame] | 84 | import java.util.Objects; |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 85 | import java.util.TreeMap; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 86 | import java.util.UUID; |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 87 | import java.util.concurrent.TimeUnit; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 88 | |
| 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 | */ |
| 98 | public class SoundTriggerService extends SystemService { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 99 | private static final String TAG = "SoundTriggerService"; |
| 100 | private static final boolean DEBUG = true; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 101 | |
| 102 | final Context mContext; |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 103 | private Object mLock; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 104 | private final SoundTriggerServiceStub mServiceStub; |
| 105 | private final LocalSoundTriggerService mLocalSoundTriggerService; |
| 106 | private SoundTriggerDbHelper mDbHelper; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 107 | private SoundTriggerHelper mSoundTriggerHelper; |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 108 | private final TreeMap<UUID, SoundModel> mLoadedModels; |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 109 | private Object mCallbacksLock; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 110 | private final TreeMap<UUID, IRecognitionStatusCallback> mCallbacks; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 111 | |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 112 | 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. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 186 | /** 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 Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 190 | public SoundTriggerService(Context context) { |
| 191 | super(context); |
| 192 | mContext = context; |
| 193 | mServiceStub = new SoundTriggerServiceStub(); |
| 194 | mLocalSoundTriggerService = new LocalSoundTriggerService(context); |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 195 | mLoadedModels = new TreeMap<UUID, SoundModel>(); |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 196 | mCallbacksLock = new Object(); |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 197 | mCallbacks = new TreeMap<>(); |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 198 | mLock = new Object(); |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 199 | mSoundModelStatTracker = new SoundModelStatTracker(); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 200 | } |
| 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 Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 211 | initSoundTriggerHelper(); |
| 212 | mLocalSoundTriggerService.setSoundTriggerHelper(mSoundTriggerHelper); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 213 | } 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 Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 226 | 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 Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 240 | 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 Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 257 | public int startRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback, |
| 258 | RecognitionConfig config) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 259 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 260 | if (!isInitialized()) return STATUS_ERROR; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 261 | if (DEBUG) { |
| 262 | Slog.i(TAG, "startRecognition(): Uuid : " + parcelUuid); |
| 263 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 264 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 265 | sEventLogger.log(new SoundTriggerLogger.StringEvent("startRecognition(): Uuid : " |
| 266 | + parcelUuid)); |
| 267 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 268 | GenericSoundModel model = getSoundModel(parcelUuid); |
| 269 | if (model == null) { |
| 270 | Slog.e(TAG, "Null model in database for id: " + parcelUuid); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 271 | |
| 272 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 273 | "startRecognition(): Null model in database for id: " + parcelUuid)); |
| 274 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 275 | return STATUS_ERROR; |
| 276 | } |
| 277 | |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 278 | int ret = mSoundTriggerHelper.startGenericRecognition(parcelUuid.getUuid(), model, |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 279 | callback, config); |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 280 | if (ret == STATUS_OK) { |
| 281 | mSoundModelStatTracker.onStart(parcelUuid.getUuid()); |
| 282 | } |
| 283 | return ret; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | @Override |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 287 | public int stopRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 288 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 289 | if (DEBUG) { |
| 290 | Slog.i(TAG, "stopRecognition(): Uuid : " + parcelUuid); |
| 291 | } |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 292 | |
| 293 | sEventLogger.log(new SoundTriggerLogger.StringEvent("stopRecognition(): Uuid : " |
| 294 | + parcelUuid)); |
| 295 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 296 | if (!isInitialized()) return STATUS_ERROR; |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 297 | |
| 298 | int ret = mSoundTriggerHelper.stopGenericRecognition(parcelUuid.getUuid(), callback); |
| 299 | if (ret == STATUS_OK) { |
| 300 | mSoundModelStatTracker.onStop(parcelUuid.getUuid()); |
| 301 | } |
| 302 | return ret; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | @Override |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 306 | public SoundTrigger.GenericSoundModel getSoundModel(ParcelUuid soundModelId) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 307 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 308 | if (DEBUG) { |
| 309 | Slog.i(TAG, "getSoundModel(): id = " + soundModelId); |
| 310 | } |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 311 | |
| 312 | sEventLogger.log(new SoundTriggerLogger.StringEvent("getSoundModel(): id = " |
| 313 | + soundModelId)); |
| 314 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 315 | SoundTrigger.GenericSoundModel model = mDbHelper.getGenericSoundModel( |
| 316 | soundModelId.getUuid()); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 317 | return model; |
| 318 | } |
| 319 | |
| 320 | @Override |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 321 | public void updateSoundModel(SoundTrigger.GenericSoundModel soundModel) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 322 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 323 | if (DEBUG) { |
| 324 | Slog.i(TAG, "updateSoundModel(): model = " + soundModel); |
| 325 | } |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 326 | |
| 327 | sEventLogger.log(new SoundTriggerLogger.StringEvent("updateSoundModel(): model = " |
| 328 | + soundModel)); |
| 329 | |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 330 | mDbHelper.updateGenericSoundModel(soundModel); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 331 | } |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 339 | |
| 340 | sEventLogger.log(new SoundTriggerLogger.StringEvent("deleteSoundModel(): id = " |
| 341 | + soundModelId)); |
| 342 | |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 343 | // Unload the model if it is loaded. |
| 344 | mSoundTriggerHelper.unloadGenericSoundModel(soundModelId.getUuid()); |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 345 | mDbHelper.deleteGenericSoundModel(soundModelId.getUuid()); |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 346 | |
| 347 | // Stop recognition if it is started. |
| 348 | mSoundModelStatTracker.onStop(soundModelId.getUuid()); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 349 | } |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 350 | |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 357 | |
| 358 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 359 | "loadGenericSoundModel(): Invalid sound model")); |
| 360 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 361 | return STATUS_ERROR; |
| 362 | } |
| 363 | if (DEBUG) { |
| 364 | Slog.i(TAG, "loadGenericSoundModel(): id = " + soundModel.uuid); |
| 365 | } |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 366 | |
| 367 | sEventLogger.log(new SoundTriggerLogger.StringEvent("loadGenericSoundModel(): id = " |
| 368 | + soundModel.uuid)); |
| 369 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 370 | 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 Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 377 | synchronized (mCallbacksLock) { |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 378 | mCallbacks.remove(soundModel.uuid); |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 379 | } |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 380 | } |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 392 | |
| 393 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 394 | "loadKeyphraseSoundModel(): Invalid sound model")); |
| 395 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 396 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 400 | |
| 401 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 402 | "loadKeyphraseSoundModel(): Only one keyphrase per model" |
| 403 | + " is currently supported.")); |
| 404 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 405 | return STATUS_ERROR; |
| 406 | } |
| 407 | if (DEBUG) { |
| 408 | Slog.i(TAG, "loadKeyphraseSoundModel(): id = " + soundModel.uuid); |
| 409 | } |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 410 | |
| 411 | sEventLogger.log(new SoundTriggerLogger.StringEvent("loadKeyphraseSoundModel(): id = " |
| 412 | + soundModel.uuid)); |
| 413 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 414 | 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 Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 421 | synchronized (mCallbacksLock) { |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 422 | mCallbacks.remove(soundModel.uuid); |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 423 | } |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 424 | } |
| 425 | mLoadedModels.put(soundModel.uuid, soundModel); |
| 426 | } |
| 427 | return STATUS_OK; |
| 428 | } |
| 429 | |
| 430 | @Override |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 431 | public int startRecognitionForService(ParcelUuid soundModelId, Bundle params, |
| 432 | ComponentName detectionService, SoundTrigger.RecognitionConfig config) { |
Daulet Zhanguzin | e789eee | 2019-12-20 17:19:33 +0000 | [diff] [blame] | 433 | Objects.requireNonNull(soundModelId); |
| 434 | Objects.requireNonNull(detectionService); |
| 435 | Objects.requireNonNull(config); |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 436 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 437 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
Philip P. Moltmann | 19402f5 | 2018-03-27 14:38:03 -0700 | [diff] [blame] | 438 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 439 | if (!isInitialized()) return STATUS_ERROR; |
| 440 | if (DEBUG) { |
| 441 | Slog.i(TAG, "startRecognition(): id = " + soundModelId); |
| 442 | } |
| 443 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 444 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 445 | "startRecognitionForService(): id = " + soundModelId)); |
| 446 | |
Philip P. Moltmann | 19402f5 | 2018-03-27 14:38:03 -0700 | [diff] [blame] | 447 | IRecognitionStatusCallback callback = |
| 448 | new RemoteSoundTriggerDetectionService(soundModelId.getUuid(), params, |
| 449 | detectionService, Binder.getCallingUserHandle(), config); |
| 450 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 451 | synchronized (mLock) { |
| 452 | SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid()); |
| 453 | if (soundModel == null) { |
| 454 | Slog.e(TAG, soundModelId + " is not loaded"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 455 | |
| 456 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 457 | "startRecognitionForService():" + soundModelId + " is not loaded")); |
| 458 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 459 | return STATUS_ERROR; |
| 460 | } |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 461 | IRecognitionStatusCallback existingCallback = null; |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 462 | synchronized (mCallbacksLock) { |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 463 | existingCallback = mCallbacks.get(soundModelId.getUuid()); |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 464 | } |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 465 | if (existingCallback != null) { |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 466 | Slog.e(TAG, soundModelId + " is already running"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 467 | |
| 468 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 469 | "startRecognitionForService():" |
| 470 | + soundModelId + " is already running")); |
| 471 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 472 | return STATUS_ERROR; |
| 473 | } |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 474 | int ret; |
| 475 | switch (soundModel.type) { |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 476 | case SoundModel.TYPE_GENERIC_SOUND: |
| 477 | ret = mSoundTriggerHelper.startGenericRecognition(soundModel.uuid, |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 478 | (GenericSoundModel) soundModel, callback, config); |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 479 | break; |
| 480 | default: |
| 481 | Slog.e(TAG, "Unknown model type"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 482 | |
| 483 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 484 | "startRecognitionForService(): Unknown model type")); |
| 485 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 486 | return STATUS_ERROR; |
| 487 | } |
| 488 | |
| 489 | if (ret != STATUS_OK) { |
| 490 | Slog.e(TAG, "Failed to start model: " + ret); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 491 | |
| 492 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 493 | "startRecognitionForService(): Failed to start model:")); |
| 494 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 495 | return ret; |
| 496 | } |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 497 | synchronized (mCallbacksLock) { |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 498 | mCallbacks.put(soundModelId.getUuid(), callback); |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 499 | } |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 500 | |
| 501 | mSoundModelStatTracker.onStart(soundModelId.getUuid()); |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 502 | } |
| 503 | return STATUS_OK; |
| 504 | } |
| 505 | |
| 506 | @Override |
Philip P. Moltmann | 19402f5 | 2018-03-27 14:38:03 -0700 | [diff] [blame] | 507 | public int stopRecognitionForService(ParcelUuid soundModelId) { |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 508 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 514 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 515 | "stopRecognitionForService(): id = " + soundModelId)); |
| 516 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 517 | synchronized (mLock) { |
| 518 | SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid()); |
| 519 | if (soundModel == null) { |
| 520 | Slog.e(TAG, soundModelId + " is not loaded"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 521 | |
| 522 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 523 | "stopRecognitionForService(): " + soundModelId |
| 524 | + " is not loaded")); |
| 525 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 526 | return STATUS_ERROR; |
| 527 | } |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 528 | IRecognitionStatusCallback callback = null; |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 529 | synchronized (mCallbacksLock) { |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 530 | callback = mCallbacks.get(soundModelId.getUuid()); |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 531 | } |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 532 | if (callback == null) { |
| 533 | Slog.e(TAG, soundModelId + " is not running"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 534 | |
| 535 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 536 | "stopRecognitionForService(): " + soundModelId |
| 537 | + " is not running")); |
| 538 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 539 | return STATUS_ERROR; |
| 540 | } |
| 541 | int ret; |
| 542 | switch (soundModel.type) { |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 543 | case SoundModel.TYPE_GENERIC_SOUND: |
| 544 | ret = mSoundTriggerHelper.stopGenericRecognition(soundModel.uuid, callback); |
| 545 | break; |
| 546 | default: |
| 547 | Slog.e(TAG, "Unknown model type"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 548 | |
| 549 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 550 | "stopRecognitionForService(): Unknown model type")); |
| 551 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 552 | return STATUS_ERROR; |
| 553 | } |
| 554 | |
| 555 | if (ret != STATUS_OK) { |
| 556 | Slog.e(TAG, "Failed to stop model: " + ret); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 557 | |
| 558 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 559 | "stopRecognitionForService(): Failed to stop model: " + ret)); |
| 560 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 561 | return ret; |
| 562 | } |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 563 | synchronized (mCallbacksLock) { |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 564 | mCallbacks.remove(soundModelId.getUuid()); |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 565 | } |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 566 | |
| 567 | mSoundModelStatTracker.onStop(soundModelId.getUuid()); |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 568 | } |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 580 | sEventLogger.log(new SoundTriggerLogger.StringEvent("unloadSoundModel(): id = " |
| 581 | + soundModelId)); |
| 582 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 583 | synchronized (mLock) { |
| 584 | SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid()); |
| 585 | if (soundModel == null) { |
| 586 | Slog.e(TAG, soundModelId + " is not loaded"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 587 | |
| 588 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 589 | "unloadSoundModel(): " + soundModelId + " is not loaded")); |
| 590 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 591 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 604 | |
| 605 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 606 | "unloadSoundModel(): Unknown model type")); |
| 607 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 608 | return STATUS_ERROR; |
| 609 | } |
| 610 | if (ret != STATUS_OK) { |
| 611 | Slog.e(TAG, "Failed to unload model"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 612 | |
| 613 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 614 | "unloadSoundModel(): Failed to unload model")); |
| 615 | |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 616 | 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 Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 627 | synchronized (mCallbacksLock) { |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 628 | IRecognitionStatusCallback callback = mCallbacks.get(parcelUuid.getUuid()); |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 629 | if (callback == null) { |
| 630 | return false; |
| 631 | } |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 632 | } |
Chris Thornton | ae5fb99 | 2017-12-07 18:26:31 -0800 | [diff] [blame] | 633 | return mSoundTriggerHelper.isRecognitionRequested(parcelUuid.getUuid()); |
Chris Thornton | ba08b79 | 2017-06-08 22:34:37 -0700 | [diff] [blame] | 634 | } |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 635 | |
| 636 | @Override |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 637 | public int getModelState(ParcelUuid soundModelId) { |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 638 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 639 | int ret = STATUS_ERROR; |
| 640 | if (!isInitialized()) return ret; |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 641 | if (DEBUG) { |
| 642 | Slog.i(TAG, "getModelState(): id = " + soundModelId); |
| 643 | } |
| 644 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 645 | sEventLogger.log(new SoundTriggerLogger.StringEvent("getModelState(): id = " |
| 646 | + soundModelId)); |
| 647 | |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 648 | synchronized (mLock) { |
| 649 | SoundModel soundModel = mLoadedModels.get(soundModelId.getUuid()); |
| 650 | if (soundModel == null) { |
| 651 | Slog.e(TAG, soundModelId + " is not loaded"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 652 | |
| 653 | sEventLogger.log(new SoundTriggerLogger.StringEvent("getModelState(): " |
| 654 | + soundModelId + " is not loaded")); |
| 655 | |
mike dooley | b2ab04a | 2018-11-07 15:48:54 +0100 | [diff] [blame] | 656 | return ret; |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 657 | } |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 658 | switch (soundModel.type) { |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 659 | case SoundModel.TYPE_GENERIC_SOUND: |
| 660 | ret = mSoundTriggerHelper.getGenericModelState(soundModel.uuid); |
| 661 | break; |
| 662 | default: |
mike dooley | baa22c7 | 2019-05-15 10:05:25 +0200 | [diff] [blame] | 663 | // SoundModel.TYPE_KEYPHRASE is not supported to increase privacy. |
| 664 | Slog.e(TAG, "Unsupported model type, " + soundModel.type); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 665 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
mike dooley | baa22c7 | 2019-05-15 10:05:25 +0200 | [diff] [blame] | 666 | "getModelState(): Unsupported model type, " |
| 667 | + soundModel.type)); |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 668 | break; |
| 669 | } |
Michael Dooley | 291751e | 2018-10-16 19:53:29 +0000 | [diff] [blame] | 670 | |
| 671 | return ret; |
| 672 | } |
| 673 | } |
Nicholas Ambur | 1aa4b4b | 2019-08-22 12:13:29 -0700 | [diff] [blame] | 674 | |
| 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 Ambur | a0be6be | 2019-10-01 10:11:39 -0700 | [diff] [blame] | 691 | |
| 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 Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 786 | } |
| 787 | |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 788 | /** |
| 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. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 853 | /** |
| 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. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 892 | } |
| 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. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 932 | /** The number of operations executed in each of the last 24 hours */ |
| 933 | private final NumOps mNumOps; |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 934 | |
| 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. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 982 | 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. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 991 | 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 Gampe | 8ce7ed9 | 2018-09-05 16:53:00 -0700 | [diff] [blame] | 1022 | @GuardedBy("mRemoteServiceLock") |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1023 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1029 | |
| 1030 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1031 | + ": Cannot remove client")); |
| 1032 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1033 | } |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1055 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid + ": destroy")); |
| 1056 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1057 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1088 | |
| 1089 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1090 | + ": Could not stop operation " + mRunningOpIds.valueAt(i))); |
| 1091 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1092 | } |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1117 | |
| 1118 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1119 | + ": " + mServiceName + " not found")); |
| 1120 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1121 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1128 | |
| 1129 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1130 | + ": " + mServiceName + " does not require " |
| 1131 | + BIND_SOUND_TRIGGER_DETECTION_SERVICE)); |
| 1132 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1133 | return; |
| 1134 | } |
| 1135 | |
| 1136 | mIsBound = mContext.bindServiceAsUser(i, this, |
Nick Moukhine | 67b5e38 | 2020-03-16 17:51:37 +0100 | [diff] [blame^] | 1137 | BIND_AUTO_CREATE | BIND_FOREGROUND_SERVICE | BIND_INCLUDE_CAPABILITIES, |
| 1138 | mUser); |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1139 | |
| 1140 | if (mIsBound) { |
| 1141 | mRemoteServiceWakeLock.acquire(); |
| 1142 | } else { |
| 1143 | Slog.w(TAG, mPuuid + ": Could not bind to " + mServiceName); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1144 | |
| 1145 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1146 | + ": Could not bind to " + mServiceName)); |
| 1147 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1148 | } |
| 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. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1163 | Slog.w(TAG, mPuuid + ": Dropped operation as already destroyed or marked for " |
| 1164 | + "destruction"); |
| 1165 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1166 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1167 | + ":Dropped operation as already destroyed or marked for destruction")); |
| 1168 | |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1169 | op.drop(); |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1170 | return; |
| 1171 | } |
| 1172 | |
| 1173 | if (mService == null) { |
| 1174 | mPendingOps.add(op); |
| 1175 | |
| 1176 | if (!mIsBound) { |
| 1177 | bind(); |
| 1178 | } |
| 1179 | } else { |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 1180 | 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. Moltmann | 3d1683b | 2018-05-07 15:53:51 -0700 | [diff] [blame] | 1188 | // As we currently cannot dropping an op safely, disable throttling |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 1189 | int opsAdded = mNumOps.getOpsAdded(); |
Philip P. Moltmann | 3d1683b | 2018-05-07 15:53:51 -0700 | [diff] [blame] | 1190 | if (false && mNumOps.getOpsAdded() >= opsAllowed) { |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1191 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1195 | |
| 1196 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1197 | + ": Dropped operation as too many operations " |
| 1198 | + "were run in last 24 hours")); |
| 1199 | |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | op.drop(); |
| 1203 | } catch (Exception e) { |
| 1204 | Slog.e(TAG, mPuuid + ": Could not drop operation", e); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1205 | |
| 1206 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1207 | + ": Could not drop operation")); |
| 1208 | |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 1209 | } |
Philip P. Moltmann | 09dd8c4 | 2018-04-11 09:23:19 -0700 | [diff] [blame] | 1210 | } else { |
| 1211 | mNumOps.addOp(currentTime); |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 1212 | |
Philip P. Moltmann | 09dd8c4 | 2018-04-11 09:23:19 -0700 | [diff] [blame] | 1213 | // Find a free opID |
| 1214 | int opId = mNumTotalOpsPerformed; |
| 1215 | do { |
| 1216 | mNumTotalOpsPerformed++; |
| 1217 | } while (mRunningOpIds.contains(opId)); |
Philip P. Moltmann | 7e25b3d | 2018-03-09 20:22:58 -0800 | [diff] [blame] | 1218 | |
Philip P. Moltmann | 09dd8c4 | 2018-04-11 09:23:19 -0700 | [diff] [blame] | 1219 | // Run OP |
| 1220 | try { |
| 1221 | if (DEBUG) Slog.v(TAG, mPuuid + ": runOp " + opId); |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1222 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1223 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1224 | + ": runOp " + opId)); |
| 1225 | |
Philip P. Moltmann | 09dd8c4 | 2018-04-11 09:23:19 -0700 | [diff] [blame] | 1226 | op.run(opId, mService); |
| 1227 | mRunningOpIds.add(opId); |
| 1228 | } catch (Exception e) { |
| 1229 | Slog.e(TAG, mPuuid + ": Could not run operation " + opId, e); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1230 | |
| 1231 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1232 | + ": Could not run operation " + opId)); |
| 1233 | |
Philip P. Moltmann | 09dd8c4 | 2018-04-11 09:23:19 -0700 | [diff] [blame] | 1234 | } |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1235 | } |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1261 | |
| 1262 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid + "->" + mServiceName |
| 1263 | + ": IGNORED onKeyphraseDetected(" + event + ")")); |
| 1264 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1265 | } |
| 1266 | |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1267 | /** |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1296 | sEventLogger.log(new SoundTriggerLogger.StringEvent("createAudioRecordForEvent")); |
| 1297 | |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1298 | return new AudioRecord(attributes, captureFormat, bufferSize, |
| 1299 | event.getCaptureSession()); |
| 1300 | } |
| 1301 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1302 | @Override |
| 1303 | public void onGenericSoundTriggerDetected(SoundTrigger.GenericRecognitionEvent event) { |
| 1304 | if (DEBUG) Slog.v(TAG, mPuuid + ": Generic sound trigger event: " + event); |
| 1305 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1306 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1307 | + ": Generic sound trigger event: " + event)); |
| 1308 | |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1309 | runOrAddOperation(new Operation( |
| 1310 | // always execute: |
| 1311 | () -> { |
mike dooley | 9b20c1c | 2019-01-23 10:38:17 +0100 | [diff] [blame] | 1312 | if (!mRecognitionConfig.allowMultipleTriggers) { |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1313 | // 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. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1326 | |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1327 | // 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. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | @Override |
| 1336 | public void onError(int status) { |
| 1337 | if (DEBUG) Slog.v(TAG, mPuuid + ": onError: " + status); |
| 1338 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1339 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1340 | + ": onError: " + status)); |
| 1341 | |
Philip P. Moltmann | a5b4403 | 2018-05-04 13:59:45 -0700 | [diff] [blame] | 1342 | 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. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | @Override |
| 1359 | public void onRecognitionPaused() { |
| 1360 | Slog.i(TAG, mPuuid + "->" + mServiceName + ": IGNORED onRecognitionPaused"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1361 | |
| 1362 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1363 | + "->" + mServiceName + ": IGNORED onRecognitionPaused")); |
| 1364 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | @Override |
| 1368 | public void onRecognitionResumed() { |
| 1369 | Slog.i(TAG, mPuuid + "->" + mServiceName + ": IGNORED onRecognitionResumed"); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1370 | |
| 1371 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1372 | + "->" + mServiceName + ": IGNORED onRecognitionResumed")); |
| 1373 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | @Override |
| 1377 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 1378 | if (DEBUG) Slog.v(TAG, mPuuid + ": onServiceConnected(" + service + ")"); |
| 1379 | |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1380 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1381 | + ": onServiceConnected(" + service + ")")); |
| 1382 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1383 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1403 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1404 | + ": onServiceDisconnected")); |
| 1405 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1406 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1415 | sEventLogger.log(new SoundTriggerLogger.StringEvent(mPuuid |
| 1416 | + ": onBindingDied")); |
| 1417 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1418 | 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1427 | sEventLogger.log(new SoundTriggerLogger.StringEvent(name + " for model " |
| 1428 | + mPuuid + " returned a null binding")); |
| 1429 | |
Philip P. Moltmann | 18e3eb8 | 2018-03-09 16:55:55 -0800 | [diff] [blame] | 1430 | synchronized (mRemoteServiceLock) { |
| 1431 | disconnectLocked(); |
| 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1436 | 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 Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1444 | synchronized void setSoundTriggerHelper(SoundTriggerHelper helper) { |
| 1445 | mSoundTriggerHelper = helper; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | @Override |
| 1449 | public int startRecognition(int keyphraseId, KeyphraseSoundModel soundModel, |
| 1450 | IRecognitionStatusCallback listener, RecognitionConfig recognitionConfig) { |
Nicholas Ambur | f94db1c | 2019-12-08 19:04:06 -0800 | [diff] [blame] | 1451 | if (!isInitialized()) throw new UnsupportedOperationException(); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1452 | return mSoundTriggerHelper.startKeyphraseRecognition(keyphraseId, soundModel, listener, |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1453 | recognitionConfig); |
| 1454 | } |
| 1455 | |
| 1456 | @Override |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1457 | public synchronized int stopRecognition(int keyphraseId, IRecognitionStatusCallback listener) { |
Nicholas Ambur | f94db1c | 2019-12-08 19:04:06 -0800 | [diff] [blame] | 1458 | if (!isInitialized()) throw new UnsupportedOperationException(); |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1459 | return mSoundTriggerHelper.stopKeyphraseRecognition(keyphraseId, listener); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | @Override |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 1463 | public ModuleProperties getModuleProperties() { |
Nicholas Ambur | f94db1c | 2019-12-08 19:04:06 -0800 | [diff] [blame] | 1464 | if (!isInitialized()) throw new UnsupportedOperationException(); |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 1465 | return mSoundTriggerHelper.getModuleProperties(); |
| 1466 | } |
| 1467 | |
| 1468 | @Override |
Nicholas Ambur | f94db1c | 2019-12-08 19:04:06 -0800 | [diff] [blame] | 1469 | 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 Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 1488 | public int unloadKeyphraseModel(int keyphraseId) { |
Nicholas Ambur | f94db1c | 2019-12-08 19:04:06 -0800 | [diff] [blame] | 1489 | if (!isInitialized()) throw new UnsupportedOperationException(); |
Arunesh Mishra | 2d1de78 | 2016-02-21 18:10:28 -0800 | [diff] [blame] | 1490 | return mSoundTriggerHelper.unloadKeyphraseSoundModel(keyphraseId); |
| 1491 | } |
| 1492 | |
| 1493 | @Override |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1494 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1495 | if (!isInitialized()) return; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1496 | mSoundTriggerHelper.dump(fd, pw, args); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1497 | // log |
| 1498 | sEventLogger.dump(pw); |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 1499 | |
Nicholas Ambur | d3ec82f | 2020-01-03 17:44:19 -0800 | [diff] [blame] | 1500 | // enrolled models |
| 1501 | mDbHelper.dump(pw); |
| 1502 | |
Benjamin Schwartz | 9e7a015 | 2019-06-06 17:39:22 -0700 | [diff] [blame] | 1503 | // stats |
| 1504 | mSoundModelStatTracker.dump(pw); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1505 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1506 | |
| 1507 | private synchronized boolean isInitialized() { |
| 1508 | if (mSoundTriggerHelper == null ) { |
| 1509 | Slog.e(TAG, "SoundTriggerHelper not initialized."); |
Jason Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1510 | |
| 1511 | sEventLogger.log(new SoundTriggerLogger.StringEvent( |
| 1512 | "SoundTriggerHelper not initialized.")); |
| 1513 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame] | 1514 | return false; |
| 1515 | } |
| 1516 | return true; |
| 1517 | } |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1518 | } |
| 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 Hsu | 1363f58 | 2019-04-16 15:35:55 +0800 | [diff] [blame] | 1526 | |
| 1527 | //================================================================= |
| 1528 | // For logging |
| 1529 | |
| 1530 | private static final SoundTriggerLogger sEventLogger = new SoundTriggerLogger(200, |
| 1531 | "SoundTrigger activity"); |
| 1532 | |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 1533 | } |