package com.hw.nativeapp.utils;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class UriUtils {

    public static String getRealFilePath( final Context context, final Uri uri ) {
        if ( null == uri ) return null;
        final String scheme = uri.getScheme();
        String data = null;
        if ( scheme == null )
            data = uri.getPath();
        else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {
            data = uri.getPath();
        } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {
            Cursor cursor = context.getContentResolver().query( uri, new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null );
            if ( null != cursor ) {
                if ( cursor.moveToFirst() ) {
                    int index = cursor.getColumnIndex( MediaStore.Images.ImageColumns.DATA );
                    if ( index > -1 ) {
                        data = cursor.getString( index );
                    }
                }
                cursor.close();
            }
        }
        return data;
    }
//
//     /**
//      * @param context 上下文对象
//      * @param uri     当前相册照片的Uri
//      * @return 解析后的Uri对应的String
//      * */
//     @SuppressLint("NewApi")
//     public static String getPath(final Context context, final Uri uri) {
//         final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
//         String pathHead = "file:///";
//         // 1. DocumentProvider
//         if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
//             // 1.1 ExternalStorageProvider
//             if (isExternalStorageDocument(uri)) {
//                 final String docId = DocumentsContract.getDocumentId(uri);
//                 final String[] split = docId.split(":");
//                 final String type = split[0];
//                 if ("primary".equalsIgnoreCase(type)) {
//                     return pathHead + Environment.getExternalStorageDirectory() + "/" + split[1];
//                     }
//             }
//             // 1.2 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 pathHead + getDataColumn(context,
//                         contentUri, null, null);
//                 }
//                 // 1.3 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 pathHead + getDataColumn(context, contentUri, selection, selectionArgs);
//                }
//                }
//            // 2. MediaStore (and general)
//         else if ("content".equalsIgnoreCase(uri.getScheme())) {
//             if (isGooglePhotosUri(uri)) {//判断是否是google相册图片
//                return uri.getLastPathSegment();
//                } else if (isGooglePlayPhotosUri(uri)) {//判断是否是Google相册图片
//                return getImageUrlWithAuthority(context, uri);
//                } else {//其他类似于media这样的图片，和android4.4以下获取图片path方法类似
//                return getFilePath_below19(context, uri);
//                }
//                }
//            // 3. 判断是否是文件形式 File
//         else if ("file".equalsIgnoreCase(uri.getScheme())) {
//             return pathHead + uri.getPath();
//            }
//            return null;
//         }
        /**
     * @param uri
     *         The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    private 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.
     */
     private 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.
     */
      private static boolean isMediaDocument(Uri uri) {
         return "com.android.providers.media.documents".equals(uri.getAuthority());
     }
  /**
     *  判断是否是Google相册的图片，类似于content://com.google.android.apps.photos.content/...*
     *  */
    public static boolean isGooglePhotosUri(Uri uri) {
         return "com.google.android.apps.photos.content".equals(uri.getAuthority());
    }

    /**
    *  判断是否是Google相册的图片，类似于content://com.google.android.apps.photos.contentprovider/0/1/mediakey:/local%3A821abd2f-9f8c-4931-bbe9-a975d1f5fabc/ORIGINAL/NONE/1075342619
   **/
     public static boolean isGooglePlayPhotosUri(Uri uri) {
         return "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority());
     }
}
