LocationMessageSendAction.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2015-2023 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package ch.threema.app.actions;
  22. import android.location.Location;
  23. import org.slf4j.Logger;
  24. import androidx.annotation.NonNull;
  25. import ch.threema.app.R;
  26. import ch.threema.app.ThreemaApplication;
  27. import ch.threema.app.messagereceiver.MessageReceiver;
  28. import ch.threema.app.services.MessageService;
  29. import ch.threema.app.utils.MessageUtil;
  30. import ch.threema.base.ThreemaException;
  31. import ch.threema.base.utils.LoggingUtil;
  32. import ch.threema.storage.models.AbstractMessageModel;
  33. public class LocationMessageSendAction extends SendAction {
  34. private static final Logger logger = LoggingUtil.getThreemaLogger("LocationMessageSendAction");
  35. protected static volatile LocationMessageSendAction instance;
  36. private static final Object instanceLock = new Object();
  37. private MessageService messageService;
  38. private LocationMessageSendAction() {
  39. // Singleton
  40. }
  41. public static LocationMessageSendAction getInstance() {
  42. if (instance == null) {
  43. synchronized (instanceLock) {
  44. if (instance == null) {
  45. instance = new LocationMessageSendAction();
  46. }
  47. }
  48. }
  49. return instance;
  50. }
  51. public boolean sendLocationMessage(
  52. final MessageReceiver[] allReceivers,
  53. final Location location,
  54. final String poiName,
  55. final ActionHandler actionHandler
  56. ) {
  57. if (actionHandler == null) {
  58. return false;
  59. }
  60. try {
  61. messageService = this.getServiceManager().getMessageService();
  62. } catch (ThreemaException e) {
  63. actionHandler.onError(e.getMessage());
  64. return false;
  65. }
  66. if (messageService == null || location == null) {
  67. actionHandler.onError("Nothing to send");
  68. return false;
  69. }
  70. if (allReceivers.length < 1) {
  71. actionHandler.onError("no message receiver");
  72. return false;
  73. }
  74. // loop all receivers (required for distribution lists)
  75. // add distribution list members to list of receivers
  76. final MessageReceiver[] resolvedReceivers = MessageUtil.addDistributionListReceivers(allReceivers);
  77. final int numReceivers = resolvedReceivers.length;
  78. sendSingleMessage(resolvedReceivers[0], location, poiName, new ActionHandler() {
  79. int receiverIndex = 0;
  80. @Override
  81. public void onError(String errorMessage) {
  82. actionHandler.onError(errorMessage);
  83. }
  84. @Override
  85. public void onWarning(String warning, boolean continueAction) {
  86. }
  87. @Override
  88. public void onProgress(int progress, int total) {
  89. actionHandler.onProgress(progress + receiverIndex, numReceivers);
  90. }
  91. @Override
  92. public void onCompleted() {
  93. if (receiverIndex < numReceivers - 1) {
  94. receiverIndex++;
  95. sendSingleMessage(resolvedReceivers[receiverIndex], location, poiName, this);
  96. } else {
  97. actionHandler.onCompleted();
  98. }
  99. }
  100. });
  101. return true;
  102. }
  103. private void sendSingleMessage(
  104. final MessageReceiver messageReceiver,
  105. final @NonNull Location location,
  106. final String poiName,
  107. final @NonNull ActionHandler actionHandler
  108. ) {
  109. if (messageReceiver == null) {
  110. actionHandler.onError("No receiver");
  111. return;
  112. }
  113. try {
  114. messageService.sendLocation(
  115. location,
  116. poiName,
  117. messageReceiver,
  118. new MessageService.CompletionHandler() {
  119. @Override
  120. public void sendComplete(AbstractMessageModel messageModel) {}
  121. @Override
  122. public void sendQueued(AbstractMessageModel messageModel) {
  123. actionHandler.onCompleted();
  124. }
  125. @Override
  126. public void sendError(int reason) {
  127. actionHandler.onError(String.format(ThreemaApplication.getAppContext().getString(R.string.an_error_occurred_more), Integer.toString(reason)));
  128. }
  129. });
  130. } catch (final Exception e) {
  131. logger.error("Could not send location message", e);
  132. actionHandler.onError(e.getMessage());
  133. }
  134. }
  135. }