package com.jacky.catlauncher.util; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.os.Environment; import android.os.StatFs; import android.telephony.TelephonyManager; import android.view.View; import android.view.ViewGroup; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.io.File; import java.lang.reflect.Method; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public final class Tools { private static String TAG = "Tools"; private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; private Tools() throws InstantiationException { throw new InstantiationException("This class is not created for instantiaation"); } public static String byteArrayToHexString(byte[] b) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) { resultSb.append(byteToHexString(b[i])); } return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; } /** * @param mobiles * @return */ public static boolean isMobileNO(String mobiles) { Pattern p = Pattern .compile("^((13[0-9])|(15[^4,\\D])|(18[0,1,3,5-9]))\\d{8}$"); Matcher m = p.matcher(mobiles); System.out.println(m.matches() + "-telnum-"); return m.matches(); } /** * @param expression * @param text * @return */ private static boolean matchingText(String expression, String text) { Pattern p = Pattern.compile(expression); Matcher m = p.matcher(text); return m.matches(); } /** * @param zipcode * @return */ public static boolean isZipcode(String zipcode) { Pattern p = Pattern.compile("[0-9]\\d{5}"); Matcher m = p.matcher(zipcode); System.out.println(m.matches() + "-zipcode-"); return m.matches(); } /** * @param email * @return */ public static boolean isValidEmail(String email) { Pattern p = Pattern .compile("^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"); Matcher m = p.matcher(email); System.out.println(m.matches() + "-email-"); return m.matches(); } /** * @param telfix * @return */ public static boolean isTelfix(String telfix) { Pattern p = Pattern.compile("d{3}-d{8}|d{4}-d{7}"); Matcher m = p.matcher(telfix); System.out.println(m.matches() + "-telfix-"); return m.matches(); } /** * @param name * @return */ public static boolean isCorrectUserName(String name) { Pattern p = Pattern.compile("([A-Za-z0-9]){2,10}"); Matcher m = p.matcher(name); System.out.println(m.matches() + "-name-"); return m.matches(); } /** * @param pwd * @return */ public static boolean isCorrectUserPwd(String pwd) { Pattern p = Pattern.compile("\\w{6,18}"); Matcher m = p.matcher(pwd); System.out.println(m.matches() + "-pwd-"); return m.matches(); } /** * @return */ public static boolean hasSdcard() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } /** * @param endTime * @param countDown * @return 剩余时间 */ public static String calculationRemainTime(String endTime, long countDown) { SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try { Date now = new Date(System.currentTimeMillis());// ��ȡ��ǰʱ�� Date endData = df.parse(endTime); long l = endData.getTime() - countDown - now.getTime(); long day = l / (24 * 60 * 60 * 1000); long hour = l / (60 * 60 * 1000) - day * 24; long min = (l / (60 * 1000)) - day * 24 * 60 - hour * 60; long s = l / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60; return "ʣ��" + day + "��" + hour + "Сʱ" + min + "��" + s + "��"; } catch (ParseException e) { e.printStackTrace(); } return ""; } public static void showLongToast(Context act, String pMsg) { Toast toast = Toast.makeText(act, pMsg, Toast.LENGTH_LONG); toast.show(); } public static void showShortToast(Context act, String pMsg) { Toast toast = Toast.makeText(act, pMsg, Toast.LENGTH_SHORT); toast.show(); } /** * @param context * @return */ public static String getImeiCode(Context context) { TelephonyManager tm = (TelephonyManager) context .getSystemService(Context.TELEPHONY_SERVICE); return tm.getDeviceId(); } /** * @param listView * @author sunglasses * @category 计算listview高度 */ public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } public static PackageInfo getAPKVersionInfo(Context context, String packageName) { PackageManager packageManager = context.getPackageManager(); PackageInfo packInfo = null; try { packInfo = packageManager.getPackageInfo(packageName, 0); } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return packInfo; } /** * 得到sd卡剩余大小 * * @return */ public static long getSDAvailableSize() { File path = Environment.getExternalStorageDirectory(); StatFs stat = new StatFs(path.getPath()); long blockSize = stat.getBlockSize(); long availableBlocks = stat.getAvailableBlocks(); return blockSize * availableBlocks / 1024; } /** * 判断是否是json结构 */ public static boolean isJson(String value) { try { new JSONObject(value); } catch (JSONException e) { return false; } return true; } public static List<ResolveInfo> findActivitiesForPackage(Context context, String packageName) { final PackageManager packageManager = context.getPackageManager(); final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); mainIntent.setPackage(packageName); final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0); return apps != null ? apps : new ArrayList<ResolveInfo>(); } static public boolean removeBond(Class btClass, BluetoothDevice btDevice) throws Exception { Method removeBondMethod = btClass.getMethod("removeBond"); Boolean returnValue = (Boolean) removeBondMethod.invoke(btDevice); return returnValue.booleanValue(); } }