/*
 * Copyright 2013 Kevin Quan (kevin.quan@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kevinquan.android.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

/**
 * Utility class for methods related to a device
 * @author Kevin Quan (kevin.quan@gmail.com)
 *
 */
public class DeviceUtils {

    /**
     * Constants for Android permissions 
     * @author Kevin Quan (kevin.quan@gmail.com)
     *
     */
    public interface Permissions {
        public static final String INTERNET = "android.permission.INTERNET";
        public static final String NETWORK_STATE = "android.permission.ACCESS_NETWORK_STATE";
        public static final String WRITE_SD = "android.permission.WRITE_EXTERNAL_STORAGE";
    }
    
    private static final String TAG = DeviceUtils.class.getSimpleName();
    
    public static final String SCHEME_FILE = "file://";
    
    public static final String LINE_BREAK = System.getProperty("line.separator");

    /**
     * Converts a dp value to the actual pixel size given the provided context
     * @param context The context that defines the dp to pixel mapping
     * @param dp The dp to convert
     * @return The pixel size for the device for the provided dp
     */
    public static int convertDpToPixels(Context context, int dp) {
        if (context == null) {
            Log.w(TAG, "Could not convert dp to pixels as context is null.");
            return dp;
        }
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        // 0.5 is added to allow proper rounding when truncating
        return (int) ((dp * displayMetrics.density) + 0.5);
    }
    
    /**
     * Converts a pixel value to the dp of the device given the provided context
     * @param context The context that defines the pixel to dp mapping
     * @param pixels The pixel value to convert
     * @return The dp value for the device for the provided pixel value
     */
    public static float convertPixelsToDp(Context context, int pixels) {
        if (context == null) {
            Log.w(TAG, "Could not convert pixels to dp as context is null.");
            return pixels;
        }
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        // 0.5 is added to allow proper rounding when truncating
        return pixels / displayMetrics.density;
    }
    
    /**
     * Retrieves the window metrics being used by the provided activity
     * @param activity The activity to check
     * @return the display metrics of the provided activity
     */
    public static DisplayMetrics getWindowMetrics(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        if (activity == null) return metrics;
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }
    
    /**
     * Checks whether a context has a particular permission
     * @param context The context to check
     * @param permission The permission to check for
     * @return True if the permission exists for the context
     */
    public static boolean hasPermission(Context context, String permission) {
        if (context == null || TextUtils.isEmpty(permission)) return false;
        return context.checkCallingOrSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
    }
    
    /**
     * Hides the keyboard implicitly if it is visible
     * @param context The context from which to get the InputMethodManager
     * @param viewToGetWindowToken The view from which to get a window token.
     */
    public static void hideKeyboard(Context context, View viewToGetWindowToken) {
        if (context == null || viewToGetWindowToken == null) return;
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(viewToGetWindowToken.getWindowToken(), 0);
    }
    
    /**
     * Shows the keyboard and sends input to the provided field
     * @param context The context from which to get the InputMethodManager
     * @param viewToSendInput The view for the keyboard to send input to.
     */
    public static void showKeyboard(Context context, View viewToSendInput) {
        if (context == null || viewToSendInput == null) return;
        InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(viewToSendInput, 0);
    }

}

@@ -16,9 +16,9 @@
  201
-3
+4
  Kev
@@ -649,16 +649,56 @@
 utils;%0A%0A
+import android.annotation.SuppressLint;%0A
 import a
@@ -718,16 +718,16 @@
 tivity;%0A
-
 import a
@@ -792,16 +792,89 @@
 anager;%0A
+import android.content.res.Configuration;%0Aimport android.graphics.Point;%0A
 import a
@@ -932,16 +932,16 @@
 etrics;%0A
-
 import a
@@ -957,16 +957,45 @@
 il.Log;%0A
+import android.view.Display;%0A
 import a
@@ -5333,12 +5333,1088 @@
 );%0A    %7D
+%0A    %0A    /**%0A     * Determines the screen orientation.  Return values are defined in %7B@link Configuration%7D%0A     * @param activity An activity to get the display from %0A     * @return A constant for the screen orientation%0A     */%0A    @SuppressWarnings(%22deprecation%22)%0A    @SuppressLint(%22NewApi%22)%0A    public static int getScreenOrientation(Activity activity) %7B%0A        if (activity == null) %7B%0A            return Configuration.ORIENTATION_UNDEFINED;%0A        %7D%0A        Display display = activity.getWindowManager().getDefaultDisplay();%0A        Point dimensions = new Point();%0A        if (BuildUtils.isHoneycombMR2OrGreater()) %7B%0A            display.getSize(dimensions);%0A        %7D else %7B%0A            dimensions.x = display.getWidth();%0A            dimensions.y = display.getHeight();%0A        %7D%0A        if (dimensions.x == dimensions.y) %7B%0A            return Configuration.ORIENTATION_SQUARE;%0A        %7D else if (dimensions.y %3E dimensions.x) %7B%0A            return Configuration.ORIENTATION_LANDSCAPE;%0A        %7D else %7B%0A            return Configuration.ORIENTATION_PORTRAIT;%0A        %7D%0A    %7D
 %0A%0A%7D%0A

Add screen orientation helper