| 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.minting; | |
| 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.minting.lib.common.WebApkConstants; | |
| 20 import org.chromium.minting.lib.common.WebApkUtils; | |
| 21 | |
| 22 import java.io.ByteArrayOutputStream; | |
| 23 | |
| 24 /** | |
| 25 * Example client activity for a minted APK. | |
| 26 */ | |
| 27 public class MainActivity extends Activity { | |
| 28 private static final String EXTRA_ID = "org.chromium.chrome.browser.webapp_i
d"; | |
| 29 private static final String EXTRA_ICON = "org.chromium.chrome.browser.webapp
_icon"; | |
| 30 private static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp
_name"; | |
| 31 private static final String EXTRA_SHORT_NAME = "org.chromium.chrome.browser.
webapp_name"; | |
| 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_SCOPE = "org.chromium.chrome.browser.webap
p_scope"; | |
| 35 private static final String EXTRA_MINTING_PACKAGE_NAME = "EXTRA_MINTING_PACK
AGE_NAME"; | |
| 36 private static final String META_DATA_HOST_URL = "hostUrl"; | |
| 37 private static final String META_DATA_MAC = "mac"; | |
| 38 private static final String META_DATA_SCOPE = "scope"; | |
| 39 | |
| 40 private static final String TAG = "cr_MintingAPKExample"; | |
| 41 | |
| 42 @Override | |
| 43 protected void onCreate(Bundle savedInstanceState) { | |
| 44 super.onCreate(savedInstanceState); | |
| 45 | |
| 46 Intent intent = getIntent(); | |
| 47 String packageName = getPackageName(); | |
| 48 String webappId = null; | |
| 49 String mac = null; | |
| 50 String name = null; | |
| 51 String url = null; | |
| 52 String scope = null; | |
| 53 String encodedIcon = null; | |
| 54 try { | |
| 55 ApplicationInfo appInfo = getPackageManager().getApplicationInfo( | |
| 56 packageName, PackageManager.GET_META_DATA); | |
| 57 Bundle bundle = appInfo.metaData; | |
| 58 url = bundle.getString(META_DATA_HOST_URL); | |
| 59 | |
| 60 String overrideUrl = intent.getDataString(); | |
| 61 // TODO(pkotwicz): Use same logic as {@code IntentHandler#shouldIgno
reIntent()} | |
| 62 if (isUrlValid(overrideUrl)) { | |
| 63 url = overrideUrl; | |
| 64 } | |
| 65 | |
| 66 scope = bundle.getString(META_DATA_SCOPE); | |
| 67 webappId = WebApkConstants.WEB_APK_ID_PREFIX + packageName; | |
| 68 mac = bundle.getString(META_DATA_MAC); | |
| 69 name = (String) getPackageManager().getApplicationLabel(appInfo); | |
| 70 // TODO(hanxi): find a neat solution to avoid encode/decode each tim
e launch the | |
| 71 // activity. | |
| 72 Bitmap icon = BitmapFactory.decodeResource(getResources(), | |
| 73 org.chromium.minting.R.drawable.app_icon); | |
| 74 encodedIcon = encodeBitmapAsString(icon); | |
| 75 Log.w(TAG, "Url of the WebAPK: " + url); | |
| 76 Log.w(TAG, "webappId of the WebAPK: " + webappId); | |
| 77 Log.w(TAG, "name of the WebAPK:" + name); | |
| 78 } catch (NameNotFoundException e) { | |
| 79 e.printStackTrace(); | |
| 80 } | |
| 81 | |
| 82 Intent newIntent = new Intent(); | |
| 83 newIntent.setComponent(new ComponentName(WebApkUtils.getHostBrowserPacka
geName(this), | |
| 84 "org.chromium.chrome.browser.webapps.WebappLauncherActivity")); | |
| 85 newIntent.putExtra(EXTRA_ID, webappId); | |
| 86 newIntent.putExtra(EXTRA_NAME, name); | |
| 87 newIntent.putExtra(EXTRA_SHORT_NAME, name); | |
| 88 newIntent.putExtra(EXTRA_MINTING_PACKAGE_NAME, packageName); | |
| 89 newIntent.putExtra(EXTRA_SCOPE, scope); | |
| 90 newIntent.putExtra(EXTRA_URL, url); | |
| 91 newIntent.putExtra(EXTRA_MAC, mac); | |
| 92 newIntent.putExtra(EXTRA_ICON, encodedIcon); | |
| 93 startActivity(newIntent); | |
| 94 finish(); | |
| 95 } | |
| 96 | |
| 97 | |
| 98 /** | |
| 99 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. | |
| 100 * The encoded string can be decoded using {@link decodeBitmapFromString(Str
ing)}. | |
| 101 * @param bitmap The Bitmap to compress and encode. | |
| 102 * @return the String encoding the Bitmap. | |
| 103 */ | |
| 104 private static String encodeBitmapAsString(Bitmap bitmap) { | |
| 105 if (bitmap == null) return ""; | |
| 106 ByteArrayOutputStream output = new ByteArrayOutputStream(); | |
| 107 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); | |
| 108 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Returns whether a url is valid. | |
| 113 * @param url The url to check. | |
| 114 * @return Whether the url is valid. | |
| 115 */ | |
| 116 private static boolean isUrlValid(String url) { | |
| 117 if (url == null) { | |
| 118 return false; | |
| 119 } | |
| 120 | |
| 121 return url.startsWith("http:") || url.startsWith("https:"); | |
| 122 } | |
| 123 } | |
| OLD | NEW |