OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.webapk.shell_apk; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.content.ComponentName; |
| 9 import android.content.Intent; |
| 10 import android.content.pm.ApplicationInfo; |
| 11 import android.content.pm.PackageManager; |
| 12 import android.content.pm.PackageManager.NameNotFoundException; |
| 13 import android.graphics.Bitmap; |
| 14 import android.graphics.BitmapFactory; |
| 15 import android.os.Bundle; |
| 16 import android.util.Base64; |
| 17 import android.util.Log; |
| 18 |
| 19 import org.chromium.webapk.lib.common.WebApkConstants; |
| 20 |
| 21 import java.io.ByteArrayOutputStream; |
| 22 |
| 23 /** |
| 24 * WebAPK's main Activity. |
| 25 */ |
| 26 public class MainActivity extends Activity { |
| 27 private static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_i
d"; |
| 28 private static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp
_icon"; |
| 29 private static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp
_name"; |
| 30 private static final String EXTRA_URL = "org.chromium.chrome.browser.webapp_
url"; |
| 31 private static final String EXTRA_MAC = "org.chromium.chrome.browser.webapp_
mac"; |
| 32 private static final String EXTRA_SCOPE = "org.chromium.chrome.browser.webap
p_scope"; |
| 33 private static final String META_DATA_HOST_URL = "hostUrl"; |
| 34 private static final String META_DATA_MAC = "mac"; |
| 35 private static final String META_DATA_SCOPE = "scope"; |
| 36 private static final String META_DATA_RUNTIME_HOST = "runtimeHost"; |
| 37 |
| 38 private static final String TAG = "cr_MainActivity"; |
| 39 |
| 40 @Override |
| 41 protected void onCreate(Bundle savedInstanceState) { |
| 42 super.onCreate(savedInstanceState); |
| 43 |
| 44 String packageName = getPackageName(); |
| 45 String webappId = null; |
| 46 String mac = null; |
| 47 String name = null; |
| 48 String url = null; |
| 49 String scope = null; |
| 50 String encodedIcon = null; |
| 51 String runtimeHost = null; |
| 52 try { |
| 53 ApplicationInfo appInfo = getPackageManager().getApplicationInfo( |
| 54 packageName, PackageManager.GET_META_DATA); |
| 55 Bundle bundle = appInfo.metaData; |
| 56 url = bundle.getString(META_DATA_HOST_URL); |
| 57 scope = bundle.getString(META_DATA_SCOPE); |
| 58 webappId = WebApkConstants.WEBAPK_ID_PREFIX + packageName; |
| 59 mac = bundle.getString(META_DATA_MAC); |
| 60 runtimeHost = bundle.getString(META_DATA_RUNTIME_HOST); |
| 61 name = (String) getPackageManager().getApplicationLabel(appInfo); |
| 62 // TODO(hanxi): find a neat solution to avoid encode/decode each tim
e launch the |
| 63 // activity. |
| 64 Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawabl
e.app_icon); |
| 65 encodedIcon = encodeBitmapAsString(icon); |
| 66 Log.w(TAG, "Url of the WebAPK: " + url); |
| 67 Log.w(TAG, "webappId of the WebAPK: " + webappId); |
| 68 Log.w(TAG, "name of the WebAPK:" + name); |
| 69 } catch (NameNotFoundException e) { |
| 70 e.printStackTrace(); |
| 71 } |
| 72 |
| 73 Intent newIntent = new Intent(); |
| 74 newIntent.setComponent(new ComponentName(runtimeHost, |
| 75 "org.chromium.chrome.browser.webapps.WebappLauncherActivity")); |
| 76 newIntent.putExtra(EXTRA_ID, webappId); |
| 77 newIntent.putExtra(EXTRA_NAME, name); |
| 78 newIntent.putExtra(EXTRA_SCOPE, scope); |
| 79 newIntent.putExtra(EXTRA_URL, url); |
| 80 newIntent.putExtra(EXTRA_MAC, mac); |
| 81 newIntent.putExtra(EXTRA_ICON, encodedIcon); |
| 82 startActivity(newIntent); |
| 83 finish(); |
| 84 } |
| 85 |
| 86 /** |
| 87 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. |
| 88 * The encoded string can be decoded using {@link decodeBitmapFromString(Str
ing)}. |
| 89 * @param bitmap The Bitmap to compress and encode. |
| 90 * @return the String encoding the Bitmap. |
| 91 */ |
| 92 private static String encodeBitmapAsString(Bitmap bitmap) { |
| 93 if (bitmap == null) return ""; |
| 94 ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 95 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); |
| 96 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT); |
| 97 } |
| 98 } |
OLD | NEW |