From 25fc061387a2cbf4392dc127e00c32a4fdf5b872 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Thu, 2 Jul 2015 15:41:45 -0700 Subject: [PATCH] SkyDemo should use explicit intents This CL switches SkyDemo over to using explicit intents for deciding which Sky apps to run. This change lets us get rid of the hacky "sky" URI scheme. Instead, we use normal URLs but we require the sender of the intent to be explicit in directing the VIEW intent to us. TBR=eseidel@google.com Review URL: https://codereview.chromium.org/1221123003. --- apk/demo/AndroidManifest.xml | 7 +---- .../org/domokit/sky/demo/SkyDemoActivity.java | 29 +++++-------------- sdk/home.dart | 5 +++- services/intents/intents.mojom | 6 ++++ .../domokit/intents/ActivityManagerImpl.java | 9 ++++++ .../org/domokit/sky/shell/SkyActivity.java | 7 +++-- tools/shelldb | 16 ++++------ 7 files changed, 38 insertions(+), 41 deletions(-) diff --git a/apk/demo/AndroidManifest.xml b/apk/demo/AndroidManifest.xml index dc6b36921..9d0ed17c8 100644 --- a/apk/demo/AndroidManifest.xml +++ b/apk/demo/AndroidManifest.xml @@ -15,11 +15,6 @@ - - - - - - \ No newline at end of file + diff --git a/apk/demo/org/domokit/sky/demo/SkyDemoActivity.java b/apk/demo/org/domokit/sky/demo/SkyDemoActivity.java index fcc2a23d0..0fff47c11 100644 --- a/apk/demo/org/domokit/sky/demo/SkyDemoActivity.java +++ b/apk/demo/org/domokit/sky/demo/SkyDemoActivity.java @@ -5,8 +5,6 @@ package org.domokit.sky.demo; import android.content.Intent; -import android.net.Uri; -import android.os.Bundle; import org.domokit.sky.shell.SkyActivity; @@ -14,28 +12,17 @@ import org.domokit.sky.shell.SkyActivity; * Main activity for SkyDemo. */ public class SkyDemoActivity extends SkyActivity { + private static final String DEFAULT_URL = "https://domokit.github.io/home.dart"; - /** - * @see android.app.Activity#onCreate(android.os.Bundle) - */ @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - String url = "https://domokit.github.io/home.dart"; + protected void onSkyReady() { Intent intent = getIntent(); - if (Intent.ACTION_VIEW.equals(intent.getAction())) { - Uri skyUri = intent.getData(); - Uri httpsUri = skyUri.buildUpon().scheme("https").build(); - // This is a hack to disable https for local testing. - // getHost may be null if we're passed a non-normalized url. - if (skyUri.getHost() != null - && skyUri.getHost().equals("localhost")) { - httpsUri = skyUri.buildUpon().scheme("http").build(); - } - url = httpsUri.toString(); - } + String action = intent.getAction(); - loadUrl(url); + if (Intent.ACTION_MAIN.equals(action)) { + loadUrl(DEFAULT_URL); + } else if (Intent.ACTION_VIEW.equals(action)) { + loadUrl(intent.getDataString()); + } } } diff --git a/sdk/home.dart b/sdk/home.dart index c3c37f2a9..782648134 100644 --- a/sdk/home.dart +++ b/sdk/home.dart @@ -21,11 +21,14 @@ import 'package:sky/widgets/tool_bar.dart'; void launch(String relativeUrl) { Uri url = Uri.base.resolve(relativeUrl); - url = url.replace(scheme: 'sky'); ActivityManagerProxy activityManager = new ActivityManagerProxy.unbound(); + ComponentName component = new ComponentName() + ..packageName = 'org.domokit.sky.demo' + ..className = 'org.domokit.sky.demo.SkyDemoActivity'; Intent intent = new Intent() ..action = 'android.intent.action.VIEW' + ..component = component ..url = url.toString(); shell.requestService(null, activityManager); activityManager.ptr.startActivity(intent); diff --git a/services/intents/intents.mojom b/services/intents/intents.mojom index accaf7e35..33cdb9272 100644 --- a/services/intents/intents.mojom +++ b/services/intents/intents.mojom @@ -4,9 +4,15 @@ module intents; +struct ComponentName { + string package_name; + string class_name; +}; + struct Intent { string action; string url; + ComponentName? component; }; // TODO(abarth): This interface seems very specific to Android. Do we want to diff --git a/services/intents/src/org/domokit/intents/ActivityManagerImpl.java b/services/intents/src/org/domokit/intents/ActivityManagerImpl.java index ad9871ea2..51ca4ca29 100644 --- a/services/intents/src/org/domokit/intents/ActivityManagerImpl.java +++ b/services/intents/src/org/domokit/intents/ActivityManagerImpl.java @@ -11,6 +11,7 @@ import android.util.Log; import org.chromium.mojo.system.MojoException; import org.chromium.mojom.intents.ActivityManager; +import org.chromium.mojom.intents.ComponentName; import org.chromium.mojom.intents.Intent; /** @@ -34,6 +35,14 @@ public class ActivityManagerImpl implements ActivityManager { public void startActivity(Intent intent) { final android.content.Intent androidIntent = new android.content.Intent( intent.action, Uri.parse(intent.url)); + + if (intent.component != null) { + ComponentName component = intent.component; + android.content.ComponentName androidComponent = + new android.content.ComponentName(component.packageName, component.className); + androidIntent.setComponent(androidComponent); + } + androidIntent.addFlags(android.content.Intent.FLAG_ACTIVITY_NEW_TASK); try { diff --git a/shell/android/org/domokit/sky/shell/SkyActivity.java b/shell/android/org/domokit/sky/shell/SkyActivity.java index 84a08af06..0724ebf9a 100644 --- a/shell/android/org/domokit/sky/shell/SkyActivity.java +++ b/shell/android/org/domokit/sky/shell/SkyActivity.java @@ -41,7 +41,7 @@ public class SkyActivity extends Activity { setContentView(mView); mTracingController = new TracingController(this); - loadSnapshotIfAvailable(); + onSkyReady(); } /** @@ -65,7 +65,10 @@ public class SkyActivity extends Activity { super.onBackPressed(); } - private void loadSnapshotIfAvailable() { + /** + * Override this function to customize startup behavior. + */ + protected void onSkyReady() { File dataDir = new File(PathUtils.getDataDirectory(this)); File snapshot = new File(dataDir, SkyApplication.SNAPSHOT); if (snapshot.exists()) { diff --git a/tools/shelldb b/tools/shelldb index 0eb49187a..48420db3e 100755 --- a/tools/shelldb +++ b/tools/shelldb @@ -26,11 +26,12 @@ SRC_ROOT = os.path.dirname(SKY_ROOT) GDB_PORT = 8888 SKY_SERVER_PORT = 9888 OBSERVATORY_PORT = 8181 -DEFAULT_URL = "sky://domokit.github.io/home.dart" +DEFAULT_URL = "https://domokit.github.io/home.dart" APK_NAME = 'SkyDemo.apk' ADB_PATH = os.path.join(SRC_ROOT, 'third_party/android_tools/sdk/platform-tools/adb') ANDROID_PACKAGE = "org.domokit.sky.demo" +ANDROID_COMPONENT = '%s/%s.SkyDemoActivity' % (ANDROID_PACKAGE, ANDROID_PACKAGE) SHA1_PATH = '/sdcard/%s/%s.sha1' % (ANDROID_PACKAGE, APK_NAME) PID_FILE_PATH = "/tmp/skydemo.pids" @@ -104,12 +105,6 @@ class Pids(object): logging.warn('Failed to write pid file: %s' % path) -def _convert_to_sky_url(url): - parts = urlparse.urlsplit(url) - parts = parts._replace(scheme='sky') - return parts.geturl() - - # A free function for possible future sharing with a 'load' command. def _url_from_args(args, pids): if urlparse.urlparse(args.url_or_path).scheme: @@ -117,9 +112,8 @@ def _url_from_args(args, pids): # The load happens on the remote device, use the remote port. remote_sky_server_port = pids.get('remote_sky_server_port', pids['sky_server_port']) - url = SkyServer.url_for_path(remote_sky_server_port, + return SkyServer.url_for_path(remote_sky_server_port, pids['sky_server_root'], args.url_or_path) - return _convert_to_sky_url(url) def dev_packages_root(build_dir): @@ -235,11 +229,11 @@ class StartSky(object): ]) pids['remote_sky_server_port'] = sky_server.port - subprocess.check_call([ADB_PATH, 'shell', 'am', 'start', '-a', 'android.intent.action.VIEW', - '-d', _url_from_args(args, pids)]) + '-d', _url_from_args(args, pids), + ANDROID_COMPONENT]) if not args.gdb: return -- GitLab