Home | History | Annotate | Download | only in utils
      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 
     15 package android.support.v17.leanback.supportleanbackshowcase.utils;
     16 
     17 import android.content.ContentResolver;
     18 import android.content.Context;
     19 import android.net.Uri;
     20 
     21 import java.io.IOException;
     22 import java.io.InputStream;
     23 
     24 /**
     25  * A collection of utility methods, all static.
     26  */
     27 public class Utils {
     28 
     29     public static int convertDpToPixel(Context ctx, int dp) {
     30         float density = ctx.getResources().getDisplayMetrics().density;
     31         return Math.round((float) dp * density);
     32     }
     33 
     34     /**
     35      * Will read the content from a given {@link InputStream} and return it as a {@link String}.
     36      *
     37      * @param inputStream The {@link InputStream} which should be read.
     38      * @return Returns <code>null</code> if the the {@link InputStream} could not be read. Else
     39      * returns the content of the {@link InputStream} as {@link String}.
     40      */
     41     public static String inputStreamToString(InputStream inputStream) {
     42         try {
     43             byte[] bytes = new byte[inputStream.available()];
     44             inputStream.read(bytes, 0, bytes.length);
     45             String json = new String(bytes);
     46             return json;
     47         } catch (IOException e) {
     48             return null;
     49         }
     50     }
     51 
     52     public static Uri getResourceUri(Context context, int resID) {
     53         return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
     54                                  context.getResources().getResourcePackageName(resID) + '/' +
     55                                  context.getResources().getResourceTypeName(resID) + '/' +
     56                                  context.getResources().getResourceEntryName(resID));
     57     }
     58 }
     59