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"; |
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.v(TAG, "Url of the WebAPK: " + url); | |
88 Log.v(TAG, "WebappId of the WebAPK: " + webappId); | |
89 Log.v(TAG, "Name of the WebAPK:" + name); | |
90 Log.v(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 // Chrome expects the ShortcutHelper.EXTRA_DISPLAY_MODE and | 92 // TODO(hanxi): find a neat solution to avoid encode/decode each time la
unch the |
96 // ShortcutHelper.EXTRA_ORIENTATION extras to be enum values. We sen
d string extras for | 93 // activity. |
97 // the display mode and orientation so have to use different keys. | 94 Bitmap icon = BitmapFactory.decodeResource(context.getResources(), appIc
onId); |
98 newIntent.putExtra(EXTRA_ID, webappId) | 95 String encodedIcon = encodeBitmapAsString(icon); |
99 .putExtra(EXTRA_SHORT_NAME, shortName) | 96 String name = metaBundle.getString(META_DATA_NAME); |
100 .putExtra(EXTRA_NAME, name) | 97 String displayMode = metaBundle.getString(META_DATA_DISPLAY_MODE); |
101 .putExtra(EXTRA_URL, url) | 98 String orientation = metaBundle.getString(META_DATA_ORIENTATION); |
102 .putExtra(EXTRA_ICON, encodedIcon) | 99 long themeColor = getLongFromBundle(metaBundle, META_DATA_THEME_COLOR); |
103 .putExtra(EXTRA_SOURCE, source) | 100 long backgroundColor = getLongFromBundle(metaBundle, META_DATA_BACKGROUN
D_COLOR); |
104 .putExtra(EXTRA_THEME_COLOR, themeColor) | 101 boolean isIconGenerated = TextUtils.isEmpty(metaBundle.getString(META_DA
TA_ICON_URL)); |
105 .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) | 102 Log.v(TAG, "Url of the WebAPK: " + url); |
106 .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated) | 103 Log.v(TAG, "WebappId of the WebAPK: " + webappId); |
107 .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName) | 104 Log.v(TAG, "Name of the WebAPK:" + name); |
108 .putExtra(WebApkConstants.EXTRA_WEBAPK_DISPLAY_MODE, displa
yMode) | 105 Log.v(TAG, "Package name of the WebAPK:" + packageName); |
109 .putExtra(WebApkConstants.EXTRA_WEBAPK_ORIENTATION, orienta
tion); | 106 |
110 startActivity(newIntent); | 107 Intent newIntent = new Intent(); |
111 finish(); | 108 newIntent.setComponent(new ComponentName( |
112 } catch (NameNotFoundException e) { | 109 runtimeHost, "org.chromium.chrome.browser.webapps.WebappLauncher
Activity")); |
| 110 |
| 111 // Chrome expects the ShortcutHelper.EXTRA_DISPLAY_MODE and |
| 112 // ShortcutHelper.EXTRA_ORIENTATION extras to be enum values. We send st
ring extras for |
| 113 // the display mode and orientation so have to use different keys. |
| 114 newIntent.putExtra(EXTRA_ID, webappId) |
| 115 .putExtra(EXTRA_SHORT_NAME, shortName) |
| 116 .putExtra(EXTRA_NAME, name) |
| 117 .putExtra(EXTRA_URL, url) |
| 118 .putExtra(EXTRA_ICON, encodedIcon) |
| 119 .putExtra(EXTRA_SOURCE, source) |
| 120 .putExtra(EXTRA_THEME_COLOR, themeColor) |
| 121 .putExtra(EXTRA_BACKGROUND_COLOR, backgroundColor) |
| 122 .putExtra(EXTRA_IS_ICON_GENERATED, isIconGenerated) |
| 123 .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName) |
| 124 .putExtra(WebApkConstants.EXTRA_WEBAPK_DISPLAY_MODE, displayMode
) |
| 125 .putExtra(WebApkConstants.EXTRA_WEBAPK_ORIENTATION, orientation)
; |
| 126 |
| 127 try { |
| 128 context.startActivity(newIntent); |
| 129 } catch (ActivityNotFoundException e) { |
113 e.printStackTrace(); | 130 e.printStackTrace(); |
114 } | 131 } |
115 } | 132 } |
116 | 133 |
117 /** | 134 /** |
118 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. | 135 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. |
119 * The encoded string can be decoded using {@link decodeBitmapFromString(Str
ing)}. | 136 * The encoded string can be decoded using {@link decodeBitmapFromString(Str
ing)}. |
120 * @param bitmap The Bitmap to compress and encode. | 137 * @param bitmap The Bitmap to compress and encode. |
121 * @return the String encoding the Bitmap. | 138 * @return the String encoding the Bitmap. |
122 */ | 139 */ |
(...skipping 13 matching lines...) Expand all Loading... |
136 if (value == null || !value.endsWith("L")) { | 153 if (value == null || !value.endsWith("L")) { |
137 return 0; | 154 return 0; |
138 } | 155 } |
139 try { | 156 try { |
140 return Long.parseLong(value.substring(0, value.length() - 1)); | 157 return Long.parseLong(value.substring(0, value.length() - 1)); |
141 } catch (NumberFormatException e) { | 158 } catch (NumberFormatException e) { |
142 } | 159 } |
143 return 0; | 160 return 0; |
144 } | 161 } |
145 } | 162 } |
OLD | NEW |