1/*
2 * Copyright (C) 2017 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
17package com.googlecode.android_scripting.facade.media;
18
19import android.app.Service;
20import android.content.Context;
21import android.media.AudioManager;
22import android.media.AudioManager.OnAudioFocusChangeListener;
23
24import com.googlecode.android_scripting.facade.EventFacade;
25import com.googlecode.android_scripting.facade.FacadeManager;
26import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
27import com.googlecode.android_scripting.rpc.Rpc;
28
29public 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