Improved API to add custom notification

This commit is contained in:
Sylvain Berfini 2014-09-08 15:59:08 +02:00
parent 9e591188f1
commit 6dc5194656
5 changed files with 25 additions and 15 deletions

View file

@ -151,7 +151,7 @@ public final class LinphoneService extends Service implements LinphoneServiceLis
bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57); bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57);
} catch (Exception e) { } catch (Exception e) {
} }
mNotif = Compatibility.createNotification(this, mNotificationTitle, "", R.drawable.status_level, IC_LEVEL_OFFLINE, bm, mNotifContentIntent); mNotif = Compatibility.createNotification(this, mNotificationTitle, "", R.drawable.status_level, IC_LEVEL_OFFLINE, bm, mNotifContentIntent, true);
LinphoneManager.createAndStart(this, this); LinphoneManager.createAndStart(this, this);
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
@ -275,7 +275,12 @@ public final class LinphoneService extends Service implements LinphoneServiceLis
} }
} }
@Deprecated
public void addNotification(Intent onClickIntent, int iconResourceID, String title, String message) { public void addNotification(Intent onClickIntent, int iconResourceID, String title, String message) {
addCustomNotification(onClickIntent, iconResourceID, title, message, true);
}
public void addCustomNotification(Intent onClickIntent, int iconResourceID, String title, String message, boolean isOngoingEvent) {
PendingIntent notifContentIntent = PendingIntent.getActivity(this, 0, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT); PendingIntent notifContentIntent = PendingIntent.getActivity(this, 0, onClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Bitmap bm = null; Bitmap bm = null;
@ -283,7 +288,7 @@ public final class LinphoneService extends Service implements LinphoneServiceLis
bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57); bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57);
} catch (Exception e) { } catch (Exception e) {
} }
mCustomNotif = Compatibility.createNotification(this, title, message, iconResourceID, 0, bm, notifContentIntent); mCustomNotif = Compatibility.createNotification(this, title, message, iconResourceID, 0, bm, notifContentIntent, isOngoingEvent);
mCustomNotif.defaults |= Notification.DEFAULT_VIBRATE; mCustomNotif.defaults |= Notification.DEFAULT_VIBRATE;
mCustomNotif.defaults |= Notification.DEFAULT_SOUND; mCustomNotif.defaults |= Notification.DEFAULT_SOUND;
@ -448,7 +453,7 @@ public final class LinphoneService extends Service implements LinphoneServiceLis
bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57); bm = BitmapFactory.decodeResource(getResources(), R.drawable.logo_linphone_57x57);
} catch (Exception e) { } catch (Exception e) {
} }
mNotif = Compatibility.createNotification(this, mNotificationTitle, text, R.drawable.status_level, level, bm, mNotifContentIntent); mNotif = Compatibility.createNotification(this, mNotificationTitle, text, R.drawable.status_level, level, bm, mNotifContentIntent, true);
notifyWrapper(NOTIF_ID, mNotif); notifyWrapper(NOTIF_ID, mNotif);
} }

View file

@ -20,7 +20,6 @@ import android.net.Uri;
import android.provider.ContactsContract; import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Im; import android.provider.ContactsContract.CommonDataKinds.Im;
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.CommonDataKinds.SipAddress;
import android.provider.ContactsContract.Intents.Insert; import android.provider.ContactsContract.Intents.Insert;
/* /*
@ -92,7 +91,7 @@ public class ApiElevenPlus {
} }
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static Notification createNotification(Context context, String title, String message, int icon, int level, Bitmap largeIcon, PendingIntent intent) { public static Notification createNotification(Context context, String title, String message, int icon, int level, Bitmap largeIcon, PendingIntent intent, boolean isOngoingEvent) {
Notification notif; Notification notif;
if (largeIcon != null) { if (largeIcon != null) {
@ -113,7 +112,9 @@ public class ApiElevenPlus {
.setWhen(System.currentTimeMillis()) .setWhen(System.currentTimeMillis())
.getNotification(); .getNotification();
} }
if (isOngoingEvent) {
notif.flags |= Notification.FLAG_ONGOING_EVENT; notif.flags |= Notification.FLAG_ONGOING_EVENT;
}
return notif; return notif;
} }

View file

@ -367,12 +367,14 @@ public class ApiFivePlus {
//manager.setMode(AudioManager.MODE_IN_CALL); //manager.setMode(AudioManager.MODE_IN_CALL);
} }
public static Notification createNotification(Context context, String title, String message, int icon, int level, PendingIntent intent) { public static Notification createNotification(Context context, String title, String message, int icon, int level, PendingIntent intent, boolean isOngoingEvent) {
Notification notif = new Notification(); Notification notif = new Notification();
notif.icon = icon; notif.icon = icon;
notif.iconLevel = level; notif.iconLevel = level;
notif.when = System.currentTimeMillis(); notif.when = System.currentTimeMillis();
if (isOngoingEvent) {
notif.flags |= Notification.FLAG_ONGOING_EVENT; notif.flags |= Notification.FLAG_ONGOING_EVENT;
}
notif.setLatestEventInfo(context, title, message, intent); notif.setLatestEventInfo(context, title, message, intent);
return notif; return notif;

View file

@ -71,12 +71,12 @@ public class ApiSixteenPlus {
.setContentIntent(intent) .setContentIntent(intent)
.setWhen(System.currentTimeMillis()) .setWhen(System.currentTimeMillis())
.setLargeIcon(contactIcon).build(); .setLargeIcon(contactIcon).build();
notif.flags |= Notification.FLAG_ONGOING_EVENT; notif.flags |= Notification.FLAG_ONGOING_EVENT;
return notif; return notif;
} }
public static Notification createNotification(Context context, String title, String message, int icon, int level, Bitmap largeIcon, PendingIntent intent) { public static Notification createNotification(Context context, String title, String message, int icon, int level, Bitmap largeIcon, PendingIntent intent, boolean isOngoingEvent) {
Notification notif; Notification notif;
if (largeIcon != null) { if (largeIcon != null) {
@ -97,7 +97,9 @@ public class ApiSixteenPlus {
.setWhen(System.currentTimeMillis()) .setWhen(System.currentTimeMillis())
.build(); .build();
} }
if (isOngoingEvent) {
notif.flags |= Notification.FLAG_ONGOING_EVENT; notif.flags |= Notification.FLAG_ONGOING_EVENT;
}
return notif; return notif;
} }

View file

@ -173,13 +173,13 @@ public class Compatibility {
return notif; return notif;
} }
public static Notification createNotification(Context context, String title, String message, int icon, int iconLevel, Bitmap largeIcon, PendingIntent intent) { public static Notification createNotification(Context context, String title, String message, int icon, int iconLevel, Bitmap largeIcon, PendingIntent intent, boolean isOngoingEvent) {
if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) { if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) {
return ApiSixteenPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent); return ApiSixteenPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent, isOngoingEvent);
} else if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30)) { } else if (Version.sdkAboveOrEqual(Version.API11_HONEYCOMB_30)) {
return ApiElevenPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent); return ApiElevenPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent, isOngoingEvent);
} else { } else {
return ApiFivePlus.createNotification(context, title, message, icon, iconLevel, intent); return ApiFivePlus.createNotification(context, title, message, icon, iconLevel, intent, isOngoingEvent);
} }
} }