1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.bluetooth.audio_util; 17 18 import android.content.Context; 19 import android.graphics.Bitmap; 20 import android.graphics.BitmapFactory; 21 import android.media.MediaDescription; 22 import android.media.MediaMetadata; 23 import android.net.Uri; 24 import android.os.Bundle; 25 import android.util.Log; 26 27 import java.io.IOException; 28 import java.io.InputStream; 29 30 /** 31 * An object to represent an image from the Media Framework 32 * 33 * <p>This object abstracts away the method used to get the bitmap and provides a way for us to 34 * determine image equality in an application/folder/item agnostic way. 35 */ 36 public class Image { 37 private static final String TAG = Image.class.getSimpleName(); 38 39 public static int SOURCE_NONE = 0; 40 public static int SOURCE_URI = 1; 41 public static int SOURCE_BITMAP = 2; 42 43 private final Context mContext; 44 45 private int mSource = SOURCE_NONE; 46 private Bitmap mImage = null; 47 48 // For use with other applications so they can conveniently assign the handle their storage 49 // solution has picked for this image and pass this object on directly. 50 private String mImageHandle = null; 51 52 /** Create an Image object from a MediaMetadata object */ Image(Context context, MediaMetadata metadata)53 public Image(Context context, MediaMetadata metadata) { 54 mContext = context; 55 56 String uri_art = null; 57 String uri_album_art = null; 58 String uri_icon = null; 59 Bitmap bmp_art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART); 60 Bitmap bmp_album_art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART); 61 Bitmap bmp_icon = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON); 62 63 if (Util.areUriImagesSupported()) { 64 uri_art = metadata.getString(MediaMetadata.METADATA_KEY_ART_URI); 65 uri_album_art = metadata.getString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI); 66 uri_icon = metadata.getString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI); 67 } 68 69 if (bmp_art != null) { 70 setImage(bmp_art); 71 } else if (bmp_album_art != null) { 72 setImage(bmp_album_art); 73 } else if (bmp_icon != null) { 74 setImage(bmp_icon); 75 } else if (uri_art != null) { 76 setImage(uri_art); 77 } else if (uri_album_art != null) { 78 setImage(uri_album_art); 79 } else if (uri_icon != null) { 80 setImage(uri_icon); 81 } 82 } 83 84 /** Create an image out of a bundle of MediaMetadata keyed values */ Image(Context context, Bundle bundle)85 public Image(Context context, Bundle bundle) { 86 mContext = context; 87 88 String uri_art = null; 89 String uri_album_art = null; 90 String uri_icon = null; 91 Bitmap bmp_art = bundle.getParcelable(MediaMetadata.METADATA_KEY_ART); 92 Bitmap bmp_album_art = bundle.getParcelable(MediaMetadata.METADATA_KEY_ALBUM_ART); 93 Bitmap bmp_icon = bundle.getParcelable(MediaMetadata.METADATA_KEY_DISPLAY_ICON); 94 95 if (Util.areUriImagesSupported()) { 96 uri_art = bundle.getString(MediaMetadata.METADATA_KEY_ART_URI); 97 uri_album_art = bundle.getString(MediaMetadata.METADATA_KEY_ALBUM_ART_URI); 98 uri_icon = bundle.getString(MediaMetadata.METADATA_KEY_DISPLAY_ICON_URI); 99 } 100 101 if (bmp_art != null) { 102 setImage(bmp_art); 103 } else if (bmp_album_art != null) { 104 setImage(bmp_album_art); 105 } else if (bmp_icon != null) { 106 setImage(bmp_icon); 107 } else if (uri_art != null) { 108 setImage(uri_art); 109 } else if (uri_album_art != null) { 110 setImage(uri_album_art); 111 } else if (uri_icon != null) { 112 setImage(uri_icon); 113 } 114 } 115 116 /** Create an Image object from a MediaDescription object */ Image(Context context, MediaDescription desc)117 public Image(Context context, MediaDescription desc) { 118 mContext = context; 119 Uri uri = desc.getIconUri(); 120 Bitmap bitmap = desc.getIconBitmap(); 121 if (bitmap != null) { 122 setImage(bitmap); 123 } else if (uri != null) { 124 setImage(uri); 125 } 126 } 127 128 /** Create an Image object from a raw image uri */ Image(Context context, Uri uri)129 public Image(Context context, Uri uri) { 130 mContext = context; 131 setImage(uri); 132 } 133 134 /** Create an Image object from a raw image */ Image(Context context, Bitmap image)135 public Image(Context context, Bitmap image) { 136 mContext = context; 137 setImage(image); 138 } 139 140 /** Set the image by resolving a URI to a bitmap */ setImage(String uriString)141 private void setImage(String uriString) { 142 if (uriString == null) return; 143 Uri uri = Uri.parse(uriString); 144 setImage(uri); 145 } 146 147 /** Set the image by resolving a URI to a bitmap */ setImage(Uri uri)148 private void setImage(Uri uri) { 149 if (uri == null) return; 150 Bitmap image = getImageFromUri(uri); 151 if (image == null) return; 152 setImage(image); 153 setSource(SOURCE_URI); 154 } 155 156 /** Set the image directly from a bitmap */ setImage(Bitmap image)157 private void setImage(Bitmap image) { 158 if (image == null) return; 159 mImage = image; 160 setSource(SOURCE_BITMAP); 161 } 162 163 /** Get the bitmap associated with this Image */ getImage()164 public Bitmap getImage() { 165 return mImage; 166 } 167 168 /** 169 * Get an image Bitmap from a Uri 170 * 171 * <p>Used to convert Uris into the images they represent 172 * 173 * @param uri A Uri pointing to an image 174 * @return A Bitmap object representing the image at the given Uri 175 */ getImageFromUri(Uri uri)176 private Bitmap getImageFromUri(Uri uri) { 177 if (uri == null) return null; 178 Bitmap art = null; 179 InputStream input = null; 180 try { 181 if (mContext == null) return null; 182 input = mContext.getContentResolver().openInputStream(uri); 183 art = BitmapFactory.decodeStream(input); 184 } catch (Exception e) { 185 Log.w("Failed to fetch image at uri=" + uri, e); 186 art = null; 187 } 188 try { 189 if (input != null) { 190 input.close(); 191 } 192 } catch (IOException e) { 193 Log.e(TAG, "Failed to close image file stream, exception=" + e); 194 } 195 return art; 196 } 197 198 /** 199 * Get the source of the image, if known 200 * 201 * <p>Images currently come from either raw bitmaps or a URI that points to a ContentProvider. 202 * This allows us to set where it came from, largely used for debug purposes. 203 */ getSource()204 public int getSource() { 205 return mSource; 206 } 207 208 /** 209 * Set the source of the image. 210 * 211 * <p>Images currently come from either raw bitmaps or a URI that points to a ContentProvider. 212 * This allows us to set where it came from, largely used for debug purposes. 213 */ setSource(int source)214 private void setSource(int source) { 215 mSource = source; 216 } 217 218 /** Assign a handle value from your storage solution to this image */ setImageHandle(String handle)219 public void setImageHandle(String handle) { 220 mImageHandle = handle; 221 } 222 223 /** Get the handle value associated with this image from your storage situation */ getImageHandle()224 public String getImageHandle() { 225 return mImageHandle; 226 } 227 228 /** Determine if two image objects are the same. */ sameAs(Image l, Image r)229 public static boolean sameAs(Image l, Image r) { 230 if (l == null && r == null) return true; 231 if (l == null || r == null) return false; 232 final Bitmap bmp = l.getImage(); 233 if (bmp == null) return (r.getImage() == null); 234 return bmp.sameAs(r.getImage()); 235 } 236 237 /** Get a string representation of the image and its metadata */ 238 @Override toString()239 public String toString() { 240 return "<Image source=" + mSource + ">"; 241 } 242 } 243