Home | History | Annotate | Download | only in tvleanback
      1 /*
      2  * Copyright (C) 2015 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 
     17 package com.example.android.tvleanback;
     18 
     19 import android.content.Context;
     20 import android.graphics.Point;
     21 import android.view.Display;
     22 import android.view.WindowManager;
     23 import android.widget.Toast;
     24 
     25 /**
     26  * A collection of utility methods, all static.
     27  */
     28 public class Utils {
     29 
     30     /*
     31      * Making sure public utility methods remain static
     32      */
     33     private Utils() {
     34     }
     35 
     36     /**
     37      * Returns the screen/display size
     38      */
     39     public static Point getDisplaySize(Context context) {
     40         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
     41         Display display = wm.getDefaultDisplay();
     42         Point size = new Point();
     43         display.getSize(size);
     44         int width = size.x;
     45         int height = size.y;
     46         return size;
     47     }
     48 
     49     /**
     50      * Shows a (long) toast
     51      */
     52     public static void showToast(Context context, String msg) {
     53         Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
     54     }
     55 
     56     /**
     57      * Shows a (long) toast.
     58      */
     59     public static void showToast(Context context, int resourceId) {
     60         Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_LONG).show();
     61     }
     62 
     63     public static int convertDpToPixel(Context ctx, int dp) {
     64         float density = ctx.getResources().getDisplayMetrics().density;
     65         return Math.round((float) dp * density);
     66     }
     67 }
     68