CallParticipantModelNotifier.java 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Daniel Calviño Sánchez
  5. * Copyright (C) 2022 Daniel Calviño Sánchez <danxuliu@gmail.com>
  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.talk.call;
  21. import android.os.Handler;
  22. import android.os.Looper;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. /**
  27. * Helper class to register and notify CallParticipantModel.Observers.
  28. *
  29. * This class is only meant for internal use by CallParticipantModel; observers must register themselves against a
  30. * CallParticipantModel rather than against a CallParticipantModelNotifier.
  31. */
  32. class CallParticipantModelNotifier {
  33. /**
  34. * Helper class to associate a CallParticipantModel.Observer with a Handler.
  35. */
  36. private static class CallParticipantModelObserverOn {
  37. public final CallParticipantModel.Observer observer;
  38. public final Handler handler;
  39. private CallParticipantModelObserverOn(CallParticipantModel.Observer observer, Handler handler) {
  40. this.observer = observer;
  41. this.handler = handler;
  42. }
  43. }
  44. private final List<CallParticipantModelObserverOn> callParticipantModelObserversOn = new ArrayList<>();
  45. public synchronized void addObserver(CallParticipantModel.Observer observer, Handler handler) {
  46. if (observer == null) {
  47. throw new IllegalArgumentException("CallParticipantModel.Observer can not be null");
  48. }
  49. removeObserver(observer);
  50. callParticipantModelObserversOn.add(new CallParticipantModelObserverOn(observer, handler));
  51. }
  52. public synchronized void removeObserver(CallParticipantModel.Observer observer) {
  53. Iterator<CallParticipantModelObserverOn> it = callParticipantModelObserversOn.iterator();
  54. while (it.hasNext()) {
  55. CallParticipantModelObserverOn observerOn = it.next();
  56. if (observerOn.observer == observer) {
  57. it.remove();
  58. return;
  59. }
  60. }
  61. }
  62. public synchronized void notifyChange() {
  63. for (CallParticipantModelObserverOn observerOn : new ArrayList<>(callParticipantModelObserversOn)) {
  64. if (observerOn.handler == null || observerOn.handler.getLooper() == Looper.myLooper()) {
  65. observerOn.observer.onChange();
  66. } else {
  67. observerOn.handler.post(() -> {
  68. observerOn.observer.onChange();
  69. });
  70. }
  71. }
  72. }
  73. public synchronized void notifyReaction(String reaction) {
  74. for (CallParticipantModelObserverOn observerOn : new ArrayList<>(callParticipantModelObserversOn)) {
  75. if (observerOn.handler == null || observerOn.handler.getLooper() == Looper.myLooper()) {
  76. observerOn.observer.onReaction(reaction);
  77. } else {
  78. observerOn.handler.post(() -> {
  79. observerOn.observer.onReaction(reaction);
  80. });
  81. }
  82. }
  83. }
  84. }