From 5f30cca79bb9627451239244077bb67aab4daa1a Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Fri, 30 Aug 2019 18:31:49 +0100 Subject: [PATCH] app head: channel creator page --- app | 2 +- .../lbry/build/templates/build.tmpl.gradle | 1 - .../build/templates/build.tmpl.gradle.arm | 1 - .../java/io/lbry/browser/MainActivity.java | 61 ++++++++++++++++++- .../browser/reactmodules/UtilityModule.java | 11 ++++ 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/app b/app index e74a419..44856e1 160000 --- a/app +++ b/app @@ -1 +1 @@ -Subproject commit e74a419ffd5b0a078a9f30da2dd8442556188392 +Subproject commit 44856e1ba16dd621e521c30263176692f73b8453 diff --git a/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle b/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle index de156ff..23c4e40 100644 --- a/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle +++ b/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle @@ -106,7 +106,6 @@ subprojects { dependencies { compile project(':@react-native-community_async-storage') compile project(':react-native-camera') - compile project(':react-native-document-picker') compile project(':react-native-exception-handler') compile project(':react-native-fast-image') compile project(':react-native-fs') diff --git a/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle.arm b/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle.arm index b031079..cbab643 100644 --- a/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle.arm +++ b/p4a/pythonforandroid/bootstraps/lbry/build/templates/build.tmpl.gradle.arm @@ -106,7 +106,6 @@ subprojects { dependencies { compile project(':@react-native-community_async-storage') compile project(':react-native-camera') - compile project(':react-native-document-picker') compile project(':react-native-exception-handler') compile project(':react-native-fast-image') compile project(':react-native-fs') diff --git a/src/main/java/io/lbry/browser/MainActivity.java b/src/main/java/io/lbry/browser/MainActivity.java index 6df7994..1e97d89 100644 --- a/src/main/java/io/lbry/browser/MainActivity.java +++ b/src/main/java/io/lbry/browser/MainActivity.java @@ -11,8 +11,10 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; import android.content.SharedPreferences; +import android.database.Cursor; import android.Manifest; import android.net.Uri; +import android.provider.MediaStore; import android.provider.Settings; import android.support.v4.app.ActivityCompat; import android.support.v4.app.NotificationManagerCompat; @@ -37,7 +39,6 @@ import com.facebook.react.modules.core.PermissionListener; import com.facebook.react.shell.MainReactPackage; import com.facebook.react.ReactRootView; import com.reactnativecommunity.asyncstorage.AsyncStoragePackage; -import io.github.elyx0.reactnativedocumentpicker.DocumentPickerPackage; import com.rnfs.RNFSPackage; import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView; import com.swmansion.gesturehandler.react.RNGestureHandlerPackage; @@ -47,8 +48,10 @@ import com.RNFetchBlob.RNFetchBlobPackage; import io.lbry.browser.reactpackages.LbryReactPackage; import io.lbry.browser.reactmodules.BackgroundMediaModule; +import java.io.File; import java.io.UnsupportedEncodingException; import java.math.BigInteger; +import java.net.URISyntaxException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; @@ -73,6 +76,8 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand private static final int PHONE_STATE_PERMISSION_REQ_CODE = 202; private static final int RECEIVE_SMS_PERMISSION_REQ_CODE = 203; + + public static final int DOCUMENT_PICKER_RESULT_CODE = 301; private BroadcastReceiver notificationsReceiver; @@ -148,7 +153,6 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand .addPackage(new MainReactPackage()) .addPackage(new AsyncStoragePackage()) .addPackage(new FastImageViewPackage()) - .addPackage(new DocumentPickerPackage()) .addPackage(new ReactVideoPackage()) .addPackage(new ReanimatedPackage()) .addPackage(new RNCameraPackage()) @@ -289,6 +293,33 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand }; registerReceiver(smsReceiver, smsFilter); } + + public static String getUriPath(Context context, Uri uri) throws URISyntaxException { + if ("content".equalsIgnoreCase(uri.getScheme())) { + String[] projection = { MediaStore.MediaColumns.DATA }; + Cursor cursor = null; + + try { + cursor = context.getContentResolver().query(uri, projection, null, null, null); + int index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); + if (cursor.moveToFirst()) { + return cursor.getString(index); + } + } catch (Exception e) { + // pass + android.util.Log.e("ReactNativeJS", e.getMessage(), e); + } finally { + if (cursor != null) { + cursor.close(); + } + } + } + else if ("file".equalsIgnoreCase(uri.getScheme())) { + return uri.getPath(); + } + + return null; + } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { @@ -299,6 +330,32 @@ public class MainActivity extends Activity implements DefaultHardwareBackBtnHand } } } + + if (requestCode == DOCUMENT_PICKER_RESULT_CODE) { + ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); + if (reactContext != null) { + if (resultCode == RESULT_OK) { + Uri fileUri = data.getData(); + try { + String filePath = getUriPath(this, fileUri); + android.util.Log.d("ReactNativeJS", "fileUri=" + filePath); + + WritableMap params = Arguments.createMap(); + params.putString("path", filePath); + reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("onDocumentPickerFilePicked", params); + } catch (URISyntaxException ex) { + // failed to get a file path + reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("onDocumentPickerCanceled", null); + } + } else if (resultCode == RESULT_CANCELED) { + // user canceled or request failed + reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit("onDocumentPickerCanceled", null); + } + } + } } public static Activity getActivity() { diff --git a/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java b/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java index c48c0ea..f6e46bd 100644 --- a/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java +++ b/src/main/java/io/lbry/browser/reactmodules/UtilityModule.java @@ -354,4 +354,15 @@ public class UtilityModule extends ReactContextBaseJavaModule { context.sendBroadcast(intent); } } + + @ReactMethod + public void openDocumentPicker(String type) { + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + intent.setType(type); + Activity activity = MainActivity.getActivity(); + if (activity != null) { + activity.startActivityForResult( + Intent.createChooser(intent, "Select a file"), MainActivity.DOCUMENT_PICKER_RESULT_CODE); + } + } }