| 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 50%
|
| 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 4e177e0ca9122bdd41dacb5a92e2d5fa616facd8..ace63e44cdd6362a3df915c97a74cc24a0ee380c 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,66 +51,82 @@ 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";
|
|
|
| - @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.v(TAG, "Url of the WebAPK: " + url);
|
| - Log.v(TAG, "WebappId of the WebAPK: " + webappId);
|
| - Log.v(TAG, "Name of the WebAPK:" + name);
|
| - Log.v(TAG, "Package name of the WebAPK:" + packageName);
|
| -
|
| - Intent newIntent = new Intent();
|
| - newIntent.setComponent(new ComponentName(runtimeHost,
|
| - "org.chromium.chrome.browser.webapps.WebappLauncherActivity"));
|
| - // Chrome expects the ShortcutHelper.EXTRA_DISPLAY_MODE and
|
| - // ShortcutHelper.EXTRA_ORIENTATION extras to be enum values. We send string extras for
|
| - // the display mode and orientation so have to use different keys.
|
| - 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_WEBAPK_DISPLAY_MODE, displayMode)
|
| - .putExtra(WebApkConstants.EXTRA_WEBAPK_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.v(TAG, "Url of the WebAPK: " + url);
|
| + Log.v(TAG, "WebappId of the WebAPK: " + webappId);
|
| + Log.v(TAG, "Name of the WebAPK:" + name);
|
| + Log.v(TAG, "Package name of the WebAPK:" + packageName);
|
| +
|
| + Intent newIntent = new Intent();
|
| + newIntent.setComponent(new ComponentName(
|
| + runtimeHost, "org.chromium.chrome.browser.webapps.WebappLauncherActivity"));
|
| +
|
| + // Chrome expects the ShortcutHelper.EXTRA_DISPLAY_MODE and
|
| + // ShortcutHelper.EXTRA_ORIENTATION extras to be enum values. We send string extras for
|
| + // the display mode and orientation so have to use different keys.
|
| + 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_WEBAPK_DISPLAY_MODE, displayMode)
|
| + .putExtra(WebApkConstants.EXTRA_WEBAPK_ORIENTATION, orientation);
|
| +
|
| + try {
|
| + context.startActivity(newIntent);
|
| + } catch (ActivityNotFoundException e) {
|
| e.printStackTrace();
|
| }
|
| }
|
|
|