From 5330671732ac2b6e794b619c97060107fe190df3 Mon Sep 17 00:00:00 2001 From: Myk Melez Date: Fri, 28 Mar 2014 11:27:46 -0700 Subject: [PATCH] Bug 982559 - use intent data to start app from legacy shorcut; r=wesj --- mobile/android/base/webapp/WebappImpl.java | 104 +++++++++++++++------ 1 file changed, 73 insertions(+), 31 deletions(-) diff --git a/mobile/android/base/webapp/WebappImpl.java b/mobile/android/base/webapp/WebappImpl.java index 83706b4deef..0cdefd90db2 100644 --- a/mobile/android/base/webapp/WebappImpl.java +++ b/mobile/android/base/webapp/WebappImpl.java @@ -51,7 +51,10 @@ public class WebappImpl extends GeckoApp implements InstallCallback { private View mSplashscreen; + private boolean mIsApk = true; private ApkResources mApkResources; + private String mManifestUrl; + private String mAppName; protected int getIndex() { return 0; } @@ -79,18 +82,36 @@ public class WebappImpl extends GeckoApp implements InstallCallback { String packageName = extras.getString("packageName"); if (packageName == null) { - // TODO Migration path! - Log.w(LOGTAG, "Can't find package name for webapp"); - setResult(RESULT_CANCELED); - finish(); - } + Log.w(LOGTAG, "no package name; treating as legacy shortcut"); - try { - mApkResources = new ApkResources(this, packageName); - } catch (NameNotFoundException e) { - Log.e(LOGTAG, "Can't find package for webapp " + packageName, e); - setResult(RESULT_CANCELED); - finish(); + mIsApk = false; + + // Shortcut apps are already installed. + isInstalled = true; + + Uri data = getIntent().getData(); + if (data == null) { + Log.wtf(LOGTAG, "can't get manifest URL from shortcut data"); + setResult(RESULT_CANCELED); + finish(); + return; + } + mManifestUrl = data.toString(); + + String shortcutName = extras.getString(Intent.EXTRA_SHORTCUT_NAME); + mAppName = shortcutName != null ? shortcutName : "Web App"; + } else { + try { + mApkResources = new ApkResources(this, packageName); + } catch (NameNotFoundException e) { + Log.e(LOGTAG, "Can't find package for webapp " + packageName, e); + setResult(RESULT_CANCELED); + finish(); + return; + } + + mManifestUrl = mApkResources.getManifestUrl(); + mAppName = mApkResources.getAppName(); } // start Gecko. @@ -113,7 +134,7 @@ public class WebappImpl extends GeckoApp implements InstallCallback { if (!GeckoThread.checkLaunchState(GeckoThread.LaunchState.GeckoRunning) || !isInstalled || isInstallCompleting) { // Show the splash screen if we need to start Gecko, or we need to install this. overridePendingTransition(R.anim.grow_fade_in_center, android.R.anim.fade_out); - showSplash(true); + showSplash(); } else { mSplashscreen.setVisibility(View.GONE); } @@ -134,10 +155,10 @@ public class WebappImpl extends GeckoApp implements InstallCallback { } return; } else { - launchWebapp(origin, mApkResources.getManifestUrl(), mApkResources.getAppName()); + launchWebapp(origin); } - setTitle(mApkResources.getAppName()); + setTitle(mAppName); } @Override @@ -150,7 +171,15 @@ public class WebappImpl extends GeckoApp implements InstallCallback { // the synthesized APK. // TODO Translate AndroidIntents into WebActivities here. - return mApkResources.getManifestUrl(); + if (mIsApk) { + return mApkResources.getManifestUrl(); + } + + // If this is a legacy shortcut, then we should have been able to get + // the URI from the intent data. Otherwise, we should have been able + // to get it from the APK resources. So we should never get here. + Log.wtf(LOGTAG, "Couldn't get URI from intent nor APK resources"); + return null; } @Override @@ -158,7 +187,7 @@ public class WebappImpl extends GeckoApp implements InstallCallback { // NOP } - private void showSplash(boolean isApk) { + private void showSplash() { // get the favicon dominant color, stored when the app was installed int dominantColor = Allocator.getInstance().getColor(getIndex()); @@ -168,7 +197,7 @@ public class WebappImpl extends GeckoApp implements InstallCallback { ImageView image = (ImageView)findViewById(R.id.splashscreen_icon); Drawable d = null; - if (isApk) { + if (mIsApk) { Uri uri = mApkResources.getAppIconUri(); image.setImageURI(uri); d = image.getDrawable(); @@ -299,7 +328,7 @@ public class WebappImpl extends GeckoApp implements InstallCallback { if (event.equals("Webapps:Postinstall")) { String origin = message.optString("origin"); - launchWebapp(origin, mApkResources.getManifestUrl(), mApkResources.getAppName()); + launchWebapp(origin); } } @@ -308,30 +337,37 @@ public class WebappImpl extends GeckoApp implements InstallCallback { Log.e(LOGTAG, "Install errored", exception); } - public void launchWebapp(String origin, String manifestUrl, String name) { + private void setOrigin(String origin) { try { mOrigin = new URL(origin); } catch (java.net.MalformedURLException ex) { - // If we can't parse the this is an app protocol, just settle for not having an origin + // If this isn't an app: URL, just settle for not having an origin. if (!origin.startsWith("app://")) { return; } - // If that failed fall back to the origin stored in the shortcut - Log.i(LOGTAG, "Webapp is not registered with allocator"); - Uri data = getIntent().getData(); - if (data != null) { - try { - mOrigin = new URL(data.toString()); - } catch (java.net.MalformedURLException ex2) { - Log.e(LOGTAG, "Unable to parse intent url: ", ex); + // If that failed fall back to the origin stored in the shortcut. + if (!mIsApk) { + Log.i(LOGTAG, "Origin is app: URL; falling back to intent URL"); + Uri data = getIntent().getData(); + if (data != null) { + try { + mOrigin = new URL(data.toString()); + } catch (java.net.MalformedURLException ex2) { + Log.e(LOGTAG, "Unable to parse intent URL: ", ex); + } } } } + } + + public void launchWebapp(String origin) { + setOrigin(origin); + try { JSONObject launchObject = new JSONObject(); - launchObject.putOpt("url", manifestUrl); - launchObject.putOpt("name", mApkResources.getAppName()); + launchObject.putOpt("url", mManifestUrl); + launchObject.putOpt("name", mAppName); Log.i(LOGTAG, "Trying to launch: " + launchObject); GeckoAppShell.sendEventToGecko(GeckoEvent.createBroadcastEvent("Webapps:Load", launchObject.toString())); } catch (JSONException e) { @@ -341,6 +377,12 @@ public class WebappImpl extends GeckoApp implements InstallCallback { @Override protected boolean getIsDebuggable() { - return mApkResources.isDebuggable(); + if (mIsApk) { + return mApkResources.isDebuggable(); + } + + // This is a legacy shortcut, which didn't provide a way to determine + // that the app is debuggable, so we say the app is not debuggable. + return false; } }