mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-08-31 06:05:12 +00:00
Extract conversations from conversation notification style
This commit is contained in:
@@ -35,10 +35,15 @@ import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.Icon;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcelable;
|
||||
import android.provider.Settings;
|
||||
import android.service.notification.StatusBarNotification;
|
||||
import android.text.SpannableString;
|
||||
import android.util.Log;
|
||||
import android.util.Pair;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.kde.kdeconnect.Helpers.AppsHelper;
|
||||
@@ -61,9 +66,6 @@ import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import androidx.annotation.RequiresApi;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
|
||||
@PluginFactory.LoadablePlugin
|
||||
public class NotificationsPlugin extends Plugin implements NotificationReceiver.NotificationListener {
|
||||
@@ -292,15 +294,66 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
||||
}
|
||||
np.set("ticker", getTickerText(notification));
|
||||
|
||||
Pair<String, String> conversation = extractConversation(notification, np);
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
np.set("title", notification.extras.getString(Notification.EXTRA_TITLE));
|
||||
np.set("text", notification.extras.getString(Notification.EXTRA_TEXT));
|
||||
|
||||
if (conversation.first != null) {
|
||||
np.set("title", conversation.first);
|
||||
} else {
|
||||
np.set("title", notification.extras.getString(Notification.EXTRA_TITLE));
|
||||
}
|
||||
|
||||
if (conversation.second != null) {
|
||||
np.set("text", conversation.second);
|
||||
} else {
|
||||
np.set("text", notification.extras.getString(Notification.EXTRA_TEXT));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
device.sendPacket(np);
|
||||
}
|
||||
|
||||
private Pair<String, String> extractConversation(Notification notification, NetworkPacket np) {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N)
|
||||
return new Pair<>(null, null);
|
||||
|
||||
if (!notification.extras.containsKey(Notification.EXTRA_MESSAGES))
|
||||
return new Pair<>(null, null);
|
||||
|
||||
Parcelable[] ms = notification.extras.getParcelableArray(Notification.EXTRA_MESSAGES);
|
||||
|
||||
if (ms == null)
|
||||
return new Pair<>(null, null);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
StringBuilder messagesBuilder = new StringBuilder();
|
||||
|
||||
for (Parcelable p : ms) {
|
||||
Bundle m = (Bundle) p;
|
||||
|
||||
if (isGroupConversation) {
|
||||
messagesBuilder.append(m.get("sender"));
|
||||
messagesBuilder.append(": ");
|
||||
}
|
||||
|
||||
messagesBuilder.append(m.getString("text"));
|
||||
messagesBuilder.append("\n");
|
||||
}
|
||||
|
||||
return new Pair<>(title, messagesBuilder.toString());
|
||||
}
|
||||
|
||||
private Bitmap drawableToBitmap(Drawable drawable) {
|
||||
if (drawable == null) return null;
|
||||
|
||||
|
Reference in New Issue
Block a user