| 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.shell_apk; |
| 6 | 6 |
| 7 import android.app.Activity; | 7 import android.app.Activity; |
| 8 import android.content.ComponentName; | 8 import android.content.ComponentName; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.pm.ApplicationInfo; | 10 import android.content.pm.ApplicationInfo; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 /** | 23 /** |
| 24 * WebAPK's main Activity. | 24 * WebAPK's main Activity. |
| 25 */ | 25 */ |
| 26 public class MainActivity extends Activity { | 26 public class MainActivity extends Activity { |
| 27 // These EXTRA_* values must stay in sync with | 27 // These EXTRA_* values must stay in sync with |
| 28 // {@link org.chromium.chrome.browser.ShortcutHelper}. | 28 // {@link org.chromium.chrome.browser.ShortcutHelper}. |
| 29 private static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_i
d"; | 29 private static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_i
d"; |
| 30 private static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp
_icon"; | 30 private static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp
_icon"; |
| 31 private static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp
_name"; | 31 private static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp
_name"; |
| 32 private static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_
url"; | 32 private static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_
url"; |
| 33 private static final String EXTRA_MAC = "org.chromium.chrome.browser.webapp_
mac"; | |
| 34 private static final String EXTRA_WEBAPK_PACKAGE_NAME = | 33 private static final String EXTRA_WEBAPK_PACKAGE_NAME = |
| 35 "org.chromium.chrome.browser.webapk_package_name"; | 34 "org.chromium.chrome.browser.webapk_package_name"; |
| 36 | 35 |
| 37 private static final String META_DATA_HOST_URL = "hostUrl"; | 36 private static final String META_DATA_HOST_URL = "hostUrl"; |
| 38 private static final String META_DATA_MAC = "mac"; | |
| 39 private static final String META_DATA_RUNTIME_HOST = "runtimeHost"; | 37 private static final String META_DATA_RUNTIME_HOST = "runtimeHost"; |
| 40 | 38 |
| 41 private static final String TAG = "cr_MainActivity"; | 39 private static final String TAG = "cr_MainActivity"; |
| 42 | 40 |
| 43 @Override | 41 @Override |
| 44 protected void onCreate(Bundle savedInstanceState) { | 42 protected void onCreate(Bundle savedInstanceState) { |
| 45 super.onCreate(savedInstanceState); | 43 super.onCreate(savedInstanceState); |
| 46 | 44 |
| 47 String packageName = getPackageName(); | 45 String packageName = getPackageName(); |
| 48 String webappId = null; | 46 String webappId = null; |
| 49 String mac = null; | |
| 50 String name = null; | 47 String name = null; |
| 51 String url = null; | 48 String url = null; |
| 52 String encodedIcon = null; | 49 String encodedIcon = null; |
| 53 String runtimeHost = null; | 50 String runtimeHost = null; |
| 54 try { | 51 try { |
| 55 ApplicationInfo appInfo = getPackageManager().getApplicationInfo( | 52 ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 56 packageName, PackageManager.GET_META_DATA); | 53 packageName, PackageManager.GET_META_DATA); |
| 57 Bundle bundle = appInfo.metaData; | 54 Bundle bundle = appInfo.metaData; |
| 58 url = bundle.getString(META_DATA_HOST_URL); | 55 url = bundle.getString(META_DATA_HOST_URL); |
| 59 | 56 |
| 60 String overrideUrl = getIntent().getDataString(); | 57 String overrideUrl = getIntent().getDataString(); |
| 61 // TODO(pkotwicz): Use same logic as {@code IntentHandler#shouldIgno
reIntent()} | 58 // TODO(pkotwicz): Use same logic as {@code IntentHandler#shouldIgno
reIntent()} |
| 62 if (overrideUrl != null && overrideUrl.startsWith("https:")) { | 59 if (overrideUrl != null && overrideUrl.startsWith("https:")) { |
| 63 url = overrideUrl; | 60 url = overrideUrl; |
| 64 } | 61 } |
| 65 | 62 |
| 66 webappId = WebApkConstants.WEBAPK_ID_PREFIX + packageName; | 63 webappId = WebApkConstants.WEBAPK_ID_PREFIX + packageName; |
| 67 mac = bundle.getString(META_DATA_MAC); | |
| 68 runtimeHost = bundle.getString(META_DATA_RUNTIME_HOST); | 64 runtimeHost = bundle.getString(META_DATA_RUNTIME_HOST); |
| 69 name = (String) getPackageManager().getApplicationLabel(appInfo); | 65 name = (String) getPackageManager().getApplicationLabel(appInfo); |
| 70 // TODO(hanxi): find a neat solution to avoid encode/decode each tim
e launch the | 66 // TODO(hanxi): find a neat solution to avoid encode/decode each tim
e launch the |
| 71 // activity. | 67 // activity. |
| 72 Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawabl
e.app_icon); | 68 Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawabl
e.app_icon); |
| 73 encodedIcon = encodeBitmapAsString(icon); | 69 encodedIcon = encodeBitmapAsString(icon); |
| 74 Log.w(TAG, "Url of the WebAPK: " + url); | 70 Log.w(TAG, "Url of the WebAPK: " + url); |
| 75 Log.w(TAG, "WebappId of the WebAPK: " + webappId); | 71 Log.w(TAG, "WebappId of the WebAPK: " + webappId); |
| 76 Log.w(TAG, "Name of the WebAPK:" + name); | 72 Log.w(TAG, "Name of the WebAPK:" + name); |
| 77 Log.w(TAG, "Package name of the WebAPK:" + packageName); | 73 Log.w(TAG, "Package name of the WebAPK:" + packageName); |
| 78 | 74 |
| 79 Intent newIntent = new Intent(); | 75 Intent newIntent = new Intent(); |
| 80 newIntent.setComponent(new ComponentName(runtimeHost, | 76 newIntent.setComponent(new ComponentName(runtimeHost, |
| 81 "org.chromium.chrome.browser.webapps.WebappLauncherActivity"
)); | 77 "org.chromium.chrome.browser.webapps.WebappLauncherActivity"
)); |
| 82 newIntent.putExtra(EXTRA_ID, webappId) | 78 newIntent.putExtra(EXTRA_ID, webappId) |
| 83 .putExtra(EXTRA_NAME, name) | 79 .putExtra(EXTRA_NAME, name) |
| 84 .putExtra(EXTRA_URL, url) | 80 .putExtra(EXTRA_URL, url) |
| 85 .putExtra(EXTRA_MAC, mac) | |
| 86 .putExtra(EXTRA_ICON, encodedIcon) | 81 .putExtra(EXTRA_ICON, encodedIcon) |
| 87 .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName); | 82 .putExtra(EXTRA_WEBAPK_PACKAGE_NAME, packageName); |
| 88 startActivity(newIntent); | 83 startActivity(newIntent); |
| 89 finish(); | 84 finish(); |
| 90 } catch (NameNotFoundException e) { | 85 } catch (NameNotFoundException e) { |
| 91 e.printStackTrace(); | 86 e.printStackTrace(); |
| 92 } | 87 } |
| 93 } | 88 } |
| 94 | 89 |
| 95 /** | 90 /** |
| 96 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. | 91 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. |
| 97 * The encoded string can be decoded using {@link decodeBitmapFromString(Str
ing)}. | 92 * The encoded string can be decoded using {@link decodeBitmapFromString(Str
ing)}. |
| 98 * @param bitmap The Bitmap to compress and encode. | 93 * @param bitmap The Bitmap to compress and encode. |
| 99 * @return the String encoding the Bitmap. | 94 * @return the String encoding the Bitmap. |
| 100 */ | 95 */ |
| 101 private static String encodeBitmapAsString(Bitmap bitmap) { | 96 private static String encodeBitmapAsString(Bitmap bitmap) { |
| 102 if (bitmap == null) return ""; | 97 if (bitmap == null) return ""; |
| 103 ByteArrayOutputStream output = new ByteArrayOutputStream(); | 98 ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 104 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); | 99 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); |
| 105 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT); | 100 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT); |
| 106 } | 101 } |
| 107 } | 102 } |
| OLD | NEW |