Skip to content

Commit e6bbf1d

Browse files
authored
Merge pull request #517 from joehattori/add-twa-startup-timestamp
Add an extra data for TWA startup timestamp
2 parents ea274dd + 3149807 commit e6bbf1d

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

androidbrowserhelper/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ android {
2929
versionCode 1
3030
versionName VERSION
3131

32+
buildConfigField "int", "LIBRARY_VERSION", "${versionCode}"
33+
3234
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
3335
}
3436

@@ -39,6 +41,10 @@ android {
3941
}
4042
}
4143

44+
buildFeatures {
45+
buildConfig true
46+
}
47+
4248
compileOptions {
4349
sourceCompatibility = JavaVersion.VERSION_1_8
4450
targetCompatibility = JavaVersion.VERSION_1_8

androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/LauncherActivity.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.graphics.Matrix;
2121
import android.net.Uri;
2222
import android.os.Bundle;
23+
import android.os.SystemClock;
2324
import android.util.Log;
2425
import android.widget.ImageView;
2526

@@ -125,10 +126,13 @@ public class LauncherActivity extends Activity {
125126
@Nullable
126127
private TwaLauncher mTwaLauncher;
127128

129+
private long mStartupUptimeMillis;
130+
128131
@Override
129132
protected void onCreate(@Nullable Bundle savedInstanceState) {
130133
super.onCreate(savedInstanceState);
131134

135+
mStartupUptimeMillis = SystemClock.uptimeMillis();
132136
sLauncherActivitiesAlive++;
133137
boolean twaAlreadyRunning = sLauncherActivitiesAlive > 1;
134138
boolean intentHasData = getIntent().getData() != null;
@@ -235,6 +239,7 @@ protected void launchTwa() {
235239
addFileDataIfPresent(twaBuilder);
236240

237241
mTwaLauncher = createTwaLauncher();
242+
mTwaLauncher.setStartupUptimeMillis(mStartupUptimeMillis);
238243
mTwaLauncher.launch(twaBuilder,
239244
getCustomTabsCallback(),
240245
mSplashScreenStrategy,

androidbrowserhelper/src/main/java/com/google/androidbrowserhelper/trusted/TwaLauncher.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import androidx.browser.trusted.TrustedWebActivityIntentBuilder;
3737
import androidx.core.content.ContextCompat;
3838

39+
import com.google.androidbrowserhelper.BuildConfig;
3940
import com.google.androidbrowserhelper.trusted.ChromeOsSupport;
4041
import com.google.androidbrowserhelper.trusted.splashscreens.SplashScreenStrategy;
4142

@@ -48,6 +49,12 @@ public class TwaLauncher {
4849

4950
private static final int DEFAULT_SESSION_ID = 96375;
5051

52+
private static final String EXTRA_STARTUP_UPTIME_MILLIS =
53+
"org.chromium.chrome.browser.customtabs.trusted.STARTUP_UPTIME_MILLIS";
54+
55+
private static final String EXTRA_ANDROID_BROWSER_HELPER_VERSION =
56+
"org.chromium.chrome.browser.ANDROID_BROWSER_HELPER_VERSION";
57+
5158
public static final FallbackStrategy CCT_FALLBACK_STRATEGY =
5259
(context, twaBuilder, providerPackage, completionCallback) -> {
5360
// CustomTabsIntent will fall back to launching the Browser if there are no Custom Tabs
@@ -96,6 +103,8 @@ public class TwaLauncher {
96103

97104
private boolean mDestroyed;
98105

106+
private long mStartupUptimeMillis;
107+
99108
public interface FallbackStrategy {
100109
void launch(Context context,
101110
TrustedWebActivityIntentBuilder twaBuilder,
@@ -269,6 +278,11 @@ private void launchWhenSplashScreenReady(TrustedWebActivityIntentBuilder builder
269278
}
270279
Log.d(TAG, "Launching Trusted Web Activity.");
271280
TrustedWebActivityIntent intent = builder.build(mSession);
281+
if (mStartupUptimeMillis != 0) {
282+
intent.getIntent().putExtra(EXTRA_STARTUP_UPTIME_MILLIS, mStartupUptimeMillis);
283+
}
284+
intent.getIntent().putExtra(
285+
EXTRA_ANDROID_BROWSER_HELPER_VERSION, BuildConfig.LIBRARY_VERSION);
272286
FocusActivity.addToIntent(intent.getIntent(), mContext);
273287
intent.launchTrustedWebActivity(mContext);
274288

@@ -299,6 +313,15 @@ public String getProviderPackage() {
299313
return mProviderPackage;
300314
}
301315

316+
/**
317+
* Sets the timestamp (in SystemClock.uptimeMillis()) when the TWA launcher
318+
* activity was created. This timestamp is used to report the full startup
319+
* duration to the browser.
320+
*/
321+
public void setStartupUptimeMillis(long startupUptimeMillis) {
322+
mStartupUptimeMillis = startupUptimeMillis;
323+
}
324+
302325
private class TwaCustomTabsServiceConnection extends CustomTabsServiceConnection {
303326
private Runnable mOnSessionCreatedRunnable;
304327
private Runnable mOnSessionCreationFailedRunnable;

0 commit comments

Comments
 (0)