package com.android.libcore.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
import android.view.ViewConfiguration;

import com.android.libcore.application.RootApplication;
import com.android.libcore.log.L;

/**
 * Description: 基础工具类，该类的所有函数如下所示(所有添加到该类的函数都应该在这标识出来，以防重复)
 * <ol>
 *     <li>{@link #dp2px(float)}用来将dp转换为px</li>
 *     <li>{@link #px2dp(float)}用来将px转换为dp</li>
 *     <li>{@link #isNetworkAvailable()}用来判断网络是否可用</li>
 *     <li>{@link #isNetworkWifi()}用来判断网络是否是wifi</li>
 *     <li>{@link #hasNavigationBar()}判断手机是否会有navigation bar</li>
 * </ol>
 *
 * @author zzp(zhao_zepeng@hotmail.com)
 * @since 2015-07-14
 */
public class CommonUtils {

    public static int dp2px(float dp){
        final float scale = RootApplication.getInstance().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public static int px2dp(float px) {
        final float scale = RootApplication.getInstance().getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    public static boolean isNetworkAvailable(){
        ConnectivityManager cm = (ConnectivityManager) RootApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        return info!=null && info.isAvailable();
    }

    public static boolean isNetworkWifi(){
        if (!isNetworkAvailable()){
            L.e("当前网络可用，请先调用isNetworkAvailable()函数");
        }
        ConnectivityManager cm = (ConnectivityManager) RootApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getActiveNetworkInfo();
        if (info.getType() == ConnectivityManager.TYPE_WIFI)
            return true;
        return false;
    }

    /**
     * 检查手机是否会有虚拟底部navigation bar
     */
    public static boolean hasNavigationBar(){
        boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
        boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);

        if(hasBackKey && hasHomeKey) {
            return false;
        }
        return true;
    }
}

@@ -356,16 +356,53 @@
 log.L;%0A%0A
+import java.security.MessageDigest;%0A%0A
 /**%0A * D
@@ -737,16 +737,66 @@
 ar%3C/li%3E%0A
+ *     %3Cli%3E%7B@link #md5(String)%7D%E7%94%A8%E6%9D%A5%E5%AF%B9%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9B%E8%A1%8Cmd5%E5%8A%A0%E5%AF%86%3C/li%3E%0A
  * %3C/ol%3E
@@ -1976,24 +1976,24 @@
 eturn true;%0A
-
         retu
@@ -2005,24 +2005,921 @@
 lse;%0A    %7D%0A%0A
+    public static String md5(String s)%7B%0A        char hexDigits%5B%5D=%7B'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'%7D;%0A%0A        try %7B%0A            byte%5B%5D btInput = s.getBytes();%0A            // %E8%8E%B7%E5%BE%97MD5%E6%91%98%E8%A6%81%E7%AE%97%E6%B3%95%E7%9A%84 MessageDigest %E5%AF%B9%E8%B1%A1%0A            MessageDigest mdInst = MessageDigest.getInstance(%22MD5%22);%0A            // %E4%BD%BF%E7%94%A8%E6%8C%87%E5%AE%9A%E7%9A%84%E5%AD%97%E8%8A%82%E6%9B%B4%E6%96%B0%E6%91%98%E8%A6%81%0A            mdInst.update(btInput);%0A            // %E8%8E%B7%E5%BE%97%E5%AF%86%E6%96%87%0A            byte%5B%5D md = mdInst.digest();%0A            // %E6%8A%8A%E5%AF%86%E6%96%87%E8%BD%AC%E6%8D%A2%E6%88%90%E5%8D%81%E5%85%AD%E8%BF%9B%E5%88%B6%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%BD%A2%E5%BC%8F%0A            int j = md.length;%0A            char str%5B%5D = new char%5Bj * 2%5D;%0A            int k = 0;%0A            for (int i = 0; i %3C j; i++) %7B%0A                byte byte0 = md%5Bi%5D;%0A                str%5Bk++%5D = hexDigits%5Bbyte0 %3E%3E%3E 4 & 0xf%5D;%0A                str%5Bk++%5D = hexDigits%5Bbyte0 & 0xf%5D;%0A            %7D%0A            return new String(str);%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            return null;%0A        %7D%0A    %7D%0A%0A
     /**%0A    

添加md5编码