package com.rngrp;

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

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;

public class GRP extends ReactContextBaseJavaModule {

  public GRP(ReactApplicationContext reactContext) {
    super(reactContext);
  }

  @Override
  public String getName() {
    return "GRP";
  }

  private WritableMap makeErrorPayload(Exception ex) {
    WritableMap error = Arguments.createMap();
    error.putString("message", ex.getMessage());
    return error;
  }

  @ReactMethod
  public void getRealPathFromURI(String uri, Callback callback) {
    try {
      Context context = getReactApplicationContext();
      String [] proj = {MediaStore.Images.Media.DATA};
      Cursor cursor = context.getContentResolver().query(Uri.parse(uri), proj,  null, null, null);
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      String path = cursor.getString(column_index); 
      cursor.close();
  
      callback.invoke(null, path);
    } catch (Exception ex) {
      ex.printStackTrace();
      callback.invoke(makeErrorPayload(ex));
    }
  }

}


@@ -45,34 +45,144 @@
 onte
-xt;%0Aimport android.net.Uri
+ntUris;%0Aimport android.content.Context;%0Aimport android.net.Uri;%0Aimport android.os.Environment;%0Aimport android.provider.DocumentsContract
 ;%0Aim
@@ -1074,16 +1074,22 @@
 ring uri
+String
 , Callba
@@ -1099,24 +1099,60 @@
 callback) %7B%0A
+    Uri uri = Uri.parse(uriString);%0A
     try %7B%0A  
@@ -1198,24 +1198,58 @@
 onContext();
+%0A%0A      if (isMediaDocument(uri))%7B
 %0A      Strin
@@ -1353,22 +1353,11 @@
 ery(
-Uri.parse(
 uri
-)
 , pr
@@ -1544,17 +1544,16 @@
 _index);
- 
 %0A      c
@@ -1567,18 +1567,16 @@
 lose();%0A
-  
 %0A      c
@@ -1603,16 +1603,773 @@
  path);%0A
+      %7D%0A      else if (isDownloadsDocument(uri)) %7B%0A%0A        final String id = DocumentsContract.getDocumentId(uri);%0A        final Uri contentUri = ContentUris.withAppendedId(%0A                Uri.parse(%22content://downloads/public_downloads%22), Long.valueOf(id));%0A%0A        callback.invoke(null, getDataColumn(context, contentUri, null, null)); ;%0A      %7D%0A      else if (isExternalStorageDocument(uri)) %7B%0A        final String docId = DocumentsContract.getDocumentId(uri);%0A        final String%5B%5D split = docId.split(%22:%22);%0A        final String type = split%5B0%5D;%0A%0A        if (%22primary%22.equalsIgnoreCase(type)) %7B%0A          callback.invoke(null, Environment.getExternalStorageDirectory() + %22/%22 + split%5B1%5D);%0A        %7D%0A        // TODO handle non-primary volumes%0A      %7D%0A
     %7D ca
@@ -2472,12 +2472,1095 @@
   %7D%0A
+%0A
   %7D%0A%0A
+  public static boolean isMediaDocument(Uri uri) %7B%0A    return %22com.android.providers.media.documents%22.equals(uri.getAuthority());%0A  %7D%0A%0A  public static boolean isDownloadsDocument(Uri uri) %7B%0A    return %22com.android.providers.downloads.documents%22.equals(uri.getAuthority());%0A  %7D%0A  public static boolean isExternalStorageDocument(Uri uri) %7B%0A    return %22com.android.externalstorage.documents%22.equals(uri.getAuthority());%0A  %7D%0A  public static String getDataColumn(Context context, Uri uri, String selection,%0A                                     String%5B%5D selectionArgs) %7B%0A%0A    Cursor cursor = null;%0A    final String column = %22_data%22;%0A    final String%5B%5D projection = %7B%0A            column%0A    %7D;%0A%0A    try %7B%0A      cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,%0A              null);%0A      if (cursor != null && cursor.moveToFirst()) %7B%0A        final int column_index = cursor.getColumnIndexOrThrow(column);%0A        return cursor.getString(column_index);%0A      %7D%0A    %7D finally %7B%0A      if (cursor != null)%0A        cursor.close();%0A    %7D%0A    return null;%0A  %7D%0A
 %7D%0A
-%0A

add support to DownloadsManager and ExternalStorage