Skip to content

Commit

Permalink
[android] move all task listeners off the UI thread
Browse files Browse the repository at this point in the history
[android] update to latest firebase sdks - previous release broken (#2122)

[skip ci]
  • Loading branch information
Salakar committed May 9, 2019
1 parent f9d2010 commit ff07600
Show file tree
Hide file tree
Showing 18 changed files with 155 additions and 130 deletions.
8 changes: 7 additions & 1 deletion packages/analytics/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="io.invertase.firebase.analytics" />
<manifest package="io.invertase.firebase.analytics"
xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*
*/

import android.annotation.SuppressLint;
import android.app.Activity;

import com.facebook.react.bridge.Arguments;
Expand All @@ -39,7 +38,6 @@ public class ReactNativeFirebaseAnalyticsModule extends ReactNativeFirebaseModul
super(reactContext, TAG);
}

@SuppressLint("MissingPermission")
@ReactMethod
public void logEvent(String name, @Nullable ReadableMap params, Promise promise) {
try {
Expand All @@ -50,7 +48,6 @@ public void logEvent(String name, @Nullable ReadableMap params, Promise promise)
}
}

@SuppressLint("MissingPermission")
@ReactMethod
public void setAnalyticsCollectionEnabled(Boolean enabled, Promise promise) {
try {
Expand Down Expand Up @@ -81,7 +78,6 @@ public void setCurrentScreen(String screenName, String screenClassOverride, Prom
}
}

@SuppressLint("MissingPermission")
@ReactMethod
public void setMinimumSessionDuration(double milliseconds, Promise promise) {
try {
Expand All @@ -92,7 +88,6 @@ public void setMinimumSessionDuration(double milliseconds, Promise promise) {
}
}

@SuppressLint("MissingPermission")
@ReactMethod
public void setSessionTimeoutDuration(double milliseconds, Promise promise) {
try {
Expand All @@ -103,7 +98,6 @@ public void setSessionTimeoutDuration(double milliseconds, Promise promise) {
}
}

@SuppressLint("MissingPermission")
@ReactMethod
public void setUserId(String id, Promise promise) {
try {
Expand All @@ -114,7 +108,6 @@ public void setUserId(String id, Promise promise) {
}
}

@SuppressLint("MissingPermission")
@ReactMethod
public void setUserProperty(String name, String value, Promise promise) {
try {
Expand All @@ -129,7 +122,7 @@ public void setUserProperty(String name, String value, Promise promise) {
public void setUserProperties(ReadableMap properties, Promise promise) {
try {
ReadableMapKeySetIterator iterator = properties.keySetIterator();
@SuppressLint("MissingPermission") FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(getContext());
FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(getContext());

while (iterator.hasNextKey()) {
String name = iterator.nextKey();
Expand All @@ -143,7 +136,6 @@ public void setUserProperties(ReadableMap properties, Promise promise) {
}
}

@SuppressLint("MissingPermission")
@ReactMethod
public void resetAnalyticsData(Promise promise) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.Nonnull;

import io.invertase.firebase.interfaces.ContextProvider;

public class ReactNativeFirebaseModule extends ReactContextBaseJavaModule implements ContextProvider {
private static Map<String, ExecutorService> executors = new HashMap<>();
private String moduleName;

public ReactNativeFirebaseModule(
Expand All @@ -45,23 +48,6 @@ public ReactNativeFirebaseModule(
this.moduleName = moduleName;
}

@Override
public void initialize() {
super.initialize();
}

public ReactContext getContext() {
return getReactApplicationContext();
}

public Context getApplicationContext() {
return getReactApplicationContext().getApplicationContext();
}

public Activity getActivity() {
return getCurrentActivity();
}

static WritableMap getExceptionMap(Exception exception) {
WritableMap exceptionMap = Arguments.createMap();
String code = "unknown";
Expand All @@ -84,14 +70,53 @@ public static void rejectPromiseWithCodeAndMessage(Promise promise, String code,
promise.reject(code, message, userInfoMap);
}

public static void rejectPromiseWithCodeAndMessage(Promise promise, String code, String message, String nativeErrorMessage) {
public static void rejectPromiseWithCodeAndMessage(
Promise promise,
String code,
String message,
String nativeErrorMessage
) {
WritableMap userInfoMap = Arguments.createMap();
userInfoMap.putString("code", code);
userInfoMap.putString("message", message);
userInfoMap.putString("nativeErrorMessage", nativeErrorMessage);
promise.reject(code, message, userInfoMap);
}

@Override
public void initialize() {
super.initialize();
}

public ReactContext getContext() {
return getReactApplicationContext();
}

public ExecutorService getExecutor() {
ExecutorService existingSingleThreadExecutor = executors.get(getName());
if (existingSingleThreadExecutor != null) return existingSingleThreadExecutor;
ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
executors.put(getName(), newSingleThreadExecutor);
return newSingleThreadExecutor;
}

@Override
public void onCatalystInstanceDestroy() {
ExecutorService existingSingleThreadExecutor = executors.get(getName());
if (existingSingleThreadExecutor != null) {
existingSingleThreadExecutor.shutdownNow();
executors.remove(getName());
}
}

public Context getApplicationContext() {
return getReactApplicationContext().getApplicationContext();
}

public Activity getActivity() {
return getCurrentActivity();
}

@Nonnull
@Override
public String getName() {
Expand Down
2 changes: 1 addition & 1 deletion packages/auth/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ project.ext {
],

firebase : [
auth: "16.2.1"
auth: "17.0.0"
],
],
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,12 +439,12 @@ public void sendPasswordResetEmail(
if (actionCodeSettings == null) {
firebaseAuth
.sendPasswordResetEmail(email)
.addOnCompleteListener(listener);
.addOnCompleteListener(getExecutor(), listener);
} else {
ActionCodeSettings settings = buildActionCodeSettings(actionCodeSettings);
firebaseAuth
.sendPasswordResetEmail(email, settings)
.addOnCompleteListener(listener);
.addOnCompleteListener(getExecutor(), listener);
}
}

Expand Down Expand Up @@ -480,7 +480,7 @@ public void sendSignInLinkToEmail(
ActionCodeSettings settings = buildActionCodeSettings(actionCodeSettings);
firebaseAuth
.sendSignInLinkToEmail(email, settings)
.addOnCompleteListener(listener);
.addOnCompleteListener(getExecutor(), listener);
}


Expand All @@ -504,7 +504,7 @@ public void delete(String appName, final Promise promise) {
if (user != null) {
user
.delete()
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "delete:onComplete:success");
promiseNoUser(promise, false);
Expand Down Expand Up @@ -539,7 +539,7 @@ public void reload(String appName, final Promise promise) {
} else {
user
.reload()
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "reload:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
Expand Down Expand Up @@ -587,12 +587,12 @@ public void sendEmailVerification(
if (actionCodeSettings == null) {
user
.sendEmailVerification()
.addOnCompleteListener(listener);
.addOnCompleteListener(getExecutor(), listener);
} else {
ActionCodeSettings settings = buildActionCodeSettings(actionCodeSettings);
user
.sendEmailVerification(settings)
.addOnCompleteListener(listener);
.addOnCompleteListener(getExecutor(), listener);
}
}
}
Expand All @@ -617,7 +617,7 @@ public void updateEmail(String appName, final String email, final Promise promis
} else {
user
.updateEmail(email)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "updateEmail:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
Expand Down Expand Up @@ -650,7 +650,7 @@ public void updatePassword(String appName, final String password, final Promise
} else {
user
.updatePassword(password)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "updatePassword:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
Expand Down Expand Up @@ -706,7 +706,7 @@ private void updatePhoneNumber(
Log.d(TAG, "updatePhoneNumber");
user
.updatePhoneNumber(credential)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "updatePhoneNumber:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
Expand Down Expand Up @@ -753,7 +753,7 @@ public void updateProfile(String appName, ReadableMap props, final Promise promi

user
.updateProfile(profileUpdates)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "updateProfile:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
Expand Down Expand Up @@ -789,7 +789,7 @@ private void signInWithCredential(
Log.d(TAG, "signInWithCredential");
firebaseAuth
.signInWithCredential(credential)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:onComplete:success");
promiseWithAuthResult(task.getResult(), promise);
Expand Down Expand Up @@ -837,7 +837,7 @@ public void onVerificationCompleted(final PhoneAuthCredential phoneAuthCredentia
// User has been automatically verified, log them in
firebaseAuth
.signInWithCredential(phoneAuthCredential)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
// onAuthStateChanged will pick up the user change
Log.d(
Expand Down Expand Up @@ -947,7 +947,7 @@ public void confirmationResultConfirm(
verificationCode
);

firebaseAuth.signInWithCredential(credential).addOnCompleteListener(task -> {
firebaseAuth.signInWithCredential(credential).addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(
TAG,
Expand Down Expand Up @@ -1118,7 +1118,7 @@ public void confirmPasswordReset(

firebaseAuth
.confirmPasswordReset(code, newPassword)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "confirmPasswordReset:onComplete:success");
promiseNoUser(promise, false);
Expand All @@ -1145,7 +1145,7 @@ public void applyActionCode(String appName, String code, final Promise promise)

firebaseAuth
.applyActionCode(code)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "applyActionCode:onComplete:success");
promiseWithUser(firebaseAuth.getCurrentUser(), promise);
Expand All @@ -1170,7 +1170,7 @@ public void checkActionCode(String appName, String code, final Promise promise)

firebaseAuth
.checkActionCode(code)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "checkActionCode:onComplete:success");
ActionCodeResult result = Objects.requireNonNull(task.getResult());
Expand Down Expand Up @@ -1248,7 +1248,7 @@ private void linkWithCredential(
if (user != null) {
user
.linkWithCredential(credential)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "link:onComplete:success");
promiseWithAuthResult(task.getResult(), promise);
Expand All @@ -1274,7 +1274,7 @@ public void unlink(final String appName, final String providerId, final Promise
if (user != null) {
user
.unlink(providerId)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "unlink:onComplete:success");
promiseWithUser(Objects.requireNonNull(task.getResult()).getUser(), promise);
Expand Down Expand Up @@ -1315,7 +1315,7 @@ private void reauthenticateWithCredential(
if (user != null) {
user
.reauthenticateAndRetrieveData(credential)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "reauthenticate:onComplete:success");
promiseWithAuthResult(task.getResult(), promise);
Expand Down Expand Up @@ -1411,7 +1411,7 @@ public void getIdToken(String appName, Boolean forceRefresh, final Promise promi

user
.getIdToken(forceRefresh)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "getIdToken:onComplete:success");
GetTokenResult tokenResult = task.getResult();
Expand Down Expand Up @@ -1446,7 +1446,7 @@ public void getIdTokenResult(String appName, Boolean forceRefresh, final Promise

user
.getIdToken(forceRefresh)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "getIdTokenResult:onComplete:success");
GetTokenResult tokenResult = task.getResult();
Expand Down Expand Up @@ -1514,7 +1514,7 @@ public void fetchSignInMethodsForEmail(String appName, String email, final Promi

firebaseAuth
.fetchSignInMethodsForEmail(email)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "fetchProvidersForEmail:onComplete:success");
List<String> providers = Objects.requireNonNull(task.getResult()).getSignInMethods();
Expand Down Expand Up @@ -1571,7 +1571,7 @@ public void verifyPasswordResetCode(String appName, String code, final Promise p

firebaseAuth
.verifyPasswordResetCode(code)
.addOnCompleteListener(task -> {
.addOnCompleteListener(getExecutor(), task -> {
if (task.isSuccessful()) {
Log.d(TAG, "verifyPasswordResetCode:onComplete:success");
promise.resolve(task.getResult());
Expand Down
Loading

0 comments on commit ff07600

Please sign in to comment.