package com.txooo.utils; import android.content.Context; import android.content.Intent; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.graphics.Color; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; import android.telecom.PhoneAccountHandle; import android.telecom.TelecomManager; import android.text.TextUtils; import android.text.format.Time; import android.view.View; import com.txooo.library.utils.phone.SimUtils; import com.txooo.ui.log.Trace; import com.txooo.ui.view.TSnackbarView; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.Random; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ToolUtil { /** * 判断字符串是不是数字 * * @param str * @return */ public static boolean isNumeric(String str) { Pattern pattern = Pattern.compile("[0-9]*"); Matcher isNum = pattern.matcher(str); if (!isNum.matches()) { return false; } return true; } //验证手机 public static boolean isMobile(String str) { Pattern p = null; Matcher m = null; boolean b = false; p = Pattern.compile("^[1][1,2,3,4,5,6,7,8,9][0-9]{9}$"); // 验证手机号 m = p.matcher(str); b = m.matches(); return b; } //隐藏手机后四位 public static String hidePhoneLastFour(String phone) { //$1,$2分别匹配第一个括号和第二个括号中的内容 return phone.replaceAll("(\\d{3})(\\d{4})\\d{4}", "$1$2****"); } // //验证邮箱 // public static boolean isEmail(String email) { // String str = "^([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)*@([a-zA-Z0-9]*[-_]?[a-zA-Z0-9]+)+[\\.][A-Za-z]{2,3}([\\.][A-Za-z]{2})?$"; // Pattern p = Pattern.compile(str); // Matcher m = p.matcher(email); // return m.matches(); // } //邮箱验证 public static boolean isEmail(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]$"; if (TextUtils.isEmpty(strPattern)) { return false; } else { return strEmail.matches(strPattern); } } //验证qq 5-10位qq public static boolean isQQ(String qq) { String str = "^[1-9]{1}[0-9]{4,9}$"; Pattern p = Pattern.compile(str); Matcher m = p.matcher(qq); return m.matches(); } /** * 验证身份证号是否符合规则 * * @param text 身份证号 * @return */ public static boolean isIdentityId(String text) { String regx = "[0-9]{17}x"; String reg1 = "[0-9]{15}"; String regex = "[0-9]{18}"; return text.matches(regx) || text.matches(reg1) || text.matches(regex); } //验证邮政编码 public static boolean checkPost(String post) { if (post.matches("[1-9]\\d{5}(?!\\d)")) { return true; } else { return false; } } /* 校验银行卡卡号 校验过程: 1、从卡号最后一位数字开始,逆向将奇数位(1、3、5等等)相加。 2、从卡号最后一位数字开始,逆向将偶数位数字,先乘以2(如果乘积为两位数,将个位十位数字相加,即将其减去9),再求和。 3、将奇数位总和加上偶数位总和,结果应该可以被10整除。 */ public static boolean checkBankCard(String bankCard) { if (bankCard.length() < 15 || bankCard.length() > 19) { return false; } char bit = getBankCardCheckCode(bankCard.substring(0, bankCard.length() - 1)); if (bit == 'N') { return false; } return bankCard.charAt(bankCard.length() - 1) == bit; } /** * 从不含校验位的银行卡卡号采用 Luhm 校验算法获得校验位 * * @param nonCheckCodeBankCard * @return */ public static char getBankCardCheckCode(String nonCheckCodeBankCard) { if (nonCheckCodeBankCard == null || nonCheckCodeBankCard.trim().length() == 0 || !nonCheckCodeBankCard.matches("\\d+")) { //如果传的不是数据返回N return 'N'; } char[] chs = nonCheckCodeBankCard.trim().toCharArray(); int luhmSum = 0; for (int i = chs.length - 1, j = 0; i >= 0; i--, j++) { int k = chs[i] - '0'; if (j % 2 == 0) { k *= 2; k = k / 10 + k % 10; } luhmSum += k; } return (luhmSum % 10 == 0) ? '0' : (char) ((10 - luhmSum % 10) + '0'); } /** * 获取过去第几天的日期 * * @param past * @return */ public static String getPastDate(int past) { Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past); Date today = calendar.getTime(); SimpleDateFormat format = new SimpleDateFormat("yyyy.MM.dd"); String result = format.format(today); return result; } //打电话 public static void dialPhoneNumber(final Context context, final String phoneNumber, View view) { try { //电话号码有时获取不到,所以获取运营商(中国移动/中国联通/中国电信) String operator1 = SimUtils.getSimOperatorName(context, 0);//卡1 String operator2 = SimUtils.getSimOperatorName(context, 1);//卡2 Trace.e("卡1=" + operator1 + ";卡2=" + operator2); if (TextUtils.isEmpty(operator1) && TextUtils.isEmpty(operator2)) {//无卡 if (view != null) { new TSnackbarView(view, "手机无SIM卡,无法拨打电话"); } } else if (!TextUtils.isEmpty(operator1) && !TextUtils.isEmpty(operator2)) {//双卡 String[] a = {"用卡1拨打电话", "用卡2拨打电话"}; // new PopupSelectSimView(context,view) // .builder() // .setData(a) // .setOnItemClickListener(new AdapterView.OnItemClickListener() { // @Override // public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // call(context,position,phoneNumber); // } // }) // .show(); } else {//单卡 Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + phoneNumber)); if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } } } catch (Exception e) { Trace.e("异常=" + e); } } public static void call(Context context, int id, String telNum) { try { TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); if (telecomManager != null) { List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts(); Intent intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + telNum)); intent.putExtra(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE, phoneAccountHandleList.get(id)); context.startActivity(intent); } } catch (Exception e) { } } public static boolean isMultiSim(Context context) { boolean result = false; TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE); if (telecomManager != null) { List<PhoneAccountHandle> phoneAccountHandleList = telecomManager.getCallCapablePhoneAccounts(); result = phoneAccountHandleList.size() >= 2; } return result; } //随机取颜色 public static ArrayList<Integer> getColors(int size) { try { ArrayList<Integer> pieColors = new ArrayList<Integer>(); for (int i = 0; i < size; i++) { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); for (int j = 0; j < 3; j++) { stringBuffer.append(Integer.toHexString(new Random().nextInt(200) + 50)); } if (pieColors.size() != 0) { for (int n = 0; n < pieColors.size(); n++) {//判断是否有重复的 if (pieColors.get(n) == Color.parseColor(stringBuffer.toString())) { i -= 1; pieColors.remove(pieColors.size() - 1); break; } else { if (n == pieColors.size() - 1) { pieColors.add(Color.parseColor(stringBuffer.toString())); break; } } } } else { pieColors.add(Color.parseColor(stringBuffer.toString())); } } return pieColors; } catch (Exception e) { Trace.e("异常=" + e); } return null; } //随机取一个颜色 public static String getOneColor() { try { StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("#"); for (int j = 0; j < 3; j++) { stringBuffer.append(Integer.toHexString(new Random().nextInt(200) + 50)); } return stringBuffer.toString(); } catch (Exception e) { Trace.e("异常=" + e); } return null; } //拨号器页面 public static void dialPhoneNumber(Context context, String phoneNumber) { Intent intent = new Intent(Intent.ACTION_DIAL); intent.setData(Uri.parse("tel:" + phoneNumber)); if (intent.resolveActivity(context.getPackageManager()) != null) { context.startActivity(intent); } } // 校验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 boolean isConnected(Context context) { ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = conn.getActiveNetworkInfo(); return (info != null && info.isConnected()); } /** * 判断当前系统时间是否在指定时间的范围内 * * @param beginHour 开始小时,例如22 * @param beginMin 开始小时的分钟数,例如30 * @param endHour 结束小时,例如 8 * @param endMin 结束小时的分钟数,例如0 * @return true表示在范围内,否则false */ public static boolean isCurrentInTimeScope(int beginHour, int beginMin, int endHour, int endMin) { boolean result = false; final long aDayInMillis = 1000 * 60 * 60 * 24; final long currentTimeMillis = System.currentTimeMillis(); Time now = new Time(); now.set(currentTimeMillis); Time startTime = new Time(); startTime.set(currentTimeMillis); startTime.hour = beginHour; startTime.minute = beginMin; Time endTime = new Time(); endTime.set(currentTimeMillis); endTime.hour = endHour; endTime.minute = endMin; if(beginHour==endHour && beginMin==endMin){ return false; } if (!startTime.before(endTime)) { // 跨天的特殊情况(比如22:00-8:00) startTime.set(startTime.toMillis(true) - aDayInMillis); result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime Time startTimeInThisDay = new Time(); startTimeInThisDay.set(startTime.toMillis(true) + aDayInMillis); if (!now.before(startTimeInThisDay)) { result = true; } } else { // 普通情况(比如 8:00 - 14:00) result = !now.before(startTime) && !now.after(endTime); // startTime <= now <= endTime } return result; } // 两次点击按钮之间的点击间隔不能少于1000毫秒 private static final int MIN_CLICK_DELAY_TIME = 1000; private static long lastClickTime; public static boolean isFastClick() { boolean flag = false; long curClickTime = System.currentTimeMillis(); if ((curClickTime - lastClickTime) >= MIN_CLICK_DELAY_TIME) { flag = true; } lastClickTime = curClickTime; return flag; } }
账号登录过滤
最新推荐文章于 2022-10-25 16:47:15 发布