mirror of
https://github.com/KDE/kdeconnect-android
synced 2025-09-01 14:45:08 +00:00
Send notification actions
Summary: Store PendingIntents from notification actions. Send list of notifications to desktop and trigger Intent when matching packet arrives. CCBUG: 366475 Test Plan: Create test notification, trigger package is received correctly. Whether intent.send() is actually successful is NOT yet tested. Reviewers: #kde_connect, sredman Reviewed By: #kde_connect, sredman Subscribers: sredman, apol, MatMaul, kdeconnect, mtijink, #kde_connect Tags: #kde_connect Differential Revision: https://phabricator.kde.org/D12294
This commit is contained in:
@@ -16,7 +16,7 @@
|
|||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.kde.kdeconnect.Plugins.NotificationsPlugin;
|
package org.kde.kdeconnect.Plugins.NotificationsPlugin;
|
||||||
|
|
||||||
@@ -40,6 +40,7 @@ import android.service.notification.StatusBarNotification;
|
|||||||
import android.text.SpannableString;
|
import android.text.SpannableString;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.kde.kdeconnect.Helpers.AppsHelper;
|
import org.kde.kdeconnect.Helpers.AppsHelper;
|
||||||
import org.kde.kdeconnect.NetworkPacket;
|
import org.kde.kdeconnect.NetworkPacket;
|
||||||
import org.kde.kdeconnect.Plugins.Plugin;
|
import org.kde.kdeconnect.Plugins.Plugin;
|
||||||
@@ -55,6 +56,8 @@ import java.security.NoSuchAlgorithmException;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -68,11 +71,15 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
private final static String PACKET_TYPE_NOTIFICATION = "kdeconnect.notification";
|
private final static String PACKET_TYPE_NOTIFICATION = "kdeconnect.notification";
|
||||||
private final static String PACKET_TYPE_NOTIFICATION_REQUEST = "kdeconnect.notification.request";
|
private final static String PACKET_TYPE_NOTIFICATION_REQUEST = "kdeconnect.notification.request";
|
||||||
private final static String PACKET_TYPE_NOTIFICATION_REPLY = "kdeconnect.notification.reply";
|
private final static String PACKET_TYPE_NOTIFICATION_REPLY = "kdeconnect.notification.reply";
|
||||||
|
private final static String PACKET_TYPE_NOTIFICATION_ACTION = "kdeconnect.notification.action";
|
||||||
|
|
||||||
|
private final static String TAG = "NotificationsPlugin";
|
||||||
|
|
||||||
private AppDatabase appDatabase;
|
private AppDatabase appDatabase;
|
||||||
|
|
||||||
private Set<String> currentNotifications;
|
private Set<String> currentNotifications;
|
||||||
private Map<String, RepliableNotification> pendingIntents;
|
private Map<String, RepliableNotification> pendingIntents;
|
||||||
|
private Map<String, List<Notification.Action>> actions;
|
||||||
private boolean serviceReady;
|
private boolean serviceReady;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -117,6 +124,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
|
|
||||||
pendingIntents = new HashMap<>();
|
pendingIntents = new HashMap<>();
|
||||||
currentNotifications = new HashSet<>();
|
currentNotifications = new HashSet<>();
|
||||||
|
actions = new HashMap<>();
|
||||||
|
|
||||||
appDatabase = new AppDatabase(context, true);
|
appDatabase = new AppDatabase(context, true);
|
||||||
|
|
||||||
@@ -153,6 +161,9 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
String id = getNotificationKeyCompat(statusBarNotification);
|
String id = getNotificationKeyCompat(statusBarNotification);
|
||||||
|
|
||||||
|
actions.remove(id);
|
||||||
|
|
||||||
NetworkPacket np = new NetworkPacket(PACKET_TYPE_NOTIFICATION);
|
NetworkPacket np = new NetworkPacket(PACKET_TYPE_NOTIFICATION);
|
||||||
np.set("id", id);
|
np.set("id", id);
|
||||||
np.set("isCancel", true);
|
np.set("isCancel", true);
|
||||||
@@ -252,6 +263,20 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
currentNotifications.add(key);
|
currentNotifications.add(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
|
if (notification.actions != null && notification.actions.length > 0) {
|
||||||
|
actions.put(key, new LinkedList<>());
|
||||||
|
JSONArray jsonArray = new JSONArray();
|
||||||
|
for (Notification.Action action : notification.actions) {
|
||||||
|
if (null == action.title)
|
||||||
|
break;
|
||||||
|
jsonArray.put(action.title.toString());
|
||||||
|
actions.get(key).add(action);
|
||||||
|
}
|
||||||
|
np.set("actions", jsonArray);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
np.set("id", key);
|
np.set("id", key);
|
||||||
np.set("isClearable", statusBarNotification.isClearable());
|
np.set("isClearable", statusBarNotification.isClearable());
|
||||||
np.set("appName", appName == null ? packageName : appName);
|
np.set("appName", appName == null ? packageName : appName);
|
||||||
@@ -476,7 +501,27 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
@Override
|
@Override
|
||||||
public boolean onPacketReceived(final NetworkPacket np) {
|
public boolean onPacketReceived(final NetworkPacket np) {
|
||||||
|
|
||||||
if (np.getBoolean("request")) {
|
if (np.getType().equals(PACKET_TYPE_NOTIFICATION_ACTION) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||||
|
|
||||||
|
String key = np.getString("key");
|
||||||
|
String title = np.getString("action");
|
||||||
|
PendingIntent intent = null;
|
||||||
|
|
||||||
|
for (Notification.Action a : actions.get(key)) {
|
||||||
|
if (a.title.equals(title)) {
|
||||||
|
intent = a.actionIntent;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (intent != null) {
|
||||||
|
try {
|
||||||
|
intent.send();
|
||||||
|
} catch (PendingIntent.CanceledException e) {
|
||||||
|
Log.e(TAG, "Triggering action failed", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (np.getBoolean("request")) {
|
||||||
|
|
||||||
if (serviceReady) {
|
if (serviceReady) {
|
||||||
NotificationReceiver.RunCommand(context, this::sendCurrentNotifications);
|
NotificationReceiver.RunCommand(context, this::sendCurrentNotifications);
|
||||||
@@ -512,7 +557,7 @@ public class NotificationsPlugin extends Plugin implements NotificationReceiver.
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] getSupportedPacketTypes() {
|
public String[] getSupportedPacketTypes() {
|
||||||
return new String[]{PACKET_TYPE_NOTIFICATION_REQUEST, PACKET_TYPE_NOTIFICATION_REPLY};
|
return new String[]{PACKET_TYPE_NOTIFICATION_REQUEST, PACKET_TYPE_NOTIFICATION_REPLY, PACKET_TYPE_NOTIFICATION_ACTION};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Reference in New Issue
Block a user