Fix notifiction on Android O + set color on API >= 21 < 26

This commit is contained in:
Erwan Croze 2017-08-29 11:33:39 +02:00
parent a67d0a6f51
commit 887d9310c0
6 changed files with 179 additions and 41 deletions

View file

@ -12,6 +12,7 @@
<color name="colorJ">#ffa645</color> <color name="colorJ">#ffa645</color>
<color name="colorK">#3eb5c0</color> <color name="colorK">#3eb5c0</color>
<color name="colorL">#96c11f</color> <color name="colorL">#96c11f</color>
<color name="notification_color_led">#FF8000</color>
<color name="transparent">#00000000</color> <color name="transparent">#00000000</color>
</resources> </resources>

View file

@ -94,6 +94,8 @@
<bool name="enable_call_notification">true</bool> <bool name="enable_call_notification">true</bool>
<bool name="kill_service_with_task_manager">true</bool> <bool name="kill_service_with_task_manager">true</bool>
<string name="notification_channel_id">linphone_notification_id</string> <string name="notification_channel_id">linphone_notification_id</string>
<integer name="notification_ms_on">1000</integer>
<integer name="notification_ms_off">7000</integer>
<!-- Tutorial settings --> <!-- Tutorial settings -->
<bool name="show_tutorials_instead_of_app">false</bool> <!-- Be careful ! Setting this to true prevent the app from working ! It will only display tutorial activities --> <bool name="show_tutorials_instead_of_app">false</bool> <!-- Be careful ! Setting this to true prevent the app from working ! It will only display tutorial activities -->

View file

@ -32,7 +32,6 @@ import org.linphone.core.LinphoneCore.GlobalState;
import org.linphone.core.LinphoneCore.RegistrationState; import org.linphone.core.LinphoneCore.RegistrationState;
import org.linphone.core.LinphoneCoreException; import org.linphone.core.LinphoneCoreException;
import org.linphone.core.LinphoneCoreFactory; import org.linphone.core.LinphoneCoreFactory;
import org.linphone.core.LinphoneCoreFactoryImpl;
import org.linphone.core.LinphoneCoreListenerBase; import org.linphone.core.LinphoneCoreListenerBase;
import org.linphone.core.LinphoneProxyConfig; import org.linphone.core.LinphoneProxyConfig;
import org.linphone.mediastream.Log; import org.linphone.mediastream.Log;
@ -314,6 +313,7 @@ public final class LinphoneService extends Service {
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNM.cancel(INCALL_NOTIF_ID); // in case of crash the icon is not removed mNM.cancel(INCALL_NOTIF_ID); // in case of crash the icon is not removed
Compatibility.CreateChannel(this);
Intent notifIntent = new Intent(this, incomingReceivedActivity); Intent notifIntent = new Intent(this, incomingReceivedActivity);
notifIntent.putExtra("Notification", true); notifIntent.putExtra("Notification", true);

View file

@ -7,6 +7,7 @@ import android.app.Notification;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.support.v4.content.ContextCompat;
import android.view.ViewTreeObserver; import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener; import android.view.ViewTreeObserver.OnGlobalLayoutListener;
/* /*
@ -50,8 +51,12 @@ public class ApiTwentyOnePlus {
.setSmallIcon(R.drawable.topbar_chat_notification) .setSmallIcon(R.drawable.topbar_chat_notification)
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent(intent) .setContentIntent(intent)
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE)
.setLargeIcon(contactIcon) .setLargeIcon(contactIcon)
.setLights(ContextCompat.getColor(context, R.color.notification_color_led),
context.getResources().getInteger(R.integer.notification_ms_on),
context.getResources().getInteger(R.integer.notification_ms_off))
.setCategory(Notification.CATEGORY_MESSAGE) .setCategory(Notification.CATEGORY_MESSAGE)
.setVisibility(Notification.VISIBILITY_PRIVATE) .setVisibility(Notification.VISIBILITY_PRIVATE)
.setPriority(Notification.PRIORITY_HIGH) .setPriority(Notification.PRIORITY_HIGH)
@ -74,6 +79,9 @@ public class ApiTwentyOnePlus {
.setCategory(Notification.CATEGORY_CALL) .setCategory(Notification.CATEGORY_CALL)
.setVisibility(Notification.VISIBILITY_PUBLIC) .setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH) .setPriority(Notification.PRIORITY_HIGH)
.setLights(ContextCompat.getColor(context, R.color.notification_color_led),
context.getResources().getInteger(R.integer.notification_ms_on),
context.getResources().getInteger(R.integer.notification_ms_off))
.build(); .build();
return notif; return notif;
@ -91,6 +99,9 @@ public class ApiTwentyOnePlus {
.setContentIntent(intent) .setContentIntent(intent)
.setCategory(Notification.CATEGORY_SERVICE) .setCategory(Notification.CATEGORY_SERVICE)
.setVisibility(Notification.VISIBILITY_SECRET) .setVisibility(Notification.VISIBILITY_SECRET)
.setLights(ContextCompat.getColor(context, R.color.notification_color_led),
context.getResources().getInteger(R.integer.notification_ms_on),
context.getResources().getInteger(R.integer.notification_ms_off))
.setPriority(priority) .setPriority(priority)
.build(); .build();
} else { } else {
@ -101,6 +112,9 @@ public class ApiTwentyOnePlus {
.setContentIntent(intent) .setContentIntent(intent)
.setCategory(Notification.CATEGORY_SERVICE) .setCategory(Notification.CATEGORY_SERVICE)
.setVisibility(Notification.VISIBILITY_SECRET) .setVisibility(Notification.VISIBILITY_SECRET)
.setLights(ContextCompat.getColor(context, R.color.notification_color_led),
context.getResources().getInteger(R.integer.notification_ms_on),
context.getResources().getInteger(R.integer.notification_ms_off))
.setPriority(priority) .setPriority(priority)
.build(); .build();
} }
@ -119,9 +133,13 @@ public class ApiTwentyOnePlus {
.setSmallIcon(R.drawable.call_status_missed) .setSmallIcon(R.drawable.call_status_missed)
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent(intent) .setContentIntent(intent)
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE)
.setCategory(Notification.CATEGORY_MESSAGE) .setCategory(Notification.CATEGORY_MESSAGE)
.setVisibility(Notification.VISIBILITY_PRIVATE) .setVisibility(Notification.VISIBILITY_PRIVATE)
.setLights(ContextCompat.getColor(context, R.color.notification_color_led),
context.getResources().getInteger(R.integer.notification_ms_on),
context.getResources().getInteger(R.integer.notification_ms_off))
.setPriority(Notification.PRIORITY_HIGH) .setPriority(Notification.PRIORITY_HIGH)
.build(); .build();
@ -135,9 +153,13 @@ public class ApiTwentyOnePlus {
.setSmallIcon(R.drawable.linphone_logo) .setSmallIcon(R.drawable.linphone_logo)
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent(intent) .setContentIntent(intent)
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE)
.setCategory(Notification.CATEGORY_MESSAGE) .setCategory(Notification.CATEGORY_MESSAGE)
.setVisibility(Notification.VISIBILITY_PRIVATE) .setVisibility(Notification.VISIBILITY_PRIVATE)
.setLights(ContextCompat.getColor(context, R.color.notification_color_led),
context.getResources().getInteger(R.integer.notification_ms_on),
context.getResources().getInteger(R.integer.notification_ms_off))
.setPriority(Notification.PRIORITY_HIGH) .setPriority(Notification.PRIORITY_HIGH)
.build(); .build();

View file

@ -1,16 +1,19 @@
package org.linphone.compatibility; package org.linphone.compatibility;
import android.annotation.TargetApi;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.widget.TextView; import android.view.ViewTreeObserver;
import android.annotation.TargetApi;
import org.linphone.R; import org.linphone.R;
/* /*
ApiTwentyThreePlus.java ApiTwentyOnePlus.java
Copyright (C) 2017 Belledonne Communications, Grenoble, France Copyright (C) 2017 Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or This program is free software; you can redistribute it and/or
@ -27,11 +30,28 @@ You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
/** /**
* @author Erwan Croze * @author Erwan Croze
*/ */
@TargetApi(26) @TargetApi(26)
public class ApiTwentySixPlus { public class ApiTwentySixPlus {
public static void CreateChannel(Context context) {
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String id = context.getString(R.string.notification_channel_id);
CharSequence name = context.getString(R.string.content_title_notification);
String description = context.getString(R.string.content_title_notification);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(context.getColor(R.color.notification_color_led));
mChannel.enableLights(true);
notificationManager.createNotificationChannel(mChannel);
}
public static Notification createMessageNotification(Context context, public static Notification createMessageNotification(Context context,
int msgCount, String msgSender, String msg, Bitmap contactIcon, int msgCount, String msgSender, String msg, Bitmap contactIcon,
PendingIntent intent) { PendingIntent intent) {
@ -39,23 +59,111 @@ public class ApiTwentySixPlus {
if (msgCount == 1) { if (msgCount == 1) {
title = msgSender; title = msgSender;
} else { } else {
title = context.getString(R.string.unread_messages) title = context.getString(R.string.unread_messages).replace("%i", String.valueOf(msgCount));
.replace("%i", String.valueOf(msgCount));
} }
Notification notif = new Notification.Builder(context) NotificationManager notificationManager =
.setContentTitle(title) (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notif = null;
notif = new Notification.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle(title)
.setContentText(msg)
.setSmallIcon(R.drawable.topbar_chat_notification)
.setAutoCancel(true)
.setContentIntent(intent)
.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE)
.setLargeIcon(contactIcon)
.setCategory(Notification.CATEGORY_MESSAGE)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setPriority(Notification.PRIORITY_HIGH)
.setNumber(msgCount)
.build();
return notif;
}
public static Notification createInCallNotification(Context context,
String title, String msg, int iconID, Bitmap contactIcon,
String contactName, PendingIntent intent) {
Notification notif = new Notification.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle(contactName)
.setContentText(msg) .setContentText(msg)
.setSmallIcon(R.drawable.topbar_chat_notification) .setSmallIcon(iconID)
.setAutoCancel(false)
.setContentIntent(intent)
.setLargeIcon(contactIcon)
.setCategory(Notification.CATEGORY_CALL)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setPriority(Notification.PRIORITY_HIGH)
.build();
return notif;
}
public static Notification createNotification(Context context, String title, String message, int icon, int level, Bitmap largeIcon, PendingIntent intent, boolean isOngoingEvent,int priority) {
Notification notif;
if (largeIcon != null) {
notif = new Notification.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(icon, level)
.setLargeIcon(largeIcon)
.setContentIntent(intent)
.setCategory(Notification.CATEGORY_SERVICE)
.setVisibility(Notification.VISIBILITY_SECRET)
.setPriority(priority)
.build();
} else {
notif = new Notification.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle(title)
.setContentText(message)
.setSmallIcon(icon, level)
.setContentIntent(intent)
.setCategory(Notification.CATEGORY_SERVICE)
.setVisibility(Notification.VISIBILITY_SECRET)
.setPriority(priority)
.build();
}
return notif;
}
public static void removeGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener keyboardListener) {
viewTreeObserver.removeOnGlobalLayoutListener(keyboardListener);
}
public static Notification createMissedCallNotification(Context context, String title, String text, PendingIntent intent) {
Notification notif = new Notification.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.call_status_missed)
.setAutoCancel(true) .setAutoCancel(true)
.setContentIntent(intent) .setContentIntent(intent)
.setDefaults( .setDefaults(Notification.DEFAULT_SOUND
Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
| Notification.DEFAULT_SOUND .setCategory(Notification.CATEGORY_MESSAGE)
| Notification.DEFAULT_VIBRATE) .setVisibility(Notification.VISIBILITY_PRIVATE)
.setWhen(System.currentTimeMillis()) .setPriority(Notification.PRIORITY_HIGH)
.setLargeIcon(contactIcon) .build();
.setNumber(msgCount)
return notif;
}
public static Notification createSimpleNotification(Context context, String title, String text, PendingIntent intent) {
Notification notif = new Notification.Builder(context, context.getString(R.string.notification_channel_id))
.setContentTitle(title)
.setContentText(text)
.setSmallIcon(R.drawable.linphone_logo)
.setAutoCancel(true)
.setContentIntent(intent)
.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_VIBRATE)
.setCategory(Notification.CATEGORY_MESSAGE)
.setVisibility(Notification.VISIBILITY_PRIVATE)
.setPriority(Notification.PRIORITY_HIGH)
.build(); .build();
return notif; return notif;

View file

@ -36,57 +36,62 @@ import android.widget.TextView;
* @author Sylvain Berfini * @author Sylvain Berfini
*/ */
public class Compatibility { public class Compatibility {
public static void CreateChannel(Context context) {
if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
ApiTwentySixPlus.CreateChannel(context);
}
}
public static Notification createSimpleNotification(Context context, String title, String text, PendingIntent intent) { public static Notification createSimpleNotification(Context context, String title, String text, PendingIntent intent) {
Notification notif = null; if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) { return ApiTwentySixPlus.createSimpleNotification(context, title, text, intent);
} else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) {
return ApiTwentyOnePlus.createSimpleNotification(context, title, text, intent); return ApiTwentyOnePlus.createSimpleNotification(context, title, text, intent);
} else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) { } else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) {
notif = ApiSixteenPlus.createSimpleNotification(context, title, text, intent); return ApiSixteenPlus.createSimpleNotification(context, title, text, intent);
} else { } else {
notif = ApiElevenPlus.createSimpleNotification(context, title, text, intent); return ApiElevenPlus.createSimpleNotification(context, title, text, intent);
} }
return notif;
} }
public static Notification createMissedCallNotification(Context context, String title, String text, PendingIntent intent) { public static Notification createMissedCallNotification(Context context, String title, String text, PendingIntent intent) {
Notification notif = null; if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) { return ApiTwentySixPlus.createMissedCallNotification(context, title, text, intent);
} else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) {
return ApiTwentyOnePlus.createMissedCallNotification(context, title, text, intent); return ApiTwentyOnePlus.createMissedCallNotification(context, title, text, intent);
} else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) { } else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) {
notif = ApiSixteenPlus.createMissedCallNotification(context, title, text, intent); return ApiSixteenPlus.createMissedCallNotification(context, title, text, intent);
} else { } else {
notif = ApiElevenPlus.createMissedCallNotification(context, title, text, intent); return ApiElevenPlus.createMissedCallNotification(context, title, text, intent);
} }
return notif;
} }
public static Notification createMessageNotification(Context context, int msgCount,String to, String msgSender, String msg, Bitmap contactIcon, PendingIntent intent) { public static Notification createMessageNotification(Context context, int msgCount,String to, String msgSender, String msg, Bitmap contactIcon, PendingIntent intent) {
Notification notif = null;
if (Version.sdkAboveOrEqual(Version.API26_O_80)) { if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
return ApiTwentySixPlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent);
} else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) { } else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) {
return ApiTwentyOnePlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent); return ApiTwentyOnePlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent);
} else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) { } else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) {
notif = ApiSixteenPlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent); return ApiSixteenPlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent);
} else { } else {
notif = ApiElevenPlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent); return ApiElevenPlus.createMessageNotification(context, msgCount, msgSender, msg, contactIcon, intent);
} }
return notif;
} }
public static Notification createInCallNotification(Context context, String title, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) { public static Notification createInCallNotification(Context context, String title, String msg, int iconID, Bitmap contactIcon, String contactName, PendingIntent intent) {
Notification notif = null; if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) { return ApiTwentySixPlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent);
} else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) {
return ApiTwentyOnePlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent); return ApiTwentyOnePlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent);
} else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) { } else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) {
notif = ApiSixteenPlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent); return ApiSixteenPlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent);
} else { } else {
notif = ApiElevenPlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent); return ApiElevenPlus.createInCallNotification(context, title, msg, iconID, contactIcon, contactName, intent);
} }
return notif;
} }
public static Notification createNotification(Context context, String title, String message, int icon, int iconLevel, Bitmap largeIcon, PendingIntent intent, boolean isOngoingEvent,int priority) { public static Notification createNotification(Context context, String title, String message, int icon, int iconLevel, Bitmap largeIcon, PendingIntent intent, boolean isOngoingEvent,int priority) {
if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) { if (Version.sdkAboveOrEqual(Version.API26_O_80)) {
return ApiTwentySixPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent, isOngoingEvent,priority);
} else if (Version.sdkAboveOrEqual(Version.API21_LOLLIPOP_50)) {
return ApiTwentyOnePlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent, isOngoingEvent,priority); return ApiTwentyOnePlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent, isOngoingEvent,priority);
} else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) { } else if (Version.sdkAboveOrEqual(Version.API16_JELLY_BEAN_41)) {
return ApiSixteenPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent, isOngoingEvent,priority); return ApiSixteenPlus.createNotification(context, title, message, icon, iconLevel, largeIcon, intent, isOngoingEvent,priority);