Эх сурвалжийг харах

Move Deck integration logic to own package

https://github.com/stefan-niedermann/nextcloud-deck/issues/208
Stefan Niedermann 5 жил өмнө
parent
commit
b0ab109e5d

+ 21 - 0
src/main/java/com/nextcloud/client/integration/NotificationHandler.java

@@ -0,0 +1,21 @@
+package com.nextcloud.client.integration;
+
+import android.content.Intent;
+
+import com.nextcloud.client.account.User;
+import com.owncloud.android.lib.resources.notifications.models.Notification;
+
+import androidx.annotation.NonNull;
+
+public interface NotificationHandler {
+
+    @NonNull
+    Intent handleNotification(@NonNull final Notification notification,
+                              @NonNull final User user) throws AppNotInstalledException, AppCannotHandelNotificationException;
+
+    class AppNotInstalledException extends Exception {
+    }
+
+    class AppCannotHandelNotificationException extends Exception {
+    }
+}

+ 55 - 0
src/main/java/com/nextcloud/client/integration/deck/DeckNotificationHandler.java

@@ -0,0 +1,55 @@
+package com.nextcloud.client.integration.deck;
+
+import android.content.Context;
+import android.content.Intent;
+import android.util.Log;
+
+import com.nextcloud.client.account.User;
+import com.nextcloud.client.integration.NotificationHandler;
+import com.owncloud.android.lib.resources.notifications.models.Notification;
+
+import androidx.annotation.NonNull;
+
+public class DeckNotificationHandler implements NotificationHandler {
+
+    private static final String TAG = DeckNotificationHandler.class.getSimpleName();
+
+    private static final String APP_NAME = "deck";
+
+    private final Context context;
+
+    public DeckNotificationHandler(@NonNull Context context) {
+        this.context = context;
+    }
+
+    @NonNull
+    @Override
+    public Intent handleNotification(@NonNull Notification notification, @NonNull User user) throws AppNotInstalledException, AppCannotHandelNotificationException {
+        if (!APP_NAME.equalsIgnoreCase(notification.app)) {
+            throw new AppCannotHandelNotificationException();
+        }
+        final String baseDeckApplicationId = "it.niedermann.nextcloud.deck";
+        final String activityToStart = "it.niedermann.nextcloud.deck.ui.PushNotificationActivity";
+        final String[] flavors = new String[]{"", ".play", ".dev"};
+        for (String flavor : flavors) {
+            final Intent intent = new Intent().setClassName(baseDeckApplicationId + flavor, activityToStart);
+            if (context.getPackageManager().resolveActivity(
+                intent, 0) != null) {
+                Log.i(TAG, "Found deck app flavor \"" + flavor + "\"");
+                return intent
+                    .putExtra("account", user.getAccountName())
+                    .putExtra("link", notification.getLink())
+                    .putExtra("objectId", notification.getObjectId())
+                    .putExtra("subject", notification.getSubject())
+                    .putExtra("subjectRich", notification.getSubjectRich())
+                    .putExtra("message", notification.getMessage())
+                    .putExtra("messageRich", notification.getMessageRich())
+                    .putExtra("user", notification.getUser())
+                    .putExtra("nid", notification.getNotificationId())
+                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+            }
+        }
+        Log.v(TAG, "Couldn't find any installed deck app.");
+        throw new AppNotInstalledException();
+    }
+}