blob: 25ddef56b25f7833ef2ab419501db2b87d16d51f [file] [log] [blame]
Alexandre Lisiona8b78722013-12-13 10:18:33 -05001/*
Alexandre Lisionc1024c02014-01-06 11:12:53 -05002 * Copyright (C) 2004-2014 Savoir-Faire Linux Inc.
Alexandre Lisiona8b78722013-12-13 10:18:33 -05003 *
4 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 * Additional permission under GNU GPL version 3 section 7:
21 *
22 * If you modify this program, or any covered work, by linking or
23 * combining it with the OpenSSL project's OpenSSL library (or a
24 * modified version of that library), containing parts covered by the
25 * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
26 * grants you additional permission to convey the resulting work.
27 * Corresponding Source for a non-source form of such a combination
28 * shall include the source code for the parts of OpenSSL used as well
29 * as that of the covered work.
30 */
31
Alexandre Lision6d867b92013-10-25 15:36:28 -040032package org.sflphone.utils;
33
34import org.sflphone.service.SipService;
35
Alexandre Lision63870a72013-10-28 16:33:47 -040036import android.content.Context;
37import android.media.AudioManager;
Alexandre Lision6e9c7e52013-10-29 15:54:11 -040038import android.media.AudioManager.OnAudioFocusChangeListener;
Alexandre Lision6d867b92013-10-25 15:36:28 -040039import android.os.Handler;
40import android.util.Log;
Alexandre Lision3c37dca2014-02-21 14:13:26 -050041import org.sflphone.utils.bluetooth.BluetoothWrapper;
Alexandre Lision6d867b92013-10-25 15:36:28 -040042
Alexandre Lision3c37dca2014-02-21 14:13:26 -050043public class MediaManager implements OnAudioFocusChangeListener, BluetoothWrapper.BluetoothChangeListener {
Alexandre Lision6d867b92013-10-25 15:36:28 -040044
45 private static final String TAG = MediaManager.class.getSimpleName();
46 private SipService mService;
47 private SettingsContentObserver mSettingsContentObserver;
Alexandre Lision63870a72013-10-28 16:33:47 -040048 AudioManager mAudioManager;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050049 private Ringer ringer;
Alexandre Lision3c37dca2014-02-21 14:13:26 -050050 //Bluetooth related
51 private BluetoothWrapper bluetoothWrapper;
Alexandre Lision6d867b92013-10-25 15:36:28 -040052
53 public MediaManager(SipService aService) {
54 mService = aService;
55 mSettingsContentObserver = new SettingsContentObserver(mService, new Handler());
Alexandre Lision63870a72013-10-28 16:33:47 -040056 mAudioManager = (AudioManager) aService.getSystemService(Context.AUDIO_SERVICE);
Alexandre Lisionf02190d2013-12-12 17:26:12 -050057
58 ringer = new Ringer(aService);
Alexandre Lision6d867b92013-10-25 15:36:28 -040059 }
60
61 public void startService() {
Alexandre Lision3c37dca2014-02-21 14:13:26 -050062 if(bluetoothWrapper == null) {
63 bluetoothWrapper = BluetoothWrapper.getInstance(mService);
64 bluetoothWrapper.setBluetoothChangeListener(this);
65 bluetoothWrapper.register();
66 }
Alexandre Lision6d867b92013-10-25 15:36:28 -040067 mService.getApplicationContext().getContentResolver()
68 .registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);
69 }
70
71 public void stopService() {
72 Log.i(TAG, "Remove media manager....");
73 mService.getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
Alexandre Lision3c37dca2014-02-21 14:13:26 -050074 if(bluetoothWrapper != null) {
75 bluetoothWrapper.unregister();
76 bluetoothWrapper.setBluetoothChangeListener(null);
77 bluetoothWrapper = null;
78 }
Alexandre Lision6d867b92013-10-25 15:36:28 -040079 }
80
Alexandre Lision63870a72013-10-28 16:33:47 -040081 public AudioManager getAudioManager() {
82 return mAudioManager;
83 }
84
Alexandre Lisionbaeeb212013-12-09 12:54:47 -050085 public void obtainAudioFocus(boolean requestSpeakerOn) {
Alexandre Lision6e9c7e52013-10-29 15:54:11 -040086 mAudioManager.requestAudioFocus(this, Compatibility.getInCallStream(false), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
Alexandre Lision7e390652013-12-06 16:58:22 -050087 mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
Alexandre Lision3c37dca2014-02-21 14:13:26 -050088 if(bluetoothWrapper != null && bluetoothWrapper.canBluetooth()) {
89 Log.d(TAG, "Try to enable bluetooth");
90 bluetoothWrapper.setBluetoothOn(true);
91 } else if (requestSpeakerOn && !mAudioManager.isWiredHeadsetOn()){
Alexandre Lisionbaeeb212013-12-09 12:54:47 -050092 RouteToSpeaker();
93 }
Alexandre Lision6e9c7e52013-10-29 15:54:11 -040094 }
95
96 @Override
97 public void onAudioFocusChange(int arg0) {
Alexandre Lision35577132013-12-06 15:21:15 -050098
Alexandre Lision6e9c7e52013-10-29 15:54:11 -040099 }
100
Alexandre Lision35577132013-12-06 15:21:15 -0500101 public void abandonAudioFocus() {
102 mAudioManager.abandonAudioFocus(this);
103 if (mAudioManager.isSpeakerphoneOn()) {
104 mAudioManager.setSpeakerphoneOn(false);
105 }
Alexandre Lision7e390652013-12-06 16:58:22 -0500106 mAudioManager.setMode(AudioManager.MODE_NORMAL);
Alexandre Lision35577132013-12-06 15:21:15 -0500107 }
108
109 public void RouteToSpeaker() {
110 mAudioManager.setSpeakerphoneOn(true);
111 }
112
113 public void RouteToInternalSpeaker() {
114 mAudioManager.setSpeakerphoneOn(false);
115 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500116
117
118 /**
119 * Start ringing announce for a given contact.
120 * It will also focus audio for us.
121 * @param remoteContact the contact to ring for. May resolve the contact ringtone if any.
122 */
123 synchronized public void startRing(String remoteContact) {
124
125 if(!ringer.isRinging()) {
126 ringer.ring(remoteContact, "USELESS");
127 }else {
128 Log.d(TAG, "Already ringing ....");
129 }
130
131 }
132
133 /**
134 * Stop all ringing. <br/>
135 * Warning, this will not unfocus audio.
136 */
137 synchronized public void stopRing() {
138 if(ringer.isRinging()) {
139 ringer.stopRing();
140 }
141 }
Alexandre Lision3c37dca2014-02-21 14:13:26 -0500142
143 @Override
144 public void onBluetoothStateChanged(int status) {
145 //setSoftwareVolume();
146 //broadcastMediaChanged();
147 }
148
Alexandre Lision6d867b92013-10-25 15:36:28 -0400149}