content: package com.rsen.util;
  
  import android.content.ActivityNotFoundException;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  import android.graphics.Point;
  import android.net.Uri;
  import android.os.Build;
  import android.telephony.TelephonyManager;
  import android.text.TextUtils;
  import android.util.DisplayMetrics;
  import android.view.WindowManager;
  
  import java.io.File;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  import java.util.Random;
  
  /**
   * Created by angcyo on 15-12-16 016 15:41 下午.
   */
  public class Utils {
  
  
      private static final String[][] MIME_MapTable = {
              //{后缀名，MIME类型}
              {".3gp", "video/3gpp"},
              {".apk", "application/vnd.android.package-archive"},
              {".asf", "video/x-ms-asf"},
              {".avi", "video/x-msvideo"},
              {".bin", "application/octet-stream"},
              {".bmp", "image/bmp"},
              {".c", "text/plain"},
              {".class", "application/octet-stream"},
              {".conf", "text/plain"},
              {".cpp", "text/plain"},
              {".doc", "application/msword"},
              {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
              {".xls", "application/vnd.ms-excel"},
              {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
              {".exe", "application/octet-stream"},
              {".gif", "image/gif"},
              {".gtar", "application/x-gtar"},
              {".gz", "application/x-gzip"},
              {".h", "text/plain"},
              {".htm", "text/html"},
              {".html", "text/html"},
              {".jar", "application/java-archive"},
              {".java", "text/plain"},
              {".jpeg", "image/jpeg"},
              {".jpg", "image/jpeg"},
              {".js", "application/x-javascript"},
              {".log", "text/plain"},
              {".m3u", "audio/x-mpegurl"},
              {".m4a", "audio/mp4a-latm"},
              {".m4b", "audio/mp4a-latm"},
              {".m4p", "audio/mp4a-latm"},
              {".m4u", "video/vnd.mpegurl"},
              {".m4v", "video/x-m4v"},
              {".mov", "video/quicktime"},
              {".mp2", "audio/x-mpeg"},
              {".mp3", "audio/x-mpeg"},
              {".mp4", "video/mp4"},
              {".mpc", "application/vnd.mpohun.certificate"},
              {".mpe", "video/mpeg"},
              {".mpeg", "video/mpeg"},
              {".mpg", "video/mpeg"},
              {".mpg4", "video/mp4"},
              {".mpga", "audio/mpeg"},
              {".msg", "application/vnd.ms-outlook"},
              {".ogg", "audio/ogg"},
              {".pdf", "application/pdf"},
              {".png", "image/png"},
              {".pps", "application/vnd.ms-powerpoint"},
              {".ppt", "application/vnd.ms-powerpoint"},
              {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
              {".prop", "text/plain"},
              {".rc", "text/plain"},
              {".rmvb", "audio/x-pn-realaudio"},
              {".rtf", "application/rtf"},
              {".sh", "text/plain"},
              {".tar", "application/x-tar"},
              {".tgz", "application/x-compressed"},
              {".txt", "text/plain"},
              {".wav", "audio/x-wav"},
              {".wma", "audio/x-ms-wma"},
              {".wmv", "audio/x-ms-wmv"},
              {".wps", "application/vnd.ms-works"},
              {".xml", "text/plain"},
              {".z", "application/x-compress"},
              {".zip", "application/x-zip-compressed"},
              {"", "*/*"}
      };
  
      /**
       * 去除字符串左右的字符
       *
       * @param des the des
       * @return the string
       */
      public static String trimMarks(String des) {
          return trimMarks(des, 1);
      }
  
      /**
       * 去除字符串左右指定个数的字符
       *
       * @param des   the des
       * @param count the count
       * @return the string
       */
      public static String trimMarks(String des, int count) {
          if (des == null || count < 0 || des.length() < count + 1) {
              return des;
          }
          return des.substring(count, des.length() - count);
      }
  
      /**
       * 返回现在的时间,不包含日期
       *
       * @return the now time
       */
      public static String getNowTime() {
          return getNowTime("HH:mm:ss");
      }
  
      /**
       * Gets now time.
       *
       * @param pattern the pattern
       * @return the now time
       */
      public static String getNowTime(String pattern) {
          SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
          return dateFormat.format(new Date());
      }
  
      /**
       * 判断字符串是否为空
       *
       * @param str the str
       * @return the boolean
       */
      public static boolean isEmpty(String str) {
          return (str == null || str.trim().length() < 1);
      }
  
      /**
       * Gets time.
       *
       * @return 按照HH :mm:ss 返回时间
       */
      public static String getTime() {
          SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
          return format.format(new Date());
      }
  
      /**
       * Gets date.
       *
       * @return 按照yyyy -MM-dd 格式返回日期
       */
      public static String getDate() {
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
          return format.format(new Date());
      }
  
      /**
       * Gets date and time.
       *
       * @return 按照yyyy -MM-dd HH:mm:ss 的格式返回日期和时间
       */
      public static String getDateAndTime() {
          SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          return format.format(new Date());
      }
  
      /**
       * Gets date.
       *
       * @param pattern 格式
       * @return 返回日期 date
       */
      public static String getDate(String pattern) {
          SimpleDateFormat format = new SimpleDateFormat(pattern);
          return format.format(new Date());
      }
  
      /**
       * Gets time.
       *
       * @param pattern 格式
       * @return 按照指定格式返回时间 time
       */
      public static String getTime(String pattern) {
          SimpleDateFormat format = new SimpleDateFormat(pattern);
          return format.format(new Date());
      }
  
  //
  
      /**
       * Gets date and time.
       *
       * @param pattern 指定的格式
       * @return 按照指定格式返回日期和时间 date and time
       */
      public static String getDateAndTime(String pattern) {
          SimpleDateFormat format = new SimpleDateFormat(pattern);
          return format.format(new Date());
      }
  
      /**
       * @param context 上下文
       * @return 返回手机号码 tel number
       */
      public static String getTelNumber(Context context) {
          TelephonyManager tm = (TelephonyManager) context
                  .getSystemService(Context.TELEPHONY_SERVICE);
          return tm.getLine1Number();
      }
  
      /**
       * Gets os version.
       *
       * @return 获取device的os version
       */
      public static String getOsVersion() {
          String string = Build.VERSION.RELEASE;
          return string;
      }
  
      /**
       * Gets os sdk.
       *
       * @return 返回设备sdk版本 os sdk
       */
      public static String getOsSdk() {
          int sdk = Build.VERSION.SDK_INT;
          return String.valueOf(sdk);
      }
  
      /**
       * Gets random.
       *
       * @return the random
       */
      public static int getRandom() {
          Random random = new Random();
          return random.nextInt();
      }
  
      /**
       * 获取随机数
       *
       * @param n 最大范围
       * @return random
       */
      public static int getRandom(int n) {
          Random random = new Random();
          return random.nextInt(n);
      }
  
      /**
       * 获取字符数组中随机的字符串
       *
       * @param context the context
       * @param resId   the res id
       * @return random string
       */
      public static String getRandomString(Context context, int resId) {
          String[] strings;
          strings = context.getResources().getStringArray(resId);
  
          return strings[getRandom(strings.length)];
      }
  
      /**
       * 从资源id获取字符串
       *
       * @param context 上下文
       * @param id      资源id
       * @return 字符串 string for res
       */
      public static String getStringForRes(Context context, int id) {
          if (context == null) {
              return "";
          }
          return context.getResources().getString(id);
      }
  
      /**
       * 返回app的版本名称.
       *
       * @param context the context
       * @return app version name
       */
      public static String getAppVersionName(Context context) {
          String version = "unknown";
  // 获取package manager的实例
          PackageManager packageManager = context.getPackageManager();
  // getPackageName()是你当前类的包名，0代表是获取版本信息
          PackageInfo packInfo;
          try {
              packInfo = packageManager.getPackageInfo(context.getPackageName(),
                      0);
              version = packInfo.versionName;
          } catch (PackageManager.NameNotFoundException e) {
              e.printStackTrace();
          }
  // Log.i("版本名称:", version);
          return version;
      }
  
      /**
       * 返回app的版本代码.
       *
       * @param context the context
       * @return app version code
       */
      public static int getAppVersionCode(Context context) {
  // 获取package manager的实例
          PackageManager packageManager = context.getPackageManager();
  // getPackageName()是你当前类的包名，0代表是获取版本信息
          int code = 1;
          PackageInfo packInfo;
          try {
              packInfo = packageManager.getPackageInfo(context.getPackageName(),
                      0);
              code = packInfo.versionCode;
          } catch (PackageManager.NameNotFoundException e) {
              e.printStackTrace();
          }
  // Log.i("版本代码:", version);
          return code;
      }
  
      /**
       * 获取屏幕的宽度高度
       *
       * @param context the context
       * @param size    the size
       * @return the display
       */
      public static DisplayMetrics getDisplay(Context context, Point size) {
          WindowManager windowManager = (WindowManager) context
                  .getSystemService(Context.WINDOW_SERVICE);
          windowManager.getDefaultDisplay().getSize(size);
          DisplayMetrics outMetrics = new DisplayMetrics();
          windowManager.getDefaultDisplay().getMetrics(outMetrics);
          return outMetrics;
      }
  
      /**
       * 返回调用行的方法 所在行号
       *
       * @return the string
       */
      public static String callMethodAndLine() {
          StringBuilder result = new StringBuilder();
          StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
          result.append(thisMethodStack.getClassName() + ".");
          result.append(thisMethodStack.getMethodName());
          result.append("(" + thisMethodStack.getFileName());
          result.append(":" + thisMethodStack.getLineNumber() + ")");
          return result.toString();
      }
  
      /**
       * 返回调用行的类名
       *
       * @return the string
       */
      public static String callClassName() {
          StringBuilder result = new StringBuilder();
          StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
          result.append(thisMethodStack.getClassName());
          return result.toString();
      }
  
      /**
       * 返回调用行的方法名
       *
       * @return the string
       */
      public static String callMethodName() {
          StringBuilder result = new StringBuilder();
          StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
          result.append(thisMethodStack.getMethodName());
          return result.toString();
      }
  
      /**
       * 返回调用行的 类名 和 方法名
       *
       * @return the string
       */
      public static String callClassMethodName() {
          StringBuilder result = new StringBuilder();
          StackTraceElement thisMethodStack = (new Exception()).getStackTrace()[1];
          result.append(thisMethodStack.getClassName() + ".");
          result.append(thisMethodStack.getMethodName());
          return result.toString();
      }
  
      /**
       * 打开网页,调用系统应用
       *
       * @param context the context
       * @param url     the url
       */
      public static void openUrl(Context context, String url) {
          if (TextUtils.isEmpty(url)) {
              return;
          }
  
          if (!url.toLowerCase().startsWith("http:") && !url.toLowerCase().startsWith("https:")) {
              url = "http:".concat(url);
          }
  
          Uri webPage = Uri.parse(url);
          Intent webIntent = new Intent(Intent.ACTION_VIEW, webPage);
          context.startActivity(webIntent);
      }
  
      /**
       * 打开文件
       *
       * @param file
       */
      public static void openFile(Context context, File file) throws ActivityNotFoundException {
  
          Intent intent = new Intent();
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          //设置intent的Action属性
          intent.setAction(Intent.ACTION_VIEW);
          //获取文件file的MIME类型
          String type = getMIMEType(file);
          //设置intent的data和Type属性。
          intent.setDataAndType(/*uri*/Uri.fromFile(file), type);
          //跳转
          context.startActivity(intent);     //这里最好try一下，有可能会报错。 //比如说你的MIME类型是打开邮箱，但是你手机里面没装邮箱客户端，就会报错。
      }
  
      /**
       * 根据文件后缀名获得对应的MIME类型。
       *
       * @param file
       */
      private static String getMIMEType(File file) {
  
          String type = "*/*";
          String fName = file.getName();
          //获取后缀名前的分隔符"."在fName中的位置。
          int dotIndex = fName.lastIndexOf(".");
          if (dotIndex < 0) {
              return type;
          }
      /* 获取文件的后缀名*/
          String end = fName.substring(dotIndex, fName.length()).toLowerCase();
          if (end == "") return type;
          //在MIME和文件类型的匹配表中找到对应的MIME类型。
          for (int i = 0; i < MIME_MapTable.length; i++) { //MIME_MapTable??在这里你一定有疑问，这个MIME_MapTable是什么？
              if (end.equals(MIME_MapTable[i][0]))
                  type = MIME_MapTable[i][1];
          }
          return type;
      }
  
      /**
       * 跳转到应用市场
       */
      public static void jumpToMarket(Context context, String packageName) {
          Uri uri = Uri.parse("market://details?id=" + packageName);
          Intent intent = new Intent(Intent.ACTION_VIEW, uri);
          intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(intent);
      }
  }
  