Utils.java revision ad31f63f5843898de645f6ee1ac244c872ded8cc
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15package android.support.v17.leanback.supportleanbackshowcase.utils; 16 17import android.content.ContentResolver; 18import android.content.Context; 19import android.graphics.drawable.Drawable; 20import android.net.Uri; 21import android.widget.ImageView; 22 23import com.squareup.picasso.Picasso; 24import com.squareup.picasso.RequestCreator; 25 26import java.io.IOException; 27import java.io.InputStream; 28import java.net.URI; 29 30/** 31 * A collection of utility methods, all static. 32 */ 33public class Utils { 34 35 public static int convertDpToPixel(Context ctx, int dp) { 36 float density = ctx.getResources().getDisplayMetrics().density; 37 return Math.round((float) dp * density); 38 } 39 40 /** 41 * Will read the content from a given {@link InputStream} and return it as a {@link String}. 42 * 43 * @param inputStream The {@link InputStream} which should be read. 44 * @return Returns <code>null</code> if the the {@link InputStream} could not be read. Else 45 * returns the content of the {@link InputStream} as {@link String}. 46 */ 47 public static String inputStreamToString(InputStream inputStream) { 48 try { 49 byte[] bytes = new byte[inputStream.available()]; 50 inputStream.read(bytes, 0, bytes.length); 51 String json = new String(bytes); 52 return json; 53 } catch (IOException e) { 54 return null; 55 } 56 } 57 58 /** 59 * The method uses {@link Picasso} to fetch an image from a given url, resize it (if required) 60 * and display it inside an {@link ImageView}. 61 * 62 * @param context Context which is used to create a {@link Picasso} instance. 63 * @param uri The {@link URI} to fetch the image from. 64 * @param target The {@link ImageView} which shall display the image. 65 * @param resizeWidthInDp The target width of the image. Pass <code>-1</code> if you don't want 66 * to resize the image. 67 * @param resizeHeightInDp The target height of the image. Pass <code>-1</code> if you don't 68 * want to resize the image. 69 * @param centerCrop Centers and scales an image to fit the requested bounds. 70 * @param errorDrawable A drawable which will be shown in case the image could not be fetched 71 * from the server. 72 * @see {@link Picasso#with(Context)} 73 * @see {@link RequestCreator#resize(int, int)} 74 * @see {@link RequestCreator#centerCrop()} 75 * @see {@link RequestCreator#error(Drawable)} 76 */ 77 public static void loadImageFromUri(Context context, URI uri, ImageView target, 78 int resizeWidthInDp, int resizeHeightInDp, 79 boolean centerCrop, Drawable errorDrawable) { 80 if (uri == null) return; 81 RequestCreator builder = Picasso.with(context).load(uri.toString()); 82 if (resizeHeightInDp != -1 && resizeWidthInDp != -1) 83 builder.resize(Utils.convertDpToPixel(context, resizeWidthInDp), 84 Utils.convertDpToPixel(context, resizeHeightInDp)); 85 if (centerCrop) builder.centerCrop(); 86 builder.error(errorDrawable).into(target); 87 } 88 89 public static Uri getResourceUri(Context context, int resID) { 90 return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + 91 context.getResources().getResourcePackageName(resID) + '/' + 92 context.getResources().getResourceTypeName(resID) + '/' + 93 context.getResources().getResourceEntryName(resID)); 94 } 95} 96