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; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 18 | import static android.hardware.soundtrigger.SoundTrigger.STATUS_ERROR; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 19 | |
| 20 | import android.content.Context; |
| 21 | import android.content.pm.PackageManager; |
| 22 | import android.Manifest; |
| 23 | import android.hardware.soundtrigger.IRecognitionStatusCallback; |
| 24 | import android.hardware.soundtrigger.SoundTrigger; |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 25 | import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 26 | import android.hardware.soundtrigger.SoundTrigger.KeyphraseSoundModel; |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 27 | import android.hardware.soundtrigger.SoundTrigger.ModuleProperties; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 28 | import android.hardware.soundtrigger.SoundTrigger.RecognitionConfig; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.ParcelUuid; |
| 31 | import android.os.RemoteException; |
| 32 | import android.util.Slog; |
| 33 | |
| 34 | import com.android.server.SystemService; |
| 35 | import com.android.internal.app.ISoundTriggerService; |
| 36 | |
| 37 | import java.io.FileDescriptor; |
| 38 | import java.io.PrintWriter; |
| 39 | import java.util.UUID; |
| 40 | |
| 41 | /** |
| 42 | * A single SystemService to manage all sound/voice-based sound models on the DSP. |
| 43 | * This services provides apis to manage sound trigger-based sound models via |
| 44 | * the ISoundTriggerService interface. This class also publishes a local interface encapsulating |
| 45 | * the functionality provided by {@link SoundTriggerHelper} for use by |
| 46 | * {@link VoiceInteractionManagerService}. |
| 47 | * |
| 48 | * @hide |
| 49 | */ |
| 50 | public class SoundTriggerService extends SystemService { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 51 | private static final String TAG = "SoundTriggerService"; |
| 52 | private static final boolean DEBUG = true; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 53 | |
| 54 | final Context mContext; |
| 55 | private final SoundTriggerServiceStub mServiceStub; |
| 56 | private final LocalSoundTriggerService mLocalSoundTriggerService; |
| 57 | private SoundTriggerDbHelper mDbHelper; |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 58 | private SoundTriggerHelper mSoundTriggerHelper; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 59 | |
| 60 | public SoundTriggerService(Context context) { |
| 61 | super(context); |
| 62 | mContext = context; |
| 63 | mServiceStub = new SoundTriggerServiceStub(); |
| 64 | mLocalSoundTriggerService = new LocalSoundTriggerService(context); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void onStart() { |
| 69 | publishBinderService(Context.SOUND_TRIGGER_SERVICE, mServiceStub); |
| 70 | publishLocalService(SoundTriggerInternal.class, mLocalSoundTriggerService); |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public void onBootPhase(int phase) { |
| 75 | if (PHASE_SYSTEM_SERVICES_READY == phase) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 76 | initSoundTriggerHelper(); |
| 77 | mLocalSoundTriggerService.setSoundTriggerHelper(mSoundTriggerHelper); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 78 | } else if (PHASE_THIRD_PARTY_APPS_CAN_START == phase) { |
| 79 | mDbHelper = new SoundTriggerDbHelper(mContext); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void onStartUser(int userHandle) { |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void onSwitchUser(int userHandle) { |
| 89 | } |
| 90 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 91 | private synchronized void initSoundTriggerHelper() { |
| 92 | if (mSoundTriggerHelper == null) { |
| 93 | mSoundTriggerHelper = new SoundTriggerHelper(mContext); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private synchronized boolean isInitialized() { |
| 98 | if (mSoundTriggerHelper == null ) { |
| 99 | Slog.e(TAG, "SoundTriggerHelper not initialized."); |
| 100 | return false; |
| 101 | } |
| 102 | return true; |
| 103 | } |
| 104 | |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 105 | class SoundTriggerServiceStub extends ISoundTriggerService.Stub { |
| 106 | @Override |
| 107 | public boolean onTransact(int code, Parcel data, Parcel reply, int flags) |
| 108 | throws RemoteException { |
| 109 | try { |
| 110 | return super.onTransact(code, data, reply, flags); |
| 111 | } catch (RuntimeException e) { |
| 112 | // The activity manager only throws security exceptions, so let's |
| 113 | // log all others. |
| 114 | if (!(e instanceof SecurityException)) { |
| 115 | Slog.wtf(TAG, "SoundTriggerService Crash", e); |
| 116 | } |
| 117 | throw e; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 122 | public int startRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback, |
| 123 | RecognitionConfig config) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 124 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 125 | if (DEBUG) { |
| 126 | Slog.i(TAG, "startRecognition(): Uuid : " + parcelUuid); |
| 127 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 128 | if (!isInitialized()) return STATUS_ERROR; |
| 129 | |
| 130 | GenericSoundModel model = getSoundModel(parcelUuid); |
| 131 | if (model == null) { |
| 132 | Slog.e(TAG, "Null model in database for id: " + parcelUuid); |
| 133 | return STATUS_ERROR; |
| 134 | } |
| 135 | |
| 136 | return mSoundTriggerHelper.startGenericRecognition(parcelUuid.getUuid(), model, |
| 137 | callback, config); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 141 | public int stopRecognition(ParcelUuid parcelUuid, IRecognitionStatusCallback callback) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 142 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 143 | if (DEBUG) { |
| 144 | Slog.i(TAG, "stopRecognition(): Uuid : " + parcelUuid); |
| 145 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 146 | if (!isInitialized()) return STATUS_ERROR; |
| 147 | return mSoundTriggerHelper.stopGenericRecognition(parcelUuid.getUuid(), callback); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 151 | public SoundTrigger.GenericSoundModel getSoundModel(ParcelUuid soundModelId) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 152 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 153 | if (DEBUG) { |
| 154 | Slog.i(TAG, "getSoundModel(): id = " + soundModelId); |
| 155 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 156 | SoundTrigger.GenericSoundModel model = mDbHelper.getGenericSoundModel( |
| 157 | soundModelId.getUuid()); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 158 | return model; |
| 159 | } |
| 160 | |
| 161 | @Override |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 162 | public void updateSoundModel(SoundTrigger.GenericSoundModel soundModel) { |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 163 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 164 | if (DEBUG) { |
| 165 | Slog.i(TAG, "updateSoundModel(): model = " + soundModel); |
| 166 | } |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 167 | mDbHelper.updateGenericSoundModel(soundModel); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | @Override |
| 171 | public void deleteSoundModel(ParcelUuid soundModelId) { |
| 172 | enforceCallingPermission(Manifest.permission.MANAGE_SOUND_TRIGGER); |
| 173 | if (DEBUG) { |
| 174 | Slog.i(TAG, "deleteSoundModel(): id = " + soundModelId); |
| 175 | } |
Arunesh Mishra | c722ec41 | 2016-01-27 13:29:12 -0800 | [diff] [blame] | 176 | mDbHelper.deleteGenericSoundModel(soundModelId.getUuid()); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | |
| 180 | public final class LocalSoundTriggerService extends SoundTriggerInternal { |
| 181 | private final Context mContext; |
| 182 | private SoundTriggerHelper mSoundTriggerHelper; |
| 183 | |
| 184 | LocalSoundTriggerService(Context context) { |
| 185 | mContext = context; |
| 186 | } |
| 187 | |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 188 | synchronized void setSoundTriggerHelper(SoundTriggerHelper helper) { |
| 189 | mSoundTriggerHelper = helper; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public int startRecognition(int keyphraseId, KeyphraseSoundModel soundModel, |
| 194 | IRecognitionStatusCallback listener, RecognitionConfig recognitionConfig) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 195 | if (!isInitialized()) return STATUS_ERROR; |
| 196 | return mSoundTriggerHelper.startKeyphraseRecognition(keyphraseId, soundModel, listener, |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 197 | recognitionConfig); |
| 198 | } |
| 199 | |
| 200 | @Override |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 201 | public synchronized int stopRecognition(int keyphraseId, IRecognitionStatusCallback listener) { |
| 202 | if (!isInitialized()) return STATUS_ERROR; |
| 203 | return mSoundTriggerHelper.stopKeyphraseRecognition(keyphraseId, listener); |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void stopAllRecognitions() { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 208 | if (!isInitialized()) return; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 209 | mSoundTriggerHelper.stopAllRecognitions(); |
| 210 | } |
| 211 | |
| 212 | @Override |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 213 | public ModuleProperties getModuleProperties() { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 214 | if (!isInitialized()) return null; |
Arunesh Mishra | 55a9b00 | 2016-02-01 14:06:37 -0800 | [diff] [blame] | 215 | return mSoundTriggerHelper.getModuleProperties(); |
| 216 | } |
| 217 | |
| 218 | @Override |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 219 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 220 | if (!isInitialized()) return; |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 221 | mSoundTriggerHelper.dump(fd, pw, args); |
| 222 | } |
Arunesh Mishra | 3fff7f5 | 2016-02-09 12:15:19 -0800 | [diff] [blame^] | 223 | |
| 224 | private synchronized boolean isInitialized() { |
| 225 | if (mSoundTriggerHelper == null ) { |
| 226 | Slog.e(TAG, "SoundTriggerHelper not initialized."); |
| 227 | return false; |
| 228 | } |
| 229 | return true; |
| 230 | } |
Arunesh Mishra | a772e5f | 2016-01-25 10:33:11 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private void enforceCallingPermission(String permission) { |
| 234 | if (mContext.checkCallingOrSelfPermission(permission) |
| 235 | != PackageManager.PERMISSION_GRANTED) { |
| 236 | throw new SecurityException("Caller does not hold the permission " + permission); |
| 237 | } |
| 238 | } |
| 239 | } |