DeckApiImpl.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Nextcloud application
  3. *
  4. * @author Stefan Niedermann
  5. * Copyright (C) 2020 Stefan Niedermann <info@niedermann.it>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.client.integrations.deck;
  21. import android.app.PendingIntent;
  22. import android.content.Context;
  23. import android.content.Intent;
  24. import android.content.pm.PackageManager;
  25. import com.nextcloud.client.account.User;
  26. import com.nextcloud.java.util.Optional;
  27. import com.owncloud.android.lib.resources.notifications.models.Notification;
  28. import androidx.annotation.NonNull;
  29. public class DeckApiImpl implements DeckApi {
  30. static final String APP_NAME = "deck";
  31. static final String[] DECK_APP_PACKAGES = new String[] {
  32. "it.niedermann.nextcloud.deck",
  33. "it.niedermann.nextcloud.deck.play",
  34. "it.niedermann.nextcloud.deck.dev"
  35. };
  36. static final String DECK_ACTIVITY_TO_START = "it.niedermann.nextcloud.deck.ui.PushNotificationActivity";
  37. private static final String EXTRA_ACCOUNT = "account";
  38. private static final String EXTRA_LINK = "link";
  39. private static final String EXTRA_OBJECT_ID = "objectId";
  40. private static final String EXTRA_SUBJECT = "subject";
  41. private static final String EXTRA_SUBJECT_RICH = "subjectRich";
  42. private static final String EXTRA_MESSAGE = "message";
  43. private static final String EXTRA_MESSAGE_RICH = "messageRich";
  44. private static final String EXTRA_USER = "user";
  45. private static final String EXTRA_NID = "nid";
  46. private final Context context;
  47. private final PackageManager packageManager;
  48. public DeckApiImpl(@NonNull Context context, @NonNull PackageManager packageManager) {
  49. this.context = context;
  50. this.packageManager = packageManager;
  51. }
  52. @NonNull
  53. @Override
  54. public Optional<PendingIntent> createForwardToDeckActionIntent(@NonNull Notification notification, @NonNull User user) {
  55. if (APP_NAME.equalsIgnoreCase(notification.app)) {
  56. final Intent intent = new Intent();
  57. for (String appPackage : DECK_APP_PACKAGES) {
  58. intent.setClassName(appPackage, DECK_ACTIVITY_TO_START);
  59. if (packageManager.resolveActivity(intent, 0) != null) {
  60. return Optional.of(createPendingIntent(intent, notification, user));
  61. }
  62. }
  63. }
  64. return Optional.empty();
  65. }
  66. private PendingIntent createPendingIntent(@NonNull Intent intent, @NonNull Notification notification, @NonNull User user) {
  67. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  68. return PendingIntent.getActivity(context, notification.getNotificationId(),
  69. putExtrasToIntent(intent, notification, user),
  70. PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_IMMUTABLE);
  71. }
  72. private Intent putExtrasToIntent(@NonNull Intent intent, @NonNull Notification notification, @NonNull User user) {
  73. return intent
  74. .putExtra(EXTRA_ACCOUNT, user.getAccountName())
  75. .putExtra(EXTRA_LINK, notification.getLink())
  76. .putExtra(EXTRA_OBJECT_ID, notification.getObjectId())
  77. .putExtra(EXTRA_SUBJECT, notification.getSubject())
  78. .putExtra(EXTRA_SUBJECT_RICH, notification.getSubjectRich())
  79. .putExtra(EXTRA_MESSAGE, notification.getMessage())
  80. .putExtra(EXTRA_MESSAGE_RICH, notification.getMessageRich())
  81. .putExtra(EXTRA_USER, notification.getUser())
  82. .putExtra(EXTRA_NID, notification.getNotificationId());
  83. }
  84. }