diff --git a/res/values/non_localizable_strings.xml b/res/values/non_localizable_strings.xml index f204815ee..2d39b0234 100644 --- a/res/values/non_localizable_strings.xml +++ b/res/values/non_localizable_strings.xml @@ -71,6 +71,7 @@ pref_preferred_video_fps_key pref_bandwidth_limit_key pref_animation_enable_key + pref_service_notification_key pref_escape_plus_key pref_friendlist_subscribe_key pref_echo_cancellation_key diff --git a/res/values/strings.xml b/res/values/strings.xml index 943878304..d949fc668 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -291,6 +291,7 @@ Debug Background mode Enable Animations + Enable service notification Start at boot time Incoming call hangup (in seconds) Sharing server diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml index 8eab25ca6..36abf4528 100644 --- a/res/xml/preferences.xml +++ b/res/xml/preferences.xml @@ -202,6 +202,10 @@ android:title="@string/pref_background_mode" android:key="@string/pref_background_mode_key"/> + + diff --git a/src/org/linphone/LinphonePreferences.java b/src/org/linphone/LinphonePreferences.java index 8c8395074..73a5893ba 100644 --- a/src/org/linphone/LinphonePreferences.java +++ b/src/org/linphone/LinphonePreferences.java @@ -1334,4 +1334,12 @@ public class LinphonePreferences { public void setActivityToLaunchOnIncomingReceived(String name) { getConfig().setString("app", "incoming_call_activity", name); } + + public boolean getServiceNotificationVisibility() { + return getConfig().getBool("app", "show_service_notification", true); + } + + public void setServiceNotificationVisibility(boolean enable) { + getConfig().setBool("app", "show_service_notification", enable); + } } diff --git a/src/org/linphone/LinphoneService.java b/src/org/linphone/LinphoneService.java index 47dedb3bc..ae23baafc 100644 --- a/src/org/linphone/LinphoneService.java +++ b/src/org/linphone/LinphoneService.java @@ -32,6 +32,7 @@ import org.linphone.core.LinphoneCoreException; import org.linphone.core.LinphoneCoreFactory; import org.linphone.core.LinphoneCoreListenerBase; import org.linphone.core.LinphoneProxyConfig; +import org.linphone.core.LpConfig; import org.linphone.mediastream.Log; import org.linphone.mediastream.Version; @@ -46,7 +47,6 @@ import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager.NameNotFoundException; -import android.database.ContentObserver; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; @@ -126,6 +126,31 @@ public final class LinphoneService extends Service { public void resetMessageNotifCount() { mMsgNotifCount = 0; } + + private boolean displayServiceNotification() { + return LinphonePreferences.instance().getServiceNotificationVisibility(); + } + + public void showServiceNotification() { + startForegroundCompat(NOTIF_ID, mNotif); + + LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); + if (lc == null) return; + LinphoneProxyConfig lpc = lc.getDefaultProxyConfig(); + if (lpc != null) { + if (lpc.isRegistered()) { + sendNotification(IC_LEVEL_ORANGE, R.string.notification_registered); + } else { + sendNotification(IC_LEVEL_ORANGE, R.string.notification_register_failure); + } + } else { + sendNotification(IC_LEVEL_ORANGE, R.string.notification_started); + } + } + + public void hideServiceNotification() { + stopForegroundCompat(NOTIF_ID); + } @SuppressWarnings("unchecked") @Override @@ -195,7 +220,7 @@ public final class LinphoneService extends Service { @Override public void globalState(LinphoneCore lc,LinphoneCore.GlobalState state, String message) { - if (state == GlobalState.GlobalOn) { + if (state == GlobalState.GlobalOn && displayServiceNotification()) { sendNotification(IC_LEVEL_ORANGE, R.string.notification_started); } } @@ -207,15 +232,15 @@ public final class LinphoneService extends Service { // return; // } if (!mDisableRegistrationStatus) { - if (state == RegistrationState.RegistrationOk && LinphoneManager.getLc().getDefaultProxyConfig() != null && LinphoneManager.getLc().getDefaultProxyConfig().isRegistered()) { + if (displayServiceNotification() && state == RegistrationState.RegistrationOk && LinphoneManager.getLc().getDefaultProxyConfig() != null && LinphoneManager.getLc().getDefaultProxyConfig().isRegistered()) { sendNotification(IC_LEVEL_ORANGE, R.string.notification_registered); } - if ((state == RegistrationState.RegistrationFailed || state == RegistrationState.RegistrationCleared) && (LinphoneManager.getLc().getDefaultProxyConfig() == null || !LinphoneManager.getLc().getDefaultProxyConfig().isRegistered())) { + if (displayServiceNotification() && (state == RegistrationState.RegistrationFailed || state == RegistrationState.RegistrationCleared) && (LinphoneManager.getLc().getDefaultProxyConfig() == null || !LinphoneManager.getLc().getDefaultProxyConfig().isRegistered())) { sendNotification(IC_LEVEL_ORANGE, R.string.notification_register_failure); } - if (state == RegistrationState.RegistrationNone) { + if (displayServiceNotification() && state == RegistrationState.RegistrationNone) { sendNotification(IC_LEVEL_ORANGE, R.string.notification_started); } } @@ -241,7 +266,9 @@ public final class LinphoneService extends Service { getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, ContactsManager.getInstance()); - startForegroundCompat(NOTIF_ID, mNotif); + if (displayServiceNotification()) { + startForegroundCompat(NOTIF_ID, mNotif); + } if (!mTestDelayElapsed) { // Only used when testing. Simulates a 5 seconds delay for launching service @@ -505,7 +532,7 @@ public final class LinphoneService extends Service { mDisableRegistrationStatus = true; } - public synchronized void sendNotification(int level, int textId) { + private synchronized void sendNotification(int level, int textId) { String text = getString(textId); if (text.contains("%s") && LinphoneManager.getLc() != null) { // Test for null lc is to avoid a NPE when Android mess up badly with the String resources. @@ -593,10 +620,10 @@ public final class LinphoneService extends Service { Intent notifIntent = new Intent(this, incomingReceivedActivity); mNotifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT); - if (mNotif != null) { + /*if (mNotif != null) { mNotif.contentIntent = mNotifContentIntent; } - notifyWrapper(NOTIF_ID, mNotif); + notifyWrapper(NOTIF_ID, mNotif);*/ } protected void onIncomingReceived() { diff --git a/src/org/linphone/SettingsFragment.java b/src/org/linphone/SettingsFragment.java index 3aa3195ef..6f6386a59 100644 --- a/src/org/linphone/SettingsFragment.java +++ b/src/org/linphone/SettingsFragment.java @@ -899,6 +899,7 @@ public class SettingsFragment extends PreferencesListFragment { ((CheckBoxPreference)findPreference(getString(R.string.pref_debug_key))).setChecked(mPrefs.isDebugEnabled()); ((CheckBoxPreference)findPreference(getString(R.string.pref_background_mode_key))).setChecked(mPrefs.isBackgroundModeEnabled()); ((CheckBoxPreference)findPreference(getString(R.string.pref_animation_enable_key))).setChecked(mPrefs.areAnimationsEnabled()); + ((CheckBoxPreference)findPreference(getString(R.string.pref_service_notification_key))).setChecked(mPrefs.getServiceNotificationVisibility()); ((CheckBoxPreference)findPreference(getString(R.string.pref_autostart_key))).setChecked(mPrefs.isAutoStartEnabled()); setPreferenceDefaultValueAndSummary(R.string.pref_image_sharing_server_key, mPrefs.getSharingPictureServerUrl()); setPreferenceDefaultValueAndSummary(R.string.pref_remote_provisioning_key, mPrefs.getRemoteProvisioningUrl()); @@ -934,6 +935,20 @@ public class SettingsFragment extends PreferencesListFragment { } }); + findPreference(getString(R.string.pref_service_notification_key)).setOnPreferenceChangeListener(new OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + boolean value = (Boolean) newValue; + mPrefs.setServiceNotificationVisibility(value); + if (value) { + LinphoneService.instance().showServiceNotification(); + } else { + LinphoneService.instance().hideServiceNotification(); + } + return true; + } + }); + findPreference(getString(R.string.pref_autostart_key)).setOnPreferenceChangeListener(new OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) {