Home | History | Annotate | Download | only in soundtrigger
      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.test.soundtrigger;
     18 
     19 import android.annotation.Nullable;
     20 import android.content.Context;
     21 import android.hardware.soundtrigger.SoundTrigger.GenericSoundModel;
     22 import android.media.soundtrigger.SoundTriggerDetector;
     23 import android.media.soundtrigger.SoundTriggerManager;
     24 import android.os.RemoteException;
     25 import android.os.ServiceManager;
     26 import android.os.ParcelUuid;
     27 import android.util.Log;
     28 
     29 import com.android.internal.app.ISoundTriggerService;
     30 
     31 import java.lang.RuntimeException;
     32 import java.util.UUID;
     33 
     34 /**
     35  * Utility class for the managing sound trigger sound models.
     36  */
     37 public class SoundTriggerUtil {
     38     private static final String TAG = "SoundTriggerTestUtil";
     39 
     40     private final ISoundTriggerService mSoundTriggerService;
     41     private final SoundTriggerManager mSoundTriggerManager;
     42     private final Context mContext;
     43 
     44     public SoundTriggerUtil(Context context) {
     45         mSoundTriggerService = ISoundTriggerService.Stub.asInterface(
     46                 ServiceManager.getService(Context.SOUND_TRIGGER_SERVICE));
     47         mSoundTriggerManager = (SoundTriggerManager) context.getSystemService(
     48                 Context.SOUND_TRIGGER_SERVICE);
     49         mContext = context;
     50     }
     51 
     52     /**
     53      * Adds/Updates a sound model.
     54      * The sound model must contain a valid UUID.
     55      *
     56      * @param soundModel The sound model to add/update.
     57      */
     58     public boolean addOrUpdateSoundModel(GenericSoundModel soundModel) {
     59         try {
     60             if (soundModel == null) {
     61                 throw new RuntimeException("Bad sound model");
     62             }
     63             mSoundTriggerService.updateSoundModel(soundModel);
     64         } catch (RemoteException e) {
     65             Log.e(TAG, "RemoteException in updateSoundModel", e);
     66         }
     67         return true;
     68     }
     69 
     70     /**
     71      * Gets the sound model for the given keyphrase, null if none exists.
     72      * If a sound model for a given keyphrase exists, and it needs to be updated,
     73      * it should be obtained using this method, updated and then passed in to
     74      * {@link #addOrUpdateSoundModel(GenericSoundModel)} without changing the IDs.
     75      *
     76      * @param modelId The model ID to look-up the sound model for.
     77      * @return The sound model if one was found, null otherwise.
     78      */
     79     @Nullable
     80     public GenericSoundModel getSoundModel(UUID modelId) {
     81         GenericSoundModel model = null;
     82         try {
     83             model = mSoundTriggerService.getSoundModel(new ParcelUuid(modelId));
     84         } catch (RemoteException e) {
     85             Log.e(TAG, "RemoteException in updateKeyphraseSoundModel");
     86         }
     87 
     88         if (model == null) {
     89             Log.w(TAG, "No models present for the given keyphrase ID");
     90             return null;
     91         } else {
     92             return model;
     93         }
     94     }
     95 
     96     /**
     97      * Deletes the sound model for the given keyphrase id.
     98      *
     99      * @param modelId The model ID to look-up the sound model for.
    100      * @return {@code true} if the call succeeds, {@code false} otherwise.
    101      */
    102     public boolean deleteSoundModel(UUID modelId) {
    103         try {
    104             mSoundTriggerService.deleteSoundModel(new ParcelUuid(modelId));
    105         } catch (RemoteException e) {
    106             Log.e(TAG, "RemoteException in deleteSoundModel");
    107             return false;
    108         }
    109         return true;
    110     }
    111 
    112     public SoundTriggerDetector createSoundTriggerDetector(UUID modelId,
    113             SoundTriggerDetector.Callback callback) {
    114         return mSoundTriggerManager.createSoundTriggerDetector(modelId, callback, null);
    115     }
    116 }
    117