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.minting; | 5 package org.chromium.minting; |
| 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.SharedPreferences; | 10 import android.content.SharedPreferences; |
| 11 import android.content.pm.ApplicationInfo; | 11 import android.content.pm.ApplicationInfo; |
| 12 import android.content.pm.PackageManager; | 12 import android.content.pm.PackageManager; |
| 13 import android.content.pm.PackageManager.NameNotFoundException; | 13 import android.content.pm.PackageManager.NameNotFoundException; |
| 14 import android.graphics.Bitmap; | 14 import android.graphics.Bitmap; |
| 15 import android.graphics.BitmapFactory; | 15 import android.graphics.BitmapFactory; |
| 16 import android.os.Bundle; | 16 import android.os.Bundle; |
| 17 import android.util.Base64; | 17 import android.util.Base64; |
| 18 import android.util.Log; | 18 import android.util.Log; |
| 19 import android.view.View; | 19 import android.view.View; |
| 20 import android.view.View.OnClickListener; | 20 import android.view.View.OnClickListener; |
| 21 | 21 |
| 22 import org.chromium.minting.lib.common.WebAPKConstants; | 22 import org.chromium.minting.lib.common.WebAPKConstants; |
| 23 import org.chromium.minting.lib.common.WebAPKUtils; | |
| 23 | 24 |
| 24 import java.io.ByteArrayOutputStream; | 25 import java.io.ByteArrayOutputStream; |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * Example client activity for a minted APK. | 28 * Example client activity for a minted APK. |
| 28 */ | 29 */ |
| 29 public class MainActivity extends Activity implements OnClickListener { | 30 public class MainActivity extends Activity { |
| 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_NAME = "org.chromium.chrome.browser.webapp _name"; | 33 private static final String EXTRA_NAME = "org.chromium.chrome.browser.webapp _name"; |
| 33 private static final String EXTRA_SHORT_NAME = "org.chromium.chrome.browser. webapp_name"; | 34 private static final String EXTRA_SHORT_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_MAC = "org.chromium.chrome.browser.webapp_ mac"; | 36 private static final String EXTRA_MAC = "org.chromium.chrome.browser.webapp_ mac"; |
| 36 private static final String EXTRA_SCOPE = "org.chromium.chrome.browser.webap p_scope"; | 37 private static final String EXTRA_SCOPE = "org.chromium.chrome.browser.webap p_scope"; |
| 37 private static final String EXTRA_MINTING_PACKAGE_NAME = "EXTRA_MINTING_PACK AGE_NAME"; | 38 private static final String EXTRA_MINTING_PACKAGE_NAME = "EXTRA_MINTING_PACK AGE_NAME"; |
| 38 private static final String EXTRA_CALLER_PACKAGE_NAME = "EXTRA_CALLER_PACKAG E_NAME"; | |
| 39 private static final String META_DATA_HOST_URL = "hostUrl"; | 39 private static final String META_DATA_HOST_URL = "hostUrl"; |
| 40 private static final String META_DATA_MAC = "mac"; | 40 private static final String META_DATA_MAC = "mac"; |
| 41 private static final String META_DATA_SCOPE = "scope"; | 41 private static final String META_DATA_SCOPE = "scope"; |
| 42 | 42 |
| 43 private static final String TAG = "cr_MintingAPKExample"; | 43 private static final String TAG = "cr_MintingAPKExample"; |
| 44 | 44 |
| 45 @Override | 45 @Override |
| 46 protected void onCreate(Bundle savedInstanceState) { | 46 protected void onCreate(Bundle savedInstanceState) { |
| 47 super.onCreate(savedInstanceState); | 47 super.onCreate(savedInstanceState); |
| 48 | 48 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 75 Bitmap icon = BitmapFactory.decodeResource(getResources(), | 75 Bitmap icon = BitmapFactory.decodeResource(getResources(), |
| 76 org.chromium.minting.R.drawable.app_icon); | 76 org.chromium.minting.R.drawable.app_icon); |
| 77 encodedIcon = encodeBitmapAsString(icon); | 77 encodedIcon = encodeBitmapAsString(icon); |
| 78 Log.w(TAG, "Url of the minted APK: " + url); | 78 Log.w(TAG, "Url of the minted APK: " + url); |
| 79 Log.w(TAG, "webappId of the minted APK: " + webappId); | 79 Log.w(TAG, "webappId of the minted APK: " + webappId); |
| 80 Log.w(TAG, "name of the minted APK:" + name); | 80 Log.w(TAG, "name of the minted APK:" + name); |
| 81 } catch (NameNotFoundException e) { | 81 } catch (NameNotFoundException e) { |
| 82 e.printStackTrace(); | 82 e.printStackTrace(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 SharedPreferences prefs = | |
| 86 getSharedPreferences(MintingApplication.MINT_PREFS, MODE_PRIVATE ); | |
| 87 String chromePackageFromPref = prefs.getString(MintingApplication.HOST_P ACKAGE_PREF, | |
| 88 MintingApplication.DEFAULT_CHROME_PACKAGE_NAME); | |
| 89 String callerPackageName = intent.getStringExtra(EXTRA_CALLER_PACKAGE_NA ME); | |
| 90 // Update value if needed | |
| 91 if (callerPackageName != null && !callerPackageName.equals(chromePackage FromPref)) { | |
| 92 prefs.edit().putString(MintingApplication.HOST_PACKAGE_PREF, callerP ackageName) | |
| 93 .apply(); | |
| 94 Log.d(TAG, "Chrome with package " + callerPackageName + " is new hos t"); | |
| 95 } | |
| 96 String chromePackageName = callerPackageName == null ? chromePackageFrom Pref : | |
| 97 callerPackageName; | |
| 98 | |
| 99 Intent newIntent = new Intent(); | 85 Intent newIntent = new Intent(); |
| 100 newIntent.setComponent(new ComponentName(chromePackageName, | 86 newIntent.setComponent(new ComponentName(WebAPKUtils.getHostBrowserPacka geName(this), |
| 101 "org.chromium.chrome.browser.webapps.WebappLauncherActivity")); | 87 "org.chromium.chrome.browser.webapps.WebappLauncherActivity")); |
| 102 newIntent.putExtra(EXTRA_ID, webappId); | 88 newIntent.putExtra(EXTRA_ID, webappId); |
| 103 newIntent.putExtra(EXTRA_NAME, name); | 89 newIntent.putExtra(EXTRA_NAME, name); |
| 104 newIntent.putExtra(EXTRA_SHORT_NAME, name); | 90 newIntent.putExtra(EXTRA_SHORT_NAME, name); |
| 105 newIntent.putExtra(EXTRA_MINTING_PACKAGE_NAME, packageName); | 91 newIntent.putExtra(EXTRA_MINTING_PACKAGE_NAME, packageName); |
| 106 newIntent.putExtra(EXTRA_SCOPE, scope); | 92 newIntent.putExtra(EXTRA_SCOPE, scope); |
| 107 newIntent.putExtra(EXTRA_URL, url); | 93 newIntent.putExtra(EXTRA_URL, url); |
| 108 newIntent.putExtra(EXTRA_MAC, mac); | 94 newIntent.putExtra(EXTRA_MAC, mac); |
| 109 newIntent.putExtra(EXTRA_ICON, encodedIcon); | 95 newIntent.putExtra(EXTRA_ICON, encodedIcon); |
| 110 startActivity(newIntent); | 96 startActivity(newIntent); |
| 111 finish(); | 97 finish(); |
| 112 } | 98 } |
| 113 | 99 |
| 114 @Override | |
| 115 public void onClick(View v) { | |
| 116 | |
| 117 } | |
| 118 | 100 |
| 119 /** | 101 /** |
| 120 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. | 102 * Compresses a bitmap into a PNG and converts into a Base64 encoded string. |
| 121 * The encoded string can be decoded using {@link decodeBitmapFromString(Str ing)}. | 103 * The encoded string can be decoded using {@link decodeBitmapFromString(Str ing)}. |
| 104 * | |
|
Xi Han
2016/04/15 14:46:49
Revert?
Yaron
2016/04/15 15:46:58
Done.
| |
| 122 * @param bitmap The Bitmap to compress and encode. | 105 * @param bitmap The Bitmap to compress and encode. |
| 123 * @return the String encoding the Bitmap. | 106 * @return the String encoding the Bitmap. |
| 124 */ | 107 */ |
| 125 private static String encodeBitmapAsString(Bitmap bitmap) { | 108 private static String encodeBitmapAsString(Bitmap bitmap) { |
| 126 if (bitmap == null) return ""; | 109 if (bitmap == null) return ""; |
| 127 ByteArrayOutputStream output = new ByteArrayOutputStream(); | 110 ByteArrayOutputStream output = new ByteArrayOutputStream(); |
| 128 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); | 111 bitmap.compress(Bitmap.CompressFormat.PNG, 100, output); |
| 129 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT); | 112 return Base64.encodeToString(output.toByteArray(), Base64.DEFAULT); |
| 130 } | 113 } |
| 131 | 114 |
| 132 /** | 115 /** |
| 133 * Returns whether a url is valid. | 116 * Returns whether a url is valid. |
| 117 * | |
|
Xi Han
2016/04/15 14:46:49
Please revert.
Yaron
2016/04/15 15:46:58
Done.
| |
| 134 * @param url The url to check. | 118 * @param url The url to check. |
| 135 * @return Whether the url is valid. | 119 * @return Whether the url is valid. |
| 136 */ | 120 */ |
| 137 private static boolean isUrlValid(String url) { | 121 private static boolean isUrlValid(String url) { |
| 138 if (url == null) { | 122 if (url == null) { |
| 139 return false; | 123 return false; |
| 140 } | 124 } |
| 141 | 125 |
| 142 return url.startsWith("http:") || url.startsWith("https:"); | 126 return url.startsWith("http:") || url.startsWith("https:"); |
| 143 } | 127 } |
| 144 } | 128 } |
| OLD | NEW |