+
+ class RefreshAvailabiltyTask extends AsyncTask<Void, Void, Void> {
+
+ @Override
+ public Void doInBackground(Void... params) {
+ int availability = internalGetInitialAvailability();
+ KeyphraseSoundModel soundModel = null;
+ // Fetch the sound model if the availability is one of the supported ones.
+ if (availability == STATE_NOT_READY
+ || availability == STATE_KEYPHRASE_UNENROLLED
+ || availability == STATE_KEYPHRASE_ENROLLED) {
+ soundModel =
+ internalGetKeyphraseSoundModel(mKeyphraseMetadata.id);
+ if (soundModel == null) {
+ availability = STATE_KEYPHRASE_UNENROLLED;
+ } else {
+ availability = STATE_KEYPHRASE_ENROLLED;
+ }
+ }
+
+ synchronized (mLock) {
+ if (DBG) {
+ Slog.d(TAG, "Hotword availability changed from " + mAvailability
+ + " -> " + availability);
+ }
+ mAvailability = availability;
+ mEnrolledSoundModel = soundModel;
+ notifyStateChangedLocked();
+ }
+ return null;
+ }
+
+ /**
+ * @return The initial availability without checking the enrollment status.
+ */
+ private int internalGetInitialAvailability() {
+ synchronized (mLock) {
+ // This detector has already been invalidated.
+ if (mAvailability == STATE_INVALID) {
+ return STATE_INVALID;
+ }
+ }
+
+ ModuleProperties dspModuleProperties = null;
+ try {
+ dspModuleProperties =
+ mModelManagementService.getDspModuleProperties(mVoiceInteractionService);
+ } catch (RemoteException e) {
+ Slog.w(TAG, "RemoteException in getDspProperties!");
+ }
+ // No DSP available
+ if (dspModuleProperties == null) {
+ return STATE_HARDWARE_UNAVAILABLE;
+ }
+ // No enrollment application supports this keyphrase/locale
+ if (mKeyphraseMetadata == null) {
+ return STATE_KEYPHRASE_UNSUPPORTED;
+ }
+ return STATE_NOT_READY;
+ }
+
+ /**
+ * @return The corresponding {@link KeyphraseSoundModel} or null if none is found.
+ */
+ private KeyphraseSoundModel internalGetKeyphraseSoundModel(int keyphraseId) {
+ List<KeyphraseSoundModel> soundModels;
+ try {
+ soundModels = mModelManagementService
+ .listRegisteredKeyphraseSoundModels(mVoiceInteractionService);
+ if (soundModels == null || soundModels.isEmpty()) {
+ Slog.i(TAG, "No available sound models for keyphrase ID: " + keyphraseId);
+ return null;
+ }
+ for (int i = 0; i < soundModels.size(); i++) {
+ KeyphraseSoundModel soundModel = soundModels.get(i);
+ if (soundModel.keyphrases == null || soundModel.keyphrases.length == 0) {
+ continue;
+ }
+ for (int j = 0; i < soundModel.keyphrases.length; j++) {
+ Keyphrase keyphrase = soundModel.keyphrases[j];
+ if (keyphrase.id == keyphraseId) {
+ return soundModel;
+ }
+ }
+ }
+ } catch (RemoteException e) {
+ Slog.w(TAG, "RemoteException in listRegisteredKeyphraseSoundModels!");
+ }
+ return null;
+ }
+ }