Home | History | Annotate | Download | only in media
      1 /*
      2  * Copyright (C) 2016 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 
     17 package com.googlecode.android_scripting.facade.media;
     18 
     19 import android.app.Service;
     20 import android.content.Context;
     21 import android.media.AudioManager;
     22 import android.media.AudioManager.OnAudioFocusChangeListener;
     23 
     24 import com.googlecode.android_scripting.facade.EventFacade;
     25 import com.googlecode.android_scripting.facade.FacadeManager;
     26 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
     27 import com.googlecode.android_scripting.rpc.Rpc;
     28 
     29 public class AudioManagerFacade extends RpcReceiver {
     30 
     31     private final Service mService;
     32     private final EventFacade mEventFacade;
     33     private final AudioManager mAudio;
     34     private final OnAudioFocusChangeListener mFocusChangeListener;
     35     private boolean mIsFocused;
     36 
     37     public AudioManagerFacade(FacadeManager manager) {
     38         super(manager);
     39         mService = manager.getService();
     40         mEventFacade = manager.getReceiver(EventFacade.class);
     41         mAudio = (AudioManager) mService.getSystemService(Context.AUDIO_SERVICE);
     42         mFocusChangeListener = new OnAudioFocusChangeListener() {
     43             public void onAudioFocusChange(int focusChange) {
     44                 if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
     45                     mIsFocused = false;
     46                 } else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
     47                     mIsFocused = true;
     48                 }
     49             }
     50         };
     51     }
     52 
     53     @Rpc(description = "Checks whether any music is active.")
     54     public Boolean audioIsMusicActive() {
     55         return mAudio.isMusicActive();
     56     }
     57 
     58     @Rpc(description = "Checks whether A2DP audio routing to the Bluetooth headset is on or off.")
     59     public Boolean audioIsBluetoothA2dpOn() {
     60         return mAudio.isBluetoothA2dpOn();
     61     }
     62 
     63     @Rpc(description = "Request audio focus for sl4a.")
     64     public Boolean audioRequestAudioFocus() {
     65         int status = mAudio.requestAudioFocus(mFocusChangeListener,
     66                                               AudioManager.STREAM_MUSIC,
     67                                               AudioManager.AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK);
     68         if (status == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
     69             mIsFocused = true;
     70             return true;
     71         }
     72         mIsFocused = false;
     73         return false;
     74     }
     75 
     76     @Rpc(description = "Whether sl4a has the audio focus or not.")
     77     public Boolean audioIsFocused() {
     78         return mIsFocused;
     79     }
     80 
     81     @Override
     82     public void shutdown() {
     83         mAudio.abandonAudioFocus(mFocusChangeListener);
     84     }
     85 }
     86