blob: 30e266289b0d4816f46f5018933f3afa3b32ba51 [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;
41
Alexandre Lision35577132013-12-06 15:21:15 -050042public class MediaManager implements OnAudioFocusChangeListener {
Alexandre Lision6d867b92013-10-25 15:36:28 -040043
44 private static final String TAG = MediaManager.class.getSimpleName();
45 private SipService mService;
46 private SettingsContentObserver mSettingsContentObserver;
Alexandre Lision63870a72013-10-28 16:33:47 -040047 AudioManager mAudioManager;
Alexandre Lisionf02190d2013-12-12 17:26:12 -050048 private Ringer ringer;
Alexandre Lision6d867b92013-10-25 15:36:28 -040049
50 public MediaManager(SipService aService) {
51 mService = aService;
52 mSettingsContentObserver = new SettingsContentObserver(mService, new Handler());
Alexandre Lision63870a72013-10-28 16:33:47 -040053 mAudioManager = (AudioManager) aService.getSystemService(Context.AUDIO_SERVICE);
Alexandre Lisionf02190d2013-12-12 17:26:12 -050054
55 ringer = new Ringer(aService);
Alexandre Lision6d867b92013-10-25 15:36:28 -040056 }
57
58 public void startService() {
59 mService.getApplicationContext().getContentResolver()
60 .registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver);
61 }
62
63 public void stopService() {
64 Log.i(TAG, "Remove media manager....");
65 mService.getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver);
66 }
67
Alexandre Lision63870a72013-10-28 16:33:47 -040068 public AudioManager getAudioManager() {
69 return mAudioManager;
70 }
71
Alexandre Lisionbaeeb212013-12-09 12:54:47 -050072 public void obtainAudioFocus(boolean requestSpeakerOn) {
Alexandre Lision6e9c7e52013-10-29 15:54:11 -040073 mAudioManager.requestAudioFocus(this, Compatibility.getInCallStream(false), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
Alexandre Lision7e390652013-12-06 16:58:22 -050074 mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
Alexandre Lisionc985b362013-12-10 17:45:03 -050075 if(requestSpeakerOn && !mAudioManager.isWiredHeadsetOn()){
Alexandre Lisionbaeeb212013-12-09 12:54:47 -050076 RouteToSpeaker();
77 }
Alexandre Lision6e9c7e52013-10-29 15:54:11 -040078 }
79
80 @Override
81 public void onAudioFocusChange(int arg0) {
Alexandre Lision35577132013-12-06 15:21:15 -050082
Alexandre Lision6e9c7e52013-10-29 15:54:11 -040083 }
84
Alexandre Lision35577132013-12-06 15:21:15 -050085 public void abandonAudioFocus() {
86 mAudioManager.abandonAudioFocus(this);
87 if (mAudioManager.isSpeakerphoneOn()) {
88 mAudioManager.setSpeakerphoneOn(false);
89 }
Alexandre Lision7e390652013-12-06 16:58:22 -050090 mAudioManager.setMode(AudioManager.MODE_NORMAL);
Alexandre Lision35577132013-12-06 15:21:15 -050091 }
92
93 public void RouteToSpeaker() {
94 mAudioManager.setSpeakerphoneOn(true);
95 }
96
97 public void RouteToInternalSpeaker() {
98 mAudioManager.setSpeakerphoneOn(false);
99 }
Alexandre Lisionf02190d2013-12-12 17:26:12 -0500100
101
102 /**
103 * Start ringing announce for a given contact.
104 * It will also focus audio for us.
105 * @param remoteContact the contact to ring for. May resolve the contact ringtone if any.
106 */
107 synchronized public void startRing(String remoteContact) {
108
109 if(!ringer.isRinging()) {
110 ringer.ring(remoteContact, "USELESS");
111 }else {
112 Log.d(TAG, "Already ringing ....");
113 }
114
115 }
116
117 /**
118 * Stop all ringing. <br/>
119 * Warning, this will not unfocus audio.
120 */
121 synchronized public void stopRing() {
122 if(ringer.isRinging()) {
123 ringer.stopRing();
124 }
125 }
Alexandre Lision6d867b92013-10-25 15:36:28 -0400126}