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.chrome.browser.webapps; |
| 6 |
| 7 import android.content.Intent; |
| 8 import android.graphics.Bitmap; |
| 9 import android.graphics.BitmapFactory; |
| 10 import android.net.Uri; |
| 11 import android.os.Bundle; |
| 12 import android.text.TextUtils; |
| 13 import android.util.Base64; |
| 14 import android.util.Log; |
| 15 |
| 16 import org.chromium.chrome.browser.ShortcutHelper; |
| 17 import org.chromium.content_public.common.ScreenOrientationValues; |
| 18 |
| 19 import java.io.ByteArrayOutputStream; |
| 20 |
| 21 /** |
| 22 * Stores info about a web app. |
| 23 */ |
| 24 public class WebappInfo { |
| 25 private boolean mIsInitialized; |
| 26 private String mId; |
| 27 private Bitmap mIcon; |
| 28 private Uri mUri; |
| 29 private String mTitle; |
| 30 private int mOrientation; |
| 31 |
| 32 public static WebappInfo createEmpty() { |
| 33 return new WebappInfo(); |
| 34 } |
| 35 |
| 36 /** |
| 37 * Construct a WebappInfo. |
| 38 * @param intent Intent containing info about the app. |
| 39 */ |
| 40 public static WebappInfo create(Intent intent) { |
| 41 String id = intent.getStringExtra(ShortcutHelper.EXTRA_ID); |
| 42 String icon = intent.getStringExtra(ShortcutHelper.EXTRA_ICON); |
| 43 String title = intent.getStringExtra(ShortcutHelper.EXTRA_TITLE); |
| 44 String url = intent.getStringExtra(ShortcutHelper.EXTRA_URL); |
| 45 int orientation = intent.getIntExtra( |
| 46 ShortcutHelper.EXTRA_ORIENTATION, ScreenOrientationValues.DEFAUL
T); |
| 47 return create(id, url, icon, title, orientation); |
| 48 } |
| 49 |
| 50 /** |
| 51 * Construct a WebappInfo. |
| 52 * @param id ID for the webapp. |
| 53 * @param url URL for the webapp. |
| 54 * @param icon Icon to show for the webapp. |
| 55 * @param title Title of the webapp. |
| 56 * @param orientation Orientation of the webapp. |
| 57 */ |
| 58 public static WebappInfo create(String id, String url, String icon, String t
itle, |
| 59 int orientation) { |
| 60 if (id == null || url == null) { |
| 61 Log.e("WebappInfo", "Data passed in was incomplete: " + id + ", " +
url); |
| 62 return null; |
| 63 } |
| 64 |
| 65 Bitmap favicon = null; |
| 66 if (!TextUtils.isEmpty(icon)) { |
| 67 byte[] decoded = Base64.decode(icon, Base64.DEFAULT); |
| 68 favicon = BitmapFactory.decodeByteArray(decoded, 0, decoded.length); |
| 69 } |
| 70 |
| 71 Uri uri = Uri.parse(url); |
| 72 return new WebappInfo(id, uri, favicon, title, orientation); |
| 73 } |
| 74 |
| 75 private WebappInfo(String id, Uri uri, Bitmap icon, String title, int orient
ation) { |
| 76 mIcon = icon; |
| 77 mId = id; |
| 78 mTitle = title; |
| 79 mUri = uri; |
| 80 mOrientation = orientation; |
| 81 mIsInitialized = mUri != null; |
| 82 } |
| 83 |
| 84 private WebappInfo() { |
| 85 } |
| 86 |
| 87 /** |
| 88 * Writes all of the data about the webapp into the given Bundle. |
| 89 * @param outState Bundle to write data into. |
| 90 */ |
| 91 void writeToBundle(Bundle outState) { |
| 92 if (!mIsInitialized) return; |
| 93 |
| 94 outState.putString(ShortcutHelper.EXTRA_ID, mId); |
| 95 outState.putString(ShortcutHelper.EXTRA_URL, mUri.toString()); |
| 96 outState.putParcelable(ShortcutHelper.EXTRA_ICON, mIcon); |
| 97 outState.putString(ShortcutHelper.EXTRA_TITLE, mTitle); |
| 98 outState.putInt(ShortcutHelper.EXTRA_ORIENTATION, mOrientation); |
| 99 } |
| 100 |
| 101 /** |
| 102 * Copies all the fields from the given WebappInfo into this instance. |
| 103 * @param newInfo Information about the new webapp. |
| 104 */ |
| 105 void copy(WebappInfo newInfo) { |
| 106 mIsInitialized = newInfo.mIsInitialized; |
| 107 mIcon = newInfo.mIcon; |
| 108 mId = newInfo.mId; |
| 109 mUri = newInfo.mUri; |
| 110 mTitle = newInfo.mTitle; |
| 111 mOrientation = newInfo.mOrientation; |
| 112 } |
| 113 |
| 114 public boolean isInitialized() { |
| 115 return mIsInitialized; |
| 116 } |
| 117 |
| 118 public String id() { |
| 119 return mId; |
| 120 } |
| 121 |
| 122 public Uri uri() { |
| 123 return mUri; |
| 124 } |
| 125 |
| 126 public Bitmap icon() { |
| 127 return mIcon; |
| 128 } |
| 129 |
| 130 public String title() { |
| 131 return mTitle; |
| 132 } |
| 133 |
| 134 public int orientation() { |
| 135 return mOrientation; |
| 136 } |
| 137 |
| 138 // This is needed for clients that want to send the icon trough an intent. |
| 139 public String getEncodedIcon() { |
| 140 if (mIcon == null) return ""; |
| 141 |
| 142 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(
); |
| 143 mIcon.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream); |
| 144 byte[] byteArray = byteArrayOutputStream.toByteArray(); |
| 145 return Base64.encodeToString(byteArray, Base64.DEFAULT); |
| 146 } |
| 147 } |
OLD | NEW |