Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,8 @@ protected CustomTabsCallback getCustomTabsCallback() {
}

protected TwaLauncher createTwaLauncher() {
return new TwaLauncher(this, getTaskId());
return new TwaLauncher(this, null, SessionStore.makeSessionId(getTaskId()),
new SharedPreferencesTokenStore(this));
}

private boolean splashScreenNeeded() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.google.androidbrowserhelper.trusted;

import androidx.annotation.Nullable;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* Manages and persists session IDs for given task IDs.
*/
public class SessionStore {
private static final Map<Integer, Integer> mTaskIdToSessionId = new HashMap<>();

/**
* Gets or creates a session ID for a given task ID. If the task ID is null,
* returns {Integer.MAX_VALUE}.
*
* @param taskId The unique ID for the task, may be null.
* @return The corresponding session ID, or {Integer.MAX_VALUE} if taskId is null.
*/
public static Integer makeSessionId(@Nullable Integer taskId) {
if(taskId == null) return Integer.MAX_VALUE;

Integer sessionId = mTaskIdToSessionId.get(taskId);
if(sessionId == null) {
Random random = new Random();
sessionId = random.nextInt(Integer.MAX_VALUE);
mTaskIdToSessionId.put(taskId, sessionId);
}

return sessionId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,14 @@

import com.google.androidbrowserhelper.BuildConfig;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
* Encapsulates the steps necessary to launch a Trusted Web Activity, such as establishing a
* connection with {@link CustomTabsService}.
*/
public class TwaLauncher {
private static final String TAG = "TwaLauncher";

private static final Map<Integer, Integer> mTaskIdToSessionId = new HashMap<>();
private static final int DEFAULT_SESSION_ID = 96375;

private static final String EXTRA_STARTUP_UPTIME_MILLIS =
"org.chromium.chrome.browser.customtabs.trusted.STARTUP_UPTIME_MILLIS";
Expand Down Expand Up @@ -118,34 +114,45 @@ void launch(Context context,
* If no browser supports TWA, will launch a usual Custom Tab (see {@link TwaProviderPicker}.
*/
public TwaLauncher(Context context) {
this(context, null);
this(context, (String) null);
}

/**
* Same as above, but also allows to specify a browser to launch. If specified, it is assumed to
* support TWAs.
*/
public TwaLauncher(Context context, @Nullable String providerPackage) {
this(context, providerPackage, DEFAULT_SESSION_ID,
new SharedPreferencesTokenStore(context));
}

/**
* Same as above, but also allows to specify a task id to distinguish several sessions running
* for the same TWA app.
* @deprecated This method is no longer recommended for use since TwaLauncher is rolling back to
* sessionId instead of taskId.
*/
@Deprecated(forRemoval = true)
public TwaLauncher(Context context, @Nullable Integer taskId) {
this(context, null, taskId);
}

/**
* Same as above, but also allows to specify a browser to launch. If specified, it is assumed to
* support TWAs.
* @deprecated This method is no longer recommended for use since TwaLauncher is rolling back to
* sessionId instead of taskId.
*/
@Deprecated(forRemoval = true)
public TwaLauncher(Context context, @Nullable String providerPackage, @Nullable Integer taskId) {
this(context, providerPackage, taskId,
this(context, providerPackage, SessionStore.makeSessionId(taskId),
new SharedPreferencesTokenStore(context));
}

/**
* Same as above, but also accepts a session id. This allows to launch multiple TWAs in the same
* task.
*/
public TwaLauncher(Context context, @Nullable String providerPackage, @Nullable Integer taskId,
public TwaLauncher(Context context, @Nullable String providerPackage, @Nullable Integer sessionId,
TokenStore tokenStore) {
mContext = context;
mSessionId = makeSessionId(taskId);
mSessionId = sessionId;
mTokenStore = tokenStore;
if (providerPackage == null) {
TwaProviderPicker.Action action =
Expand All @@ -158,19 +165,6 @@ public TwaLauncher(Context context, @Nullable String providerPackage, @Nullable
}
}

private static Integer makeSessionId(@Nullable Integer taskId) {
if(taskId == null) return Integer.MAX_VALUE;

Integer sessionId = mTaskIdToSessionId.get(taskId);
if(sessionId == null) {
Random random = new Random();
sessionId = random.nextInt(Integer.MAX_VALUE);
mTaskIdToSessionId.put(taskId, sessionId);
}

return sessionId;
}

/**
* Opens the specified url in a TWA.
* When TWA is already running in the current task, the url will be opened in existing TWA,
Expand Down