From 5fb71717cb79b2e7efc762ae6f5ba3d371c518d0 Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Mon, 18 Apr 2011 15:18:00 +0200 Subject: [PATCH] Soft volume. Forced for galaxy S. Pref. access mgr. --- src/org/linphone/DialerActivity.java | 13 +---- src/org/linphone/IncallActivity.java | 3 +- src/org/linphone/LinphoneActivity.java | 1 + src/org/linphone/LinphoneManager.java | 20 +++++-- .../linphone/LinphonePreferenceManager.java | 54 +++++++++++++++++++ .../linphone/LinphonePreferencesActivity.java | 30 ++++++----- ...oftVolume.java => SoftVolumeActivity.java} | 37 +++++++------ src/org/linphone/VideoCallActivity.java | 4 +- src/org/linphone/core/Hacks.java | 4 ++ 9 files changed, 118 insertions(+), 48 deletions(-) create mode 100644 src/org/linphone/LinphonePreferenceManager.java rename src/org/linphone/{SoftVolume.java => SoftVolumeActivity.java} (62%) diff --git a/src/org/linphone/DialerActivity.java b/src/org/linphone/DialerActivity.java index cf2af5346..84f8ea840 100644 --- a/src/org/linphone/DialerActivity.java +++ b/src/org/linphone/DialerActivity.java @@ -34,7 +34,6 @@ import org.linphone.ui.HangCallButton; import org.linphone.ui.MuteMicButton; import org.linphone.ui.SpeakerButton; -import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; @@ -45,7 +44,6 @@ import android.os.Bundle; import android.os.PowerManager; import android.preference.PreferenceManager; import android.util.Log; -import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; @@ -63,7 +61,7 @@ import android.widget.Toast; * * */ -public class DialerActivity extends Activity implements LinphoneGuiListener, NewOutgoingCallUiListener { +public class DialerActivity extends SoftVolumeActivity implements LinphoneGuiListener, NewOutgoingCallUiListener { private TextView mStatus; private View mHangup; @@ -85,7 +83,6 @@ public class DialerActivity extends Activity implements LinphoneGuiListener, New private SharedPreferences mPref; private boolean useIncallActivity; private boolean useVideoActivity; - private SoftVolume softVolume; private static final String CURRENT_ADDRESS = "org.linphone.current-address"; private static final String CURRENT_DISPLAYNAME = "org.linphone.current-displayname"; @@ -104,8 +101,6 @@ public class DialerActivity extends Activity implements LinphoneGuiListener, New super.onCreate(savedInstanceState); setContentView(R.layout.dialer); - softVolume = new SoftVolume(this); - useIncallActivity = getResources().getBoolean(R.bool.use_incall_activity); useVideoActivity = getResources().getBoolean(R.bool.use_video_activity); // Don't use Linphone Manager in the onCreate as it takes time in LinphoneService to initialize it. @@ -422,10 +417,4 @@ public class DialerActivity extends Activity implements LinphoneGuiListener, New } } - - @Override - public boolean onKeyDown(int keyCode, KeyEvent event) { - if (softVolume.onKeyDown(keyCode, event)) return true; - return super.onKeyDown(keyCode, event); - } } diff --git a/src/org/linphone/IncallActivity.java b/src/org/linphone/IncallActivity.java index 1c4730579..754637cc7 100644 --- a/src/org/linphone/IncallActivity.java +++ b/src/org/linphone/IncallActivity.java @@ -23,7 +23,6 @@ import java.util.TimerTask; import org.linphone.ui.HangCallButton; -import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; @@ -33,7 +32,7 @@ import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; -public class IncallActivity extends Activity implements OnClickListener { +public class IncallActivity extends SoftVolumeActivity implements OnClickListener { public static final String CONTACT_KEY = "contact"; private View numpadClose; diff --git a/src/org/linphone/LinphoneActivity.java b/src/org/linphone/LinphoneActivity.java index 01209100f..3e74452d1 100644 --- a/src/org/linphone/LinphoneActivity.java +++ b/src/org/linphone/LinphoneActivity.java @@ -111,6 +111,7 @@ public class LinphoneActivity extends TabActivity { instance = this; setContentView(R.layout.main); + LinphonePreferenceManager.setContext(this); useFirstLoginActivity = getResources().getBoolean(R.bool.useFirstLoginActivity); useMenuSettings = getResources().getBoolean(R.bool.useMenuSettings); useMenuAbout = getResources().getBoolean(R.bool.useMenuAbout); diff --git a/src/org/linphone/LinphoneManager.java b/src/org/linphone/LinphoneManager.java index 074f897d3..5408cdc57 100644 --- a/src/org/linphone/LinphoneManager.java +++ b/src/org/linphone/LinphoneManager.java @@ -156,6 +156,8 @@ public final class LinphoneManager implements LinphoneCoreListener { } public static final String TAG="Linphone"; + private static final int LINPHONE_VOLUME_STREAM = STREAM_VOICE_CALL; + private static final int dbStep = 4; /** Called when the activity is first created. */ private final String linphoneInitialConfigFile; private final String linphoneConfigFile; @@ -331,9 +333,6 @@ public final class LinphoneManager implements LinphoneCoreListener { this, linphoneConfigFile, linphoneInitialConfigFile, null); mLc.enableIpv6(mPref.getBoolean(getString(R.string.pref_ipv6_key), false)); - if (mPref.getBoolean(getString(R.string.pref_audio_soft_volume_key), false)) { - adjustSoftwareVolume(0); // Set maximum - } mLc.setPlaybackGain(3); mLc.setRing(null); @@ -696,6 +695,12 @@ public final class LinphoneManager implements LinphoneCoreListener { mAudioManager.setMode(MODE_NORMAL); } + if (state == State.Connected) { + if (Hacks.needSoftvolume() || LinphonePreferenceManager.getInstance().useSoftvolume()) { + adjustSoftwareVolume(0); // Synchronize + } + } + mCurrentCallState=state; serviceListener.onCallStateChanged(call, state, message); } @@ -861,7 +866,14 @@ public final class LinphoneManager implements LinphoneCoreListener { } public void adjustSoftwareVolume(int i) { - mLc.adjustSoftwareVolume(i); + int oldVolume = mAudioManager.getStreamVolume(LINPHONE_VOLUME_STREAM); + int maxVolume = mAudioManager.getStreamMaxVolume(LINPHONE_VOLUME_STREAM); + + int nextVolume = oldVolume +i; + if (nextVolume > maxVolume) nextVolume = maxVolume; + if (nextVolume < 0) nextVolume = 0; + + mLc.adjustSoftwareVolume((nextVolume - maxVolume)* dbStep); } } diff --git a/src/org/linphone/LinphonePreferenceManager.java b/src/org/linphone/LinphonePreferenceManager.java new file mode 100644 index 000000000..484331bf7 --- /dev/null +++ b/src/org/linphone/LinphonePreferenceManager.java @@ -0,0 +1,54 @@ +/* +PreferenceManager.java +Copyright (C) 2011 Belledonne Communications, Grenoble, France + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +package org.linphone; + +import android.content.Context; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; + +public class LinphonePreferenceManager { + + private static LinphonePreferenceManager instance; + private static Context c; + private static SharedPreferences p; + + public LinphonePreferenceManager() { + p = PreferenceManager.getDefaultSharedPreferences(c); + } + + private String getString(int key) { + return c.getString(key); + } + + public static final synchronized LinphonePreferenceManager getInstance() { + if (c == null) throw new RuntimeException("need a context"); + if (instance == null) instance = new LinphonePreferenceManager(); + return instance; + } + + public static final void setContext(Context context) { + c = context.getApplicationContext(); + } + + + public boolean useSoftvolume() { + return p.getBoolean( + getString(R.string.pref_audio_soft_volume_key), false); + } +} diff --git a/src/org/linphone/LinphonePreferencesActivity.java b/src/org/linphone/LinphonePreferencesActivity.java index ec2859b3f..259372067 100644 --- a/src/org/linphone/LinphonePreferencesActivity.java +++ b/src/org/linphone/LinphonePreferencesActivity.java @@ -32,6 +32,7 @@ import java.util.Arrays; import java.util.List; import org.linphone.LinphoneManager.EcCalibrationListener; +import org.linphone.core.Hacks; import org.linphone.core.LinphoneCoreException; import org.linphone.core.Version; import org.linphone.core.LinphoneCore.EcCalibratorStatus; @@ -96,7 +97,7 @@ public class LinphonePreferencesActivity extends PreferenceActivity implements E // No video if (Version.sdkStrictlyBelow(5) || !fastCpu || !LinphoneManager.getInstance().hasCamera()) { - disableCheckbox(pref_video_enable_key); + uncheckAndDisableCheckbox(pref_video_enable_key); } if (prefs().getBoolean(LinphoneActivity.PREF_FIRST_LAUNCH,true)) { if (fastCpu) { @@ -110,8 +111,10 @@ public class LinphonePreferencesActivity extends PreferenceActivity implements E detectVideoCodec(R.string.pref_video_codec_h264_key, "H264"); if (!AndroidCameraRecordManager.getInstance().hasFrontCamera()) { - disableAndHideCheckbox(R.string.pref_video_use_front_camera_key); + uncheckDisableAndHideCheckbox(R.string.pref_video_use_front_camera_key); } + + if (Hacks.needSoftvolume()) checkAndDisableCheckbox(R.string.pref_audio_soft_volume_key); } @@ -194,19 +197,22 @@ public class LinphonePreferencesActivity extends PreferenceActivity implements E }); } - private void disableAndHideCheckbox(int key) { - writeBoolean(key, false); - CheckBoxPreference box = (CheckBoxPreference) findPreference(key); - box.setEnabled(false); - box.setChecked(false); - box.setLayoutResource(R.layout.hidden); + private void uncheckDisableAndHideCheckbox(int key) { + manageCheckbox(key, false, false, true); } - private void disableCheckbox(int key) { - writeBoolean(key, false); + private void uncheckAndDisableCheckbox(int key) { + manageCheckbox(key, false, false, false); + } + private void checkAndDisableCheckbox(int key) { + manageCheckbox(key, true, false, false); + } + private void manageCheckbox(int key, boolean value, boolean enabled, boolean hidden) { CheckBoxPreference box = (CheckBoxPreference) findPreference(key); - box.setEnabled(false); - box.setChecked(false); + box.setEnabled(enabled); + box.setChecked(value); + writeBoolean(key, value); + if (hidden) box.setLayoutResource(R.layout.hidden); } private Preference findPreference(int key) { diff --git a/src/org/linphone/SoftVolume.java b/src/org/linphone/SoftVolumeActivity.java similarity index 62% rename from src/org/linphone/SoftVolume.java rename to src/org/linphone/SoftVolumeActivity.java index 43867f300..f8c731f5f 100644 --- a/src/org/linphone/SoftVolume.java +++ b/src/org/linphone/SoftVolumeActivity.java @@ -18,32 +18,39 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ package org.linphone; -import android.content.Context; -import android.preference.PreferenceManager; +import org.linphone.core.Hacks; + +import android.app.Activity; import android.view.KeyEvent; -public class SoftVolume { +/** + * Activity which handles softvolume. + * @author Guillaume Beraudo + * + */ +public class SoftVolumeActivity extends Activity { - private Context c; + private static boolean preventVolumeBarToDisplay = false; - public SoftVolume(Context context) { - c = context.getApplicationContext(); - } public boolean onKeyDown(int keyCode, KeyEvent event) { - if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean( - c.getString(R.string.pref_audio_soft_volume_key), false)) + if (keyCode != KeyEvent.KEYCODE_VOLUME_UP && keyCode != KeyEvent.KEYCODE_VOLUME_DOWN) { + return false; + } + + if (!Hacks.needSoftvolume() && !LinphonePreferenceManager.getInstance().useSoftvolume()) return false; + if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { - LinphoneManager.getInstance().adjustSoftwareVolume(6); - return true; + LinphoneManager.getInstance().adjustSoftwareVolume(1); } else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { - LinphoneManager.getInstance().adjustSoftwareVolume(-6); - return true; + LinphoneManager.getInstance().adjustSoftwareVolume(-1); } - - return false; + + return preventVolumeBarToDisplay; } + + } diff --git a/src/org/linphone/VideoCallActivity.java b/src/org/linphone/VideoCallActivity.java index 3d6714ce1..901454ed3 100644 --- a/src/org/linphone/VideoCallActivity.java +++ b/src/org/linphone/VideoCallActivity.java @@ -25,7 +25,6 @@ import org.linphone.core.Version; import org.linphone.core.VideoSize; import org.linphone.core.video.AndroidCameraRecordManager; -import android.app.Activity; import android.content.Context; import android.content.pm.ActivityInfo; import android.os.Bundle; @@ -43,7 +42,7 @@ import android.view.ViewGroup.LayoutParams; * @author Guillaume Beraudo * */ -public class VideoCallActivity extends Activity { +public class VideoCallActivity extends SoftVolumeActivity { private SurfaceView mVideoView; private SurfaceView mVideoCaptureView; private AndroidCameraRecordManager recordManager; @@ -207,5 +206,4 @@ public class VideoCallActivity extends Activity { super.onPause(); } - } diff --git a/src/org/linphone/core/Hacks.java b/src/org/linphone/core/Hacks.java index 1f96da119..23ebcb487 100644 --- a/src/org/linphone/core/Hacks.java +++ b/src/org/linphone/core/Hacks.java @@ -97,4 +97,8 @@ public final class Hacks { Log.d(LinphoneManager.TAG, sb.toString()); } + + public static boolean needSoftvolume() { + return isGalaxySOrTab(); + } }