package com.pcstar.people.utils;
/**
 * 描述：此类为工具类包含一些项目中经常用的静态方法（函数）
 * 
 */
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.friendlyarm.AndroidSDK.HardwareControler;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.telephony.SmsManager;

public class Utils
{
	public static Context context;
	
	/**
	 * 描述：检查网络可用
	 * @param activity 当前activity
	 * @return 返回是否可用
	 */
	public static boolean checkNet()
	{
		ConnectivityManager manager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		if(manager == null)
		{
			return false;
		}
		NetworkInfo networkInfo = manager.getActiveNetworkInfo();
		if(networkInfo == null || !networkInfo.isAvailable())
		{
			return false;
		}
		return true;
	}
	/**
	 * 描述：弹出连接网络错误提示框
	 */
	public static void AlertNetError()
	{
		AlertDialog.Builder alertError = new AlertDialog.Builder(context);
		alertError.setTitle("网络错误");
		alertError.setMessage("无法连接网络, 请检查网络配置");
		alertError.setNegativeButton("退出", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				android.os.Process.killProcess(android.os.Process.myPid()); 
				System.exit(0);
			}
		});
		alertError.setPositiveButton("设置", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				context.startActivity(new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS));
			}
		});
		alertError.create().show();
	}
	
	/**
	 * 描述：杀进程退出程序
	 */
	public static void exit(){
		android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
	}
	/**
	 * 描述：判断手机号是否合法
	 */
	public static boolean isMobile(String mobiles){
		String str="^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
		Pattern p = Pattern.compile(str);	  
		Matcher m = p.matcher(mobiles);  		  
		return m.matches();
	} 
	/**
	 * 描述：检查SD卡是否可用
	 * @return true表示可用
	 */
	public static boolean checkSDCard()
	{
		if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
		{
			return true;
		}
		return false;
	}
	/**
	 * @description  显示退出对话框
	 * @param context 上下文
	 */
	public static void showExitDialog(final Context context)
	{
		new AlertDialog.Builder(context).setTitle("系统提示！").setMessage("确定退出程序?").
		setPositiveButton("确定", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		}).setNegativeButton("取消", null).create().show();
	}
	/**
	 * 描述：获得系统当前时间
	 * @return
	 */
	@SuppressLint("SimpleDateFormat")
	public static String getTime(){
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");       
    	Date curDate = new Date(System.currentTimeMillis());//获取当前时间        
    	String str = formatter.format(curDate);  
		return str;		
	}
	/**
	 * @description  显示报警信息对话框
	 * @param str  关闭报警指令
	 */
	public static void showAlertingDialog(final String str)
	{
		new AlertDialog.Builder(context).setTitle("报警提示！").setMessage("正在报警……").
		setPositiveButton("取消报警信息", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				write(str);
			}
		}).setNegativeButton("取消", null).create().show();
	}
	/**
	 * 描述：向串口发指令
	 * @param str
	 */
	public static void write(String str){
		try {
			HardwareControler.write(Configuration.fd, str.getBytes("utf-8"));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 描述：检查IP是否能连接成功
	 * @param ip
	 * @return
	 */
	public static boolean pingIP(String ip){
		boolean state = false;
		try{
			InetAddress ad = InetAddress.getByName(ip);
			state = ad.isReachable(1000);//测试是否可以达到该地址
		}
		catch(Exception e)
		{
			System.err.println("连接失败");
		}
		return state;
		
	} 
	/**
	  * 往指定的手机号上发送短信
	  * @param destPhone
	  * @param message
	  */
	public static void sendSms(String destPhone,String message){
	   SmsManager smsManager=SmsManager.getDefault();
	   //判断所发内容是否超过70个汉字
	   if (message.length() > 70) {
	        ArrayList<String> msgs = smsManager.divideMessage(message);
	        for (String msg : msgs) {
	                smsManager.sendTextMessage(destPhone, null, msg, null, null);
	        }
	   } else {
	           smsManager.sendTextMessage(destPhone, null, message, null, null);
	   }
	}	
}
