import android.content.Context;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.net.Uri;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by wuxy on 2017/5/19.
*/
public class PhoneUtils {

//拨打电话
public static void call(Context context, String phone) {
if (context == null) {
return;
}
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
/* *//**验证手机号码
* @param mobiles
* @return
*//*
public static boolean isMobleNo(String mobiles){
String telRegex = "[1][345789]\\d{9}";//"[1]"代表第1位为数字1,"[34578]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。
if (TextUtils.isEmpty(mobiles)) return false;
else return mobiles.matches(telRegex);
}*/

/**
* 验证手机格式
*/
public static boolean isMobleNo(String phone) {
boolean flag = true;
String regex = "^((13[0-9])|(14[5|7|9])|(15([0-3]|[5-9]))|(16[6])|(17[013678])|(18[0-9])|(19[8|9]))\\d{8}$";
if (phone.length() != 11 || TextUtils.isEmpty(phone)) {
flag = false;
return flag;
} else {
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(phone);
boolean isMatch = m.matches();
if (!isMatch) {
flag = false;
}
}
return flag;
}

/**
* 验证电话号码
*
* @param tel
* @return
*/
public static boolean isTelNo(String tel) {
// Pattern p = Pattern.compile("^(0\\d{2,3}-\\d{7,8}(-\\d{3,5}){0,1})|(1[3578]\\d{9})$");
Pattern p = Pattern.compile("^(400|800)([0-9\\\\-]{7,10})|(0?[0-9]{3}(-| )?)?([0-9]{7,8})$");
Matcher m = p.matcher(tel);
return m.matches();
}

/**
* 验证是否为手机号或电话号
*
* @param phoneNumber
* @return
*/
public static boolean isPhoneNumber(String phoneNumber) {
return isMobleNo(phoneNumber) || isTelNo(phoneNumber);
}

/**
* 隐藏手机号中间四位
*
* @param phone
* @return
*/
public static String formatPhone(String phone) {
return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}


/* public static String formatDisplayPhoneByPermission(Context context, String phone) {
if (!SpDataUtils.getIsLogin(context)) {
return formatPhone(phone);
} else {
return phone;
}
}*/

/**
* 获取验证码倒计时
*/
public static void setRegisterTimeCount(final TextView view, long fen, long miao) {
CountDownTimer timer = new CountDownTimer(fen, miao) {
ColorStateList originalColor = view.getTextColors();

@Override
public void onTick(long millisUntilFinished) {
view.setEnabled(false);
view.setAllCaps(false);
view.setTextColor(0xfff47f32);
view.setText(millisUntilFinished / 1000 + "s");
}

@Override
public void onFinish() {
view.setText("重新发送");
view.setTextColor(originalColor);
view.setEnabled(true);
}
};
timer.start();
}
}