Skip to content

Commit 4836997

Browse files
committed
fix
1 parent 283e2c8 commit 4836997

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

firebase-functions/src/androidTest/java/com/google/firebase/functions/CallTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
import androidx.test.InstrumentationRegistry;
2424
import androidx.test.ext.junit.runners.AndroidJUnit4;
25-
2625
import com.google.android.gms.tasks.Task;
2726
import com.google.android.gms.tasks.Tasks;
2827
import com.google.firebase.FirebaseApp;
@@ -94,7 +93,8 @@ public void testToken() throws InterruptedException, ExecutionException {
9493
HttpsCallableContext context = new HttpsCallableContext("token", null, null);
9594
return Tasks.forResult(context);
9695
},
97-
TestOnlyExecutors.lite());
96+
TestOnlyExecutors.lite(),
97+
TestOnlyExecutors.ui());
9898

9999
HttpsCallableReference function = functions.getHttpsCallable("tokenTest");
100100
Task<HttpsCallableResult> result = function.call(new HashMap<>());
@@ -116,7 +116,8 @@ public void testInstanceId() throws InterruptedException, ExecutionException {
116116
HttpsCallableContext context = new HttpsCallableContext(null, "iid", null);
117117
return Tasks.forResult(context);
118118
},
119-
TestOnlyExecutors.lite());
119+
TestOnlyExecutors.lite(),
120+
TestOnlyExecutors.ui());
120121

121122
HttpsCallableReference function = functions.getHttpsCallable("instanceIdTest");
122123
Task<HttpsCallableResult> result = function.call(new HashMap<>());
@@ -138,7 +139,8 @@ public void testAppCheck() throws InterruptedException, ExecutionException {
138139
HttpsCallableContext context = new HttpsCallableContext(null, null, "appCheck");
139140
return Tasks.forResult(context);
140141
},
141-
TestOnlyExecutors.lite());
142+
TestOnlyExecutors.lite(),
143+
TestOnlyExecutors.ui());
142144

143145
HttpsCallableReference function = functions.getHttpsCallable("appCheckTest");
144146
Task<HttpsCallableResult> result = function.call(new HashMap<>());

firebase-functions/src/main/java/com/google/firebase/functions/FirebaseFunctions.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.firebase.functions;
1616

1717
import android.content.Context;
18-
import android.os.Handler;
1918
import android.util.Log;
2019
import androidx.annotation.NonNull;
2120
import androidx.annotation.Nullable;
@@ -28,6 +27,7 @@
2827
import com.google.android.gms.tasks.Tasks;
2928
import com.google.firebase.FirebaseApp;
3029
import com.google.firebase.annotations.concurrent.Lightweight;
30+
import com.google.firebase.annotations.concurrent.UiThread;
3131
import com.google.firebase.emulators.EmulatedServiceSettings;
3232
import com.google.firebase.functions.FirebaseFunctionsException.Code;
3333
import java.io.IOException;
@@ -94,7 +94,8 @@ public class FirebaseFunctions {
9494
String projectId,
9595
String regionOrCustomDomain,
9696
ContextProvider contextProvider,
97-
@Lightweight Executor executor) {
97+
@Lightweight Executor executor,
98+
@UiThread Executor uiExecutor) {
9899
this.app = app;
99100
this.executor = executor;
100101
this.client = new OkHttpClient();
@@ -118,15 +119,16 @@ public class FirebaseFunctions {
118119
this.customDomain = regionOrCustomDomain;
119120
}
120121

121-
maybeInstallProviders(context);
122+
maybeInstallProviders(context, uiExecutor);
122123
}
123124

124125
/**
125126
* Runs ProviderInstaller.installIfNeededAsync once per application instance.
126127
*
127128
* @param context The application context.
129+
* @param uiExecutor
128130
*/
129-
private static void maybeInstallProviders(Context context) {
131+
private static void maybeInstallProviders(Context context, Executor uiExecutor) {
130132
// Make sure this only runs once.
131133
synchronized (providerInstalled) {
132134
if (providerInstallStarted) {
@@ -137,7 +139,7 @@ private static void maybeInstallProviders(Context context) {
137139

138140
// Package installIfNeededAsync into a Runnable so it can be run on the main thread.
139141
// installIfNeededAsync checks to make sure it is on the main thread, and throws otherwise.
140-
Runnable runnable =
142+
uiExecutor.execute(
141143
() ->
142144
ProviderInstaller.installIfNeededAsync(
143145
context,
@@ -152,10 +154,7 @@ public void onProviderInstallFailed(int i, android.content.Intent intent) {
152154
Log.d("FirebaseFunctions", "Failed to update ssl context");
153155
providerInstalled.setResult(null);
154156
}
155-
});
156-
157-
Handler handler = new Handler(context.getMainLooper());
158-
handler.post(runnable);
157+
}));
159158
}
160159

161160
/**

firebase-functions/src/main/java/com/google/firebase/functions/FunctionsMultiResourceComponent.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import android.content.Context;
1818
import androidx.annotation.GuardedBy;
19+
import androidx.annotation.UiThread;
1920
import com.google.firebase.FirebaseApp;
2021
import com.google.firebase.annotations.concurrent.Lightweight;
2122
import java.util.HashMap;
@@ -35,17 +36,20 @@ class FunctionsMultiResourceComponent {
3536
private final Context applicationContext;
3637
private final ContextProvider contextProvider;
3738
private final FirebaseApp app;
38-
private final Executor executor;
39+
private final Executor liteExecutor;
40+
private final Executor uiExecutor;
3941

4042
FunctionsMultiResourceComponent(
4143
Context applicationContext,
4244
ContextProvider contextProvider,
4345
FirebaseApp app,
44-
@Lightweight Executor executor) {
46+
@Lightweight Executor liteExecutor,
47+
@UiThread Executor uiExecutor) {
4548
this.applicationContext = applicationContext;
4649
this.contextProvider = contextProvider;
4750
this.app = app;
48-
this.executor = executor;
51+
this.liteExecutor = liteExecutor;
52+
this.uiExecutor = uiExecutor;
4953
}
5054

5155
synchronized FirebaseFunctions get(String regionOrCustomDomain) {
@@ -55,7 +59,13 @@ synchronized FirebaseFunctions get(String regionOrCustomDomain) {
5559
if (functions == null) {
5660
functions =
5761
new FirebaseFunctions(
58-
app, applicationContext, projectId, regionOrCustomDomain, contextProvider, executor);
62+
app,
63+
applicationContext,
64+
projectId,
65+
regionOrCustomDomain,
66+
contextProvider,
67+
liteExecutor,
68+
uiExecutor);
5969
instances.put(regionOrCustomDomain, functions);
6070
}
6171
return functions;

firebase-functions/src/main/java/com/google/firebase/functions/FunctionsRegistrar.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import androidx.annotation.Keep;
1919
import com.google.firebase.FirebaseApp;
2020
import com.google.firebase.annotations.concurrent.Lightweight;
21+
import com.google.firebase.annotations.concurrent.UiThread;
2122
import com.google.firebase.appcheck.interop.InternalAppCheckTokenProvider;
2223
import com.google.firebase.auth.internal.InternalAuthProvider;
2324
import com.google.firebase.components.Component;
@@ -42,11 +43,14 @@ public class FunctionsRegistrar implements ComponentRegistrar {
4243
@Override
4344
public List<Component<?>> getComponents() {
4445
Qualified<Executor> liteExecutor = Qualified.qualified(Lightweight.class, Executor.class);
46+
Qualified<Executor> uiExecutor = Qualified.qualified(UiThread.class, Executor.class);
4547
return Arrays.asList(
4648
Component.builder(ContextProvider.class)
4749
.add(Dependency.optionalProvider(InternalAuthProvider.class))
4850
.add(Dependency.requiredProvider(FirebaseInstanceIdInternal.class))
4951
.add(Dependency.deferred(InternalAppCheckTokenProvider.class))
52+
.add(Dependency.required(liteExecutor))
53+
.add(Dependency.required(uiExecutor))
5054
.factory(
5155
c ->
5256
new FirebaseContextProvider(
@@ -66,7 +70,8 @@ public List<Component<?>> getComponents() {
6670
c.get(Context.class),
6771
c.get(ContextProvider.class),
6872
c.get(FirebaseApp.class),
69-
c.get(liteExecutor)))
73+
c.get(liteExecutor),
74+
c.get(uiExecutor)))
7075
.build(),
7176
LibraryVersionComponent.create(LIBRARY_NAME, BuildConfig.VERSION_NAME));
7277
}

0 commit comments

Comments
 (0)