|
| 1 | +package com.reactnativedocumentpicker; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.ContentResolver; |
| 5 | +import android.content.Intent; |
| 6 | +import android.database.Cursor; |
| 7 | +import android.net.Uri; |
| 8 | +import android.os.Bundle; |
| 9 | +import android.provider.MediaStore; |
| 10 | +import android.provider.OpenableColumns; |
| 11 | +import android.util.Log; |
| 12 | +import android.webkit.MimeTypeMap; |
| 13 | + |
| 14 | +import com.facebook.react.bridge.ActivityEventListener; |
| 15 | +import com.facebook.react.bridge.Arguments; |
| 16 | +import com.facebook.react.bridge.Callback; |
| 17 | +import com.facebook.react.bridge.ReactApplicationContext; |
| 18 | +import com.facebook.react.bridge.ReactContextBaseJavaModule; |
| 19 | +import com.facebook.react.bridge.ReactMethod; |
| 20 | +import com.facebook.react.bridge.ReadableArray; |
| 21 | +import com.facebook.react.bridge.ReadableMap; |
| 22 | +import com.facebook.react.bridge.WritableMap; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.io.FileOutputStream; |
| 26 | +import java.io.IOException; |
| 27 | +import java.net.URL; |
| 28 | +import java.nio.channels.Channels; |
| 29 | +import java.nio.channels.ReadableByteChannel; |
| 30 | + |
| 31 | +/** |
| 32 | + * @see <a href="https://developer.android.com/guide/topics/providers/document-provider.html">android documentation</a> |
| 33 | + */ |
| 34 | +public class DocumentPicker extends ReactContextBaseJavaModule implements ActivityEventListener { |
| 35 | + private static final String NAME = "RNDocumentPicker"; |
| 36 | + private static final int READ_REQUEST_CODE = 41; |
| 37 | + |
| 38 | + private static class Fields { |
| 39 | + //private static final String FILE_PATH = "filepath"; |
| 40 | + private static final String FILE_SIZE = "fileSize"; |
| 41 | + private static final String FILE_NAME = "fileName"; |
| 42 | + private static final String TYPE = "type"; |
| 43 | + } |
| 44 | + |
| 45 | + private Callback callback; |
| 46 | + |
| 47 | + public DocumentPicker(ReactApplicationContext reactContext) { |
| 48 | + super(reactContext); |
| 49 | + reactContext.addActivityEventListener(this); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getName() { |
| 54 | + return NAME; |
| 55 | + } |
| 56 | + |
| 57 | + @ReactMethod |
| 58 | + public void show(ReadableMap args, Callback callback) { |
| 59 | + Intent intent; |
| 60 | + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { |
| 61 | + intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); |
| 62 | + } else { |
| 63 | + intent = new Intent(Intent.ACTION_PICK); |
| 64 | + } |
| 65 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 66 | + |
| 67 | + if (!args.isNull("filetype")) { |
| 68 | + ReadableArray filetypes = args.getArray("filetype"); |
| 69 | + if (filetypes.size() > 0) { |
| 70 | + intent.setType(filetypes.getString(0)); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + this.callback = callback; |
| 75 | + |
| 76 | + getReactApplicationContext().startActivityForResult(intent, READ_REQUEST_CODE, Bundle.EMPTY); |
| 77 | + } |
| 78 | + |
| 79 | + // removed @Override temporarily just to get it working on RN0.33 and RN0.32 - will remove |
| 80 | + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { |
| 81 | + onActivityResult(requestCode, resultCode, data); |
| 82 | + } |
| 83 | + |
| 84 | + // removed @Override temporarily just to get it working on RN0.33 and RN0.32 |
| 85 | + public void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 86 | + if (requestCode != READ_REQUEST_CODE) |
| 87 | + return; |
| 88 | + |
| 89 | + if (resultCode != Activity.RESULT_OK) { |
| 90 | + callback.invoke("Bad result code: " + resultCode, null); |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (data == null) { |
| 95 | + callback.invoke("No data", null); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + try { |
| 100 | + Uri uri = data.getData(); |
| 101 | + callback.invoke(null, toMapWithMetadata(uri)); |
| 102 | + } catch (Exception e) { |
| 103 | + Log.e(NAME, "Failed to read", e); |
| 104 | + callback.invoke(e.getMessage(), null); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private WritableMap toMapWithMetadata(Uri uri) { |
| 109 | + WritableMap map; |
| 110 | + if(uri.toString().startsWith("/")) { |
| 111 | + map = metaDataFromFile(new File(uri.toString())); |
| 112 | + } else if (uri.toString().startsWith("http")) { |
| 113 | + map = metaDataFromUri(uri); |
| 114 | + } else { |
| 115 | + map = metaDataFromContentResolver(uri); |
| 116 | + } |
| 117 | + |
| 118 | + map.putString("uri", uri.toString()); |
| 119 | + |
| 120 | + return map; |
| 121 | + } |
| 122 | + |
| 123 | + private WritableMap metaDataFromUri(Uri uri) { |
| 124 | + WritableMap map = Arguments.createMap(); |
| 125 | + |
| 126 | + File outputDir = getReactApplicationContext().getCacheDir(); |
| 127 | + try { |
| 128 | + File downloaded = download(uri, outputDir); |
| 129 | + //map.putString(Fields.FILE_PATH, downloaded.getAbsolutePath()); |
| 130 | + map.putInt(Fields.FILE_SIZE, (int) downloaded.length()); |
| 131 | + map.putString(Fields.FILE_NAME, downloaded.getName()); |
| 132 | + map.putString(Fields.TYPE, mimeTypeFromName(uri.toString())); |
| 133 | + } catch (IOException e) { |
| 134 | + Log.e("DocumentPicker", "Failed to download file", e); |
| 135 | + } |
| 136 | + |
| 137 | + return map; |
| 138 | + } |
| 139 | + |
| 140 | + private WritableMap metaDataFromFile(File file) { |
| 141 | + WritableMap map = Arguments.createMap(); |
| 142 | + |
| 143 | + if(!file.exists()) |
| 144 | + return map; |
| 145 | + //map.putString(Fields.FILE_PATH, file.getAbsolutePath()); |
| 146 | + map.putInt(Fields.FILE_SIZE, (int) file.length()); |
| 147 | + map.putString(Fields.FILE_NAME, file.getName()); |
| 148 | + map.putString(Fields.TYPE, mimeTypeFromName(file.getAbsolutePath())); |
| 149 | + |
| 150 | + return map; |
| 151 | + } |
| 152 | + |
| 153 | + |
| 154 | + private WritableMap metaDataFromContentResolver(Uri uri) { |
| 155 | + WritableMap map = Arguments.createMap(); |
| 156 | + |
| 157 | + ContentResolver contentResolver = getReactApplicationContext().getContentResolver(); |
| 158 | + |
| 159 | + map.putString(Fields.TYPE, contentResolver.getType(uri)); |
| 160 | + |
| 161 | + Cursor cursor = contentResolver.query(uri, null, null, null, null, null); |
| 162 | + |
| 163 | + try { |
| 164 | + if (cursor != null && cursor.moveToFirst()) { |
| 165 | + |
| 166 | + //map.putString(Fields.FILE_PATH, cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA))); |
| 167 | + //map.putString(Fields.FILE_PATH, ""); |
| 168 | + map.putString(Fields.FILE_NAME, cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))); |
| 169 | + |
| 170 | + int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE); |
| 171 | + if (!cursor.isNull(sizeIndex)) { |
| 172 | + String size = cursor.getString(sizeIndex); |
| 173 | + if (size != null) |
| 174 | + map.putInt(Fields.FILE_SIZE, Integer.valueOf(size)); |
| 175 | + } |
| 176 | + } |
| 177 | + } finally { |
| 178 | + if (cursor != null) { |
| 179 | + cursor.close(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + return map; |
| 184 | + } |
| 185 | + |
| 186 | + private static File download(Uri uri, File outputDir) throws IOException { |
| 187 | + File file = File.createTempFile("prefix", "extension", outputDir); |
| 188 | + |
| 189 | + URL url = new URL(uri.toString()); |
| 190 | + |
| 191 | + ReadableByteChannel channel = Channels.newChannel(url.openStream()); |
| 192 | + try{ |
| 193 | + FileOutputStream stream = new FileOutputStream(file); |
| 194 | + |
| 195 | + try { |
| 196 | + stream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE); |
| 197 | + return file; |
| 198 | + } finally { |
| 199 | + stream.close(); |
| 200 | + } |
| 201 | + } finally { |
| 202 | + channel.close(); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + private static String mimeTypeFromName(String absolutePath) { |
| 207 | + String extension = MimeTypeMap.getFileExtensionFromUrl(absolutePath); |
| 208 | + if (extension != null) { |
| 209 | + return MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); |
| 210 | + } else { |
| 211 | + return null; |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + // Required for RN 0.30+ modules than implement ActivityEventListener |
| 216 | + public void onNewIntent(Intent intent) { |
| 217 | + } |
| 218 | +} |
0 commit comments