Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.webapk.shell_apk; | 5 package org.chromium.webapk.lib.runtime_library; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.content.ActivityNotFoundException; |
| 8 import android.content.ComponentName; | 8 import android.content.ComponentName; |
| 9 import android.content.Context; | |
| 9 import android.content.Intent; | 10 import android.content.Intent; |
| 10 import android.content.pm.ApplicationInfo; | 11 import android.content.pm.ApplicationInfo; |
| 11 import android.content.pm.PackageManager; | 12 import android.content.pm.PackageManager; |
| 12 import android.content.pm.PackageManager.NameNotFoundException; | 13 import android.content.pm.PackageManager.NameNotFoundException; |
| 13 import android.graphics.Bitmap; | 14 import android.graphics.Bitmap; |
| 14 import android.graphics.BitmapFactory; | 15 import android.graphics.BitmapFactory; |
| 15 import android.os.Bundle; | 16 import android.os.Bundle; |
| 16 import android.text.TextUtils; | 17 import android.text.TextUtils; |
| 17 import android.util.Base64; | 18 import android.util.Base64; |
| 18 import android.util.Log; | 19 import android.util.Log; |
| 19 | 20 |
| 20 import org.chromium.webapk.lib.common.WebApkConstants; | 21 import org.chromium.webapk.lib.common.WebApkConstants; |
| 21 | 22 |
| 22 import java.io.ByteArrayOutputStream; | 23 import java.io.ByteArrayOutputStream; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 * WebAPK's main Activity. | 26 * Launches Chrome in WebAPK mode. |
| 26 */ | 27 */ |
| 27 public class MainActivity extends Activity { | 28 public class HostBrowserLauncher { |
| 28 // These EXTRA_* values must stay in sync with | 29 // These EXTRA_* values must stay in sync with |
| 29 // {@link org.chromium.chrome.browser.ShortcutHelper}. | 30 // {@link org.chromium.chrome.browser.ShortcutHelper}. |
| 30 private static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_i d"; | 31 private static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_i d"; |
| 31 private static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp _icon"; | 32 private static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp _icon"; |
| 32 private static final String EXTRA_SHORT_NAME = "org.chromium.chrome.browser. webapp_short_name"; | 33 private static final String EXTRA_SHORT_NAME = "org.chromium.chrome.browser. webapp_short_name"; |
| 33 private static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp _name"; | 34 private static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp _name"; |
| 34 private static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_ url"; | 35 private static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_ url"; |
| 35 private static final String EXTRA_SOURCE = "org.chromium.chrome.browser.weba pp_source"; | 36 private static final String EXTRA_SOURCE = "org.chromium.chrome.browser.weba pp_source"; |
| 36 private static final String EXTRA_THEME_COLOR = "org.chromium.chrome.browser .theme_color"; | 37 private static final String EXTRA_THEME_COLOR = "org.chromium.chrome.browser .theme_color"; |
| 37 private static final String EXTRA_BACKGROUND_COLOR = | 38 private static final String EXTRA_BACKGROUND_COLOR = |
| 38 "org.chromium.chrome.browser.background_color"; | 39 "org.chromium.chrome.browser.background_color"; |
| 39 private static final String EXTRA_IS_ICON_GENERATED = | 40 private static final String EXTRA_IS_ICON_GENERATED = |
| 40 "org.chromium.chrome.browser.is_icon_generated"; | 41 "org.chromium.chrome.browser.is_icon_generated"; |
| 41 private static final String EXTRA_WEBAPK_PACKAGE_NAME = | 42 private static final String EXTRA_WEBAPK_PACKAGE_NAME = |
| 42 "org.chromium.chrome.browser.webapk_package_name"; | 43 "org.chromium.chrome.browser.webapk_package_name"; |
| 43 | 44 |
| 44 private static final String META_DATA_RUNTIME_HOST = "runtimeHost"; | 45 private static final String META_DATA_RUNTIME_HOST = "runtimeHost"; |
| 45 private static final String META_DATA_START_URL = "startUrl"; | 46 private static final String META_DATA_START_URL = "startUrl"; |
| 46 private static final String META_DATA_NAME = "name"; | 47 private static final String META_DATA_NAME = "name"; |
| 47 private static final String META_DATA_DISPLAY_MODE = "displayMode"; | 48 private static final String META_DATA_DISPLAY_MODE = "displayMode"; |
| 48 private static final String META_DATA_ORIENTATION = "orientation"; | 49 private static final String META_DATA_ORIENTATION = "orientation"; |
| 49 private static final String META_DATA_THEME_COLOR = "themeColor"; | 50 private static final String META_DATA_THEME_COLOR = "themeColor"; |
| 50 private static final String META_DATA_BACKGROUND_COLOR = "backgroundColor"; | 51 private static final String META_DATA_BACKGROUND_COLOR = "backgroundColor"; |
| 51 private static final String META_DATA_ICON_URL = "iconUrl"; | 52 private static final String META_DATA_ICON_URL = "iconUrl"; |
| 52 | 53 |
| 53 private static final String TAG = "cr_MainActivity"; | 54 /** |
| 55 * Key for passing app icon id in Bundle to {@link #launch()}. | |
| 56 */ | |
| 57 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.
| |
| 54 | 58 |
| 55 @Override | 59 private static final String TAG = "cr_HostBrowserLauncher"; |
| 56 protected void onCreate(Bundle savedInstanceState) { | |
| 57 super.onCreate(savedInstanceState); | |
| 58 | 60 |
| 59 String packageName = getPackageName(); | 61 /** |
| 62 * Launches Chrome in WebAPK mode. | |
| 63 * @param context Application context. | |
| 64 * @param intent Intent used to launch WebAPK. | |
| 65 * @param bundle Contains extra parameters. | |
| 66 */ | |
| 67 public void launch(Context context, Intent intent, Bundle bundle) { | |
| 68 int appIconId = bundle.getInt(KEY_APP_ICON_ID); | |
| 69 | |
| 70 String packageName = context.getPackageName(); | |
| 71 ApplicationInfo appInfo; | |
| 60 try { | 72 try { |
| 61 ApplicationInfo appInfo = getPackageManager().getApplicationInfo( | 73 appInfo = context.getPackageManager().getApplicationInfo( |
| 62 packageName, PackageManager.GET_META_DATA); | 74 packageName, PackageManager.GET_META_DATA); |
| 63 Bundle bundle = appInfo.metaData; | 75 } catch (NameNotFoundException e) { |
| 64 String url = bundle.getString(META_DATA_START_URL); | 76 return; |
| 77 } | |
| 65 | 78 |
| 66 Intent intent = getIntent(); | 79 Bundle metaBundle = appInfo.metaData; |
| 67 String overrideUrl = intent.getDataString(); | 80 String url = metaBundle.getString(META_DATA_START_URL); |
| 68 // TODO(pkotwicz): Use same logic as {@code IntentHandler#shouldIgno reIntent()} | |
| 69 if (overrideUrl != null && overrideUrl.startsWith("https:")) { | |
| 70 url = overrideUrl; | |
| 71 } | |
| 72 int source = intent.getIntExtra(EXTRA_SOURCE, 0); | |
| 73 | 81 |
| 74 String webappId = WebApkConstants.WEBAPK_ID_PREFIX + packageName; | 82 String overrideUrl = intent.getDataString(); |
| 75 String runtimeHost = bundle.getString(META_DATA_RUNTIME_HOST); | 83 // TODO(pkotwicz): Use same logic as {@code IntentHandler#shouldIgnoreIn tent()} |
| 76 String shortName = (String) getPackageManager().getApplicationLabel( appInfo); | 84 if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
| 77 // TODO(hanxi): find a neat solution to avoid encode/decode each tim e launch the | 85 url = overrideUrl; |
| 78 // activity. | 86 } |
| 79 Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawabl e.app_icon); | 87 int source = intent.getIntExtra(EXTRA_SOURCE, 0); |
| 80 String encodedIcon = encodeBitmapAsString(icon); | |
| 81 String name = bundle.getString(META_DATA_NAME); | |
| 82 String displayMode = bundle.getString(META_DATA_DISPLAY_MODE); | |
| 83 String orientation = bundle.getString(META_DATA_ORIENTATION); | |
| 84 long themeColor = getLongFromBundle(bundle, META_DATA_THEME_COLOR); | |
| 85 long backgroundColor = getLongFromBundle(bundle, META_DATA_BACKGROUN D_COLOR); | |
| 86 boolean isIconGenerated = TextUtils.isEmpty(bundle.getString(META_DA TA_ICON_URL)); | |
| 87 Log.w(TAG, "Url of the WebAPK: " + url); | |
| 88 Log.w(TAG, "WebappId of the WebAPK: " + webappId); | |
| 89 Log.w(TAG, "Name of the WebAPK:" + name); | |
| 90 Log.w(TAG, "Package name of the WebAPK:" + packageName); | |
| 91 | 88 |
| 92 Intent newIntent = new Intent(); | 89 String webappId = WebApkConstants.WEBAPK_ID_PREFIX + packageName; |
| 93 newIntent.setComponent(new ComponentName(runtimeHost, | 90 String runtimeHost = metaBundle.getString(META_DATA_RUNTIME_HOST); |
| 94 "org.chromium.chrome.browser.webapps.WebappLauncherActivity" )); | 91 String shortName = (String) context.getPackageManager().getApplicationLa bel(appInfo); |
| 95 newIntent.putExtra(EXTRA_ID, webappId) | 92 // TODO(hanxi): find a neat solution to avoid encode/decode each time la unch the |
| 96 .putExtra(EXTRA_SHORT_NAME, shortName) | 93 // activity. |
| 97 .putExtra(EXTRA_NAME, name) | 94 Bitmap icon = BitmapFactory.decodeResource(context.getResources(), appIc onId); |
| 98 .putExtra(EXTRA_URL, url) | 95 String encodedIcon = encodeBitmapAsString(icon); |
| 99 .putExtra(EXTRA_ICON, encodedIcon) | 96 String name = metaBundle.getString(META_DATA_NAME); |
| 100 .putExtra(EXTRA_SOURCE, source) | 97 String displayMode = metaBundle.getString(META_DATA_DISPLAY_MODE); |
| 101 .putExtra(EXTRA_THEME_COLOR, themeColor) | 98 String orientation = metaBundle.getString(META_DATA_ORIENTATION); |
| 102 .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) | 99 long themeColor = getLongFromBundle(metaBundle, META_DATA_THEME_COLOR); |
| 103 .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated) | 100 long backgroundColor = getLongFromBundle(metaBundle, META_DATA_BACKGROUN D_COLOR); |
| 104 .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName) | 101 boolean isIconGenerated = TextUtils.isEmpty(metaBundle.getString(META_DA TA_ICON_URL)); |
| 105 .putExtra(WebApkConstants.EXTRA_DISPLAY_MODE, displayMode) | 102 Log.w(TAG, "Url of the WebAPK: " + url); |
| 106 .putExtra(WebApkConstants.EXTRA_ORIENTATION, orientation); | 103 Log.w(TAG, "WebappId of the WebAPK: " + webappId); |
| 107 startActivity(newIntent); | 104 Log.w(TAG, "Name of the WebAPK:" + name); |
| 108 finish(); | 105 Log.w(TAG, "Package name of the WebAPK:" + packageName); |
| 109 } catch (NameNotFoundException e) { | 106 |
| 107 Intent newIntent = new Intent(); | |
| 108 newIntent.setComponent(new ComponentName( | |
| 109 runtimeHost, "org.chromium.chrome.browser.webapps.WebappLauncher Activity")); | |
| 110 newIntent.putExtra(EXTRA_ID, webappId) | |
| 111 .putExtra(EXTRA_SHORT_NAME, shortName) | |
| 112 .putExtra(EXTRA_NAME, name) | |
| 113 .putExtra(EXTRA_URL, url) | |
| 114 .putExtra(EXTRA_ICON, encodedIcon) | |
| 115 .putExtra(EXTRA_SOURCE, source) | |
| 116 .putExtra(EXTRA_THEME_COLOR, themeColor) | |
| 117 .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) | |
| 118 .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated) | |
| 119 .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName) | |
| 120 .putExtra(WebApkConstants.EXTRA_DISPLAY_MODE, displayMode) | |
| 121 .putExtra(WebApkConstants.EXTRA_ORIENTATION, orientation); | |
| 122 | |
| 123 try { | |
| 124 context.startActivity(newIntent); | |
| 125 } catch (ActivityNotFoundException e) { | |
| 110 e.printStackTrace(); | 126 e.printStackTrace(); |
| 111 } | 127 } |
| 112 } | 128 } |
| 113 | 129 |
| 114 /** | 130 /** |
| 115 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. | 131 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. |
| 116 * The encoded string can be decoded using {@link decodeBitmapFromString(Str ing)}. | 132 * The encoded string can be decoded using {@link decodeBitmapFromString(Str ing)}. |
| 117 * @param bitmap The Bitmap to compress and encode. | 133 * @param bitmap The Bitmap to compress and encode. |
| 118 * @return the String encoding the Bitmap. | 134 * @return the String encoding the Bitmap. |
| 119 */ | 135 */ |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 133 if (value == null || !value.endsWith("L")) { | 149 if (value == null || !value.endsWith("L")) { |
| 134 return 0; | 150 return 0; |
| 135 } | 151 } |
| 136 try { | 152 try { |
| 137 return Long.parseLong(value.substring(0, value.length() - 1)); | 153 return Long.parseLong(value.substring(0, value.length() - 1)); |
| 138 } catch (NumberFormatException e) { | 154 } catch (NumberFormatException e) { |
| 139 } | 155 } |
| 140 return 0; | 156 return 0; |
| 141 } | 157 } |
| 142 } | 158 } |
| OLD | NEW |