Skip to content

Commit

Permalink
Merge pull request #80 from software-mansion-labs/share-utils/android…
Browse files Browse the repository at this point in the history
…-other-filetypes-support

android support for remaining file types
  • Loading branch information
BrtqKr authored Mar 7, 2024
2 parents 2a35623 + 7865e2b commit 4284e15
Show file tree
Hide file tree
Showing 15 changed files with 397 additions and 183 deletions.
2 changes: 2 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ dependencies {
implementation "com.squareup.okhttp3:okhttp-urlconnection:4.+"

implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0")

implementation("com.google.code.gson:gson:2.8.8")
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)
Expand Down
48 changes: 42 additions & 6 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,48 @@
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />

<!-- Images -->
<data android:mimeType="image/jpg" />
<data android:mimeType="image/jpeg" />
<data android:mimeType="image/gif" />
<data android:mimeType="image/png" />
<data android:mimeType="image/tif" />
<data android:mimeType="image/tiff" />

<!-- Documents -->
<data android:mimeType="application/pdf" />
<data android:mimeType="application/msword" />
<data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />
<data android:mimeType="application/rtf" />
<data android:mimeType="application/zip" />
<data android:mimeType="message/rfc822" />

<!-- Text / HTML -->
<data android:mimeType="text/plain" />
<data android:mimeType="text/html" />
<data android:mimeType="text/xml" />

<!-- Audio / Video -->
<data android:mimeType="audio/mpeg" />
<data android:mimeType="audio/aac" />
<data android:mimeType="audio/flac" />
<data android:mimeType="audio/wav" />
<data android:mimeType="audio/x-wav" />
<data android:mimeType="audio/mp3" />
<data android:mimeType="audio/vorbis" />
<data android:mimeType="audio/x-vorbis" />
<data android:mimeType="audio/opus" />

<data android:mimeType="video/mp4" />
<data android:mimeType="video/mp2t" />
<data android:mimeType="video/webm" />
<data android:mimeType="video/avc" />
<data android:mimeType="video/hevc" />
<data android:mimeType="video/x-vnd.on2.vp8" />
<data android:mimeType="video/x-vnd.on2.vp9" />
<data android:mimeType="video/av01" />

</intent-filter>
</activity>

Expand Down
11 changes: 9 additions & 2 deletions android/app/src/main/java/com/expensify/chat/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ class MainActivity : ReactActivity() {
)
}

Log.i("TestLaunchIntent", "on create attempt")
if (intent != null) {
Log.i("TestLaunchIntent", "on create handle $intent")
handleIntent(intent)
}
}
Expand All @@ -69,8 +71,13 @@ class MainActivity : ReactActivity() {
}

private fun handleIntent(intent: Intent) {
val intentHandler = IntentHandlerFactory.getIntentHandler(this, intent.type)
intentHandler?.handle(intent)
try {
val intentHandler = IntentHandlerFactory.getIntentHandler(this, intent.type)
intentHandler?.handle(intent)
} catch (exception: Exception) {
Log.e("handleIntent", exception.toString())
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.expensify.chat
import android.content.Context
import android.util.Log
import com.expensify.chat.intentHandler.IntentHandlerConstants
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
Expand All @@ -16,15 +15,10 @@ class ShareActionHandlerModule(reactContext: ReactApplicationContext) : ReactCon
fun processFiles(callback: Callback) {
try {
val sharedPreferences = reactApplicationContext.getSharedPreferences(IntentHandlerConstants.preferencesFile, Context.MODE_PRIVATE)
val fileSet = sharedPreferences.getStringSet(IntentHandlerConstants.fileArrayProperty, setOf())
val fileArray: ArrayList<String> = ArrayList(fileSet)
val shareObject = sharedPreferences.getString(IntentHandlerConstants.shareObjectProperty, "{}")

val resultArray = Arguments.createArray()
for (file in fileArray) {
resultArray.pushString(file)
}

callback.invoke(resultArray)
Log.i("TestLaunchIntent", shareObject.toString())
callback.invoke(shareObject)
} catch (exception: Exception) {
Log.e("ImageIntentHandler", exception.toString())
callback.invoke(exception.toString(), null)
Expand Down
114 changes: 0 additions & 114 deletions android/app/src/main/java/com/expensify/chat/image/ImageUtils.kt

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
package com.expensify.chat.intentHandler

import android.content.Context
import com.expensify.chat.utils.FileUtils.clearInternalStorageDirectory

abstract class AbstractIntentHandler: IntentHandler {
override fun onCompleted() {}

protected fun clearTemporaryFiles(context: Context) {
// Clear data present in the shared preferences
val sharedPreferences = context.getSharedPreferences(IntentHandlerConstants.preferencesFile, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.clear()
editor.apply()

// Clear leftover temporary files from previous share attempts
clearInternalStorageDirectory(context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.expensify.chat.intentHandler

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import com.expensify.chat.utils.FileUtils


class ApplicationIntentHandler(private val context: Context) : AbstractIntentHandler() {
override fun handle(intent: Intent): Boolean {
super.clearTemporaryFiles(context)
when(intent.action) {
Intent.ACTION_SEND -> {
handleApplicationIntent(intent, context)
onCompleted()
return true
}
}
return false
}

private fun handleApplicationIntent(intent: Intent, context: Context) {
(intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM))?.let { fileUri ->
// Update UI to reflect image being shared
if (fileUri == null) {
return
}
Log.i("AppIntentHandler", "uri: $fileUri")

val resultingPath: String? = FileUtils.copyUriToStorage(fileUri, context)
if (resultingPath != null) {
val shareFileObject = ShareFileObject(resultingPath, intent.type)

val sharedPreferences = context.getSharedPreferences(IntentHandlerConstants.preferencesFile, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString(IntentHandlerConstants.shareObjectProperty, shareFileObject.toString())
editor.apply()
}
}
}


override fun onCompleted() {
val uri: Uri = Uri.parse("new-expensify://share/root")
val deepLinkIntent = Intent(Intent.ACTION_VIEW, uri)
deepLinkIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(deepLinkIntent)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.expensify.chat.intentHandler

import android.content.Context
import android.content.Intent
import android.net.Uri
import com.expensify.chat.utils.FileUtils


class AudioVideoIntentHandler(private val context: Context) : AbstractIntentHandler() {
override fun handle(intent: Intent): Boolean {
super.clearTemporaryFiles(context)
when(intent.action) {
Intent.ACTION_SEND -> {
handleAudioVideoIntent(intent, context)
onCompleted()
return true
}
}
return false
}

private fun handleAudioVideoIntent(intent: Intent, context: Context) {
(intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM))?.let { fileUri ->
// Update UI to reflect image being shared
if (fileUri == null) {
return
}

val resultingPath: String? = FileUtils.copyUriToStorage(fileUri, context)
if (resultingPath != null) {
val shareFileObject = ShareFileObject(resultingPath, intent.type)

val sharedPreferences = context.getSharedPreferences(IntentHandlerConstants.preferencesFile, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString(IntentHandlerConstants.shareObjectProperty, shareFileObject.toString())
editor.apply()
}
}
}


override fun onCompleted() {
val uri: Uri = Uri.parse("new-expensify://share/root")
val deepLinkIntent = Intent(Intent.ACTION_VIEW, uri)
deepLinkIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(deepLinkIntent)
}

}
Loading

0 comments on commit 4284e15

Please sign in to comment.