From 924597d0f4dd0b0c52f04fbe9c42da61796890fe Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Thu, 6 Feb 2014 17:20:32 +0100 Subject: [PATCH] Improve service notification creation/update to fix Android 4.4 (and probably 4.3) not updating issue --- src/org/linphone/LinphoneService.java | 48 ++++++++-------- .../compatibility/ApiEighteenPlus.java | 55 +++++++++++++++++++ .../linphone/compatibility/ApiElevenPlus.java | 6 -- .../linphone/compatibility/ApiFivePlus.java | 15 +++-- .../linphone/compatibility/Compatibility.java | 16 +++--- 5 files changed, 100 insertions(+), 40 deletions(-) create mode 100644 src/org/linphone/compatibility/ApiEighteenPlus.java diff --git a/src/org/linphone/LinphoneService.java b/src/org/linphone/LinphoneService.java index 9339f5fd1..6062aa121 100644 --- a/src/org/linphone/LinphoneService.java +++ b/src/org/linphone/LinphoneService.java @@ -141,18 +141,17 @@ public final class LinphoneService extends Service implements LinphoneServiceLis mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNM.cancel(INCALL_NOTIF_ID); // in case of crash the icon is not removed - - mNotif = new Notification(); - mNotif.icon = R.drawable.status_level; - mNotif.when = System.currentTimeMillis(); - mNotif.iconLevel=IC_LEVEL_ORANGE; - mNotif.flags |= Notification.FLAG_ONGOING_EVENT; Intent notifIntent = new Intent(this, incomingReceivedActivity); notifIntent.putExtra("Notification", true); mNotifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT); - - Compatibility.setNotificationLatestEventInfo(mNotif, this, mNotificationTitle, "", mNotifContentIntent); + + Bitmap bm = null; + try { + bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57); + } catch (Exception e) { + } + mNotif = Compatibility.createNotification(this, mNotificationTitle, "", R.drawable.status_level, IC_LEVEL_OFFLINE, bm, mNotifContentIntent); LinphoneManager.createAndStart(this, this); mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); @@ -279,20 +278,17 @@ public final class LinphoneService extends Service implements LinphoneServiceLis public void addNotification(Intent onClickIntent, int iconResourceID, String title, String message) { PendingIntent notifContentIntent = PendingIntent.getActivity(this, 0, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT); - if (mCustomNotif == null) { - mCustomNotif = new Notification(); + Bitmap bm = null; + try { + bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57); + } catch (Exception e) { } - - mCustomNotif.icon = iconResourceID; - mCustomNotif.iconLevel = 0; - mCustomNotif.when = System.currentTimeMillis(); - mCustomNotif.flags &= Notification.FLAG_ONGOING_EVENT; + mCustomNotif = Compatibility.createNotification(this, title, message, iconResourceID, 0, bm, notifContentIntent); mCustomNotif.defaults |= Notification.DEFAULT_VIBRATE; mCustomNotif.defaults |= Notification.DEFAULT_SOUND; mCustomNotif.defaults |= Notification.DEFAULT_LIGHTS; - Compatibility.setNotificationLatestEventInfo(mCustomNotif, this, title, message, notifContentIntent); notifyWrapper(CUSTOM_NOTIF_ID, mCustomNotif); } @@ -441,8 +437,6 @@ public final class LinphoneService extends Service implements LinphoneServiceLis } private synchronized void sendNotification(int level, int textId) { - mNotif.iconLevel = level; - mNotif.when=System.currentTimeMillis(); 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. @@ -450,8 +444,13 @@ public final class LinphoneService extends Service implements LinphoneServiceLis String id = lpc != null ? lpc.getIdentity() : ""; text = String.format(text, id); } - - Compatibility.setNotificationLatestEventInfo(mNotif, this, mNotificationTitle, text, mNotifContentIntent); + + Bitmap bm = null; + try { + bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57); + } catch (Exception e) { + } + mNotif = Compatibility.createNotification(this, mNotificationTitle, text, R.drawable.status_level, level, bm, mNotifContentIntent); notifyWrapper(NOTIF_ID, mNotif); } @@ -462,7 +461,7 @@ public final class LinphoneService extends Service implements LinphoneServiceLis * stop linphone as soon as it is started. Transport configured with TLS. */ private synchronized void notifyWrapper(int id, Notification notification) { - if (instance != null) { + if (instance != null && notification != null) { mNM.notify(id, notification); } else { Log.i("Service not ready, discarding notification"); @@ -532,6 +531,7 @@ public final class LinphoneService extends Service implements LinphoneServiceLis if ((state == RegistrationState.RegistrationFailed || state == RegistrationState.RegistrationCleared) && (LinphoneManager.getLc().getDefaultProxyConfig() == null || !LinphoneManager.getLc().getDefaultProxyConfig().isRegistered())) { sendNotification(IC_LEVEL_OFFLINE, R.string.notification_register_failure); } + if (state == RegistrationState.RegistrationNone) { sendNotification(IC_LEVEL_OFFLINE, R.string.notification_started); } @@ -553,7 +553,11 @@ public final class LinphoneService extends Service implements LinphoneServiceLis private void resetIntentLaunchedOnNotificationClick() { Intent notifIntent = new Intent(this, incomingReceivedActivity); mNotifContentIntent = PendingIntent.getActivity(this, 0, notifIntent, PendingIntent.FLAG_UPDATE_CURRENT); - Compatibility.setNotificationLatestEventInfo(mNotif, this, mNotificationTitle, "", mNotifContentIntent); + + if (mNotif != null) { + mNotif.contentIntent = mNotifContentIntent; + } + notifyWrapper(NOTIF_ID, mNotif); } protected void onIncomingReceived() { diff --git a/src/org/linphone/compatibility/ApiEighteenPlus.java b/src/org/linphone/compatibility/ApiEighteenPlus.java new file mode 100644 index 000000000..30371810f --- /dev/null +++ b/src/org/linphone/compatibility/ApiEighteenPlus.java @@ -0,0 +1,55 @@ +package org.linphone.compatibility; + +/* +ApiEighteenPlus.java +Copyright (C) 2012 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. +*/ + +import android.annotation.TargetApi; +import android.app.Notification; +import android.app.PendingIntent; +import android.content.Context; +import android.graphics.Bitmap; + +@TargetApi(18) +public class ApiEighteenPlus { + public static Notification createNotification(Context context, String title, String message, int icon, int level, Bitmap largeIcon, PendingIntent intent) { + Notification notif; + + if (largeIcon != null) { + notif = new Notification.Builder(context) + .setContentTitle(title) + .setContentText(message) + .setSmallIcon(icon, level) + .setLargeIcon(largeIcon) + .setContentIntent(intent) + .setWhen(System.currentTimeMillis()) + .build(); + } else { + notif = new Notification.Builder(context) + .setContentTitle(title) + .setContentText(message) + .setSmallIcon(icon, level) + .setContentIntent(intent) + .setWhen(System.currentTimeMillis()) + .build(); + } + notif.flags |= Notification.FLAG_ONGOING_EVENT; + + return notif; + } +} diff --git a/src/org/linphone/compatibility/ApiElevenPlus.java b/src/org/linphone/compatibility/ApiElevenPlus.java index 04debf7e1..10bbfc3ed 100644 --- a/src/org/linphone/compatibility/ApiElevenPlus.java +++ b/src/org/linphone/compatibility/ApiElevenPlus.java @@ -88,12 +88,6 @@ public class ApiElevenPlus { return notif; } - @SuppressWarnings("deprecation") - public static void setNotificationLatestEventInfo(Notification notif, - Context context, String title, String content, PendingIntent intent) { - notif.setLatestEventInfo(context, title, content, intent); - } - public static void copyTextToClipboard(Context context, String msg) { ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); ClipData clip = android.content.ClipData.newPlainText("Message", msg); diff --git a/src/org/linphone/compatibility/ApiFivePlus.java b/src/org/linphone/compatibility/ApiFivePlus.java index ed5152033..93eb98fae 100644 --- a/src/org/linphone/compatibility/ApiFivePlus.java +++ b/src/org/linphone/compatibility/ApiFivePlus.java @@ -297,10 +297,6 @@ public class ApiFivePlus { return notif; } - public static void setNotificationLatestEventInfo(Notification notif, Context context, String title, String content, PendingIntent intent) { - notif.setLatestEventInfo(context, title, content, intent); - } - public static void setPreferenceChecked(Preference preference, boolean checked) { ((CheckBoxPreference) preference).setChecked(checked); } @@ -370,4 +366,15 @@ public class ApiFivePlus { /* Do not use MODE_IN_CALL, because it is reserved to GSM. This is causing conflicts on audio system resulting in silenced audio.*/ //manager.setMode(AudioManager.MODE_IN_CALL); } + + public static Notification createNotification(Context context, String title, String message, int icon, int level, PendingIntent intent) { + Notification notif = new Notification(); + notif.icon = icon; + notif.iconLevel = level; + notif.when = System.currentTimeMillis(); + notif.flags |= Notification.FLAG_ONGOING_EVENT; + notif.setLatestEventInfo(context, title, message, intent); + + return notif; + } } diff --git a/src/org/linphone/compatibility/Compatibility.java b/src/org/linphone/compatibility/Compatibility.java index f4d208de3..51261bba2 100644 --- a/src/org/linphone/compatibility/Compatibility.java +++ b/src/org/linphone/compatibility/Compatibility.java @@ -164,14 +164,6 @@ public class Compatibility { return null; } - public static void setNotificationLatestEventInfo(Notification notif, Context context, String title, String content, PendingIntent intent) { - if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30)) { - ApiElevenPlus.setNotificationLatestEventInfo(notif, context, title, content, intent); - } else { - ApiFivePlus.setNotificationLatestEventInfo(notif, context, title, content, intent); - } - } - public static CompatibilityScaleGestureDetector getScaleGestureDetector(Context context, CompatibilityScaleGestureListener listener) { if (Version.sdkAboveOrEqual(Version.API08_FROYO_22)) { CompatibilityScaleGestureDetector csgd = new CompatibilityScaleGestureDetector(context); @@ -274,4 +266,12 @@ public class Compatibility { ApiFivePlus.setAudioManagerInCallMode(manager); } } + + public static Notification createNotification(Context context, String title, String message, int icon, int iconLevel, Bitmap largeIcon, PendingIntent intent) { + if (Version.sdkAboveOrEqual(18)) { + return ApiEighteenPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent); + } else { + return ApiFivePlus.createNotification(context, title, message, icon, iconLevel, intent); + } + } }