/*
 * *********************************************************
 *   author   colin
 *   email    wanglin2046@126.com
 *   date     20-3-12 下午4:45
 * ********************************************************
 */

package com.zcolin.frame.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.widget.Toast;

import java.io.File;

/**
 * 使用第三方程序打开文件 操作工具类
 */
public class FileOpenUtil {

    /*文件后缀名和MIME类型组成的二维数组*/
    private static final String[][] MIME_MapTable = {{".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"},
                                                     {".rar", "application/x-rar-compressed"},
                                                     {"", "*/*"}};

    /**
     * 打开文件
     *
     * @param file 需要打开的文件
     */
    public static void openFile(Context context, File file) {
        String type = getMIMEType(file);
        openFile(context, type, file);
    }

    /**
     * 打开文件
     *
     * @param type 文件类型
     * @param file 需要打开的文件
     */
    public static void openFile(Context context, String type, File file) {
        Intent intent = new Intent();
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        }
        intent.setDataAndType(NUriParseUtil.get(Uri.fromFile(file)), type);

        String suffixName;
        int index = file.getName().lastIndexOf(".");
        if (index >= 0) {
            suffixName = file.getName().substring(index + 1);
        } else {
            suffixName = file.getName();
        }

        try {
            context.startActivity(intent);
        } catch (Exception e) {
            Toast.makeText(context, "本机暂无可打开此文件格式（" + suffixName + ")的应用程序，请前往应用商店下载.", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 文件选择
     *
     * @param type 文件类型
     */
    public static void selectFile(Activity context, String type, int requestCode) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_GET_CONTENT);
        intent.setType(/*"image/*"*/type);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivityForResult(intent, requestCode);
    }

    /**
     * 根据文件后缀名获得对应的MIME类型。
     *
     * @param file 文件对象
     *
     * @return 文件类型
     */
    @SuppressLint("DefaultLocale")
    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).toLowerCase();
        if (end.length() == 0) {
            return type;
        }
        //在MIME和文件类型的匹配表中找到对应的MIME类型。 
        for (String[] aMIME_MapTable : MIME_MapTable) {
            if (end.equals(aMIME_MapTable[0])) {
                type = aMIME_MapTable[1];
                break;
            }
        }
        return type;
    }
}
