package com.device.comm.mylibrary.NativeDevice.cgmadi.util;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import java.io.PrintStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DataFormatUtil
{
  public DataFormatUtil() {}
  
  public static boolean isServiceRunning(Context mContext, String className)
  {
    boolean isRunning = false;
    ActivityManager activityManager = (ActivityManager)mContext.getSystemService("activity");
    List<RunningServiceInfo> serviceList = activityManager.getRunningServices(100);
    for (int i = 0; i < serviceList.size(); i++) {
      if (((RunningServiceInfo)serviceList.get(i)).service.getClassName().equals(className))
      {
        isRunning = ((RunningServiceInfo)serviceList.get(i)).started;
        break;
      }
    }
    return isRunning;
  }
  
  public static String date2String(Date date)
  {
    SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return dateformat1.format(date);
  }
  
  public static String date2StringWithoutSecond(Date date)
  {
    SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    return dateformat1.format(date);
  }
  
  public static String date2String(Calendar date)
  {
    SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return dateformat1.format(date.getTime());
  }
  
  public static String date2String(Date date, String format)
  {
    SimpleDateFormat dateformat1 = new SimpleDateFormat(format);
    return dateformat1.format(date);
  }
  
  public static Date string2Date(String source)
  {
    if ((source == null) || ("".equals(source))) {
      return null;
    }
    SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    try
    {
      return dateformat1.parse(source);
    }
    catch (ParseException e) {}
    return null;
  }
  
  public static String double2String(double d, int n)
  {
    return String.format("%." + n + "f", new Object[] { Double.valueOf(d) });
  }
  
  public static String float2String(float d, int n)
  {
    return String.format("%." + n + "f", new Object[] { Float.valueOf(d) });
  }
  
  public static int byte2int(byte b)
  {
    return b < 0 ? b & 0xFF : b;
  }
  
  public static byte int2byte(int i)
  {
    return (byte)(i & 0xFF);
  }
  
  public static byte[] int2WordBytes(int n)
  {
//    if (!$assertionsDisabled) {
//      if (((n >= 0 ? 1 : 0) & (n <= 65535 ? 1 : 0)) == 0) {
//        throw new AssertionError();
//      }
//    }
    int i = n & 0xFFFF;
    byte[] barray = new byte[2];
    barray[0] = ((byte)(i >> 8));
    barray[1] = ((byte)i);
    return barray;
  }
  
  public static byte[] int2WordBytesAndLow(int n)
  {
    return changeHighAndLow(int2WordBytes(n));
  }
  
  public static int wordBytes2int(byte[] data)
  {
    int ret = 0;
    int maxlen = data.length > 2 ? 2 : data.length;
    for (int i = 0; i < maxlen; i++) {
      ret = (int)(ret + byte2int(data[i]) * Math.pow(256.0D, maxlen - 1 - i));
    }
    return ret;
  }
  
  public static byte[] long2DwordBytes(long value)
  {
    byte[] data = new byte[4];
    for (int i = 0; i < data.length; i++) {
      data[(data.length - 1 - i)] = ((byte)(int)(value >> 8 * i));
    }
    return data;
  }
  
  public static long dwordBytes2Long(byte[] data)
  {
    long ret = 0L;
    int maxlen = data.length > 4 ? 4 : data.length;
    for (int i = 0; i < maxlen; i++) {
//      ret = (ret + byte2int(data[i]) * Math.pow(256.0D, maxlen - 1 - i));
    }
    return ret;
  }
  
  public static byte decade2BCDByte(int number)
  {
    byte retbyte = 0;
    byte one = (byte)(number % 10);
    byte ten = (byte)(number / 10 % 10);
    retbyte = (byte)(ten << 4 | one);
    return retbyte;
  }
  
  public static byte decade2BCDByte(String strNum)
  {
    int number = Integer.valueOf(strNum).intValue();
    return decade2BCDByte(number);
  }
  
  public static byte[] date2BCDBytes(Date date)
  {
    byte[] retbytes = new byte[6];
    Calendar datetime = Calendar.getInstance();
    datetime.setTime(date);
//    retbytes[0] = decade2BCDByte(datetime.get(1));
//    retbytes[1] = decade2BCDByte(datetime.get(2) + 1);
//    retbytes[2] = decade2BCDByte(datetime.get(5));
//    retbytes[3] = decade2BCDByte(datetime.get(11));
//    retbytes[4] = decade2BCDByte(datetime.get(12));
//    retbytes[5] = decade2BCDByte(datetime.get(13));
    return retbytes;
  }
  
  public static byte[] assemblyByteArrays(List<byte[]> arrays)
  {
    int size = 0;
    for (int i = 0; i < arrays.size(); i++) {
      size += ((byte[])arrays.get(i)).length;
    }
    byte[] result = new byte[size];
    int startIndex = 0;
    for (int i = 0; i < arrays.size(); i++)
    {
      byte[] array = (byte[])arrays.get(i);
      System.arraycopy(array, 0, result, startIndex, array.length);
      startIndex += array.length;
    }
    return result;
  }
  
  public static byte[] mobilePhone2BCDBytes(String strPhone)
  {
    String phoneStr = new String(strPhone);
    while (phoneStr.length() < 12) {
      phoneStr = "0" + phoneStr;
    }
    byte[] bytesPhone = new byte[6];
    for (int i = 0; i < 6; i++) {
      bytesPhone[i] = decade2BCDByte(phoneStr.substring(2 * i, 2 + 2 * i));
    }
    return bytesPhone;
  }
  
  public static String BCDBytes2MobilePhone(byte[] bytesPhone)
  {
    StringBuilder phoneNo = new StringBuilder();
    for (int i = 0; i < bytesPhone.length; i++)
    {
      byte b = bytesPhone[i];
      phoneNo.append(byte2int((byte)(b >> 4 & 0xF)));
      phoneNo.append(byte2int((byte)(b & 0xF)));
    }
    String phone = phoneNo.toString();
    return phone.startsWith("0") ? phone.substring(1) : phone;
  }
  
  public static byte[] bitthday2BCDBytes(String birthday)
  {
    byte[] bytesBirthday = new byte[4];
    for (int i = 0; i < bytesBirthday.length; i++) {
      bytesBirthday[i] = decade2BCDByte(birthday.substring(2 * i, 2 + 2 * i));
    }
    return bytesBirthday;
  }
  
  public static ArrayList<Byte> getByteListFromByteArray(byte[] bytes)
  {
    ArrayList<Byte> byteList = new ArrayList(bytes.length);
    for (int i = 0; i < bytes.length; i++) {
      byteList.add(Byte.valueOf(bytes[i]));
    }
    return byteList;
  }
  
  public static byte[] getByteArrayFromByteList(List<Byte> byteList)
  {
    byte[] byteArray = new byte[byteList.size()];
    for (int i = 0; i < byteList.size(); i++) {
      byteArray[i] = ((Byte)byteList.get(i)).byteValue();
    }
    return byteArray;
  }
  
  public static String bytes2HexString(byte[] b)
  {
    StringBuilder ret = new StringBuilder("");
    for (int i = 0; i < b.length; i++)
    {
      String hex = Integer.toHexString(b[i] & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      ret.append(hex.toUpperCase());
    }
    return ret.toString();
  }
  
  public static String bytes2HexString(List<Byte> b)
  {
    StringBuilder ret = new StringBuilder("");
    for (int i = 0; i < b.size(); i++)
    {
      String hex = Integer.toHexString(((Byte)b.get(i)).byteValue() & 0xFF);
      if (hex.length() == 1) {
        hex = '0' + hex;
      }
      ret.append(hex.toUpperCase());
    }
    return ret.toString();
  }
  
  public static String byte2HexString(byte b)
  {
    String hex = Integer.toHexString(b & 0xFF);
    if (hex.length() == 1) {
      hex = '0' + hex;
    }
    return hex;
  }
  
  public static int bytesToUint(byte[] bytes)
  {
    int ret = 0;
    int maxlen = bytes.length > 4 ? 4 : bytes.length;
    for (int i = 0; i < maxlen; i++) {
      ret = (int)(ret + byte2int(bytes[i]) * Math.pow(256.0D, maxlen - 1 - i));
    }
    return ret;
  }
  
  public static byte[] getRange(byte[] src, int start, int count)
  {
    byte[] ret = new byte[count];
    for (int i = 0; i < count; i++) {
      ret[i] = src[(start + i)];
    }
    return ret;
  }
  
  public static byte[] getRange(byte[] src, int start)
  {
    int count = src.length - start;
    return getRange(src, start, count);
  }
  
//  public static String getLocationString(Location location)
//  {
//    Date gpsTime = new Date(location.getTime());
//    double latitude = location.getLatitude();
//    double longitude = location.getLongitude();
//    String latitudeType;
//    String latitudeType;
//    if (latitude >= 0.0D) {
//      latitudeType = "N";
//    } else {
//      latitudeType = "S";
//    }
//    String longitudeType;
//    String longitudeType;
//    if (longitude >= 0.0D) {
//      longitudeType = "E";
//    } else {
//      longitudeType = "W";
//    }
//    float speed = location.getSpeed();
//    int height = (int)location.getAltitude();
//    int direction = (int)location.getBearing();
//    StringBuilder sb = new StringBuilder();
//    sb.append("Time:" + date2String(gpsTime) + ",");
//    sb.append("latitude:" + latitude + latitudeType + ",");
//    sb.append("longitude:" + latitude + longitudeType + ",");
//    sb.append("speed:" + (int)(speed * 100.0F) + ",");
//    sb.append("height:" + height + ",");
//    sb.append("direction:" + direction);
//    return sb.toString();
//  }
  
  public static boolean isNetworkAvailable(Context context)
  {
    ConnectivityManager connectivity = (ConnectivityManager)context.getSystemService("connectivity");
    if (connectivity == null) {
      return false;
    }
    NetworkInfo info = connectivity.getActiveNetworkInfo();
    if (info == null) {
      return false;
    }
    if (info.isAvailable()) {
      return true;
    }
    System.out.println("**** newwork is off");
    return false;
  }
  
  public static boolean isWiFiActive(Context inContext)
  {
    WifiManager mWifiManager = (WifiManager)inContext.getSystemService("wifi");
    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    int ipAddress = wifiInfo == null ? 0 : wifiInfo.getIpAddress();
    if ((mWifiManager.isWifiEnabled()) && (ipAddress != 0)) {
      return true;
    }
    return false;
  }
  
  public static boolean isEmail(String email)
  {
    String regex = "^[a-zA-Z0-9]+([-._]+\\w+)*@\\w+([-._]+\\w+)*\\.\\w+([-._]+\\w+)*$";
    Pattern p = Pattern.compile(regex, 2);
    Matcher m = p.matcher(email);
    return m.matches();
  }
  
  public static boolean isMobilePhone(String phone)
  {
    String regex = "^(1(([35][0-9])|(47)|[8][0-9]))\\d{8}$";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(phone);
    return m.matches();
  }
  
  public static boolean isWordNumber(String str)
  {
    String regex = "^[0-9a-zA-Z]*$";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(str);
    return m.matches();
  }
  
  public static boolean isIDCard(String idCard)
  {
    String regex = "=/^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$/";
    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(idCard);
    boolean is15 = m.matches();
    if (!is15)
    {
      regex = "/^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$/";
      p = Pattern.compile(regex);
      m = p.matcher(idCard);
      return m.matches();
    }
    return Boolean.TRUE.booleanValue();
  }
  
  public static boolean isChinese(Context mContext)
  {
    String able = mContext.getResources().getConfiguration().locale.getCountry();
    return "CN".equals(able);
  }
  
  public static boolean isTrueLength(String str, int min, int max)
  {
    if ((str.length() < min) || (str.length() > max)) {
      return Boolean.FALSE.booleanValue();
    }
    return Boolean.TRUE.booleanValue();
  }
  
  public static boolean isEmpty(Object obj)
  {
    if (obj == null) {
      return Boolean.TRUE.booleanValue();
    }
    return Boolean.FALSE.booleanValue();
  }
  
  public static boolean isEmpty(String str)
  {
    if ((str == null) || (str.trim().length() == 0)) {
      return Boolean.TRUE.booleanValue();
    }
    return Boolean.FALSE.booleanValue();
  }
  
  public static boolean isEmpty(Collection c)
  {
    if ((c == null) || (c.size() == 0)) {
      return Boolean.TRUE.booleanValue();
    }
    return Boolean.FALSE.booleanValue();
  }
  
  public static boolean isInteger(String value)
  {
    try
    {
      Integer.valueOf(value);
      return Boolean.TRUE.booleanValue();
    }
    catch (Exception e) {}
    return Boolean.FALSE.booleanValue();
  }
  
  public static boolean isFloat(String value)
  {
    try
    {
      Float.valueOf(value);
      return Boolean.TRUE.booleanValue();
    }
    catch (Exception e) {}
    return Boolean.FALSE.booleanValue();
  }
  
  public static boolean isDouble(String value)
  {
    try
    {
      Double.valueOf(value);
      return Boolean.TRUE.booleanValue();
    }
    catch (Exception e) {}
    return Boolean.FALSE.booleanValue();
  }
  
  public static boolean isSQLRepeatError(Exception e)
  {
    if (e.getMessage().indexOf("are not unique") >= 0) {
      return Boolean.TRUE.booleanValue();
    }
    return Boolean.FALSE.booleanValue();
  }
  
  public static Double roundDouble(double val, int precision)
  {
    Double ret = null;
    try
    {
      double factor = Math.pow(10.0D, precision);
      ret = Double.valueOf(Math.floor(val * factor + 0.5D) / factor);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }
    return ret;
  }
  
  public static String complement(String src, char film, int filmlength)
  {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < filmlength; i++) {
      sb.append(film);
    }
    sb.append(src);
    return sb.toString();
  }
  
  public static byte[] changeHighAndLow(byte[] changeByte)
  {
    if (changeByte == null) {
      return null;
    }
    if (changeByte.length == 0) {
      return null;
    }
    int length = changeByte.length;
    if (changeByte.length % 2 == 1) {
      length--;
    }
    byte[] afterChangeByte = new byte[length];
    for (int i = 0; i < length; i += 2)
    {
      afterChangeByte[i] = changeByte[(i + 1)];
      afterChangeByte[(i + 1)] = changeByte[i];
    }
    return afterChangeByte;
  }
  
  public static Integer[] byteArray2Int(byte[] byteArray)
  {
    if (byteArray == null) {
      return null;
    }
    if (byteArray.length == 0) {
      return null;
    }
    int intArrayLength = byteArray.length / 2;
    Integer[] byte2IntArray = new Integer[intArrayLength];
    for (int i = 0; i < intArrayLength; i++)
    {
      byte[] temp = { byteArray[(i * 2)], byteArray[(i * 2 + 1)] };
      byte2IntArray[i] = Integer.valueOf(wordBytes2int(temp));
    }
    return byte2IntArray;
  }
  
  public static Double[] byteArray2Elec(byte[] byteArray)
  {
    if (byteArray == null) {
      return null;
    }
    if (byteArray.length == 0) {
      return null;
    }
    int intArrayLength = byteArray.length / 2;
    Double[] byte2IntArray = new Double[intArrayLength];
    for (int i = 0; i < intArrayLength; i++)
    {
      byte[] temp = { byteArray[(i * 2)], byteArray[(i * 2 + 1)] };
      byte2IntArray[i] = Double.valueOf(wordBytes2int(temp) * 0.1D);
    }
    return byte2IntArray;
  }
  
  public static Double[] byteArray2Vcc(byte[] byteArray)
  {
    if (byteArray == null) {
      return null;
    }
    if (byteArray.length == 0) {
      return null;
    }
    int intArrayLength = byteArray.length / 2;
    Double[] byte2IntArray = new Double[intArrayLength];
    for (int i = 0; i < intArrayLength; i++)
    {
      byte[] temp = { byteArray[(i * 2)], byteArray[(i * 2 + 1)] };
      byte2IntArray[i] = Double.valueOf(wordBytes2int(temp) * 0.01D);
    }
    return byte2IntArray;
  }
}


/* Location:              /Users/lvwang2002/Desktop/CodeSource/work/DY_GLUCOSE/thapi_test/libs/cgmthapi.jar!/com/sanme/cgmadi/bluetooth4/util/DataFormatUtil.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */