Skip to content

Commit

Permalink
Support --url parameter on Android (#4400)
Browse files Browse the repository at this point in the history
Use `adb shell am start --esa args "--url=YOUR_URL"` to launch arbitrary
startup url. `--url` does not have restrictions like deep links.

Also removed old splash screen args.

b/377946714
  • Loading branch information
johnxwork authored Nov 8, 2024
1 parent ae843b4 commit 01bf374
Showing 1 changed file with 10 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,9 @@ public abstract class CobaltActivity extends Activity {
private static final String URL_ARG = "--url=";
private static final java.lang.String META_DATA_APP_URL = "cobalt.APP_URL";

private static final String SPLASH_URL_ARG = "--fallback_splash_screen_url=";
private static final String SPLASH_TOPICS_ARG = "--fallback_splash_screen_topics=";
private static final java.lang.String META_DATA_SPLASH_URL = "cobalt.SPLASH_URL";
private static final java.lang.String META_DATA_SPLASH_TOPICS = "cobalt.SPLASH_TOPIC";

private static final String FORCE_MIGRATION_FOR_STORAGE_PARTITIONING =
"--force_migration_for_storage_partitioning";
private static final String META_FORCE_MIGRATION_FOR_STORAGE_PARTITIONING =
"cobalt.force_migration_for_storage_partitioning";

private static final String EVERGREEN_LITE = "--evergreen_lite";
private static final java.lang.String META_DATA_EVERGREEN_LITE = "cobalt.EVERGREEN_LITE";

private static final String ACTIVE_SHELL_URL_KEY = "activeUrl";
public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";

// Native switch - shell_switches::kRunWebTests
private static final String RUN_WEB_TESTS_SWITCH = "run-web-tests";

private static final Pattern URL_PARAM_PATTERN = Pattern.compile("^[a-zA-Z0-9_=]*$");

public static final int JAVA_BRIDGE_INITIALIZATION_DELAY_MILLI_SECONDS = 100;
Expand Down Expand Up @@ -177,10 +161,17 @@ protected void createContent(final Bundle savedInstanceState) {
mWindowAndroid.setAnimationPlaceholderView(
mShellManager.getContentViewRenderView().getSurfaceView());

// TODO(cobalt, b/376148547): set Chrobalt initial url and remove this function.
if (mStartupUrl.isEmpty()) {
if (mStartupUrl == null || mStartupUrl.isEmpty()) {
mStartupUrl = getUrlFromIntent(getIntent());
}
if (mStartupUrl == null || mStartupUrl.isEmpty()) {
String[] args = getStarboardBridge().getArgs();
mStartupUrl = Arrays.stream(args)
.filter(line -> line.contains(URL_ARG))
.findAny()
.map(arg -> arg.substring(arg.indexOf(URL_ARG) + URL_ARG.length()))
.orElse(null);
}
if (!TextUtils.isEmpty(mStartupUrl)) {
mShellManager.setStartupUrl(Shell.sanitizeUrl(mStartupUrl));
}
Expand Down Expand Up @@ -279,11 +270,6 @@ protected void shellHandleIntent(Intent intent) {
}
}

// TODO(cobalt, b/376148547): set Chrobalt initial url and remove this function.
protected void setStartupUrl(String url) {
mStartupUrl = url;
}

protected void toggleFullscreenMode(boolean enterFullscreen) {
LinearLayout toolBar = (LinearLayout) findViewById(R.id.toolbar);
toolBar.setVisibility(enterFullscreen ? View.GONE : View.VISIBLE);
Expand Down Expand Up @@ -355,7 +341,6 @@ protected void onCreate(Bundle savedInstanceState) {
// STREAM_MUSIC whenever the target activity or fragment is visible.
setVolumeControlStream(AudioManager.STREAM_MUSIC);

setStartupUrl("https://www.youtube.com/tv");
super.onCreate(savedInstanceState);
createContent(savedInstanceState);

Expand Down Expand Up @@ -504,13 +489,7 @@ protected String[] getArgs() {

// If the URL arg isn't specified, get it from AndroidManifest.xml.
boolean hasUrlArg = hasArg(args, URL_ARG);
// If the splash screen url arg isn't specified, get it from AndroidManifest.xml.
boolean hasSplashUrlArg = hasArg(args, SPLASH_URL_ARG);
// If the splash screen topics arg isn't specified, get it from AndroidManifest.xml.
boolean hasSplashTopicsArg = hasArg(args, SPLASH_TOPICS_ARG);
// If the Evergreen-Lite arg isn't specified, get it from AndroidManifest.xml.
boolean hasEvergreenLiteArg = hasArg(args, EVERGREEN_LITE);
if (!hasUrlArg || !hasSplashUrlArg || !hasSplashTopicsArg || !hasEvergreenLiteArg) {
if (!hasUrlArg) {
try {
ActivityInfo ai =
getPackageManager()
Expand All @@ -522,24 +501,6 @@ protected String[] getArgs() {
args.add(URL_ARG + url);
}
}
if (!hasSplashUrlArg) {
String splashUrl = ai.metaData.getString(META_DATA_SPLASH_URL);
if (splashUrl != null) {
args.add(SPLASH_URL_ARG + splashUrl);
}
}
if (!hasSplashTopicsArg) {
String splashTopics = ai.metaData.getString(META_DATA_SPLASH_TOPICS);
if (splashTopics != null) {
args.add(SPLASH_TOPICS_ARG + splashTopics);
}
}
if (!hasEvergreenLiteArg && ai.metaData.getBoolean(META_DATA_EVERGREEN_LITE)) {
args.add(EVERGREEN_LITE);
}
if (ai.metaData.getBoolean(META_FORCE_MIGRATION_FOR_STORAGE_PARTITIONING)) {
args.add(FORCE_MIGRATION_FOR_STORAGE_PARTITIONING);
}
}
} catch (NameNotFoundException e) {
throw new RuntimeException("Error getting activity info", e);
Expand Down

0 comments on commit 01bf374

Please sign in to comment.