mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-31 22:25:08 +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:
committed by
Nicolas Fella
parent
cf247b4d18
commit
cbbec32beb
@@ -39,9 +39,11 @@ import android.os.Parcelable;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.StatusBarNotification;
|
||||
import android.text.SpannableString;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
@@ -65,6 +67,7 @@ import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
||||
@@ -253,33 +256,38 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
|
||||
Pair<String, String> conversation = extractConversation(notification);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
|
||||
if (conversation.first != null) {
|
||||
np.set("title", conversation.first);
|
||||
} else {
|
||||
np.set("title", notification.extras.getString(Notification.EXTRA_TITLE));
|
||||
np.set("title", extractStringFromExtra(getExtras(notification), NotificationCompat.EXTRA_TITLE));
|
||||
}
|
||||
|
||||
np.set("text", extractText(notification, conversation));
|
||||
}
|
||||
}
|
||||
|
||||
device.sendPacket(np);
|
||||
}
|
||||
|
||||
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
|
||||
private String extractText(Notification notification, Pair<String, String> conversation) {
|
||||
|
||||
if (conversation.second != null) {
|
||||
return conversation.second;
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && notification.extras.containsKey(Notification.EXTRA_BIG_TEXT)) {
|
||||
return extractStringFromExtra(notification.extras, Notification.EXTRA_BIG_TEXT);
|
||||
Bundle extras = getExtras(notification);
|
||||
|
||||
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) {
|
||||
@@ -359,13 +367,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
|
||||
String title = notification.extras.getString(Notification.EXTRA_CONVERSATION_TITLE);
|
||||
|
||||
boolean isGroupConversation = false;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
boolean isGroupConversation = notification.extras.getBoolean(NotificationCompat.EXTRA_IS_GROUP_CONVERSATION);
|
||||
|
||||
StringBuilder messagesBuilder = new StringBuilder();
|
||||
|
||||
@@ -377,7 +379,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
messagesBuilder.append(": ");
|
||||
}
|
||||
|
||||
messagesBuilder.append(m.getString("text"));
|
||||
messagesBuilder.append(extractStringFromExtra(m, "text"));
|
||||
messagesBuilder.append("\n");
|
||||
}
|
||||
|
||||
@@ -491,13 +493,12 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
private String getTickerText(Notification notification) {
|
||||
String ticker = "";
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
try {
|
||||
Bundle extras = notification.extras;
|
||||
String extraTitle = extractStringFromExtra(extras, Notification.EXTRA_TITLE);
|
||||
String extraText = extractStringFromExtra(extras, Notification.EXTRA_TEXT);
|
||||
Bundle extras = getExtras(notification);
|
||||
String extraTitle = extractStringFromExtra(extras, NotificationCompat.EXTRA_TITLE);
|
||||
String extraText = extractStringFromExtra(extras, NotificationCompat.EXTRA_TEXT);
|
||||
|
||||
if (extraTitle != null && extraText != null && !extraText.isEmpty()) {
|
||||
if (extraTitle != null && !TextUtils.isEmpty(extraText)) {
|
||||
ticker = extraTitle + ": " + extraText;
|
||||
} else if (extraTitle != null) {
|
||||
ticker = extraTitle;
|
||||
@@ -507,7 +508,6 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "problem parsing notification extras for " + notification.tickerText, e);
|
||||
}
|
||||
}
|
||||
|
||||
if (ticker.isEmpty()) {
|
||||
ticker = (notification.tickerText != null) ? notification.tickerText.toString() : "";
|
||||
@@ -594,7 +594,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
|
||||
//For compat with API<21, because lollipop changed the way to cancel notifications
|
||||
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);
|
||||
} else {
|
||||
int first = compatKey.indexOf(':');
|
||||
@@ -623,7 +623,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
String tag = statusBarNotification.getTag();
|
||||
if (tag != null && tag.startsWith("kdeconnectId:"))
|
||||
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();
|
||||
} else {
|
||||
String packageName = statusBarNotification.getPackageName();
|
||||
|
Reference in New Issue
Block a user