Browse Source

Use Optional for DeckIntegration and better name for interface

https://github.com/stefan-niedermann/nextcloud-deck/issues/208
Stefan Niedermann 5 năm trước cách đây
mục cha
commit
9a00819286

+ 0 - 25
src/main/java/com/nextcloud/client/integration/AppCannotHandleNotificationException.java

@@ -1,25 +0,0 @@
-/*
- * Nextcloud application
- *
- * @author Stefan Niedermann
- * Copyright (C) 2020 Stefan Niedermann <info@niedermann.it>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.nextcloud.client.integration;
-
-public class AppCannotHandleNotificationException extends Exception {
-    private static final long serialVersionUID = 1L;
-}

+ 0 - 25
src/main/java/com/nextcloud/client/integration/AppNotInstalledException.java

@@ -1,25 +0,0 @@
-/*
- * Nextcloud application
- *
- * @author Stefan Niedermann
- * Copyright (C) 2020 Stefan Niedermann <info@niedermann.it>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.nextcloud.client.integration;
-
-public class AppNotInstalledException extends Exception {
-    private static final long serialVersionUID = 1L;
-}

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

@@ -1,15 +0,0 @@
-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, AppCannotHandleNotificationException;
-}

+ 16 - 0
src/main/java/com/nextcloud/client/integration/deck/DeckActionOverride.java

@@ -0,0 +1,16 @@
+package com.nextcloud.client.integration.deck;
+
+import android.app.PendingIntent;
+import android.content.Intent;
+
+import com.nextcloud.client.account.User;
+import com.nextcloud.java.util.Optional;
+import com.owncloud.android.lib.resources.notifications.models.Notification;
+
+import androidx.annotation.NonNull;
+
+public interface DeckActionOverride {
+
+    @NonNull
+    Optional<PendingIntent> handleNotification(@NonNull final Notification notification, @NonNull final User user);
+}

+ 13 - 14
src/main/java/com/nextcloud/client/integration/deck/DeckNotificationHandler.java → src/main/java/com/nextcloud/client/integration/deck/DeckActionOverrideImpl.java

@@ -20,46 +20,45 @@
 
 package com.nextcloud.client.integration.deck;
 
+import android.app.PendingIntent;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
 import android.util.Log;
 
 import com.nextcloud.client.account.User;
-import com.nextcloud.client.integration.AppCannotHandleNotificationException;
-import com.nextcloud.client.integration.AppNotInstalledException;
-import com.nextcloud.client.integration.NotificationHandler;
+import com.nextcloud.java.util.Optional;
 import com.owncloud.android.lib.resources.notifications.models.Notification;
 
 import androidx.annotation.NonNull;
 
-public class DeckNotificationHandler implements NotificationHandler {
+public class DeckActionOverrideImpl implements DeckActionOverride {
 
-    private static final String TAG = DeckNotificationHandler.class.getSimpleName();
+    private static final String TAG = DeckActionOverrideImpl.class.getSimpleName();
 
     private static final String APP_NAME = "deck";
     private static final String DECK_APP_ID_BASE = "it.niedermann.nextcloud.deck";
     private static final String[] DECK_APP_ID_FLAVOR_SUFFIXES = new String[]{"", ".play", ".dev"};
     private static final String DECK_ACTIVITY_TO_START = "it.niedermann.nextcloud.deck.ui.PushNotificationActivity";
 
-    private final PackageManager packageManager;
+    private final Context context;
 
-    public DeckNotificationHandler(@NonNull Context context) {
-        this.packageManager = context.getPackageManager();
+    public DeckActionOverrideImpl(@NonNull Context context) {
+        this.context = context;
     }
 
     @NonNull
     @Override
-    public Intent handleNotification(@NonNull Notification notification, @NonNull User user) throws AppNotInstalledException, AppCannotHandleNotificationException {
+    public Optional<PendingIntent> handleNotification(@NonNull Notification notification, @NonNull User user) {
         if (!APP_NAME.equalsIgnoreCase(notification.app)) {
-            throw new AppCannotHandleNotificationException();
+            return Optional.empty();
         }
         final Intent intent = new Intent();
         for (String flavor : DECK_APP_ID_FLAVOR_SUFFIXES) {
             intent.setClassName(DECK_APP_ID_BASE + flavor, DECK_ACTIVITY_TO_START);
-            if (packageManager.resolveActivity(intent, 0) != null) {
+            if (context.getPackageManager().resolveActivity(intent, 0) != null) {
                 Log.i(TAG, "Found deck app flavor \"" + flavor + "\"");
-                return intent
+                return Optional.of(PendingIntent.getActivity(context, 0, intent
                     .putExtra("account", user.getAccountName())
                     .putExtra("link", notification.getLink())
                     .putExtra("objectId", notification.getObjectId())
@@ -69,9 +68,9 @@ public class DeckNotificationHandler implements NotificationHandler {
                     .putExtra("messageRich", notification.getMessageRich())
                     .putExtra("user", notification.getUser())
                     .putExtra("nid", notification.getNotificationId())
-                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), PendingIntent.FLAG_ONE_SHOT));
             }
         }
-        throw new AppNotInstalledException();
+        return Optional.empty();
     }
 }