Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 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 Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 32 | package org.sflphone.utils; |
| 33 | |
| 34 | import org.sflphone.service.SipService; |
| 35 | |
Alexandre Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.media.AudioManager; |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 38 | import android.media.AudioManager.OnAudioFocusChangeListener; |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 39 | import android.os.Handler; |
| 40 | import android.util.Log; |
| 41 | |
Alexandre Lision | 3557713 | 2013-12-06 15:21:15 -0500 | [diff] [blame] | 42 | public class MediaManager implements OnAudioFocusChangeListener { |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 43 | |
| 44 | private static final String TAG = MediaManager.class.getSimpleName(); |
| 45 | private SipService mService; |
| 46 | private SettingsContentObserver mSettingsContentObserver; |
Alexandre Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 47 | AudioManager mAudioManager; |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 48 | private Ringer ringer; |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 49 | |
| 50 | public MediaManager(SipService aService) { |
| 51 | mService = aService; |
| 52 | mSettingsContentObserver = new SettingsContentObserver(mService, new Handler()); |
Alexandre Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 53 | mAudioManager = (AudioManager) aService.getSystemService(Context.AUDIO_SERVICE); |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 54 | |
| 55 | ringer = new Ringer(aService); |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 56 | } |
| 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 Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 68 | public AudioManager getAudioManager() { |
| 69 | return mAudioManager; |
| 70 | } |
| 71 | |
Alexandre Lision | baeeb21 | 2013-12-09 12:54:47 -0500 | [diff] [blame] | 72 | public void obtainAudioFocus(boolean requestSpeakerOn) { |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 73 | mAudioManager.requestAudioFocus(this, Compatibility.getInCallStream(false), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
Alexandre Lision | 7e39065 | 2013-12-06 16:58:22 -0500 | [diff] [blame] | 74 | mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); |
Alexandre Lision | c985b36 | 2013-12-10 17:45:03 -0500 | [diff] [blame] | 75 | if(requestSpeakerOn && !mAudioManager.isWiredHeadsetOn()){ |
Alexandre Lision | baeeb21 | 2013-12-09 12:54:47 -0500 | [diff] [blame] | 76 | RouteToSpeaker(); |
| 77 | } |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void onAudioFocusChange(int arg0) { |
Alexandre Lision | 3557713 | 2013-12-06 15:21:15 -0500 | [diff] [blame] | 82 | |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 83 | } |
| 84 | |
Alexandre Lision | 3557713 | 2013-12-06 15:21:15 -0500 | [diff] [blame] | 85 | public void abandonAudioFocus() { |
| 86 | mAudioManager.abandonAudioFocus(this); |
| 87 | if (mAudioManager.isSpeakerphoneOn()) { |
| 88 | mAudioManager.setSpeakerphoneOn(false); |
| 89 | } |
Alexandre Lision | 7e39065 | 2013-12-06 16:58:22 -0500 | [diff] [blame] | 90 | mAudioManager.setMode(AudioManager.MODE_NORMAL); |
Alexandre Lision | 3557713 | 2013-12-06 15:21:15 -0500 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | public void RouteToSpeaker() { |
| 94 | mAudioManager.setSpeakerphoneOn(true); |
| 95 | } |
| 96 | |
| 97 | public void RouteToInternalSpeaker() { |
| 98 | mAudioManager.setSpeakerphoneOn(false); |
| 99 | } |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 100 | |
| 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 Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 126 | } |