Chromium Code Reviews| Index: chrome/android/webapk/libs/runtime_library/src/org/chromium/webapk/lib/runtime_library/HostBrowserLauncher.java |
| diff --git a/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/MainActivity.java b/chrome/android/webapk/libs/runtime_library/src/org/chromium/webapk/lib/runtime_library/HostBrowserLauncher.java |
| similarity index 52% |
| copy from chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/MainActivity.java |
| copy to chrome/android/webapk/libs/runtime_library/src/org/chromium/webapk/lib/runtime_library/HostBrowserLauncher.java |
| index b9d482adbc4a22219e85a10a3586c27c4195b3da..abf93a0a4d913e43ba6105923d1737109a6ae29e 100644 |
| --- a/chrome/android/webapk/shell_apk/src/org/chromium/webapk/shell_apk/MainActivity.java |
| +++ b/chrome/android/webapk/libs/runtime_library/src/org/chromium/webapk/lib/runtime_library/HostBrowserLauncher.java |
| @@ -2,10 +2,11 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| -package org.chromium.webapk.shell_apk; |
| +package org.chromium.webapk.lib.runtime_library; |
| -import android.app.Activity; |
| +import android.content.ActivityNotFoundException; |
| import android.content.ComponentName; |
| +import android.content.Context; |
| import android.content.Intent; |
| import android.content.pm.ApplicationInfo; |
| import android.content.pm.PackageManager; |
| @@ -22,9 +23,9 @@ import org.chromium.webapk.lib.common.WebApkConstants; |
| import java.io.ByteArrayOutputStream; |
| /** |
| - * WebAPK's main Activity. |
| + * Launches Chrome in WebAPK mode. |
| */ |
| -public class MainActivity extends Activity { |
| +public class HostBrowserLauncher { |
| // These EXTRA_* values must stay in sync with |
| // {@link org.chromium.chrome.browser.ShortcutHelper}. |
| private static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_id"; |
| @@ -50,63 +51,78 @@ public class MainActivity extends Activity { |
| private static final String META_DATA_BACKGROUND_COLOR = "backgroundColor"; |
| private static final String META_DATA_ICON_URL = "iconUrl"; |
| - private static final String TAG = "cr_MainActivity"; |
| + /** |
| + * Key for passing app icon id in Bundle to {@link #launch()}. |
| + */ |
| + private static final String KEY_APP_ICON_ID = "app_icon_id"; |
|
Xi Han
2016/06/27 18:27:08
How about to put it in the WebApkConstants? So we
pkotwicz
2016/06/27 20:44:28
I want to keep libs/common as small as possible. l
Xi Han
2016/06/28 13:53:39
It makes sense.
|
| - @Override |
| - protected void onCreate(Bundle savedInstanceState) { |
| - super.onCreate(savedInstanceState); |
| + private static final String TAG = "cr_HostBrowserLauncher"; |
| - String packageName = getPackageName(); |
| + /** |
| + * Launches Chrome in WebAPK mode. |
| + * @param context Application context. |
| + * @param intent Intent used to launch WebAPK. |
| + * @param bundle Contains extra parameters. |
| + */ |
| + public void launch(Context context, Intent intent, Bundle bundle) { |
| + int appIconId = bundle.getInt(KEY_APP_ICON_ID); |
| + |
| + String packageName = context.getPackageName(); |
| + ApplicationInfo appInfo; |
| try { |
| - ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| + appInfo = context.getPackageManager().getApplicationInfo( |
| packageName, PackageManager.GET_META_DATA); |
| - Bundle bundle = appInfo.metaData; |
| - String url = bundle.getString(META_DATA_START_URL); |
| - |
| - Intent intent = getIntent(); |
| - String overrideUrl = intent.getDataString(); |
| - // TODO(pkotwicz): Use same logic as {@code IntentHandler#shouldIgnoreIntent()} |
| - if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
| - url = overrideUrl; |
| - } |
| - int source = intent.getIntExtra(EXTRA_SOURCE, 0); |
| - |
| - String webappId = WebApkConstants.WEBAPK_ID_PREFIX + packageName; |
| - String runtimeHost = bundle.getString(META_DATA_RUNTIME_HOST); |
| - String shortName = (String) getPackageManager().getApplicationLabel(appInfo); |
| - // TODO(hanxi): find a neat solution to avoid encode/decode each time launch the |
| - // activity. |
| - Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.app_icon); |
| - String encodedIcon = encodeBitmapAsString(icon); |
| - String name = bundle.getString(META_DATA_NAME); |
| - String displayMode = bundle.getString(META_DATA_DISPLAY_MODE); |
| - String orientation = bundle.getString(META_DATA_ORIENTATION); |
| - long themeColor = getLongFromBundle(bundle, META_DATA_THEME_COLOR); |
| - long backgroundColor = getLongFromBundle(bundle, META_DATA_BACKGROUND_COLOR); |
| - boolean isIconGenerated = TextUtils.isEmpty(bundle.getString(META_DATA_ICON_URL)); |
| - Log.w(TAG, "Url of the WebAPK: " + url); |
| - Log.w(TAG, "WebappId of the WebAPK: " + webappId); |
| - Log.w(TAG, "Name of the WebAPK:" + name); |
| - Log.w(TAG, "Package name of the WebAPK:" + packageName); |
| - |
| - Intent newIntent = new Intent(); |
| - newIntent.setComponent(new ComponentName(runtimeHost, |
| - "org.chromium.chrome.browser.webapps.WebappLauncherActivity")); |
| - newIntent.putExtra(EXTRA_ID, webappId) |
| - .putExtra(EXTRA_SHORT_NAME, shortName) |
| - .putExtra(EXTRA_NAME, name) |
| - .putExtra(EXTRA_URL, url) |
| - .putExtra(EXTRA_ICON, encodedIcon) |
| - .putExtra(EXTRA_SOURCE, source) |
| - .putExtra(EXTRA_THEME_COLOR, themeColor) |
| - .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) |
| - .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated) |
| - .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName) |
| - .putExtra(WebApkConstants.EXTRA_DISPLAY_MODE, displayMode) |
| - .putExtra(WebApkConstants.EXTRA_ORIENTATION, orientation); |
| - startActivity(newIntent); |
| - finish(); |
| } catch (NameNotFoundException e) { |
| + return; |
| + } |
| + |
| + Bundle metaBundle = appInfo.metaData; |
| + String url = metaBundle.getString(META_DATA_START_URL); |
| + |
| + String overrideUrl = intent.getDataString(); |
| + // TODO(pkotwicz): Use same logic as {@code IntentHandler#shouldIgnoreIntent()} |
| + if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
| + url = overrideUrl; |
| + } |
| + int source = intent.getIntExtra(EXTRA_SOURCE, 0); |
| + |
| + String webappId = WebApkConstants.WEBAPK_ID_PREFIX + packageName; |
| + String runtimeHost = metaBundle.getString(META_DATA_RUNTIME_HOST); |
| + String shortName = (String) context.getPackageManager().getApplicationLabel(appInfo); |
| + // TODO(hanxi): find a neat solution to avoid encode/decode each time launch the |
| + // activity. |
| + Bitmap icon = BitmapFactory.decodeResource(context.getResources(), appIconId); |
| + String encodedIcon = encodeBitmapAsString(icon); |
| + String name = metaBundle.getString(META_DATA_NAME); |
| + String displayMode = metaBundle.getString(META_DATA_DISPLAY_MODE); |
| + String orientation = metaBundle.getString(META_DATA_ORIENTATION); |
| + long themeColor = getLongFromBundle(metaBundle, META_DATA_THEME_COLOR); |
| + long backgroundColor = getLongFromBundle(metaBundle, META_DATA_BACKGROUND_COLOR); |
| + boolean isIconGenerated = TextUtils.isEmpty(metaBundle.getString(META_DATA_ICON_URL)); |
| + Log.w(TAG, "Url of the WebAPK: " + url); |
| + Log.w(TAG, "WebappId of the WebAPK: " + webappId); |
| + Log.w(TAG, "Name of the WebAPK:" + name); |
| + Log.w(TAG, "Package name of the WebAPK:" + packageName); |
| + |
| + Intent newIntent = new Intent(); |
| + newIntent.setComponent(new ComponentName( |
| + runtimeHost, "org.chromium.chrome.browser.webapps.WebappLauncherActivity")); |
| + newIntent.putExtra(EXTRA_ID, webappId) |
| + .putExtra(EXTRA_SHORT_NAME, shortName) |
| + .putExtra(EXTRA_NAME, name) |
| + .putExtra(EXTRA_URL, url) |
| + .putExtra(EXTRA_ICON, encodedIcon) |
| + .putExtra(EXTRA_SOURCE, source) |
| + .putExtra(EXTRA_THEME_COLOR, themeColor) |
| + .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) |
| + .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated) |
| + .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName) |
| + .putExtra(WebApkConstants.EXTRA_DISPLAY_MODE, displayMode) |
| + .putExtra(WebApkConstants.EXTRA_ORIENTATION, orientation); |
| + |
| + try { |
| + context.startActivity(newIntent); |
| + } catch (ActivityNotFoundException e) { |
| e.printStackTrace(); |
| } |
| } |