2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-08-31 06:05:12 +00:00

Fix text extraction from notifications

Summary:
Fixes an error of extracting SpannableString as String (which resulted in notification text being null).
Fixes group conversation text extraction on API <28.
Also includes some minor refactoring.

Reviewers: nicolasfella

Reviewed By: nicolasfella

Subscribers: kdeconnect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D22140
This commit is contained in:
Dmitriy Bogdanov
2019-07-05 11:09:04 +02:00
committed by Nicolas Fella
parent cf247b4d18
commit cbbec32beb

View File

@@ -39,9 +39,11 @@ import android.os.Parcelable;
import android.provider.Settings; import android.provider.Settings;
import android.service.notification.StatusBarNotification; import android.service.notification.StatusBarNotification;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
@@ -65,6 +67,7 @@ import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.Set; import java.util.Set;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@@ -253,33 +256,38 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
Pair<String, String> conversation = extractConversation(notification); Pair<String, String> conversation = extractConversation(notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { if (conversation.first != null) {
np.set("title", conversation.first);
if (conversation.first != null) { } else {
np.set("title", conversation.first); np.set("title", extractStringFromExtra(getExtras(notification), NotificationCompat.EXTRA_TITLE));
} else {
np.set("title", notification.extras.getString(Notification.EXTRA_TITLE));
}
np.set("text", extractText(notification, conversation));
} }
np.set("text", extractText(notification, conversation));
} }
device.sendPacket(np); device.sendPacket(np);
} }
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
private String extractText(Notification notification, Pair<String, String> conversation) { private String extractText(Notification notification, Pair<String, String> conversation) {
if (conversation.second != null) { if (conversation.second != null) {
return conversation.second; return conversation.second;
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && notification.extras.containsKey(Notification.EXTRA_BIG_TEXT)) { Bundle extras = getExtras(notification);
return extractStringFromExtra(notification.extras, Notification.EXTRA_BIG_TEXT);
if (extras.containsKey(NotificationCompat.EXTRA_BIG_TEXT)) {
return extractStringFromExtra(extras, NotificationCompat.EXTRA_BIG_TEXT);
} }
return notification.extras.getString(Notification.EXTRA_TEXT); return extractStringFromExtra(extras, NotificationCompat.EXTRA_TEXT);
}
@NonNull
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
private static Bundle getExtras(Notification notification) {
// NotificationCompat.getExtras() is expected to return non-null values for JELLY_BEAN+
return Objects.requireNonNull(NotificationCompat.getExtras(notification));
} }
private void attachIcon(NetworkPacket np, Bitmap appIcon) { private void attachIcon(NetworkPacket np, Bitmap appIcon) {
@@ -359,13 +367,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
String title = notification.extras.getString(Notification.EXTRA_CONVERSATION_TITLE); String title = notification.extras.getString(Notification.EXTRA_CONVERSATION_TITLE);
boolean isGroupConversation = false; boolean isGroupConversation = notification.extras.getBoolean(NotificationCompat.EXTRA_IS_GROUP_CONVERSATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
if (notification.extras.containsKey(Notification.EXTRA_IS_GROUP_CONVERSATION)) {
isGroupConversation = notification.extras.getBoolean(Notification.EXTRA_IS_GROUP_CONVERSATION);
}
}
StringBuilder messagesBuilder = new StringBuilder(); StringBuilder messagesBuilder = new StringBuilder();
@@ -377,7 +379,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
messagesBuilder.append(": "); messagesBuilder.append(": ");
} }
messagesBuilder.append(m.getString("text")); messagesBuilder.append(extractStringFromExtra(m, "text"));
messagesBuilder.append("\n"); messagesBuilder.append("\n");
} }
@@ -491,22 +493,20 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
private String getTickerText(Notification notification) { private String getTickerText(Notification notification) {
String ticker = ""; String ticker = "";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { try {
try { Bundle extras = getExtras(notification);
Bundle extras = notification.extras; String extraTitle = extractStringFromExtra(extras, NotificationCompat.EXTRA_TITLE);
String extraTitle = extractStringFromExtra(extras, Notification.EXTRA_TITLE); String extraText = extractStringFromExtra(extras, NotificationCompat.EXTRA_TEXT);
String extraText = extractStringFromExtra(extras, Notification.EXTRA_TEXT);
if (extraTitle != null && extraText != null && !extraText.isEmpty()) { if (extraTitle != null && !TextUtils.isEmpty(extraText)) {
ticker = extraTitle + ": " + extraText; ticker = extraTitle + ": " + extraText;
} else if (extraTitle != null) { } else if (extraTitle != null) {
ticker = extraTitle; ticker = extraTitle;
} else if (extraText != null) { } else if (extraText != null) {
ticker = extraText; ticker = extraText;
}
} catch (Exception e) {
Log.e(TAG, "problem parsing notification extras for " + notification.tickerText, e);
} }
} catch (Exception e) {
Log.e(TAG, "problem parsing notification extras for " + notification.tickerText, e);
} }
if (ticker.isEmpty()) { if (ticker.isEmpty()) {
@@ -594,7 +594,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
//For compat with API<21, because lollipop changed the way to cancel notifications //For compat with API<21, because lollipop changed the way to cancel notifications
private static void cancelNotificationCompat(NotificationReceiver service, String compatKey) { private static void cancelNotificationCompat(NotificationReceiver service, String compatKey) {
if (Build.VERSION.SDK_INT >= 21) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
service.cancelNotification(compatKey); service.cancelNotification(compatKey);
} else { } else {
int first = compatKey.indexOf(':'); int first = compatKey.indexOf(':');
@@ -623,7 +623,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
String tag = statusBarNotification.getTag(); String tag = statusBarNotification.getTag();
if (tag != null && tag.startsWith("kdeconnectId:")) if (tag != null && tag.startsWith("kdeconnectId:"))
result = Integer.toString(statusBarNotification.getId()); result = Integer.toString(statusBarNotification.getId());
else if (Build.VERSION.SDK_INT >= 21) { else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
result = statusBarNotification.getKey(); result = statusBarNotification.getKey();
} else { } else {
String packageName = statusBarNotification.getPackageName(); String packageName = statusBarNotification.getPackageName();