From 2d2e14647f409794ae13e98f866118185b7a0d24 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Tue, 24 Jul 2012 15:22:44 +0200 Subject: [PATCH] Better notifications for JB + improved shadowed avatar --- res/layout/avatar.xml | 14 +-- src/org/linphone/LinphoneService.java | 85 +++++++++++-------- .../compatibility/ApiSixteenPlus.java | 57 +++++++++++++ .../linphone/compatibility/Compatibility.java | 48 +++++++++++ 4 files changed, 161 insertions(+), 43 deletions(-) create mode 100644 src/org/linphone/compatibility/ApiSixteenPlus.java diff --git a/res/layout/avatar.xml b/res/layout/avatar.xml index 9f0b1161e..fd202987e 100644 --- a/res/layout/avatar.xml +++ b/res/layout/avatar.xml @@ -12,13 +12,13 @@ android:src="@drawable/avatar_shadow_left" /> + android:id="@+id/picture" + android:layout_width="100dp" + android:layout_height="100dp" + android:layout_gravity="center" + android:contentDescription="@string/content_description_contact_picture" + android:scaleType="fitEnd" + android:src="@drawable/unknown_small" /> messagesNotifications; public static boolean isReady() { return instance!=null && instance.mTestDelayElapsed; } @@ -103,6 +111,7 @@ public final class LinphoneService extends Service implements LinphoneServiceLis private Notification mNotif; private Notification mIncallNotif; private Notification mMsgNotif; + private Notification mCustomNotif; private int mMsgNotifCount; private PendingIntent mNotifContentIntent; private String mNotificationTitle; @@ -192,33 +201,45 @@ public final class LinphoneService extends Service implements LinphoneServiceLis private synchronized void setIncallIcon(IncallIconState state) { if (state == mCurrentIncallIconState) return; mCurrentIncallIconState = state; - if (mIncallNotif == null) mIncallNotif = new Notification(); int notificationTextId = 0; + int inconId = 0; + switch (state) { case IDLE: mNM.cancel(INCALL_NOTIF_ID); return; case INCALL: - mIncallNotif.icon = R.drawable.conf_unhook; + inconId = R.drawable.conf_unhook; notificationTextId = R.string.incall_notif_active; break; case PAUSE: - mIncallNotif.icon = R.drawable.conf_status_paused; + inconId = R.drawable.conf_status_paused; notificationTextId = R.string.incall_notif_paused; break; case VIDEO: - mIncallNotif.icon = R.drawable.conf_video; + inconId = R.drawable.conf_video; notificationTextId = R.string.incall_notif_video; break; default: throw new IllegalArgumentException("Unknown state " + state); } + + String userName = LinphoneManager.getLc().getCurrentCall().getRemoteAddress().getUserName(); + String domain = LinphoneManager.getLc().getCurrentCall().getRemoteAddress().getDomain(); + String displayName = LinphoneManager.getLc().getCurrentCall().getRemoteAddress().getDisplayName(); + LinphoneAddress address = LinphoneCoreFactoryImpl.instance().createLinphoneAddress("sip:" + userName + "@" + domain); + address.setDisplayName(displayName); + + Uri pictureUri = LinphoneUtils.findUriPictureOfContactAndSetDisplayName(address, getContentResolver()); + Bitmap bm = null; + try { + bm = MediaStore.Images.Media.getBitmap(getContentResolver(), pictureUri); + } catch (Exception e) { + bm = BitmapFactory.decodeResource(getResources(), R.drawable.unknown_small); + } + mIncallNotif = Compatibility.createInCallNotification(getApplicationContext(), mNotificationTitle, getString(notificationTextId), inconId, bm, mNotifContentIntent); - mIncallNotif.iconLevel = 0; - mIncallNotif.when=System.currentTimeMillis(); - mIncallNotif.flags &= Notification.FLAG_ONGOING_EVENT; - mIncallNotif.setLatestEventInfo(this, mNotificationTitle, getString(notificationTextId), mNotifContentIntent); notifyWrapper(INCALL_NOTIF_ID, mIncallNotif); } @@ -243,22 +264,22 @@ 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_CANCEL_CURRENT); - if (mMsgNotif == null) { - mMsgNotif = new Notification(); + if (mCustomNotif == null) { + mCustomNotif = new Notification(); } - mMsgNotif.icon = iconResourceID; - mMsgNotif.iconLevel = 0; - mMsgNotif.when = System.currentTimeMillis(); - mMsgNotif.flags &= Notification.FLAG_ONGOING_EVENT; + mCustomNotif.icon = iconResourceID; + mCustomNotif.iconLevel = 0; + mCustomNotif.when = System.currentTimeMillis(); + mCustomNotif.flags &= Notification.FLAG_ONGOING_EVENT; - mMsgNotif.defaults |= Notification.DEFAULT_VIBRATE; - mMsgNotif.defaults |= Notification.DEFAULT_SOUND; - mMsgNotif.defaults |= Notification.DEFAULT_LIGHTS; + mCustomNotif.defaults |= Notification.DEFAULT_VIBRATE; + mCustomNotif.defaults |= Notification.DEFAULT_SOUND; + mCustomNotif.defaults |= Notification.DEFAULT_LIGHTS; - mMsgNotif.setLatestEventInfo(this, title, message, notifContentIntent); + mCustomNotif.setLatestEventInfo(this, title, message, notifContentIntent); - notifyWrapper(CUSTOM_NOTIF_ID, mMsgNotif); + notifyWrapper(CUSTOM_NOTIF_ID, mCustomNotif); } public void displayMessageNotification(String fromSipUri, String fromName, String message) { @@ -274,27 +295,19 @@ public final class LinphoneService extends Service implements LinphoneServiceLis if (mMsgNotif == null) { mMsgNotifCount = 1; - mMsgNotif = new Notification(); - - mMsgNotif.icon = R.drawable.chat_icon_over; - mMsgNotif.iconLevel = 0; - mMsgNotif.when = System.currentTimeMillis(); - mMsgNotif.flags &= Notification.FLAG_ONGOING_EVENT; - - mMsgNotif.defaults |= Notification.DEFAULT_VIBRATE; - mMsgNotif.defaults |= Notification.DEFAULT_SOUND; - mMsgNotif.defaults |= Notification.DEFAULT_LIGHTS; - - String title = "New message from %s :".replace("%s", fromName); - mMsgNotif.setLatestEventInfo(this, title, message, notifContentIntent); } else { mMsgNotifCount++; - mMsgNotif.when = System.currentTimeMillis(); - - String title = mMsgNotifCount + " new messages from %s".replace("%s", fromName); - mMsgNotif.setLatestEventInfo(this, title, "", notifContentIntent); } + Uri pictureUri = LinphoneUtils.findUriPictureOfContactAndSetDisplayName(LinphoneCoreFactoryImpl.instance().createLinphoneAddress(fromSipUri), getContentResolver()); + Bitmap bm = null; + try { + bm = MediaStore.Images.Media.getBitmap(getContentResolver(), pictureUri); + } catch (Exception e) { + bm = BitmapFactory.decodeResource(getResources(), R.drawable.unknown_small); + } + mMsgNotif = Compatibility.createMessageNotification(getApplicationContext(), mMsgNotifCount, fromName, message, bm, notifContentIntent); + notifyWrapper(MESSAGE_NOTIF_ID, mMsgNotif); } diff --git a/src/org/linphone/compatibility/ApiSixteenPlus.java b/src/org/linphone/compatibility/ApiSixteenPlus.java new file mode 100644 index 000000000..46a81333a --- /dev/null +++ b/src/org/linphone/compatibility/ApiSixteenPlus.java @@ -0,0 +1,57 @@ +package org.linphone.compatibility; + +import org.linphone.R; + +import android.annotation.TargetApi; +import android.app.Notification; +import android.app.PendingIntent; +import android.content.Context; +import android.graphics.Bitmap; + +@TargetApi(16) +public class ApiSixteenPlus { + + public static Notification createMessageNotification(Context context, int msgCount, String msgSender, String msg, Bitmap contactIcon, PendingIntent intent) { + String title, summary; + if (msgCount == 1) { + title = "Unread message from %s".replace("%s", msgSender); + summary = ""; + } else { + title = "%i unread messages".replace("%i", String.valueOf(msgCount)); + summary = "+" + (msgCount - 1) + " more"; + } + + Notification notif = new Notification.BigPictureStyle( + new Notification.Builder(context) + .setContentTitle(title) + .setContentText(msg) + .setSmallIcon(R.drawable.chat_icon_default) + .setAutoCancel(true) + .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE) + .setWhen(System.currentTimeMillis()) + .setLargeIcon(contactIcon)) + .setSummaryText(summary) + .build(); + notif.contentIntent = intent; + + return notif; + } + + public static Notification createInCallNotification(Context context, + String title, String msg, int iconID, Bitmap contactIcon, + PendingIntent intent) { + + Notification notif = new Notification.BigPictureStyle( + new Notification.Builder(context) + .setContentTitle(title) + .setContentText(msg) + .setSmallIcon(iconID) + .setAutoCancel(false) + .setWhen(System.currentTimeMillis()) + .setLargeIcon(contactIcon)) + .build(); + notif.contentIntent = intent; + + return notif; + } +} diff --git a/src/org/linphone/compatibility/Compatibility.java b/src/org/linphone/compatibility/Compatibility.java index 31afed040..425dc26cb 100644 --- a/src/org/linphone/compatibility/Compatibility.java +++ b/src/org/linphone/compatibility/Compatibility.java @@ -21,13 +21,18 @@ import java.io.InputStream; import java.util.List; import org.linphone.Contact; +import org.linphone.R; import org.linphone.core.LinphoneAddress; import org.linphone.mediastream.Version; import android.app.Activity; +import android.app.Notification; +import android.app.PendingIntent; import android.content.ContentResolver; +import android.content.Context; import android.content.Intent; import android.database.Cursor; +import android.graphics.Bitmap; import android.net.Uri; /** * @author Sylvain Berfini @@ -119,4 +124,47 @@ public class Compatibility { } return null; } + + public static Notification createMessageNotification(Context context, int msgCount, String msgSender, String msg, Bitmap contactIcon, PendingIntent intent) { + Notification notif = null; + String title; + if (msgCount == 1) { + title = "Unread message from %s".replace("%s", msgSender); + } else { + title = "%i unread messages".replace("%i", String.valueOf(msgCount)); + } + + if (Version.sdkAboveOrEqual(16)) { + notif = ApiSixteenPlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent); + } else { + notif = new Notification(); + notif.icon = R.drawable.chat_icon_over; + notif.iconLevel = 0; + notif.when = System.currentTimeMillis(); + notif.flags &= Notification.FLAG_ONGOING_EVENT; + + notif.defaults |= Notification.DEFAULT_VIBRATE; + notif.defaults |= Notification.DEFAULT_SOUND; + notif.defaults |= Notification.DEFAULT_LIGHTS; + + notif.setLatestEventInfo(context, title, msg, intent); + } + return notif; + } + + public static Notification createInCallNotification(Context context, String title, String msg, int iconID, Bitmap contactIcon, PendingIntent intent) { + Notification notif = null; + + if (Version.sdkAboveOrEqual(16)) { + notif = ApiSixteenPlus.createInCallNotification(context, title, msg, iconID, contactIcon, intent); + } else { + notif = new Notification(); + notif.iconLevel = 0; + notif.when=System.currentTimeMillis(); + notif.flags &= Notification.FLAG_ONGOING_EVENT; + + notif.setLatestEventInfo(context, title, msg, intent); + } + return notif; + } }