package de.beyondsoft.ownchat.utils;/*
 * Copyright (C) 2007-2008 OpenIntents.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.ContactsContract;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.util.Log;
import android.webkit.MimeTypeMap;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * @author Peli
 * @author paulburke (ipaulpro)
 * @version 2013-12-11
 */
public class FileUtils {
    private FileUtils() {
    } //private constructor to enforce Singleton pattern

    /** TAG for log messages. */
    static final String TAG = "FileUtils";
    private static final boolean DEBUG = false; // Set to true to enable logging

    public static final String MIME_TYPE_AUDIO = "audio/*";
    public static final String MIME_TYPE_TEXT = "text/*";
    private static final String MIME_TYPE_IMAGE = "image/*";
    public static final String MIME_TYPE_VIDEO = "video/*";
    public static final String MIME_TYPE_APP = "application/*";

    private static final String HIDDEN_PREFIX = ".";

    private static final HashMap<String, String> types = new HashMap<>();
    private static HashMap<String, String> getTypes() {
        if(types.size() == 0) {
            Log.d("GETTING_MIME_TYPES", "types have a size of 0");
            types.put("323","text/h323");
            types.put("3g2","video/3gpp2");
            types.put("3gp","video/3gpp");
            types.put("3gp2","video/3gpp2");
            types.put("3gpp","video/3gpp");
            types.put("7z","application/x-7z-compressed");
            types.put("aa","audio/audible");
            types.put("AAC","audio/aac");
            types.put("aaf","application/octet-stream");
            types.put("aax","audio/vnd.audible.aax");
            types.put("ac3","audio/ac3");
            types.put("aca","application/octet-stream");
            types.put("accda","application/msaccess.addin");
            types.put("accdb","application/msaccess");
            types.put("accdc","application/msaccess.cab");
            types.put("accde","application/msaccess");
            types.put("accdr","application/msaccess.runtime");
            types.put("accdt","application/msaccess");
            types.put("accdw","application/msaccess.webapplication");
            types.put("accft","application/msaccess.ftemplate");
            types.put("acx","application/internet-property-stream");
            types.put("AddIn","text/xml");
            types.put("ade","application/msaccess");
            types.put("adobebridge","application/x-bridge-url");
            types.put("adp","application/msaccess");
            types.put("ADT","audio/vnd.dlna.adts");
            types.put("ADTS","audio/aac");
            types.put("afm","application/octet-stream");
            types.put("ai","application/postscript");
            types.put("aif","audio/aiff");
            types.put("aifc","audio/aiff");
            types.put("aiff","audio/aiff");
            types.put("air","application/vnd.adobe.air-application-installer-package+zip");
            types.put("amc","application/mpeg");
            types.put("anx","application/annodex");
            types.put("apk","application/vnd.android.package-archive");
            types.put("application","application/x-ms-application");
            types.put("art","image/x-jg");
            types.put("asa","application/xml");
            types.put("asax","application/xml");
            types.put("ascx","application/xml");
            types.put("asd","application/octet-stream");
            types.put("asf","video/x-ms-asf");
            types.put("ashx","application/xml");
            types.put("asi","application/octet-stream");
            types.put("asm","text/plain");
            types.put("asmx","application/xml");
            types.put("aspx","application/xml");
            types.put("asr","video/x-ms-asf");
            types.put("asx","video/x-ms-asf");
            types.put("atom","application/atom+xml");
            types.put("au","audio/basic");
            types.put("avi","video/x-msvideo");
            types.put("axa","audio/annodex");
            types.put("axs","application/olescript");
            types.put("axv","video/annodex");
            types.put("bas","text/plain");
            types.put("bcpio","application/x-bcpio");
            types.put("bin","application/octet-stream");
            types.put("bmp","image/bmp");
            types.put("c","text/plain");
            types.put("cab","application/octet-stream");
            types.put("caf","audio/x-caf");
            types.put("calx","application/vnd.ms-office.calx");
            types.put("cat","application/vnd.ms-pki.seccat");
            types.put("cc","text/plain");
            types.put("cd","text/plain");
            types.put("cdda","audio/aiff");
            types.put("cdf","application/x-cdf");
            types.put("cer","application/x-x509-ca-cert");
            types.put("cfg","text/plain");
            types.put("chm","application/octet-stream");
            types.put("class","application/x-java-applet");
            types.put("clp","application/x-msclip");
            types.put("cmd","text/plain");
            types.put("cmx","image/x-cmx");
            types.put("cnf","text/plain");
            types.put("cod","image/cis-cod");
            types.put("config","application/xml");
            types.put("contact","text/x-ms-contact");
            types.put("coverage","application/xml");
            types.put("cpio","application/x-cpio");
            types.put("cpp","text/plain");
            types.put("crd","application/x-mscardfile");
            types.put("crl","application/pkix-crl");
            types.put("crt","application/x-x509-ca-cert");
            types.put("cs","text/plain");
            types.put("csdproj","text/plain");
            types.put("csh","application/x-csh");
            types.put("csproj","text/plain");
            types.put("css","text/css");
            types.put("csv","text/csv");
            types.put("cur","application/octet-stream");
            types.put("cxx","text/plain");
            types.put("dat","application/octet-stream");
            types.put("datasource","application/xml");
            types.put("dbproj","text/plain");
            types.put("dcr","application/x-director");
            types.put("def","text/plain");
            types.put("deploy","application/octet-stream");
            types.put("der","application/x-x509-ca-cert");
            types.put("dgml","application/xml");
            types.put("dib","image/bmp");
            types.put("dif","video/x-dv");
            types.put("dir","application/x-director");
            types.put("disco","text/xml");
            types.put("divx","video/divx");
            types.put("dll","application/x-msdownload");
            types.put("dll.config","text/xml");
            types.put("dlm","text/dlm");
            types.put("doc","application/msword");
            types.put("docm","application/vnd.ms-word.document.macroEnabled.12");
            types.put("docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
            types.put("dot","application/msword");
            types.put("dotm","application/vnd.ms-word.template.macroEnabled.12");
            types.put("dotx","application/vnd.openxmlformats-officedocument.wordprocessingml.template");
            types.put("dsp","application/octet-stream");
            types.put("dsw","text/plain");
            types.put("dtd","text/xml");
            types.put("dtsConfig","text/xml");
            types.put("dv","video/x-dv");
            types.put("dvi","application/x-dvi");
            types.put("dwf","drawing/x-dwf");
            types.put("dwp","application/octet-stream");
            types.put("dxr","application/x-director");
            types.put("eml","message/rfc822");
            types.put("emz","application/octet-stream");
            types.put("eot","application/vnd.ms-fontobject");
            types.put("eps","application/postscript");
            types.put("etl","application/etl");
            types.put("etx","text/x-setext");
            types.put("evy","application/envoy");
            types.put("exe","application/octet-stream");
            types.put("exe.config","text/xml");
            types.put("fdf","application/vnd.fdf");
            types.put("fif","application/fractals");
            types.put("filters","application/xml");
            types.put("fla","application/octet-stream");
            types.put("flac","audio/flac");
            types.put("flr","x-world/x-vrml");
            types.put("flv","video/x-flv");
            types.put("fsscript","application/fsharp-script");
            types.put("fsx","application/fsharp-script");
            types.put("generictest","application/xml");
            types.put("gif","image/gif");
            types.put("group","text/x-ms-group");
            types.put("gsm","audio/x-gsm");
            types.put("gtar","application/x-gtar");
            types.put("gz","application/x-gzip");
            types.put("h","text/plain");
            types.put("hdf","application/x-hdf");
            types.put("hdml","text/x-hdml");
            types.put("hhc","application/x-oleobject");
            types.put("hhk","application/octet-stream");
            types.put("hhp","application/octet-stream");
            types.put("hlp","application/winhlp");
            types.put("hpp","text/plain");
            types.put("hqx","application/mac-binhex40");
            types.put("hta","application/hta");
            types.put("htc","text/x-component");
            types.put("htm","text/html");
            types.put("html","text/html");
            types.put("htt","text/webviewhtml");
            types.put("hxa","application/xml");
            types.put("hxc","application/xml");
            types.put("hxd","application/octet-stream");
            types.put("hxe","application/xml");
            types.put("hxf","application/xml");
            types.put("hxh","application/octet-stream");
            types.put("hxi","application/octet-stream");
            types.put("hxk","application/xml");
            types.put("hxq","application/octet-stream");
            types.put("hxr","application/octet-stream");
            types.put("hxs","application/octet-stream");
            types.put("hxt","text/html");
            types.put("hxv","application/xml");
            types.put("hxw","application/octet-stream");
            types.put("hxx","text/plain");
            types.put("i","text/plain");
            types.put("ico","image/x-icon");
            types.put("ics","application/octet-stream");
            types.put("idl","text/plain");
            types.put("ief","image/ief");
            types.put("iii","application/x-iphone");
            types.put("inc","text/plain");
            types.put("inf","application/octet-stream");
            types.put("ini","text/plain");
            types.put("inl","text/plain");
            types.put("ins","application/x-internet-signup");
            types.put("ipa","application/x-itunes-ipa");
            types.put("ipg","application/x-itunes-ipg");
            types.put("ipproj","text/plain");
            types.put("ipsw","application/x-itunes-ipsw");
            types.put("iqy","text/x-ms-iqy");
            types.put("isp","application/x-internet-signup");
            types.put("ite","application/x-itunes-ite");
            types.put("itlp","application/x-itunes-itlp");
            types.put("itms","application/x-itunes-itms");
            types.put("itpc","application/x-itunes-itpc");
            types.put("IVF","video/x-ivf");
            types.put("jar","application/java-archive");
            types.put("java","application/octet-stream");
            types.put("jck","application/liquidmotion");
            types.put("jcz","application/liquidmotion");
            types.put("jfif","image/pjpeg");
            types.put("jnlp","application/x-java-jnlp-file");
            types.put("jpb","application/octet-stream");
            types.put("jpe","image/jpeg");
            types.put("jpeg","image/jpeg");
            types.put("jpg","image/jpeg");
            types.put("js","application/javascript");
            types.put("json","application/json");
            types.put("jsx","text/jscript");
            types.put("jsxbin","text/plain");
            types.put("latex","application/x-latex");
            types.put("library-ms","application/windows-library+xml");
            types.put("lit","application/x-ms-reader");
            types.put("loadtest","application/xml");
            types.put("lpk","application/octet-stream");
            types.put("lsf","video/x-la-asf");
            types.put("lst","text/plain");
            types.put("lsx","video/x-la-asf");
            types.put("lzh","application/octet-stream");
            types.put("m13","application/x-msmediaview");
            types.put("m14","application/x-msmediaview");
            types.put("m1v","video/mpeg");
            types.put("m2t","video/vnd.dlna.mpeg-tts");
            types.put("m2ts","video/vnd.dlna.mpeg-tts");
            types.put("m2v","video/mpeg");
            types.put("m3u","audio/x-mpegurl");
            types.put("m3u8","audio/x-mpegurl");
            types.put("m4a","audio/m4a");
            types.put("m4b","audio/m4b");
            types.put("m4p","audio/m4p");
            types.put("m4r","audio/x-m4r");
            types.put("m4v","video/x-m4v");
            types.put("mac","image/x-macpaint");
            types.put("mak","text/plain");
            types.put("man","application/x-troff-man");
            types.put("manifest","application/x-ms-manifest");
            types.put("map","text/plain");
            types.put("master","application/xml");
            types.put("mda","application/msaccess");
            types.put("mdb","application/x-msaccess");
            types.put("mde","application/msaccess");
            types.put("mdp","application/octet-stream");
            types.put("me","application/x-troff-me");
            types.put("mfp","application/x-shockwave-flash");
            types.put("mht","message/rfc822");
            types.put("mhtml","message/rfc822");
            types.put("mid","audio/mid");
            types.put("midi","audio/mid");
            types.put("mix","application/octet-stream");
            types.put("mk","text/plain");
            types.put("mmf","application/x-smaf");
            types.put("mno","text/xml");
            types.put("mny","application/x-msmoney");
            types.put("mod","video/mpeg");
            types.put("mov","video/quicktime");
            types.put("movie","video/x-sgi-movie");
            types.put("mp2","video/mpeg");
            types.put("mp2v","video/mpeg");
            types.put("mp3","audio/mpeg");
            types.put("mp4","video/mp4");
            types.put("mp4v","video/mp4");
            types.put("mpa","video/mpeg");
            types.put("mpe","video/mpeg");
            types.put("mpeg","video/mpeg");
            types.put("mpf","application/vnd.ms-mediapackage");
            types.put("mpg","video/mpeg");
            types.put("mpp","application/vnd.ms-project");
            types.put("mpv2","video/mpeg");
            types.put("mqv","video/quicktime");
            types.put("ms","application/x-troff-ms");
            types.put("msi","application/octet-stream");
            types.put("mso","application/octet-stream");
            types.put("mts","video/vnd.dlna.mpeg-tts");
            types.put("mtx","application/xml");
            types.put("mvb","application/x-msmediaview");
            types.put("mvc","application/x-miva-compiled");
            types.put("mxp","application/x-mmxp");
            types.put("nc","application/x-netcdf");
            types.put("nsc","video/x-ms-asf");
            types.put("nws","message/rfc822");
            types.put("ocx","application/octet-stream");
            types.put("oda","application/oda");
            types.put("odb","application/vnd.oasis.opendocument.database");
            types.put("odc","application/vnd.oasis.opendocument.chart");
            types.put("odf","application/vnd.oasis.opendocument.formula");
            types.put("odg","application/vnd.oasis.opendocument.graphics");
            types.put("odh","text/plain");
            types.put("odi","application/vnd.oasis.opendocument.image");
            types.put("odl","text/plain");
            types.put("odm","application/vnd.oasis.opendocument.text-master");
            types.put("odp","application/vnd.oasis.opendocument.presentation");
            types.put("ods","application/vnd.oasis.opendocument.spreadsheet");
            types.put("odt","application/vnd.oasis.opendocument.text");
            types.put("oga","audio/ogg");
            types.put("ogg","audio/ogg");
            types.put("ogv","video/ogg");
            types.put("ogx","application/ogg");
            types.put("one","application/onenote");
            types.put("onea","application/onenote");
            types.put("onepkg","application/onenote");
            types.put("onetmp","application/onenote");
            types.put("onetoc","application/onenote");
            types.put("onetoc2","application/onenote");
            types.put("opus","audio/ogg");
            types.put("orderedtest","application/xml");
            types.put("osdx","application/opensearchdescription+xml");
            types.put("otf","application/font-sfnt");
            types.put("otg","application/vnd.oasis.opendocument.graphics-template");
            types.put("oth","application/vnd.oasis.opendocument.text-web");
            types.put("otp","application/vnd.oasis.opendocument.presentation-template");
            types.put("ots","application/vnd.oasis.opendocument.spreadsheet-template");
            types.put("ott","application/vnd.oasis.opendocument.text-template");
            types.put("oxt","application/vnd.openofficeorg.extension");
            types.put("p10","application/pkcs10");
            types.put("p12","application/x-pkcs12");
            types.put("p7b","application/x-pkcs7-certificates");
            types.put("p7c","application/pkcs7-mime");
            types.put("p7m","application/pkcs7-mime");
            types.put("p7r","application/x-pkcs7-certreqresp");
            types.put("p7s","application/pkcs7-signature");
            types.put("pbm","image/x-portable-bitmap");
            types.put("pcast","application/x-podcast");
            types.put("pct","image/pict");
            types.put("pcx","application/octet-stream");
            types.put("pcz","application/octet-stream");
            types.put("pdf","application/pdf");
            types.put("pfb","application/octet-stream");
            types.put("pfm","application/octet-stream");
            types.put("pfx","application/x-pkcs12");
            types.put("pgm","image/x-portable-graymap");
            types.put("pic","image/pict");
            types.put("pict","image/pict");
            types.put("pkgdef","text/plain");
            types.put("pkgundef","text/plain");
            types.put("pko","application/vnd.ms-pki.pko");
            types.put("pls","audio/scpls");
            types.put("pma","application/x-perfmon");
            types.put("pmc","application/x-perfmon");
            types.put("pml","application/x-perfmon");
            types.put("pmr","application/x-perfmon");
            types.put("pmw","application/x-perfmon");
            types.put("png","image/png");
            types.put("pnm","image/x-portable-anymap");
            types.put("pnt","image/x-macpaint");
            types.put("pntg","image/x-macpaint");
            types.put("pnz","image/png");
            types.put("pot","application/vnd.ms-powerpoint");
            types.put("potm","application/vnd.ms-powerpoint.template.macroEnabled.12");
            types.put("potx","application/vnd.openxmlformats-officedocument.presentationml.template");
            types.put("ppa","application/vnd.ms-powerpoint");
            types.put("ppam","application/vnd.ms-powerpoint.addin.macroEnabled.12");
            types.put("ppm","image/x-portable-pixmap");
            types.put("pps","application/vnd.ms-powerpoint");
            types.put("ppsm","application/vnd.ms-powerpoint.slideshow.macroEnabled.12");
            types.put("ppsx","application/vnd.openxmlformats-officedocument.presentationml.slideshow");
            types.put("ppt","application/vnd.ms-powerpoint");
            types.put("pptm","application/vnd.ms-powerpoint.presentation.macroEnabled.12");
            types.put("pptx","application/vnd.openxmlformats-officedocument.presentationml.presentation");
            types.put("prf","application/pics-rules");
            types.put("prm","application/octet-stream");
            types.put("prx","application/octet-stream");
            types.put("ps","application/postscript");
            types.put("psc1","application/PowerShell");
            types.put("psd","application/octet-stream");
            types.put("psess","application/xml");
            types.put("psm","application/octet-stream");
            types.put("psp","application/octet-stream");
            types.put("pub","application/x-mspublisher");
            types.put("pwz","application/vnd.ms-powerpoint");
            types.put("qht","text/x-html-insertion");
            types.put("qhtm","text/x-html-insertion");
            types.put("qt","video/quicktime");
            types.put("qti","image/x-quicktime");
            types.put("qtif","image/x-quicktime");
            types.put("qtl","application/x-quicktimeplayer");
            types.put("qxd","application/octet-stream");
            types.put("ra","audio/x-pn-realaudio");
            types.put("ram","audio/x-pn-realaudio");
            types.put("rar","application/x-rar-compressed");
            types.put("ras","image/x-cmu-raster");
            types.put("rat","application/rat-file");
            types.put("rc","text/plain");
            types.put("rc2","text/plain");
            types.put("rct","text/plain");
            types.put("rdlc","application/xml");
            types.put("reg","text/plain");
            types.put("resx","application/xml");
            types.put("rf","image/vnd.rn-realflash");
            types.put("rgb","image/x-rgb");
            types.put("rgs","text/plain");
            types.put("rm","application/vnd.rn-realmedia");
            types.put("rmi","audio/mid");
            types.put("rmp","application/vnd.rn-rn_music_package");
            types.put("roff","application/x-troff");
            types.put("rpm","audio/x-pn-realaudio-plugin");
            types.put("rqy","text/x-ms-rqy");
            types.put("rtf","application/rtf");
            types.put("rtx","text/richtext");
            types.put("ruleset","application/xml");
            types.put("s","text/plain");
            types.put("safariextz","application/x-safari-safariextz");
            types.put("scd","application/x-msschedule");
            types.put("scr","text/plain");
            types.put("sct","text/scriptlet");
            types.put("sd2","audio/x-sd2");
            types.put("sdp","application/sdp");
            types.put("sea","application/octet-stream");
            types.put("searchConnector-ms","application/windows-search-connector+xml");
            types.put("setpay","application/set-payment-initiation");
            types.put("setreg","application/set-registration-initiation");
            types.put("settings","application/xml");
            types.put("sgimb","application/x-sgimb");
            types.put("sgml","text/sgml");
            types.put("sh","application/x-sh");
            types.put("shar","application/x-shar");
            types.put("shtml","text/html");
            types.put("sit","application/x-stuffit");
            types.put("sitemap","application/xml");
            types.put("skin","application/xml");
            types.put("sldm","application/vnd.ms-powerpoint.slide.macroEnabled.12");
            types.put("sldx","application/vnd.openxmlformats-officedocument.presentationml.slide");
            types.put("slk","application/vnd.ms-excel");
            types.put("sln","text/plain");
            types.put("slupkg-ms","application/x-ms-license");
            types.put("smd","audio/x-smd");
            types.put("smi","application/octet-stream");
            types.put("smx","audio/x-smd");
            types.put("smz","audio/x-smd");
            types.put("snd","audio/basic");
            types.put("snippet","application/xml");
            types.put("snp","application/octet-stream");
            types.put("sol","text/plain");
            types.put("sor","text/plain");
            types.put("spc","application/x-pkcs7-certificates");
            types.put("spl","application/futuresplash");
            types.put("spx","audio/ogg");
            types.put("src","application/x-wais-source");
            types.put("srf","text/plain");
            types.put("SSISDeploymentManifest","text/xml");
            types.put("ssm","application/streamingmedia");
            types.put("sst","application/vnd.ms-pki.certstore");
            types.put("stl","application/vnd.ms-pki.stl");
            types.put("sv4cpio","application/x-sv4cpio");
            types.put("sv4crc","application/x-sv4crc");
            types.put("svc","application/xml");
            types.put("svg","image/svg+xml");
            types.put("swf","application/x-shockwave-flash");
            types.put("t","application/x-troff");
            types.put("tar","application/x-tar");
            types.put("tcl","application/x-tcl");
            types.put("testrunconfig","application/xml");
            types.put("testsettings","application/xml");
            types.put("tex","application/x-tex");
            types.put("texi","application/x-texinfo");
            types.put("texinfo","application/x-texinfo");
            types.put("tgz","application/x-compressed");
            types.put("thmx","application/vnd.ms-officetheme");
            types.put("thn","application/octet-stream");
            types.put("tif","image/tiff");
            types.put("tiff","image/tiff");
            types.put("tlh","text/plain");
            types.put("tli","text/plain");
            types.put("toc","application/octet-stream");
            types.put("tr","application/x-troff");
            types.put("trm","application/x-msterminal");
            types.put("trx","application/xml");
            types.put("ts","video/vnd.dlna.mpeg-tts");
            types.put("tsv","text/tab-separated-values");
            types.put("ttf","application/font-sfnt");
            types.put("tts","video/vnd.dlna.mpeg-tts");
            types.put("txt","text/plain");
            types.put("u32","application/octet-stream");
            types.put("uls","text/iuls");
            types.put("user","text/plain");
            types.put("ustar","application/x-ustar");
            types.put("vb","text/plain");
            types.put("vbdproj","text/plain");
            types.put("vbk","video/mpeg");
            types.put("vbproj","text/plain");
            types.put("vbs","text/vbscript");
            types.put("vcf","text/x-vcard");
            types.put("vcproj","application/xml");
            types.put("vcs","text/plain");
            types.put("vcxproj","application/xml");
            types.put("vddproj","text/plain");
            types.put("vdp","text/plain");
            types.put("vdproj","text/plain");
            types.put("vdx","application/vnd.ms-visio.viewer");
            types.put("vml","text/xml");
            types.put("vscontent","application/xml");
            types.put("vsct","text/xml");
            types.put("vsd","application/vnd.visio");
            types.put("vsi","application/ms-vsi");
            types.put("vsix","application/vsix");
            types.put("vsixlangpack","text/xml");
            types.put("vsixmanifest","text/xml");
            types.put("vsmdi","application/xml");
            types.put("vspscc","text/plain");
            types.put("vss","application/vnd.visio");
            types.put("vsscc","text/plain");
            types.put("vssettings","text/xml");
            types.put("vssscc","text/plain");
            types.put("vst","application/vnd.visio");
            types.put("vstemplate","text/xml");
            types.put("vsto","application/x-ms-vsto");
            types.put("vsw","application/vnd.visio");
            types.put("vsx","application/vnd.visio");
            types.put("vtx","application/vnd.visio");
            types.put("wav","audio/wav");
            types.put("wave","audio/wav");
            types.put("wax","audio/x-ms-wax");
            types.put("wbk","application/msword");
            types.put("wbmp","image/vnd.wap.wbmp");
            types.put("wcm","application/vnd.ms-works");
            types.put("wdb","application/vnd.ms-works");
            types.put("wdp","image/vnd.ms-photo");
            types.put("webarchive","application/x-safari-webarchive");
            types.put("webm","video/webm");
            types.put("webp","image/webp");
            types.put("webtest","application/xml");
            types.put("wiq","application/xml");
            types.put("wiz","application/msword");
            types.put("wks","application/vnd.ms-works");
            types.put("WLMP","application/wlmoviemaker");
            types.put("wlpginstall","application/x-wlpg-detect");
            types.put("wlpginstall3","application/x-wlpg3-detect");
            types.put("wm","video/x-ms-wm");
            types.put("wma","audio/x-ms-wma");
            types.put("wmd","application/x-ms-wmd");
            types.put("wmf","application/x-msmetafile");
            types.put("wml","text/vnd.wap.wml");
            types.put("wmlc","application/vnd.wap.wmlc");
            types.put("wmls","text/vnd.wap.wmlscript");
            types.put("wmlsc","application/vnd.wap.wmlscriptc");
            types.put("wmp","video/x-ms-wmp");
            types.put("wmv","video/x-ms-wmv");
            types.put("wmx","video/x-ms-wmx");
            types.put("wmz","application/x-ms-wmz");
            types.put("woff","application/font-woff");
            types.put("wpl","application/vnd.ms-wpl");
            types.put("wps","application/vnd.ms-works");
            types.put("wri","application/x-mswrite");
            types.put("wrl","x-world/x-vrml");
            types.put("wrz","x-world/x-vrml");
            types.put("wsc","text/scriptlet");
            types.put("wsdl","text/xml");
            types.put("wvx","video/x-ms-wvx");
            types.put("x","application/directx");
            types.put("xaf","x-world/x-vrml");
            types.put("xaml","application/xaml+xml");
            types.put("xap","application/x-silverlight-app");
            types.put("xbap","application/x-ms-xbap");
            types.put("xbm","image/x-xbitmap");
            types.put("xdr","text/plain");
            types.put("xht","application/xhtml+xml");
            types.put("xhtml","application/xhtml+xml");
            types.put("xla","application/vnd.ms-excel");
            types.put("xlam","application/vnd.ms-excel.addin.macroEnabled.12");
            types.put("xlc","application/vnd.ms-excel");
            types.put("xld","application/vnd.ms-excel");
            types.put("xlk","application/vnd.ms-excel");
            types.put("xll","application/vnd.ms-excel");
            types.put("xlm","application/vnd.ms-excel");
            types.put("xls","application/vnd.ms-excel");
            types.put("xlsb","application/vnd.ms-excel.sheet.binary.macroEnabled.12");
            types.put("xlsm","application/vnd.ms-excel.sheet.macroEnabled.12");
            types.put("xlsx","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            types.put("xlt","application/vnd.ms-excel");
            types.put("xltm","application/vnd.ms-excel.template.macroEnabled.12");
            types.put("xltx","application/vnd.openxmlformats-officedocument.spreadsheetml.template");
            types.put("xlw","application/vnd.ms-excel");
            types.put("xml","text/xml");
            types.put("xmta","application/xml");
            types.put("xof","x-world/x-vrml");
            types.put("XOML","text/plain");
            types.put("xpm","image/x-xpixmap");
            types.put("xps","application/vnd.ms-xpsdocument");
            types.put("xrm-ms","text/xml");
            types.put("xsc","application/xml");
            types.put("xsd","text/xml");
            types.put("xsf","text/xml");
            types.put("xsl","text/xml");
            types.put("xslt","text/xml");
            types.put("xsn","application/octet-stream");
            types.put("xss","application/xml");
            types.put("xspf","application/xspf+xml");
            types.put("xtp","application/octet-stream");
            types.put("xwd","image/x-xwindowdump");
            types.put("z","application/x-compress");
            types.put("zip","application/zip");
        } else {
            Log.d("GETTING_MIME_TYPES", "types have a size greater than 0");
        }

        return types;
    }

    /**
     * Gets the extension of a file name, like ".png" or ".jpg".
     *
     * @return Extension including the dot("."); "" if there is no extension;
     * null if uri was null.
     */
    private static String getExtension(String uri) {
        if (uri == null) {
            return null;
        }

        int dot = uri.lastIndexOf(".");

        if (dot >= 0) {
            return uri.substring(dot);
        } else {
            // No extension.
            return "";
        }
    }

    /**
     * @return Whether the URI is a local one.
     */
    private static boolean isLocal(String url) {
        if (url != null && !url.startsWith("http://") && !url.startsWith("https://")) {
            return true;
        }
        return false;
    }

    /**
     * @return True if Uri is a MediaStore Uri.
     * @author paulburke
     */
    private static boolean isMediaUri(Uri uri) {
        return "media".equalsIgnoreCase(uri.getAuthority());
    }

    /**
     * Convert File into Uri.
     *
     * @return uri
     */
    private static Uri getUri(File file) {
        if (file != null) {
            return Uri.fromFile(file);
        }

        return null;
    }

    /**
     * Returns the path only (without file name).
     */
    public static File getPathWithoutFilename(File file) {
        if (file != null) {
            if (file.isDirectory()) {
                // no file to be split off. Return everything
                return file;
            } else {
                String filename = file.getName();
                String filepath = file.getAbsolutePath();

                // Construct path without file name.
                String pathwithoutname = filepath.substring(0,
                        filepath.length() - filename.length());
                if (pathwithoutname.endsWith("/")) {
                    pathwithoutname = pathwithoutname.substring(0, pathwithoutname.length() - 1);
                }
                return new File(pathwithoutname);
            }
        }
        return null;
    }

    /**
     * @return The MIME type for the given file.
     */
    public static String getMimeType(File file) {
        String extension = getExtension(file.getName().toLowerCase());

        if(extension.contains(".")) {
            extension = extension.substring(1);
        }

        if (extension.length() > 0) {
            return getTypes().get(extension);
        }

        return "application/octet-stream";
    }

    /**
     * @return The MIME type for the give Uri.
     */
    public static String getMimeType(Context context, Uri uri) {
        File file = new File(getPath(context, uri));
        return getMimeType(file);
    }

    /**
     * @param uri The Uri to check.
     *
     * @return Whether the Uri authority is ExternalStorageProvider.
     * @author paulburke
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     *
     * @return Whether the Uri authority is DownloadsProvider.
     * @author paulburke
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     *
     * @return Whether the Uri authority is MediaProvider.
     * @author paulburke
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     *
     * @return Whether the Uri authority is Google Photos.
     */
    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

    /**
     * @param uri The Uri to check.
     *
     * @return Whether the Uri authority is Google Photos.
     */
    public static boolean isGoogleDriveUri(Uri uri) {
        return "com.google.android.apps.docs.storage".equals(uri.getAuthority());
    }

    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context       The context.
     * @param uri           The Uri to query.
     * @param selection     (Optional) Filter used in the query.
     * @param selectionArgs (Optional) Selection arguments used in the query.
     *
     * @return The value of the _data column, which is typically a file path.
     * @author paulburke
     */
    public static String getDataColumn(Context context, Uri uri, String selection,
                                       String[] selectionArgs) {

        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = {
            column
        };

        try {
            cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);

            if (cursor != null && cursor.moveToFirst()) {
                if (DEBUG) {
                    DatabaseUtils.dumpCursor(cursor);
                }

                final int column_index = cursor.getColumnIndexOrThrow(column);

                return cursor.getString(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
        return null;
    }

    /**
     * Get a file path from a Uri. This will get the the path for Storage Access
     * Framework Documents, as well as the _data field for the MediaStore and
     * other file-based ContentProviders.<br>
     * <br>
     * Callers should check whether the path is local before assuming it
     * represents a local file.
     *
     * @param context The context.
     * @param uri     The Uri to query.
     *
     * @author paulburke
     * @see #isLocal(String)
     * @see #getFile(Context, Uri)
     */
    public static String getPath(final Context context, final Uri uri) {

        if (DEBUG) {
            Log.d(TAG + " File -",
                    "Authority: " + uri.getAuthority() +
                            ", Fragment: " + uri.getFragment() +
                            ", Port: " + uri.getPort() +
                            ", Query: " + uri.getQuery() +
                            ", Scheme: " + uri.getScheme() +
                            ", Host: " + uri.getHost() +
                            ", Segments: " + uri.getPathSegments().toString()
            );
        }

        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
            // LocalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/" + split[1];
                }

                // TODO handle non-primary volumes
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {

                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));

                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];

                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }

                final String selection = "_id=?";
                final String[] selectionArgs = new String[]{
                        split[1]
                };

                return getDataColumn(context, contentUri, selection, selectionArgs);
            }

        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {

            // Return the remote address
            if (isGooglePhotosUri(uri)) {
                return uri.getLastPathSegment();
            }

            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

        return null;
    }

    /**
     * Convert Uri into File, if possible.
     *
     * @return file A local file that the Uri was pointing to, or null if the
     * Uri is unsupported or pointed to a remote resource.
     * @author paulburke
     * @see #getPath(Context, Uri)
     */
    public static File getFile(Context context, Uri uri) {
        if (uri != null) {
            String path = getPath(context, uri);
            if (path != null && isLocal(path)) {
                return new File(path);
            }
        }
        return null;
    }

    /**
     * Get the file size in a human-readable string.
     *
     * @author paulburke
     */
    public static String getReadableFileSize(int size) {
        final int BYTES_IN_KILOBYTES = 1024;
        final DecimalFormat dec = new DecimalFormat("###.#");
        final String KILOBYTES = " KB";
        final String MEGABYTES = " MB";
        final String GIGABYTES = " GB";
        float fileSize = 0;
        String suffix = KILOBYTES;

        if (size > BYTES_IN_KILOBYTES) {
            fileSize = size / BYTES_IN_KILOBYTES;
            if (fileSize > BYTES_IN_KILOBYTES) {
                fileSize = fileSize / BYTES_IN_KILOBYTES;
                if (fileSize > BYTES_IN_KILOBYTES) {
                    fileSize = fileSize / BYTES_IN_KILOBYTES;
                    suffix = GIGABYTES;
                } else {
                    suffix = MEGABYTES;
                }
            }
        }
        return String.valueOf(dec.format(fileSize) + suffix);
    }

    /**
     * Attempt to retrieve the thumbnail of given File from the MediaStore. This
     * should not be called on the UI thread.
     *
     * @author paulburke
     */
    public static Bitmap getThumbnail(Context context, File file) {
        return getThumbnail(context, getUri(file), getMimeType(file));
    }

    /**
     * Attempt to retrieve the thumbnail of given Uri from the MediaStore. This
     * should not be called on the UI thread.
     *
     * @author paulburke
     */
    public static Bitmap getThumbnail(Context context, Uri uri) {
        return getThumbnail(context, uri, getMimeType(context, uri));
    }

    /**
     * Attempt to retrieve the thumbnail of given Uri from the MediaStore. This
     * should not be called on the UI thread.
     *
     * @author paulburke
     */
    public static Bitmap getThumbnail(Context context, Uri uri, String mimeType) {
        if (DEBUG) {
            Log.d(TAG, "Attempting to get thumbnail");
        }

        if (!isMediaUri(uri)) {
            Log.e(TAG, "You can only retrieve thumbnails for images and videos.");
            return null;
        }

        Bitmap bm = null;
        if (uri != null) {
            final ContentResolver resolver = context.getContentResolver();
            Cursor cursor = null;
            try {
                cursor = resolver.query(uri, null, null, null, null);
                if (cursor.moveToFirst()) {
                    final int id = cursor.getInt(0);
                    if (DEBUG) {
                        Log.d(TAG, "Got thumb ID: " + id);
                    }

                    if (mimeType.contains("video")) {
                        bm = MediaStore.Video.Thumbnails.getThumbnail(
                                resolver,
                                id,
                                MediaStore.Video.Thumbnails.MINI_KIND,
                                null);
                    } else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) {
                        bm = MediaStore.Images.Thumbnails.getThumbnail(
                                resolver,
                                id,
                                MediaStore.Images.Thumbnails.MINI_KIND,
                                null);
                    }
                }
            } catch (Exception e) {
                if (DEBUG) {
                    Log.e(TAG, "getThumbnail", e);
                }
            } finally {
                if (cursor != null) {
                    cursor.close();
                }
            }
        }
        return bm;
    }

    /**
     * File and folder comparator. TODO Expose sorting option method
     *
     * @author paulburke
     */
    public static Comparator<File> sComparator = (f1, f2) -> {
        // Sort alphabetically by lower case, which is much cleaner
        return f1.getName().toLowerCase().compareTo(
                f2.getName().toLowerCase());
    };

    /**
     * File (not directories) filter.
     *
     * @author paulburke
     */
    public static FileFilter sFileFilter = file -> {
        final String fileName = file.getName();
        // Return files only (not directories) and skip hidden files
        return file.isFile() && !fileName.startsWith(HIDDEN_PREFIX);
    };

    /**
     * Folder (directories) filter.
     *
     * @author paulburke
     */
    public static FileFilter sDirFilter = file -> {
        final String fileName = file.getName();
        // Return directories only and skip hidden directories
        return file.isDirectory() && !fileName.startsWith(HIDDEN_PREFIX);
    };

    /**
     * Get the Intent for selecting content to be used in an Intent Chooser.
     *
     * @return The intent for opening a file with Intent.createChooser()
     * @author paulburke
     */
    public static Intent createGetContentIntent() {
        // Implicitly allow the user to select a particular kind of data
        final Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        // The MIME data type filter
        intent.setType("*/*");
        // Only return URIs that can be opened with ContentResolver
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        return intent;
    }

    public static String getFileTypeFromFilePath(String filePath) {
        String extension = "";
        int i = filePath.lastIndexOf('.');
        if (i > 0) {
            extension = filePath.substring(i + 1);
        }
        if (extension.contains("doc") || extension.contains("docx")) {
            // Word document
            extension = "application/msword";
        } else if (extension.contains("pdf")) {
            // PDF file
            extension = "application/pdf";
        } else if (extension.contains("ppt") || extension.contains("pptx")) {
            // Powerpoint file
            extension = "application/vnd.ms-powerpoint";
        } else if (extension.contains("xls") || extension.contains("xlsx")) {
            // Excel file
            extension = "application/vnd.ms-excel";
        } else if (extension.contains("zip") || extension.contains("rar")) {
            // WAV audio file
            extension = "application/x-wav";
        } else if (extension.contains("rtf")) {
            // RTF file
            extension = "application/rtf";
        } else if (extension.contains("wav") || extension.contains("mp3")) {
            // WAV audio file
            extension = "audio/x-wav";
        } else if (extension.contains("gif")) {
            // GIF file
            extension = "image/gif";
        } else if (extension.contains("jpg") || extension.contains("JPG") ||extension.contains("jpeg") || extension.contains("png")) {
            // JPG file
            extension = "image/jpeg";
        } else if (extension.contains("txt")) {
            // Text file
            extension = "text/plain";
        } else if (extension.contains("3gp") || extension.contains("mpg") || extension.contains("mpeg") || extension.contains("mpe") || extension.contains("mp4") || extension.contains("avi")) {
            // Video files
            extension = "video/*";
        } else {
            extension = "*/*";
        }

        return extension;
    }

    private static String getTmpFilesDir(Context context) {
        String dirName = context.getExternalFilesDir(null) + "/" + Constants.TMP_FILE_DIR;

        boolean mkdir = new File(dirName).mkdir();

        return dirName;
    }

    public static void clearTmpFilesDir(Context context) {
        File dir = new File(getTmpFilesDir(context));

        if(!dir.exists()) {
            return;
        }

        for(File f : dir.listFiles()) {
            boolean delete = f.delete();
        }
    }

    public static File scaleImageDown(File image, Context context) {
        File tmpFile = null;

        try {
            String tmpFileLocation = FileUtils.getTmpFilesDir(context) + "/" + image.getName();
            FileUtils.clearTmpFilesDir(context);

            BitmapFactory.Options options = new BitmapFactory.Options();

            Bitmap bitmapToScale = BitmapFactory.decodeFile(image.getPath(), options);

            int originalWidth = options.outWidth;
            int originalHeight = options.outHeight;

            int newHeight, newWidth;
            double aspectRatio = ((double) originalWidth) / ((double) originalHeight);

            if(originalHeight > originalWidth) {
                aspectRatio = ((double) originalHeight) / ((double) originalWidth);
                newWidth = 1080;
                newHeight = (int) (newWidth * aspectRatio);
            } else {
                newHeight = 1080;
                newWidth = (int) (newHeight * aspectRatio);
            }

            Bitmap bitmap = Bitmap.createScaledBitmap(bitmapToScale, newWidth, newHeight, false);

            tmpFile = new File(tmpFileLocation);
            //noinspection ResultOfMethodCallIgnored
            tmpFile.createNewFile();

            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outputStream);
            FileOutputStream fileOutputStream = new FileOutputStream(tmpFile);
            fileOutputStream.write(outputStream.toByteArray());
            bitmap.recycle();
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Utils.copyExifData(image, tmpFile, null);

        Log.i("COMPRESSING IMAGE", "file size after compression: " + tmpFile.length() + " bytes. Before compression: " + image.length());

        return tmpFile;
    }

    public static String generateAudioRecordingFileName(Context context) {
        String audioDirectoryPath = context.getExternalFilesDir(null) + File.separator + Constants.AUDIO_RECORDINGS_DIR;

        File audioDir = new File(audioDirectoryPath);
        if(!audioDir.exists()) {
            audioDir.mkdir();
        }

        String fileName = "rec_";
        String fileSuffix = ".m4a";

        Date today = Calendar.getInstance().getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYYmmddHHMMss", Locale.GERMAN);

        fileName += simpleDateFormat.format(today);

        fileName = audioDirectoryPath + File.separator + fileName;
        String newFileName = fileName;

        int i = 1;
        while((new File(newFileName + fileSuffix)).exists()) {
            newFileName = fileName + "_" + i;
        }

        return newFileName + fileSuffix;
    }
}