| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.voice; |
| 18 |
|
| 19 |
import static java.util.Objects.requireNonNull; |
| 20 |
|
| 21 |
import android.annotation.DurationMillisLong; |
| 22 |
import android.annotation.IntDef; |
| 23 |
import android.annotation.NonNull; |
| 24 |
import android.annotation.Nullable; |
| 25 |
import android.annotation.SdkConstant; |
| 26 |
import android.annotation.SuppressLint; |
| 27 |
import android.annotation.SystemApi; |
| 28 |
import android.app.Service; |
| 29 |
import android.content.ContentCaptureOptions; |
| 30 |
import android.content.Context; |
| 31 |
import android.content.Intent; |
| 32 |
import android.hardware.soundtrigger.SoundTrigger; |
| 33 |
import android.media.AudioFormat; |
| 34 |
import android.media.AudioSystem; |
| 35 |
import android.os.Bundle; |
| 36 |
import android.os.IBinder; |
| 37 |
import android.os.IRemoteCallback; |
| 38 |
import android.os.ParcelFileDescriptor; |
| 39 |
import android.os.PersistableBundle; |
| 40 |
import android.os.RemoteException; |
| 41 |
import android.os.SharedMemory; |
| 42 |
import android.util.Log; |
| 43 |
import android.view.contentcapture.ContentCaptureManager; |
| 44 |
import android.view.contentcapture.IContentCaptureManager; |
| 45 |
|
| 46 |
import java.lang.annotation.Documented; |
| 47 |
import java.lang.annotation.Retention; |
| 48 |
import java.lang.annotation.RetentionPolicy; |
| 49 |
import java.util.Locale; |
| 50 |
import java.util.function.IntConsumer; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@SystemApi |
| 73 |
public abstract class HotwordDetectionService extends Service { |
| 74 |
private static final String TAG = "HotwordDetectionService"; |
| 75 |
private static final boolean DBG = false; |
| 76 |
|
| 77 |
private static final long UPDATE_TIMEOUT_MILLIS = 20000; |
| 78 |
|
| 79 |
|
| 80 |
public static final String KEY_INITIALIZATION_STATUS = "initialization_status"; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public static final int MAXIMUM_NUMBER_OF_INITIALIZATION_STATUS_CUSTOM_ERROR = 2; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static final boolean ENABLE_PROXIMITY_RESULT = true; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static final int INITIALIZATION_STATUS_SUCCESS = 0; |
| 101 |
|
| 102 |
|
| 103 |
t. |
| 104 |
|
| 105 |
|
| 106 |
public static final int INITIALIZATION_STATUS_UNKNOWN = 100; |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
@Documented |
| 114 |
@Retention(RetentionPolicy.SOURCE) |
| 115 |
@IntDef({ |
| 116 |
AUDIO_SOURCE_MICROPHONE, |
| 117 |
AUDIO_SOURCE_EXTERNAL |
| 118 |
}) |
| 119 |
@interface AudioSource {} |
| 120 |
|
| 121 |
|
| 122 |
public static final int AUDIO_SOURCE_MICROPHONE = 1; |
| 123 |
|
| 124 |
public static final int AUDIO_SOURCE_EXTERNAL = 2; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
@SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION) |
| 133 |
public static final String SERVICE_INTERFACE = |
| 134 |
"android.service.voice.HotwordDetectionService"; |
| 135 |
|
| 136 |
@Nullable |
| 137 |
private ContentCaptureManager mContentCaptureManager; |
| 138 |
|
| 139 |
private final IHotwordDetectionService mInterface = new IHotwordDetectionService.Stub() { |
| 140 |
@Override |
| 141 |
public void detectFromDspSource( |
| 142 |
SoundTrigger.KeyphraseRecognitionEvent event, |
| 143 |
AudioFormat audioFormat, |
| 144 |
long timeoutMillis, |
| 145 |
IDspHotwordDetectionCallback callback) |
| 146 |
throws RemoteException { |
| 147 |
if (DBG) { |
| 148 |
Log.d(TAG, "#detectFromDspSource"); |
| 149 |
} |
| 150 |
HotwordDetectionService.this.onDetect( |
| 151 |
new AlwaysOnHotwordDetector.EventPayload.Builder(event).build(), |
| 152 |
timeoutMillis, |
| 153 |
new Callback(callback)); |
| 154 |
} |
| 155 |
|
| 156 |
@Override |
| 157 |
public void updateState(PersistableBundle options, SharedMemory sharedMemory, |
| 158 |
IRemoteCallback callback) throws RemoteException { |
| 159 |
Log.v(TAG, "#updateState" + (callback != null ? " with callback" : "")); |
| 160 |
HotwordDetectionService.this.onUpdateStateInternal( |
| 161 |
options, |
| 162 |
sharedMemory, |
| 163 |
callback); |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void detectFromMicrophoneSource( |
| 168 |
ParcelFileDescriptor audioStream, |
| 169 |
@AudioSource int audioSource, |
| 170 |
AudioFormat audioFormat, |
| 171 |
PersistableBundle options, |
| 172 |
IDspHotwordDetectionCallback callback) |
| 173 |
throws RemoteException { |
| 174 |
if (DBG) { |
| 175 |
Log.d(TAG, "#detectFromMicrophoneSource"); |
| 176 |
} |
| 177 |
switch (audioSource) { |
| 178 |
case AUDIO_SOURCE_MICROPHONE: |
| 179 |
HotwordDetectionService.this.onDetect( |
| 180 |
new Callback(callback)); |
| 181 |
break; |
| 182 |
case AUDIO_SOURCE_EXTERNAL: |
| 183 |
HotwordDetectionService.this.onDetect( |
| 184 |
audioStream, |
| 185 |
audioFormat, |
| 186 |
options, |
| 187 |
new Callback(callback)); |
| 188 |
break; |
| 189 |
default: |
| 190 |
Log.i(TAG, "Unsupported audio source " + audioSource); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public void updateAudioFlinger(IBinder audioFlinger) { |
| 196 |
AudioSystem.setAudioFlingerBinder(audioFlinger); |
| 197 |
} |
| 198 |
|
| 199 |
@Override |
| 200 |
public void updateContentCaptureManager(IContentCaptureManager manager, |
| 201 |
ContentCaptureOptions options) { |
| 202 |
mContentCaptureManager = new ContentCaptureManager( |
| 203 |
HotwordDetectionService.this, manager, options); |
| 204 |
} |
| 205 |
|
| 206 |
@Override |
| 207 |
public void ping(IRemoteCallback callback) throws RemoteException { |
| 208 |
callback.sendResult(null); |
| 209 |
} |
| 210 |
|
| 211 |
@Override |
| 212 |
public void stopDetection() { |
| 213 |
HotwordDetectionService.this.onStopDetection(); |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
@Override |
| 218 |
@Nullable |
| 219 |
public final IBinder onBind(@NonNull Intent intent) { |
| 220 |
if (SERVICE_INTERFACE.equals(intent.getAction())) { |
| 221 |
return mInterface.asBinder(); |
| 222 |
} |
| 223 |
Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " |
| 224 |
+ intent); |
| 225 |
return null; |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
@SuppressLint("OnNameExpected") |
| 230 |
public @Nullable Object getSystemService(@ServiceName @NonNull String name) { |
| 231 |
if (Context.CONTENT_CAPTURE_MANAGER_SERVICE.equals(name)) { |
| 232 |
return mContentCaptureManager; |
| 233 |
} else { |
| 234 |
return super.getSystemService(name); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
@SystemApi |
| 247 |
public static int getMaxCustomInitializationStatus() { |
| 248 |
return MAXIMUM_NUMBER_OF_INITIALIZATION_STATUS_CUSTOM_ERROR; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
@SystemApi |
| 269 |
public void onDetect( |
| 270 |
@NonNull AlwaysOnHotwordDetector.EventPayload eventPayload, |
| 271 |
@DurationMillisLong long timeoutMillis, |
| 272 |
@NonNull Callback callback) { |
| 273 |
|
| 274 |
throw new UnsupportedOperationException(); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
@SystemApi |
| 299 |
public void onUpdateState( |
| 300 |
@Nullable PersistableBundle options, |
| 301 |
@Nullable SharedMemory sharedMemory, |
| 302 |
@DurationMillisLong long callbackTimeoutMillis, |
| 303 |
@Nullable IntConsumer statusCallback) {} |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public void onDetect(@NonNull Callback callback) { |
| 317 |
|
| 318 |
throw new UnsupportedOperationException(); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
public void onDetect( |
| 339 |
@NonNull ParcelFileDescriptor audioStream, |
| 340 |
@NonNull AudioFormat audioFormat, |
| 341 |
@Nullable PersistableBundle options, |
| 342 |
@NonNull Callback callback) { |
| 343 |
|
| 344 |
throw new UnsupportedOperationException(); |
| 345 |
} |
| 346 |
|
| 347 |
private void onUpdateStateInternal(@Nullable PersistableBundle options, |
| 348 |
@Nullable SharedMemory sharedMemory, IRemoteCallback callback) { |
| 349 |
IntConsumer intConsumer = null; |
| 350 |
if (callback != null) { |
| 351 |
intConsumer = |
| 352 |
value -> { |
| 353 |
if (value > getMaxCustomInitializationStatus()) { |
| 354 |
throw new IllegalArgumentException( |
| 355 |
"The initialization status is invalid for " + value); |
| 356 |
} |
| 357 |
try { |
| 358 |
Bundle status = new Bundle(); |
| 359 |
status.putInt(KEY_INITIALIZATION_STATUS, value); |
| 360 |
callback.sendResult(status); |
| 361 |
} catch (RemoteException e) { |
| 362 |
throw e.rethrowFromSystemServer(); |
| 363 |
} |
| 364 |
}; |
| 365 |
} |
| 366 |
onUpdateState(options, sharedMemory, UPDATE_TIMEOUT_MILLIS, intConsumer); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
public void onStopDetection() { |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
@SystemApi |
| 384 |
public static final class Callback { |
| 385 |
|
| 386 |
private final IDspHotwordDetectionCallback mRemoteCallback; |
| 387 |
|
| 388 |
private Callback(IDspHotwordDetectionCallback remoteCallback) { |
| 389 |
mRemoteCallback = remoteCallback; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
public void onDetected(@NonNull HotwordDetectedResult result) { |
| 399 |
requireNonNull(result); |
| 400 |
final PersistableBundle persistableBundle = result.getExtras(); |
| 401 |
if (!persistableBundle.isEmpty() && HotwordDetectedResult.getParcelableSize( |
| 402 |
persistableBundle) > HotwordDetectedResult.getMaxBundleSize()) { |
| 403 |
throw new IllegalArgumentException( |
| 404 |
"The bundle size of result is larger than max bundle size (" |
| 405 |
+ HotwordDetectedResult.getMaxBundleSize() |
| 406 |
+ ") of HotwordDetectedResult"); |
| 407 |
} |
| 408 |
try { |
| 409 |
mRemoteCallback.onDetected(result); |
| 410 |
} catch (RemoteException e) { |
| 411 |
throw e.rethrowFromSystemServer(); |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public void onRejected(@NonNull HotwordRejectedResult result) { |
| 425 |
requireNonNull(result); |
| 426 |
try { |
| 427 |
mRemoteCallback.onRejected(result); |
| 428 |
} catch (RemoteException e) { |
| 429 |
throw e.rethrowFromSystemServer(); |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
} |