Ver código fonte

Convert to kt

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 ano atrás
pai
commit
c57a5e0b10

+ 13 - 17
app/src/main/java/com/nextcloud/client/integrations/deck/DeckApi.kt

@@ -17,23 +17,18 @@
  * 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.integrations.deck
 
-package com.nextcloud.client.integrations.deck;
-
-import android.app.PendingIntent;
-
-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;
+import android.app.PendingIntent
+import com.nextcloud.client.account.User
+import com.nextcloud.java.util.Optional
+import com.owncloud.android.lib.resources.notifications.models.Notification
 
 /**
- * This API is for an integration with the <a href="https://github.com/stefan-niedermann/nextcloud-deck">Nextcloud
- * Deck</a> app for android.
+ * This API is for an integration with the [Nextcloud
+ * Deck](https://github.com/stefan-niedermann/nextcloud-deck) app for android.
  */
-public interface DeckApi {
-
+interface DeckApi {
     /**
      * Creates a PendingIntent that can be used in a NotificationBuilder to open the notification link in Deck app
      *
@@ -41,9 +36,10 @@ public interface DeckApi {
      * @param user         The user that is affected by the notification
      * @return If notification can be consumed by Deck, a PendingIntent opening notification link in Deck app; empty
      * value otherwise
-     * @see <a href="https://apps.nextcloud.com/apps/deck">Deck Server App</a>
+     * @see [Deck Server App](https://apps.nextcloud.com/apps/deck)
      */
-    @NonNull
-    Optional<PendingIntent> createForwardToDeckActionIntent(@NonNull final Notification notification,
-                                                            @NonNull final User user);
+    fun createForwardToDeckActionIntent(
+        notification: Notification,
+        user: User
+    ): Optional<PendingIntent?>
 }

+ 46 - 57
app/src/main/java/com/nextcloud/client/integrations/deck/DeckApiImpl.kt

@@ -17,73 +17,43 @@
  * 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.integrations.deck
 
-package com.nextcloud.client.integrations.deck;
+import android.app.PendingIntent
+import android.content.Context
+import android.content.Intent
+import android.content.pm.PackageManager
+import com.nextcloud.client.account.User
+import com.nextcloud.java.util.Optional
+import com.owncloud.android.lib.resources.notifications.models.Notification
 
-import android.app.PendingIntent;
-import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-
-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 class DeckApiImpl implements DeckApi {
-
-    static final String APP_NAME = "deck";
-    static final String[] DECK_APP_PACKAGES = new String[] {
-        "it.niedermann.nextcloud.deck",
-        "it.niedermann.nextcloud.deck.play",
-        "it.niedermann.nextcloud.deck.dev"
-    };
-    static final String DECK_ACTIVITY_TO_START = "it.niedermann.nextcloud.deck.ui.PushNotificationActivity";
-
-    private static final String EXTRA_ACCOUNT = "account";
-    private static final String EXTRA_LINK = "link";
-    private static final String EXTRA_OBJECT_ID = "objectId";
-    private static final String EXTRA_SUBJECT = "subject";
-    private static final String EXTRA_SUBJECT_RICH = "subjectRich";
-    private static final String EXTRA_MESSAGE = "message";
-    private static final String EXTRA_MESSAGE_RICH = "messageRich";
-    private static final String EXTRA_USER = "user";
-    private static final String EXTRA_NID = "nid";
-
-    private final Context context;
-    private final PackageManager packageManager;
-
-    public DeckApiImpl(@NonNull Context context, @NonNull PackageManager packageManager) {
-        this.context = context;
-        this.packageManager = packageManager;
-    }
-
-    @NonNull
-    @Override
-    public Optional<PendingIntent> createForwardToDeckActionIntent(@NonNull Notification notification, @NonNull User user) {
-        if (APP_NAME.equalsIgnoreCase(notification.app)) {
-            final Intent intent = new Intent();
-            for (String appPackage : DECK_APP_PACKAGES) {
-                intent.setClassName(appPackage, DECK_ACTIVITY_TO_START);
+class DeckApiImpl(private val context: Context, private val packageManager: PackageManager) : DeckApi {
+    override fun createForwardToDeckActionIntent(notification: Notification, user: User): Optional<PendingIntent?> {
+        if (APP_NAME.equals(notification.app, ignoreCase = true)) {
+            val intent = Intent()
+            for (appPackage in DECK_APP_PACKAGES) {
+                intent.setClassName(appPackage, DECK_ACTIVITY_TO_START)
                 if (packageManager.resolveActivity(intent, 0) != null) {
-                    return Optional.of(createPendingIntent(intent, notification, user));
+                    return Optional.of(createPendingIntent(intent, notification, user))
                 }
             }
         }
-        return Optional.empty();
+        return Optional.empty()
     }
 
-    private PendingIntent createPendingIntent(@NonNull Intent intent, @NonNull Notification notification, @NonNull User user) {
-        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        return PendingIntent.getActivity(context, notification.getNotificationId(),
-                                         putExtrasToIntent(intent, notification, user),
-                                         PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
+    private fun createPendingIntent(intent: Intent, notification: Notification, user: User): PendingIntent {
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+        return PendingIntent.getActivity(
+            context,
+            notification.getNotificationId(),
+            putExtrasToIntent(intent, notification, user),
+            PendingIntent.FLAG_ONE_SHOT or PendingIntent.FLAG_IMMUTABLE
+        )
     }
 
-    private Intent putExtrasToIntent(@NonNull Intent intent, @NonNull Notification notification, @NonNull User user) {
+    private fun putExtrasToIntent(intent: Intent, notification: Notification, user: User): Intent {
         return intent
-            .putExtra(EXTRA_ACCOUNT, user.getAccountName())
+            .putExtra(EXTRA_ACCOUNT, user.accountName)
             .putExtra(EXTRA_LINK, notification.getLink())
             .putExtra(EXTRA_OBJECT_ID, notification.getObjectId())
             .putExtra(EXTRA_SUBJECT, notification.getSubject())
@@ -91,6 +61,25 @@ public class DeckApiImpl implements DeckApi {
             .putExtra(EXTRA_MESSAGE, notification.getMessage())
             .putExtra(EXTRA_MESSAGE_RICH, notification.getMessageRich())
             .putExtra(EXTRA_USER, notification.getUser())
-            .putExtra(EXTRA_NID, notification.getNotificationId());
+            .putExtra(EXTRA_NID, notification.getNotificationId())
+    }
+
+    companion object {
+        const val APP_NAME = "deck"
+        val DECK_APP_PACKAGES = arrayOf(
+            "it.niedermann.nextcloud.deck",
+            "it.niedermann.nextcloud.deck.play",
+            "it.niedermann.nextcloud.deck.dev"
+        )
+        const val DECK_ACTIVITY_TO_START = "it.niedermann.nextcloud.deck.ui.PushNotificationActivity"
+        private const val EXTRA_ACCOUNT = "account"
+        private const val EXTRA_LINK = "link"
+        private const val EXTRA_OBJECT_ID = "objectId"
+        private const val EXTRA_SUBJECT = "subject"
+        private const val EXTRA_SUBJECT_RICH = "subjectRich"
+        private const val EXTRA_MESSAGE = "message"
+        private const val EXTRA_MESSAGE_RICH = "messageRich"
+        private const val EXTRA_USER = "user"
+        private const val EXTRA_NID = "nid"
     }
 }

+ 1 - 1
app/src/main/java/com/nextcloud/client/jobs/NotificationWork.kt

@@ -142,7 +142,7 @@ class NotificationWork constructor(
 
         val deckActionOverrideIntent = deckApi.createForwardToDeckActionIntent(notification, user)
 
-        val pendingIntent: PendingIntent
+        val pendingIntent: PendingIntent?
         if (deckActionOverrideIntent.isPresent) {
             pendingIntent = deckActionOverrideIntent.get()
         } else {