Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloud_firestore): Fix Android Firestore transaction crash when running in background caused by null Activity. #7627

Merged
merged 10 commits into from Jan 18, 2022
Merged
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -54,3 +54,4 @@ Akora Ing. DKB <akoraingdkb@gmail.com>
Samay Bhattacharyya <contact@samay.dev>
Kevin McGill <kevin@mcgilldevtech.com>
KikiManjaro <kylian.meulin@gmail.com>
Timur Dyushaliev <timurdyushaliev@gmail.com>
Expand Up @@ -393,7 +393,7 @@ public void onMethodCall(MethodCall call, @NonNull final MethodChannel.Result re
final String transactionId = UUID.randomUUID().toString().toLowerCase(Locale.US);
final TransactionStreamHandler handler =
new TransactionStreamHandler(
activity, transaction -> transactions.put(transactionId, transaction));
transaction -> transactions.put(transactionId, transaction));

registerEventChannel(METHOD_CHANNEL_NAME + "/transaction", transactionId, handler);
transactionHandlers.put(transactionId, handler);
Expand Down
@@ -1,6 +1,7 @@
package io.flutter.plugins.firebase.firestore.streamhandler;

import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.Nullable;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FieldPath;
Expand All @@ -19,7 +20,6 @@
import java.util.Objects;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class TransactionStreamHandler implements OnTransactionResultListener, StreamHandler {

Expand All @@ -28,18 +28,15 @@ public interface OnTransactionStartedListener {
void onStarted(Transaction transaction);
}

final AtomicReference<Activity> activityRef;
final OnTransactionStartedListener onTransactionStartedListener;

public TransactionStreamHandler(
AtomicReference<Activity> activityRef,
OnTransactionStartedListener onTransactionStartedListener) {
this.activityRef = activityRef;
public TransactionStreamHandler(OnTransactionStartedListener onTransactionStartedListener) {
this.onTransactionStartedListener = onTransactionStartedListener;
}

final Semaphore semaphore = new Semaphore(0);
final Map<String, Object> response = new HashMap<>();
final Handler mainLooper = new Handler(Looper.getMainLooper());

@Override
public void onListen(Object arguments, EventSink events) {
Expand Down Expand Up @@ -68,7 +65,7 @@ public void onListen(Object arguments, EventSink events) {
Map<String, Object> attemptMap = new HashMap<>();
attemptMap.put("appName", firestore.getApp().getName());

activityRef.get().runOnUiThread(() -> events.success(attemptMap));
mainLooper.post(() -> events.success(attemptMap));

try {
if (!semaphore.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
Expand Down Expand Up @@ -149,8 +146,11 @@ public void onListen(Object arguments, EventSink events) {
map.put("complete", true);
}

activityRef.get().runOnUiThread(() -> events.success(map));
activityRef.get().runOnUiThread(events::endOfStream);
mainLooper.post(
() -> {
events.success(map);
events.endOfStream();
});
});
}

Expand Down