package com.wdkl.ncs.s433.transbox.utils;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.ThumbnailUtils;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.InputType;
import android.text.TextUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;


import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtils {
    public static final String REGEX_PASSWORD = "^[a-zA-Z0-9]{6,20}$";

    private final static Pattern emailer = Pattern
            .compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
    private final static Pattern backCard = Pattern
            .compile(" /^(\\[0-9]{16}|\\[0-9]{18}|\\[0-9]{19})$/;");
    private final static ThreadLocal<SimpleDateFormat> dateFormater = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    private final static ThreadLocal<SimpleDateFormat> dateFormater2 = new ThreadLocal<SimpleDateFormat>() {
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };


    public static String getAppVersionName(Context context) {
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(
                    context.getPackageName(), 0);
            return info.versionName;
            // return info.versionCode;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return 1.0 + "";
    }

    /**
     * 将字符串转为日期类型
     */
    public static Date toDate(String sdate) {
        try {
            return dateFormater.get().parse(sdate);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 以友好的方式显示时间
     */
    public static String friendly_time(String sdate) {
        Date time = toDate(sdate);
        if (time == null) {
            return "Unknown";
        }
        String ftime = "";
        Calendar cal = Calendar.getInstance();

        // 判断是否是同一天
        String curDate = dateFormater2.get().format(cal.getTime());
        String paramDate = dateFormater2.get().format(time);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";
            return ftime;
        }

        long lt = time.getTime() / 86400000;
        long ct = cal.getTimeInMillis() / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);
            if (hour == 0)
                ftime = Math.max(
                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)
                        + "分钟前";
            else
                ftime = hour + "小时前";
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天";
        } else if (days > 2 && days <= 10) {
            ftime = days + "天前";
        } else if (days > 10) {
            ftime = dateFormater2.get().format(time);
        }
        return ftime;
    }

    /**
     * 判断给定字符串时间是否为今日
     */
    public static boolean isToday(String sdate) {
        boolean b = false;
        Date time = toDate(sdate);
        Date today = new Date();
        if (time != null) {
            String nowDate = dateFormater2.get().format(today);
            String timeDate = dateFormater2.get().format(time);
            if (nowDate.equals(timeDate)) {
                b = true;
            }
        }
        return b;
    }

    /**
     * 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     */
    public static boolean isEmpty(String input) {
        if (input == null || "".equals(input))
            return true;

        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                return false;
            }
        }
        return true;
    }

    public static boolean isPassword(String password) {
        return Pattern.matches(REGEX_PASSWORD, password);
    }

    public static boolean notEmpty(String input) {

        if (null != input && input.length() > 0
                && !input.equals("null") && !input.equals("NULL")) {
            return true;
        }
        return false;
    }

    public static boolean listNotEmpty(List list) {
        if (list != null && list.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 判断是不是一个合法的银行卡
     */
    public static boolean isCardNum(String cardNum) {
        if (cardNum == null || cardNum.trim().length() == 0)
            return false;
        return backCard.matcher(cardNum).matches();
    }

    /**
     * 判断是不是一个合法的电子邮件地址
     */
    public static boolean isEmail(String email) {
        if (email == null || email.trim().length() == 0)
            return false;
        return emailer.matcher(email).matches();
    }

    /**
     * 字符串转整数
     */
    public static int toInt(String str, int defValue) {
        try {
            return Integer.parseInt(str);
        } catch (Exception e) {
        }
        return defValue;
    }


    /**
     * 检查是否是金额（12.00）
     */
    public static boolean checkNumEx(String phone) {
        Pattern pattern = Pattern.compile("^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$");
        Matcher match = pattern.matcher(phone);
        return match.matches();
    }


    /**
     * 对象转整数
     *
     * @return 转换异常返回 0
     */
    public static int toInt(Object obj) {
        if (obj == null)
            return 0;
        return toInt(obj.toString(), 0);
    }

    /**
     * 对象转整数
     *
     * @return 转换异常返回 0
     */
    public static long toLong(String obj) {
        try {
            return Long.parseLong(obj);
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 字符串转布尔值
     *
     * @return 转换异常返回 false
     */
    public static boolean toBool(String b) {
        try {
            return Boolean.parseBoolean(b);
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * 是否是英文字
     */
    public static boolean isLetter(char c) {
        int k = 0x80;
        return c / k == 0 ? true : false;
    }

    /**
     * 获取英文字长度
     */
    public static int length(String s) {
        if (s == null)
            return 0;
        char[] c = s.toCharArray();
        int len = 0;
        int length = c.length;
        for (int i = 0; i < length; i++) {
            len++;
            if (!isLetter(c[i])) {
                len++;
            }
        }
        return len;
    }

    /**
     * 去掉html标签
     */
    public static String delHTMLTag(String htmlStr) {
        String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式
        String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式
        String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式

        Pattern p_script = Pattern.compile(regEx_script,
                Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll(""); // 过滤script标签

        Pattern p_style = Pattern
                .compile(regEx_style, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll(""); // 过滤style标签

        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll(""); // 过滤html标签

        htmlStr = htmlStr.replaceAll("&nbsp;", "");

        return htmlStr.trim(); // 返回文本字符串
    }

    /**
     * 去掉换行符
     */
    public static String removeLineChar(String str) {
        if (str == null)
            return "";
        return str.replaceAll("\r\n", "").replaceAll("\n", "");
    }

    /**
     * 以中文字长度计算，截取字符串
     */
    public static String substring(String origin, int len, String c) {
        if (origin == null || origin.equals("") || len < 1)
            return "";
        String temp = removeLineChar(origin);
        byte[] strByte = new byte[len];
        if (len > length(origin)) {
            return temp;
        }
        try {
            System.arraycopy(temp.getBytes("GBK"), 0, strByte, 0, len);
            int count = 0;
            for (int i = 0; i < len; i++) {
                int value = (int) strByte[i];
                if (value < 0) {
                    count++;
                }
            }
            if (count % 2 != 0) {
                len = (len == 1) ? ++len : --len;
            }
            return new String(strByte, 0, len, "GBK") + c;
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static int StrToIntDef(String s, int defaultValue) {
        int res = defaultValue;
        try {
            res = Integer.parseInt(s, 10);
        } catch (Exception e) {
            res = defaultValue;
        }
        return res;
    }

    /**
     * 将数据集合转化拼成字符串
     *
     * @param collection 集合
     * @param delimiter  分隔符
     */
    public static String join(Collection<?> collection, String delimiter) {
        StringBuilder builder = new StringBuilder();
        Iterator<?> iter = collection.iterator();
        while (iter.hasNext()) {
            builder.append(iter.next());
            if (iter.hasNext()) {
                builder.append(delimiter);
            }
        }
        return builder.toString();
    }

    /**
     * 验证身份证是否有效15位或18位<包括对年月日的合法性进行验证>
     */
    public static boolean isIDCard(String idcard) {
        Pattern pattern = Pattern.compile("^\\d{15}(\\d{2}[0-9xX])?$");
        Matcher macher = pattern.matcher(idcard);
        if (macher.find()) {// 对年月日字符串的验证
            String power = idcard.substring(idcard.length() - 12,
                    idcard.length() - 4);
            pattern = Pattern
                    .compile("^[1-2]+([0-9]{3})+(0[1-9][0-2][0-9]|0[1-9]3[0-1]|1[0-2][0-3][0-1]|1[0-2][0-2][0-9])");
            macher = pattern.matcher(power);
        }
        return macher.find();
    }

    /**
     * 检查是否是正确的email
     */
    public static boolean checkEmail(String email) {
        String regex = "\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 验证邮箱格式
     */
    public static boolean isEmailOther(String strEmail) {
        String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }

    /**
     * 判断姓名只能输入中文，或者英文,或者数字
     */
    public static boolean isUserName(String name) {
        if (name.matches("(([\u4E00-\u9FA5]{2,7})|([a-zA-Z0-9]{3,10}))")) {
            return true;
        }
        return false;
    }

    /**
     * 判断姓名只能输入中文，或者英文,或者数字
     */
    public static boolean isUserName2(String name) {
        if (name.matches("([\u4E00-\u9FA5a-zA-Z0-9]{2,10})")) {
            return true;
        }
        return false;
    }

    /**
     * 对银行卡进行加*号保护设置并格式化
     *
     * @return 如：6222 **** **** 1234
     */
    public static String formateBankNumber(String cardNo) {
        String star = " **** **** ";
        int cardNoLength = cardNo.length();
        if (cardNoLength >= 4) {
            String start = cardNo.substring(0, 4);
            String end = cardNo.substring(cardNoLength - 4, cardNoLength);
            return start + star + end;
        } else {
            return cardNo;
        }
    }

    /**
     * 手机号码加星保护
     *
     * @return 如：136*****256
     */
    public static String formatPhone(String phone) {
        String star = "******";
        return phone.substring(0, 3) + star
                + phone.substring(phone.length() - 3, phone.length());
    }

    /**
     * 检查是否是数字
     *
     * @return
     */
    public static boolean checkNum(String phone) {
        Pattern pattern = Pattern.compile("[0-9]+");
        Matcher match = pattern.matcher(phone);
        return match.matches();
    }

    /**
     * 检查是否是电话号码
     */
    public static boolean isMobileNo(String paramString) {
        if (!notEmpty(paramString)) {
            return false;
        }
        return Pattern.compile("^1[3|4|5|6|7|8|9]\\d{9}$").matcher(paramString)
                .matches();

    }

    /**
     * 检查用户输入账号格式是否正确,只能是邮箱或电话号码
     */
    public static boolean checkAccount(String account) {
        return (isMobileNo(account) || checkEmail(account));
    }

    // 校验Tag Alias 只能是数字,英文字母和中文
    public static boolean isValidTagAndAlias(String s) {
        Pattern p = Pattern.compile("^[\u4E00-\u9FA50-9a-zA-Z_-]{0,}$");
        Matcher m = p.matcher(s);
        return m.matches();
    }

    public static final String KEY_APP_KEY = "JPUSH_APPKEY";

    /**
     * 获取jpush AppKey
     */
    public static String getAppKey(Context context) {
        Bundle metaData = null;
        String appKey = null;
        try {
            ApplicationInfo ai = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            if (null != ai)
                metaData = ai.metaData;
            if (null != metaData) {
                appKey = metaData.getString(KEY_APP_KEY);
                if ((null == appKey) || appKey.length() != 24) {
                    appKey = null;
                }
            }
        } catch (NameNotFoundException e) {

        }
        return appKey;
    }

    /**
     * 将输入流转化为String
     */
    public static String readDataFromStream(InputStream is) {
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder builder = new StringBuilder();
        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return builder.toString();
    }

    /**
     * 补零操作
     */
    public static String zerofill(Object obj) {
        String parten = "00";
        if (obj instanceof String)
            obj = Double.parseDouble(obj.toString());
        DecimalFormat decimal = new DecimalFormat(parten);
        return decimal.format(obj);
    }

    /**
     * ASCII表中可见字符从!开始，偏移位值为33(Decimal)
     */
    static final char DBC_CHAR_START = 33; // 半角!  

    /**
     * ASCII表中可见字符到~结束，偏移位值为126(Decimal)
     */
    static final char DBC_CHAR_END = 126; // 半角~  

    /**
     * 全角对应于ASCII表的可见字符从！开始，偏移值为65281
     */
    static final char SBC_CHAR_START = 65281; // 全角！  

    /**
     * 全角对应于ASCII表的可见字符到～结束，偏移值为65374
     */
    static final char SBC_CHAR_END = 65374; // 全角～  

    /**
     * ASCII表中除空格外的可见字符与对应的全角字符的相对偏移
     */
    static final int CONVERT_STEP = 65248; // 全角半角转换间隔  

    /**
     * 全角空格的值，它没有遵从与ASCII的相对偏移，必须单独处理
     */
    static final char SBC_SPACE = 12288; // 全角空格 12288  

    /**
     * 半角空格的值，在ASCII中为32(Decimal)
     */
    static final char DBC_SPACE = ' '; // 半角空格  


    /**
     * <PRE>
     * 半角字符->全角字符转换
     * 只处理空格，!到&tilde;之间的字符，忽略其他
     * </PRE>
     */
    public static String bj2qj(String src) {
        if (src == null) {
            return src;
        }
        StringBuilder buf = new StringBuilder(src.length());
        char[] ca = src.toCharArray();
        for (int i = 0; i < ca.length; i++) {
            if (ca[i] == DBC_SPACE) { // 如果是半角空格，直接用全角空格替代  
                buf.append(SBC_SPACE);
            } else if ((ca[i] >= DBC_CHAR_START) && (ca[i] <= DBC_CHAR_END)) { // 字符是!到~之间的可见字符  
                buf.append((char) (ca[i] + CONVERT_STEP));
            } else { // 不对空格以及ascii表中其他可见字符之外的字符做任何处理  
                buf.append(ca[i]);
            }
        }
        return buf.toString();
    }

    /**
     * <PRE>
     * 全角字符->半角字符转换
     * 只处理全角的空格，全角！到全角～之间的字符，忽略其他
     * </PRE>
     */
    public static String qj2bj(String src) {
        if (src == null) {
            return src;
        }
        StringBuilder buf = new StringBuilder(src.length());
        char[] ca = src.toCharArray();
        for (int i = 0; i < src.length(); i++) {
            if (ca[i] >= SBC_CHAR_START && ca[i] <= SBC_CHAR_END) { // 如果位于全角！到全角～区间内  
                buf.append((char) (ca[i] - CONVERT_STEP));
            } else if (ca[i] == SBC_SPACE) { // 如果是全角空格  
                buf.append(DBC_SPACE);
            } else { // 不处理全角空格，全角！到全角～区间外的字符  
                buf.append(ca[i]);
            }
        }
        return buf.toString();
    }

    /**
     * 字符串按索引截取
     */
    public static String substringByLengh(String str, int start, int end) {
        if (str == null) {
            return "";
        }
        if (start > end) {
            return "";
        }
        if (str.length() - 1 < start || str.length() - 1 < end) {
            return "";
        }

        return str.substring(start, end);
    }

    /**
     * 字符串按单个截取
     */
    public static ArrayList<String> substringBySing(String str) {
        if (null == str || "".equals(str)) {
            return null;
        }
        ArrayList<String> list = new ArrayList<>();
        char[] c = str.toCharArray();
        for (int i = 0; i < c.length; i++) {
            String s = String.valueOf(c[i]);
            list.add(s);
        }
        return list;
    }

    /**
     * 追加内容到当前String对象<br>
     */
    public static String append(String oldStr, String appendStr) {
        if (oldStr == null) {
            return appendStr;
        }
        StringBuffer sb = new StringBuffer(oldStr);
        return sb.append(appendStr).toString();
    }

    /**
     * 删除指定位置的字符<br>
     */
    public static String deleteCharAt(String oldStr, int index) {
        if (oldStr == null) {
            return null;
        }
        if (oldStr.length() <= 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer(oldStr);
        return sb.deleteCharAt(index).toString();
    }

    /**
     * String对象中插入内容<br>
     */
    public static String insert(String oldStr, String insertStr, int index) {
        if (oldStr == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer(oldStr);
        return sb.insert(index, insertStr).toString();
    }

    /**
     * String对象内容反转<br>
     */
    public static String reverse(String oldStr) {
        if (oldStr == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer(oldStr);
        return sb.reverse().toString();
    }

    /**
     * 修改索引值为index位置的字符<br>
     */
    public static String setCharAt(String oldStr, char charStr, int index) {
        if (oldStr == null) {
            return null;
        }
        StringBuffer sb = new StringBuffer(oldStr);
        sb.setCharAt(index, charStr);

        return sb.toString();
    }

    /**
     * 对某个字符加*操作<br>
     *
     * @return String
     */
    public static String changeTextAddsiix(String oldStr, int[] index) {
        String newStr = oldStr;
        for (int i = 0; i < index.length; i++) {
            if (index[i] < oldStr.length()) {
                newStr = setCharAt(newStr, '*', index[i]);
            }
        }
        return newStr;
    }

    /**
     * 根据逗号分隔转为list<br>
     *
     * @return String
     */
    public static List<String> changeTextByList(String oldStr) {
        if (!notEmpty(oldStr)) return null;
        return Arrays.asList(oldStr.split(","));//根据逗号分隔转化为list
    }

    /**
     * 移除所有空格<br>
     *
     * @return String
     */
    public static String replaceNull(String oldStr) {
        if (null == oldStr || oldStr.length() <= 0) return "";
        return oldStr.replaceAll(" ", "").replaceAll(" ", "").replaceAll("\\b", "").replaceAll("\\r", "").replaceAll("\\n", "");
    }

    /**
     * 移除某个字符<br>
     *
     * @return String
     */
    public static String replaceStr(String oldStr, CharSequence replaceStr) {
        if (null == oldStr || oldStr.length() <= 0) return "";
        return oldStr.replace(replaceStr, "");
    }

    /**
     * 字符串大写变小写<br>
     *
     * @return String
     */
    public static String smallerCapitalTo(String oldStr) {
        if (null == oldStr || oldStr.length() <= 0) return "";
        return oldStr.toLowerCase();
    }

    /**
     * 字符串小写变大写<br>
     *
     * @return String
     */
    public static String bigCapitalTo(String oldStr) {
        if (null == oldStr || oldStr.length() <= 0) return "";
        return oldStr.toUpperCase();
    }

    /**
     * 对double类型的数据保留2位小数
     */
    public static double get2Decimal(double d) {
        return Math.round(d * 100) / 100d;
        // BigDecimal b = new BigDecimal(Double.toString(d));
        // return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 对double类型的数据取整
     */
    public static double get0Decimal(double d) {
        String str = String.valueOf(d);
        String strNum = str.substring(0, str.lastIndexOf("."));//截取从字符串开始到小数点位置的字符串，就是整数部分
        return parseDouble(strNum);
    }

    /**
     * 判断一个字符串 能否转成一个Double类型的
     */
    public static boolean isDouble(String str) {
        try {
            Double.parseDouble(str);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    /**
     * 字符串转成一个Double类型的
     */
    public static double parseDouble(String str) {
        if (isDouble(str)) {
            return Double.parseDouble(str);
        }
        return 0.0;
    }

    /**
     * 判断一个字符串 能否转成一个long类型的
     */
    public static boolean isLong(String str) {
        try {
            Long.parseLong(str);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    /**
     * 字符串转成一个Long类型的
     */
    public static long parseLong(String str) {
        if (isLong(str)) {
            return Long.parseLong(str);
        }
        return 0;
    }

    /**
     * 判断一个字符串 能否转成一个int类型的
     */
    public static boolean isInt(String str) {
        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    /**
     * 字符串转成一个Int类型的
     */
    public static int parseInt(String str) {
        if (isInt(str)) {
            return Integer.parseInt(str);
        }
        return 0;
    }

    /**
     * 判断一个字符串 能否转成一个float类型的
     */
    public static boolean isFloat(String str) {
        try {
            Float.parseFloat(str);
            return true;
        } catch (NumberFormatException ex) {
            return false;
        }
    }

    /**
     * 字符串转成一个float类型的
     */
    public static float parseFloat(String str) {
        if (isFloat(str)) {
            return Float.parseFloat(str);
        }
        return 0f;
    }

    /**
     * 判断一个字符串 能否转成一个Double类型的
     */
    public static boolean checkDouble(String str) {
        Pattern pattern = Pattern.compile("^[-//+]?//d+(//.//d*)?|//.//d+$");
        return pattern.matcher(str).matches();
    }

    /**
     * String字符串的匹配（做模糊查询）
     *
     * @return boolean
     */
    public static boolean vagueInquiry(String objStr, String inquiryStr) {
        if (null == objStr || null == inquiryStr) {
            return false;
        }
        Pattern p = Pattern.compile(inquiryStr);
        Matcher m = p.matcher(objStr);
        return m.find();
    }

    /**
     * 是否是汉字
     */
    public static boolean isChinese(String str) {
        String regEx = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regEx);
        Matcher matcher = pat.matcher(str);
        boolean flg = false;
        if (matcher.find())
            flg = true;
        return flg;
    }

    public boolean judgeContainsStr(String cardNum) {
        String regex = ".*[*]+.*";
        Matcher m = Pattern.compile(regex).matcher(cardNum);
        return m.matches();
    }

    /**
     * 是否为车牌号
     */
    public static boolean isCar(String carNum) {
        String regex = "^[京,津,渝,沪,冀,晋,辽,吉,黑,苏,浙,皖,闽,赣,鲁,豫,鄂,湘,粤,琼,川,贵,云,陕,秦,甘,陇,青,台,蒙,桂,宁,新,藏,澳,港,军,海,航,警,使,古][A-Z][0-9,A-Z]{5}$";
        Matcher m = Pattern.compile(regex).matcher(carNum);
        return m.matches();
    }

    /**
     * unicode码转成汉字
     */
    public static String revert(String str) {
        str = (str == null ? "" : str);
        if (str.indexOf("\\u") == -1)//如果不是unicode码则原样返回
            return str;

        StringBuffer sb = new StringBuffer(1000);

        for (int i = 0; i < str.length() - 6; ) {
            String strTemp = str.substring(i, i + 6);
            String value = strTemp.substring(2);
            int c = 0;
            for (int j = 0; j < value.length(); j++) {
                char tempChar = value.charAt(j);
                int t = 0;
                switch (tempChar) {
                    case 'a':
                        t = 10;
                        break;
                    case 'b':
                        t = 11;
                        break;
                    case 'c':
                        t = 12;
                        break;
                    case 'd':
                        t = 13;
                        break;
                    case 'e':
                        t = 14;
                        break;
                    case 'f':
                        t = 15;
                        break;
                    default:
                        t = tempChar - 48;
                        break;
                }

                c += t * ((int) Math.pow(16, (value.length() - j - 1)));
            }
            sb.append((char) c);
            i = i + 6;
        }
        return sb.toString();
    }

    /**
     * 获取手机号码运营商信息
     */
    public static String getProvidersName(String phone) {
        String CMCC = "中国移动";
        String UNICOM = "中国联通";
        String TELECOM = "中国电信";
        String UNKNOWN = "未知";
        String head1 = "";
        String head2 = "";
        if (!notEmpty(phone)) return UNKNOWN;
        phone = phone.trim(); // 去除前后的空白
        if (phone.length() < 11) {  // 判断输入的电话号码是否合法
            return UNKNOWN;
        } else {
            // 处理国内的+86开头
            if (phone.startsWith("+")) phone = phone.substring(1);
            if (phone.startsWith("86")) phone = phone.substring(2);
        }
        if (!isMobileNo(phone)) { // 判断去除+86后剩余的是否是手机号
            return UNKNOWN;
        }
        // 截取前3或前4位电话号码，判断运营商
        head1 = phone.substring(0, 3);
        head2 = phone.substring(0, 4);
        // 移动前三位
        boolean cmcctemp3 = head1.equals("134") || head1.equals("135") || head1.equals("136")
                || head1.equals("137") || head1.equals("138")
                || head1.equals("139") || head1.equals("147")
                || head1.equals("150") || head1.equals("151")
                || head1.equals("152") || head1.equals("157")
                || head1.equals("158") || head1.equals("159")
                || head1.equals("182") || head1.equals("183")
                || head1.equals("184") || head1.equals("178")
                || head1.equals("187") || head1.equals("188") || head1.equals("198");
        if (cmcctemp3) {
            return CMCC;
        }
        // 移动前4位
        boolean cmcctemp4 = head2.equals("1340") || head2.equals("1341")
                || head2.equals("1342") || head2.equals("1343")
                || head2.equals("1344") || head2.equals("1345")
                || head2.equals("1346") || head2.equals("1347")
                || head2.equals("1348") || head2.equals("1705");
        if (cmcctemp4) {
            return CMCC;
        }
        // 联通前3位
        boolean unicomtemp = head1.equals("130") || head1.equals("131")
                || head1.equals("132") || head1.equals("145")
                || head1.equals("155") || head1.equals("156") || head1.equals("176")
                || head1.equals("185") || head1.equals("186") || head1.equals("166");
        if (unicomtemp) {
            return UNICOM;
        }
        boolean unicomtemp4 = head1.equals("1709");
        if (unicomtemp4) {
            return UNICOM;
        }
        // 电信前3位
        boolean telecomtemp = head1.equals("133") || head1.equals("153")
                || head1.equals("181") || head1.equals("177")
                || head1.equals("180") || head1.equals("189") || head1.equals("199");
        if (telecomtemp) {
            return TELECOM;
        }
        boolean telecomtemp4 = head1.equals("1700");
        if (telecomtemp4) {
            return TELECOM;
        }
        return UNKNOWN;
    }

    /**
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s, String pattern) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
        long lt = new Long(s);
        Date date = new Date(lt * 1000L);
        res = simpleDateFormat.format(date);
        return res;
    }

    /**
     * 将时间转换为时间戳
     */
    public static String dateToStamp(String s, String dateType) {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateType);
        Date date = null;
        try {
            date = simpleDateFormat.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return null;
        } else {
            long ts = date.getTime();
            res = String.valueOf(ts / 1000);
        }
        return res;
    }

    /**
     * 获取当前时间戳
     */
    public static String getDateTamp() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(date.getTime());
        //时间戳
        String create_time = dateToStamp(time, "yyyy/MM/dd hh:mm:ss");
        return create_time;
    }

    /**
     * 随机6位数
     */
    public static String randomNumber() {
        return (int) ((Math.random() * 9 + 1) * 100000) + "";
    }


    /**
     * 获取mac地址
     */
    public static String tryGetMAC(WifiManager manager) {
        WifiInfo wifiInfo = manager.getConnectionInfo();
        if (wifiInfo == null || notEmpty(wifiInfo.getMacAddress())) {
            return null;
        }
        String mac = wifiInfo.getMacAddress().trim().toUpperCase();
        return mac;
    }

    /**
     * 设置EditText可不可编辑
     */
    public void setEditTextEnable(EditText editText, boolean mode) {
        editText.setFocusable(mode);
        editText.setFocusableInTouchMode(mode);
        editText.setLongClickable(mode);
        editText.setInputType(mode ? InputType.TYPE_CLASS_TEXT : InputType.TYPE_NULL);

    }

    /**
     * bitmap to byte
     */
    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }



    public static void copy(Context context, String copyStr) {
        // 获取系统剪贴板
        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        // 创建一个剪贴数据集，包含一个普通文本数据条目（需要复制的数据）
        ClipData clipData = ClipData.newPlainText(null, copyStr);
        // 把数据集设置（复制）到剪贴板
        clipboard.setPrimaryClip(clipData);
    }


    /**
     * 根据资源的名字获取它的ID
     *
     * @param name    要获取的资源的名字
     * @param defType 资源的类型，如drawable, string 。。。
     * @return 资源的id
     */
    public static int getResId(Context context, String name, String defType) {
        String packageName = context.getApplicationInfo().packageName;


        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(name);
        if (m.matches()) {
            return 0;
        }

        return context.getResources().getIdentifier(name, defType, packageName);

    }

    /**
     * 把bitmap同比例放大
     *
     * @param scale  需要放大的倍数
     * @param bitmap 源bitmap
     * @return
     */
    private static Bitmap scale2Bitmap(float scale, Bitmap bitmap) {
        Bitmap bitmap2;
        if (bitmap != null) {
            Matrix matrix = new Matrix();
            matrix.postScale(scale, scale); // 长和宽放大缩小的比例
            bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    bitmap.getHeight(), matrix, true);
            return bitmap2;
        }
        return null;
    }

    /**
     * 解码
     *
     * @param s1
     * @return
     */
    public static String getCountry(String s1) {
        try {
            byte[] converttoBytes = s1.getBytes("UTF-8");
            String s2 = new String(converttoBytes, "UTF-8");
            return s2;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return s1;
    }

    /**
     * 隐藏输入法
     *
     * @param paramContext
     * @param paramEditText
     */
    public static void hideSoftKeyboard(Context paramContext,
                                        EditText paramEditText) {
        ((InputMethodManager) paramContext
                .getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(paramEditText.getWindowToken(), 0);
    }

    /**
     * 显示输入法
     *
     * @param paramContext
     * @param paramEditText
     */
    public static void showSoftKeyborad(Context paramContext,
                                        EditText paramEditText) {
        ((InputMethodManager) paramContext
                .getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(
                paramEditText, InputMethodManager.SHOW_FORCED);
    }

    /**
     * 获取视频的缩略图
     * 先通过ThumbnailUtils来创建一个视频的缩略图，然后再利用ThumbnailUtils来生成指定大小的缩略图。
     * 如果想要的缩略图的宽和高都小于MICRO_KIND，则类型要使用MICRO_KIND作为kind的值，这样会节省内存。
     *
     * @param videoPath 视频的路径
     * @param width     指定输出视频缩略图的宽度
     * @param height    指定输出视频缩略图的高度度
     * @param kind      参照MediaStore.Images(Video).Thumbnails类中的常量MINI_KIND和MICRO_KIND。
     *                  其中，MINI_KIND: 512 x 384，MICRO_KIND: 96 x 96
     * @return 指定大小的视频缩略图
     */
    public static Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) {
        Bitmap bitmap = null;
        // 获取视频的缩略图
        bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind); //調用ThumbnailUtils類的靜態方法createVideoThumbnail獲取視頻的截圖；
        if (bitmap != null) {
            bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                    ThumbnailUtils.OPTIONS_RECYCLE_INPUT);//調用ThumbnailUtils類的靜態方法extractThumbnail將原圖片（即上方截取的圖片）轉化為指定大小；
        }
        return bitmap;
    }

    /**
     * 保存临时temp图片类
     */
    public static String savePic(Bitmap bm) {
        String fileHost = Environment.getExternalStorageDirectory() + "/korean_cache/image_cache";
        File file = new File(fileHost);
        if (!file.exists()) {
            file.mkdirs();
        }
        String filePath = fileHost + "/temp_" + System.currentTimeMillis() + ".jpg";
        boolean isOk = false;
        File image = new File(filePath);
        if (image.exists()) {
            image.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(image);
            isOk = bm.compress(Bitmap.CompressFormat.PNG, 80, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (isOk)
            return filePath;
        return null;
    }

    /**
     * 字段，描述
     * filter[0][field]：字段名称，必须与POJO中大小写相同
     * filter[0][data][value]  ：查找的值
     * filter[0][data][comparison]：比较符。接受：gt,lt,eq,gte,lte
     * filter[0][data][type]：字段类型。接受：number,string,date,list,boolean
     * sort：排序字段，必须与POJO中大小写相同
     * dir：排序，DESC/ASC
     * <p>
     * 或取分页查询的传参
     */
    public static Map<String, String> getPageParam(Map<String, String> map, int postion, String name, String value, String comparison, String type) {
        String key1 = "filter[" + postion + "][field]";
        String key2 = "filter[" + postion + "][data][value]";
        String key3 = "filter[" + postion + "][data][comparison]";
        String key4 = "filter[" + postion + "][data][type]";
        map.put(key1, name);
        map.put(key2, value);
        map.put(key3, comparison);
        map.put(key4, type);
        return map;
    }

    public static List<Object> removeElements(List<Object> list, int start, int end) {

        if (list != null && list.size() > 0) {
            for (int i = start; i < end - 1; i++) {
                Object o = list.get(i);
                if (o != null) {
                    list.remove(i);
                }
            }
        }
        return list;
    }

    // 两次点击按钮之间的点击间隔不能少于1000毫秒
    private static final int MIN_CLICK_DELAY_TIME = 1500;
    private static long lastClickTime = 0;

    public static boolean isFastClick() {
        boolean flag = false;
        long curClickTime = System.currentTimeMillis();
        if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) {
            lastClickTime = curClickTime;
            flag = true;
        }

        return flag;
    }

    public static String appendSpace(String para) {
        try {
            StringBuilder b = new StringBuilder();
            for (int i = 0; i < para.length(); i++) {
                String c = String.valueOf(para.charAt(i));
                b.append(c).append(" ");
            }

            return b.toString();
        } catch (Exception e) {
            //
        }

        return para;
    }



    private static String getSpit(String str) {
        String str1 = "";
        String[] a = str.split("-");
        for (int i = 0; i <= a.length - 1; i++) {
            str1 += a[i];
        }
        return str1;
    }

    //获取文件名
    public static String getFileName(String path) {
        if(TextUtils.isEmpty(path)){
            return "";
        }
        int start = path.lastIndexOf("/");
        if (start != -1 ) {
            return path.substring(start + 1);
        } else {
            return "";
        }
    }
}
