@@ -54,9 +54,6 @@ public class TwaLauncher {
54
54
@ Nullable
55
55
private CustomTabsSession mSession ;
56
56
57
- @ Nullable
58
- private Runnable mOnSessionCreatedRunnable ;
59
-
60
57
private boolean mDestroyed ;
61
58
62
59
/**
@@ -151,17 +148,28 @@ private void launchTwa(TrustedWebActivityIntentBuilder twaBuilder,
151
148
152
149
Runnable onSessionCreatedRunnable = () ->
153
150
launchWhenSessionEstablished (twaBuilder , splashScreenStrategy , completionCallback );
151
+
154
152
if (mSession != null ) {
155
153
onSessionCreatedRunnable .run ();
156
154
return ;
157
155
}
158
156
159
- mOnSessionCreatedRunnable = onSessionCreatedRunnable ;
157
+ Runnable onSessionCreationFailedRunnable = () -> {
158
+ // The provider has been unable to create a session for us, we can't launch a
159
+ // Trusted Web Activity. We could either exit, forcing the user to try again,
160
+ // hopefully successfully this time or we could launch a Custom Tab giving the user
161
+ // a subpar experience (compared to a TWA).
162
+ // We'll open in a CCT, but pay attention to what users want.
163
+ launchCct (twaBuilder , completionCallback );
164
+ };
165
+
160
166
if (mServiceConnection == null ) {
161
167
mServiceConnection = new TwaCustomTabsServiceConnection ();
162
168
}
163
- CustomTabsClient .bindCustomTabsService (mContext , mProviderPackage ,
164
- mServiceConnection );
169
+
170
+ mServiceConnection .setSessionCreationRunnables (
171
+ onSessionCreatedRunnable , onSessionCreationFailedRunnable );
172
+ CustomTabsClient .bindCustomTabsService (mContext , mProviderPackage , mServiceConnection );
165
173
}
166
174
167
175
private void launchWhenSessionEstablished (TrustedWebActivityIntentBuilder twaBuilder ,
@@ -211,17 +219,31 @@ public String getProviderPackage() {
211
219
}
212
220
213
221
private class TwaCustomTabsServiceConnection extends CustomTabsServiceConnection {
222
+ private Runnable mOnSessionCreatedRunnable ;
223
+ private Runnable mOnSessionCreationFailedRunnable ;
224
+
225
+ private void setSessionCreationRunnables (@ Nullable Runnable onSuccess ,
226
+ @ Nullable Runnable onFailure ) {
227
+ mOnSessionCreatedRunnable = onSuccess ;
228
+ mOnSessionCreationFailedRunnable = onFailure ;
229
+ }
230
+
214
231
@ Override
215
232
public void onCustomTabsServiceConnected (ComponentName componentName ,
216
233
CustomTabsClient client ) {
217
234
if (TrustedWebUtils .warmupIsRequired (mContext , mProviderPackage )) {
218
235
client .warmup (0 );
219
236
}
220
237
mSession = client .newSession (null , mSessionId );
221
- if (mOnSessionCreatedRunnable != null ) {
238
+
239
+ if (mSession != null && mOnSessionCreatedRunnable != null ) {
222
240
mOnSessionCreatedRunnable .run ();
223
- mOnSessionCreatedRunnable = null ;
241
+ } else if (mSession == null && mOnSessionCreationFailedRunnable != null ) {
242
+ mOnSessionCreationFailedRunnable .run ();
224
243
}
244
+
245
+ mOnSessionCreatedRunnable = null ;
246
+ mOnSessionCreationFailedRunnable = null ;
225
247
}
226
248
227
249
@ Override
0 commit comments