Skip to content

Revert "Revert "Share extension Android and iOS implementation"" #58834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,52 @@
<data android:scheme="https" android:host="staging.new.expensify.com" android:pathPrefix="/track-expense"/>
<data android:scheme="https" android:host="staging.new.expensify.com" android:pathPrefix="/submit-expense"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />

<!-- 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>

<meta-data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public List<NativeModule> createNativeModules(
List<NativeModule> modules = new ArrayList<>();

modules.add(new StartupTimer(reactContext));
modules.add(new ShareActionHandlerModule(reactContext));

return modules;
}
Expand Down
25 changes: 23 additions & 2 deletions android/app/src/main/java/com/expensify/chat/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package com.expensify.chat

import expo.modules.ReactActivityDelegateWrapper

import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.view.WindowInsets
import com.expensify.chat.bootsplash.BootSplash
import com.expensify.chat.intenthandler.IntentHandlerFactory
import com.expensify.reactnativekeycommand.KeyCommandModule
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import expo.modules.ReactActivityDelegateWrapper

import com.oblador.performance.RNPerformance

Expand Down Expand Up @@ -52,6 +54,25 @@ class MainActivity : ReactActivity() {
defaultInsets.systemWindowInsetBottom
)
}

if (intent != null) {
handleIntent(intent)
}
}

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
setIntent(intent) // Must store the new intent unless getIntent() will return the old one
handleIntent(intent)
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.expensify.chat

import android.content.Context
import android.graphics.BitmapFactory
import android.media.MediaMetadataRetriever
import com.expensify.chat.intenthandler.IntentHandlerConstants
import com.facebook.react.bridge.Callback
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import android.util.Log
import com.facebook.react.bridge.ReactMethod
import org.json.JSONObject
import java.io.File

class ShareActionHandlerModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {

override fun getName(): String {
return "ShareActionHandler"
}

@ReactMethod
fun processFiles(callback: Callback) {
try {
val sharedPreferences = reactApplicationContext.getSharedPreferences(
IntentHandlerConstants.preferencesFile,
Context.MODE_PRIVATE
)

val shareObjectString = sharedPreferences.getString(IntentHandlerConstants.shareObjectProperty, null)
if (shareObjectString == null) {
callback.invoke("No data found", null)
return
}

val shareObject = JSONObject(shareObjectString)
val content = shareObject.optString("content")
val mimeType = shareObject.optString("mimeType")
val fileUriPath = "file://$content"
val timestamp = System.currentTimeMillis()

val file = File(content)
if (!file.exists()) {
val textObject = JSONObject().apply {
put("id", "text")
put("content", content)
put("mimeType", "txt")
put("processedAt", timestamp)
}
callback.invoke(textObject.toString())
return
}

val identifier = file.name
var aspectRatio = 0.0f

if (mimeType.startsWith("image/")) {
val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
BitmapFactory.decodeFile(content, options)
aspectRatio = if (options.outHeight != 0) options.outWidth.toFloat() / options.outHeight else 1.0f
} else if (mimeType.startsWith("video/")) {
val retriever = MediaMetadataRetriever()
try {
retriever.setDataSource(content)
val videoWidth = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)?.toFloatOrNull() ?: 1f
val videoHeight = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)?.toFloatOrNull() ?: 1f
if (videoHeight != 0f) aspectRatio = videoWidth / videoHeight
} catch (e: Exception) {
Log.e("ShareActionHandlerModule", "Error retrieving video metadata: ${e.message}")
} finally {
retriever.release()
}
}

val fileData = JSONObject().apply {
put("id", identifier)
put("content", fileUriPath)
put("mimeType", mimeType)
put("processedAt", timestamp)
put("aspectRatio", aspectRatio)
}

callback.invoke(fileData.toString())

} catch (e: Exception) {
callback.invoke(e.toString(), null)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +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,42 @@
package com.expensify.chat.intenthandler

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

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

private fun handleSingleFileIntent(intent: Intent, context: Context) {
(intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM))?.let { 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,12 @@
package com.expensify.chat.intenthandler

import android.content.Intent

object IntentHandlerConstants {
const val preferencesFile = "shareActionHandler"
const val shareObjectProperty = "shareObject"
}
interface IntentHandler {
fun handle(intent: Intent): Boolean
fun onCompleted()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.expensify.chat.intenthandler

import android.content.Context

object IntentHandlerFactory {
fun getIntentHandler(context: Context, mimeType: String?, rest: String?): IntentHandler? {
if (mimeType == null) return null

return when {
mimeType.matches(Regex("(image|application|audio|video)/.*")) -> FileIntentHandler(context)
mimeType.startsWith("text/") -> TextIntentHandler(context)
else -> throw UnsupportedOperationException("Unsupported MIME type: $mimeType")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.expensify.chat.intenthandler

import org.json.JSONObject

data class ShareFileObject(
val content: String,
val mimeType: String?,
) {
override fun toString(): String {
return JSONObject().apply {
put("content", content)
put("mimeType", mimeType)
}.toString()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.expensify.chat.intenthandler

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


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

private fun handleTextIntent(intent: Intent, context: Context) {
when {
intent.type == "text/plain" -> {
val extras = intent.extras
if (extras != null) {
when {
extras.containsKey(Intent.EXTRA_STREAM) -> {
handleTextFileIntent(intent, context)
}
extras.containsKey(Intent.EXTRA_TEXT) -> {
handleTextPlainIntent(intent, context)
}
else -> {
throw UnsupportedOperationException("Unknown text/plain content")
}
}
}
}
Regex("text/.*").matches(intent.type ?: "") -> handleTextFileIntent(intent, context)
else -> throw UnsupportedOperationException("Unsupported MIME type: ${intent.type}")
}
}

private fun saveToSharedPreferences(key: String, value: String) {
val sharedPreferences = context.getSharedPreferences(IntentHandlerConstants.preferencesFile, Context.MODE_PRIVATE)
val editor = sharedPreferences.edit()
editor.putString(key, value)
editor.apply()
}

private fun handleTextFileIntent(intent: Intent, context: Context) {
(intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM))?.let { fileUri ->
val resultingPath: String? = FileUtils.copyUriToStorage(fileUri, context)
if (resultingPath != null) {
val shareFileObject = ShareFileObject(resultingPath, intent.type)
saveToSharedPreferences(IntentHandlerConstants.shareObjectProperty, shareFileObject.toString())
}
}
}

private fun handleTextPlainIntent(intent: Intent, context: Context) {
var intentTextContent = intent.getStringExtra(Intent.EXTRA_TEXT)
if(intentTextContent != null) {
val shareFileObject = ShareFileObject(intentTextContent, intent.type)
saveToSharedPreferences(IntentHandlerConstants.shareObjectProperty, shareFileObject.toString())
}
}

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
Loading