From b7412a9054c4a03ef752138af90da7a01d9a0ec8 Mon Sep 17 00:00:00 2001 From: Levin Li Date: Mon, 7 Jun 2021 19:33:54 +0800 Subject: [PATCH] Avoid crash in app status reporter --- app/src/main/AndroidManifest.xml | 2 +- .../utils/AppStatusReporter.java | 29 +++++++++++++------ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2e115711..b1152823 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -13,7 +13,7 @@ diff --git a/app/src/main/java/space/celestia/mobilecelestia/utils/AppStatusReporter.java b/app/src/main/java/space/celestia/mobilecelestia/utils/AppStatusReporter.java index 66a814a1..7382f886 100644 --- a/app/src/main/java/space/celestia/mobilecelestia/utils/AppStatusReporter.java +++ b/app/src/main/java/space/celestia/mobilecelestia/utils/AppStatusReporter.java @@ -70,37 +70,48 @@ public interface Listener { } public void register(Listener listener) { - if (!listeners.contains(listener)) - listeners.add(listener); + synchronized (lock) { + if (!listeners.contains(listener)) + listeners.add(listener); + } } public void unregister(Listener listener) { - listeners.remove(listener); + synchronized (lock) { + listeners.remove(listener); + } } @Override public void onCelestiaProgress(@NonNull String progress) { - synchronized (lock) { - String result = String.format(CelestiaAppCore.getLocalizedString("Loading: %s"), progress); - updateStatus(result); - } + updateStatus(String.format(CelestiaAppCore.getLocalizedString("Loading: %s"), progress)); } public void updateState(@NonNull State state) { + ArrayList currentListeners; synchronized (lock) { this.state = state; + currentListeners = new ArrayList<>(listeners.size()); for (Listener listener : listeners) { - listener.celestiaLoadingStateChanged(state); + currentListeners.add(listener); } } + for (Listener listener : currentListeners) { + listener.celestiaLoadingStateChanged(state); + } } public void updateStatus(@NonNull String status) { + ArrayList currentListeners; synchronized (lock) { this.status = status; + currentListeners = new ArrayList<>(listeners.size()); for (Listener listener : listeners) { - listener.celestiaLoadingProgress(status); + currentListeners.add(listener); } } + for (Listener listener : currentListeners) { + listener.celestiaLoadingProgress(status); + } } }