Home | History | Annotate | Download | only in leanback
      1 /*
      2  * Copyright (C) 2013 Google Inc. All Rights Reserved.
      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.leanback;
     18 
     19 import android.app.AlertDialog;
     20 import android.content.Context;
     21 import android.content.DialogInterface;
     22 import android.graphics.Point;
     23 import android.view.Display;
     24 import android.view.WindowManager;
     25 import android.widget.Toast;
     26 
     27 /**
     28  * A collection of utility methods, all static.
     29  */
     30 public class Utils {
     31 
     32     /*
     33      * Making sure public utility methods remain static
     34      */
     35     private Utils() {
     36     }
     37 
     38     /**
     39      * Returns the screen/display size
     40      *
     41      * @param context
     42      * @return
     43      */
     44     public static Point getDisplaySize(Context context) {
     45         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
     46         Display display = wm.getDefaultDisplay();
     47         Point size = new Point();
     48         display.getSize(size);
     49         int width = size.x;
     50         int height = size.y;
     51         return new Point(width, height);
     52     }
     53 
     54     /**
     55      * Shows an error dialog with a given text message.
     56      *
     57      * @param context
     58      * @param errorString
     59      */
     60 
     61     public static final void showErrorDialog(Context context, String errorString) {
     62         new AlertDialog.Builder(context).setTitle(R.string.error)
     63                 .setMessage(errorString)
     64                 .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
     65                     @Override
     66                     public void onClick(DialogInterface dialog, int id) {
     67                         dialog.cancel();
     68                     }
     69                 })
     70                 .create()
     71                 .show();
     72     }
     73 
     74     /**
     75      * Shows a (long) toast
     76      *
     77      * @param context
     78      * @param msg
     79      */
     80     public static void showToast(Context context, String msg) {
     81         Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
     82     }
     83 
     84     /**
     85      * Shows a (long) toast.
     86      *
     87      * @param context
     88      * @param resourceId
     89      */
     90     public static void showToast(Context context, int resourceId) {
     91         Toast.makeText(context, context.getString(resourceId), Toast.LENGTH_LONG).show();
     92     }
     93 
     94     public static int dpToPx(int dp, Context ctx) {
     95         float density = ctx.getResources().getDisplayMetrics().density;
     96         return Math.round((float) dp * density);
     97     }
     98 }
     99