|
| 1 | +/* |
| 2 | + * Copyright (C) 2013-2019 microG Project Team |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.microg.gms.wearable; |
| 18 | + |
| 19 | +import android.app.Notification; |
| 20 | +import android.content.ComponentName; |
| 21 | +import android.content.Context; |
| 22 | +import android.content.Intent; |
| 23 | +import android.content.ServiceConnection; |
| 24 | +import android.os.IBinder; |
| 25 | +import android.service.notification.NotificationListenerService; |
| 26 | +import android.service.notification.StatusBarNotification; |
| 27 | +import android.util.Log; |
| 28 | + |
| 29 | +import com.google.android.gms.wearable.MessageApi; |
| 30 | +import com.google.android.gms.wearable.Node; |
| 31 | +import com.google.android.gms.wearable.NodeApi; |
| 32 | +import com.google.android.gms.wearable.Wearable; |
| 33 | + |
| 34 | +import java.io.ByteArrayOutputStream; |
| 35 | +import java.io.DataOutputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.nio.charset.StandardCharsets; |
| 38 | +import java.util.List; |
| 39 | + |
| 40 | +/** |
| 41 | + * NotificationListenerService that syncs notifications to connected WearOS devices. |
| 42 | + * |
| 43 | + * This service listens for system notifications and forwards them to paired watches |
| 44 | + * using the WearOS MessageApi over the Bluetooth transport layer. |
| 45 | + */ |
| 46 | +public class WearableNotificationListener extends NotificationListenerService { |
| 47 | + |
| 48 | + private static final String TAG = "GmsWearNotif"; |
| 49 | + private static final String NOTIFICATION_PATH = "/wearable/notification"; |
| 50 | + private static final String NOTIFICATION_DISMISSED_PATH = "/wearable/notification/dismissed"; |
| 51 | + |
| 52 | + @Override |
| 53 | + public void onNotificationPosted(StatusBarNotification sbn) { |
| 54 | + try { |
| 55 | + // Get notification details |
| 56 | + Notification notification = sbn.getNotification(); |
| 57 | + String packageName = sbn.getPackageName(); |
| 58 | + int notificationId = sbn.getId(); |
| 59 | + |
| 60 | + // Extract notification data |
| 61 | + CharSequence title = notification.extras.getCharSequence(Notification.EXTRA_TITLE); |
| 62 | + CharSequence text = notification.extras.getCharSequence(Notification.EXTRA_TEXT); |
| 63 | + |
| 64 | + if (title == null && text == null) { |
| 65 | + return; // Skip empty notifications |
| 66 | + } |
| 67 | + |
| 68 | + // Serialize notification data |
| 69 | + byte[] notificationData = serializeNotification( |
| 70 | + packageName, |
| 71 | + notificationId, |
| 72 | + title != null ? title.toString() : "", |
| 73 | + text != null ? text.toString() : "" |
| 74 | + ); |
| 75 | + |
| 76 | + // Send to all connected watches |
| 77 | + sendToWearables(NOTIFICATION_PATH, notificationData); |
| 78 | + |
| 79 | + Log.d(TAG, "Notification synced: " + title + " from " + packageName); |
| 80 | + |
| 81 | + } catch (Exception e) { |
| 82 | + Log.e(TAG, "Error posting notification to wearable", e); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void onNotificationRemoved(StatusBarNotification sbn) { |
| 88 | + try { |
| 89 | + String packageName = sbn.getPackageName(); |
| 90 | + int notificationId = sbn.getId(); |
| 91 | + |
| 92 | + // Notify watch that notification was dismissed |
| 93 | + byte[] dismissData = serializeDismissal(packageName, notificationId); |
| 94 | + sendToWearables(NOTIFICATION_DISMISSED_PATH, dismissData); |
| 95 | + |
| 96 | + Log.d(TAG, "Notification dismissed: " + notificationId + " from " + packageName); |
| 97 | + |
| 98 | + } catch (Exception e) { |
| 99 | + Log.e(TAG, "Error removing notification from wearable", e); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Serialize notification data into a compact byte array for transmission. |
| 105 | + */ |
| 106 | + private byte[] serializeNotification(String packageName, int id, String title, String text) throws IOException { |
| 107 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 108 | + DataOutputStream dos = new DataOutputStream(baos); |
| 109 | + |
| 110 | + // Write notification data |
| 111 | + dos.writeUTF(packageName); |
| 112 | + dos.writeInt(id); |
| 113 | + dos.writeUTF(title); |
| 114 | + dos.writeUTF(text); |
| 115 | + dos.writeLong(System.currentTimeMillis()); |
| 116 | + |
| 117 | + dos.flush(); |
| 118 | + return baos.toByteArray(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Serialize dismissal notification. |
| 123 | + */ |
| 124 | + private byte[] serializeDismissal(String packageName, int id) throws IOException { |
| 125 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 126 | + DataOutputStream dos = new DataOutputStream(baos); |
| 127 | + |
| 128 | + dos.writeUTF(packageName); |
| 129 | + dos.writeInt(id); |
| 130 | + |
| 131 | + dos.flush(); |
| 132 | + return baos.toByteArray(); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Send message to all connected WearOS devices. |
| 137 | + */ |
| 138 | + private void sendToWearables(String path, byte[] data) { |
| 139 | + // Get connected nodes |
| 140 | + Wearable.NodeApi.getConnectedNodes(null).setResultCallback(result -> { |
| 141 | + List<Node> nodes = result.getNodes(); |
| 142 | + |
| 143 | + for (Node node : nodes) { |
| 144 | + // Send message to each connected watch |
| 145 | + Wearable.MessageApi.sendMessage(null, node.getId(), path, data) |
| 146 | + .setResultCallback(sendResult -> { |
| 147 | + if (sendResult.getStatus().isSuccess()) { |
| 148 | + Log.d(TAG, "Message sent to " + node.getDisplayName()); |
| 149 | + } else { |
| 150 | + Log.w(TAG, "Failed to send to " + node.getDisplayName() + ": " + sendResult.getStatus()); |
| 151 | + } |
| 152 | + }); |
| 153 | + } |
| 154 | + }); |
| 155 | + } |
| 156 | +} |
0 commit comments