-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into chore.upgrade-wmdb
- Loading branch information
Showing
78 changed files
with
1,206 additions
and
1,696 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
android/app/src/main/java/chat/rocket/reactnative/share/ShareActivity.java
This file was deleted.
Oops, something went wrong.
137 changes: 137 additions & 0 deletions
137
android/app/src/main/java/chat/rocket/reactnative/share/ShareActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package chat.rocket.reactnative.share | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.util.Log | ||
import androidx.appcompat.app.AppCompatActivity | ||
import java.io.File | ||
import java.io.FileOutputStream | ||
import java.util.* | ||
|
||
class ShareActivity : AppCompatActivity() { | ||
|
||
private val appScheme = "rocketchat" | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
handleIntent(intent) | ||
} | ||
|
||
private fun handleIntent(intent: Intent?) { | ||
// Check if the intent contains shared content | ||
if (intent?.action == Intent.ACTION_SEND || intent?.action == Intent.ACTION_SEND_MULTIPLE) { | ||
when { | ||
intent.type?.startsWith("text/") == true -> handleText(intent) | ||
intent.type?.startsWith("image/") == true -> handleMedia(intent, "data") | ||
intent.type?.startsWith("video/") == true -> handleMedia(intent, "data") | ||
intent.type?.startsWith("application/") == true -> handleMedia(intent, "data") | ||
intent.type == "*/*" -> handleMedia(intent, "data") | ||
intent.type == "text/plain" -> handleText(intent) | ||
else -> completeRequest() // No matching type, complete the request | ||
} | ||
} else { | ||
completeRequest() // No relevant intent action, complete the request | ||
} | ||
} | ||
|
||
private fun handleText(intent: Intent) { | ||
// Handle sharing text | ||
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT) | ||
if (sharedText != null) { | ||
val encoded = Uri.encode(sharedText) | ||
val url = Uri.parse("$appScheme://shareextension?text=$encoded") | ||
openURL(url) | ||
} | ||
completeRequest() | ||
} | ||
|
||
private fun handleMedia(intent: Intent, type: String) { | ||
val mediaUris = StringBuilder() | ||
var valid = true | ||
|
||
val uris = when (intent.action) { | ||
Intent.ACTION_SEND -> listOf(intent.getParcelableExtra(Intent.EXTRA_STREAM) as Uri?) | ||
Intent.ACTION_SEND_MULTIPLE -> intent.getParcelableArrayListExtra<Uri>(Intent.EXTRA_STREAM) | ||
else -> null | ||
} | ||
|
||
uris?.forEachIndexed { index, uri -> | ||
val mediaUri = uri?.let { handleMediaUri(it, type) } | ||
if (mediaUri != null) { | ||
mediaUris.append(mediaUri) | ||
if (index < uris.size - 1) { | ||
mediaUris.append(",") | ||
} | ||
} else { | ||
valid = false | ||
} | ||
} | ||
|
||
if (valid) { | ||
val encoded = Uri.encode(mediaUris.toString()) | ||
val url = Uri.parse("$appScheme://shareextension?mediaUris=$encoded") | ||
openURL(url) | ||
} | ||
completeRequest() | ||
} | ||
|
||
private fun handleMediaUri(uri: Uri, type: String): String? { | ||
return try { | ||
val inputStream = contentResolver.openInputStream(uri) | ||
val originalFilename = getFileName(uri) | ||
val filename = originalFilename ?: UUID.randomUUID().toString() + getFileExtension(uri, type) | ||
val fileUri = saveDataToCacheDir(inputStream?.readBytes(), filename) | ||
fileUri?.toString() | ||
} catch (e: Exception) { | ||
Log.e("ShareRocketChat", "Failed to process media", e) | ||
null | ||
} | ||
} | ||
|
||
private fun getFileName(uri: Uri): String? { | ||
// Attempt to get the original filename from the Uri | ||
val cursor = contentResolver.query(uri, null, null, null, null) | ||
return cursor?.use { | ||
if (it.moveToFirst()) { | ||
val nameIndex = it.getColumnIndex("_display_name") | ||
if (nameIndex != -1) it.getString(nameIndex) else null | ||
} else null | ||
} | ||
} | ||
|
||
private fun getFileExtension(uri: Uri, type: String): String { | ||
// Determine the file extension based on the mime type, with fallbacks | ||
val mimeType = contentResolver.getType(uri) | ||
return when { | ||
mimeType?.startsWith("image/") == true -> ".jpeg" | ||
mimeType?.startsWith("video/") == true -> ".mp4" | ||
else -> "" // Ignore the file if the type is not recognized | ||
} | ||
} | ||
|
||
private fun saveDataToCacheDir(data: ByteArray?, filename: String): Uri? { | ||
// Save the shared data to the app's cache directory and return the file URI | ||
return try { | ||
val file = File(cacheDir, filename) | ||
FileOutputStream(file).use { it.write(data) } | ||
Uri.fromFile(file) // Return the file URI with file:// scheme | ||
} catch (e: Exception) { | ||
Log.e("ShareRocketChat", "Failed to save data", e) | ||
null | ||
} | ||
} | ||
|
||
private fun openURL(uri: Uri) { | ||
// Open the custom URI in the associated app | ||
val intent = Intent(Intent.ACTION_VIEW, uri) | ||
if (intent.resolveActivity(packageManager) != null) { | ||
startActivity(intent) | ||
} | ||
} | ||
|
||
private fun completeRequest() { | ||
// Finish the share activity | ||
finish() | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
android/app/src/main/java/chat/rocket/reactnative/share/ShareApplication.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
{ | ||
"name": "RocketChatRN", | ||
"share": "ShareRocketChatRN", | ||
"displayName": "RocketChatRN" | ||
"name": "RocketChatRN" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,17 @@ | ||
import { Action } from 'redux'; | ||
|
||
import { IShareServer, IShareUser, TShareSettings } from '../reducers/share'; | ||
import { TShareParams } from '../reducers/share'; | ||
import { SHARE } from './actionsTypes'; | ||
|
||
interface IShareSelectServer extends Action { | ||
server: IShareServer; | ||
interface IShareSetParams extends Action { | ||
params: TShareParams; | ||
} | ||
|
||
interface IShareSetSettings extends Action { | ||
settings: TShareSettings; | ||
} | ||
|
||
interface IShareSetUser extends Action { | ||
user: IShareUser; | ||
} | ||
|
||
export type TActionsShare = IShareSelectServer & IShareSetSettings & IShareSetUser; | ||
|
||
export function shareSelectServer(server: IShareServer): IShareSelectServer { | ||
return { | ||
type: SHARE.SELECT_SERVER, | ||
server | ||
}; | ||
} | ||
|
||
export function shareSetSettings(settings: TShareSettings): IShareSetSettings { | ||
return { | ||
type: SHARE.SET_SETTINGS, | ||
settings | ||
}; | ||
} | ||
export type TActionsShare = IShareSetParams; | ||
|
||
export function shareSetUser(user: IShareUser): IShareSetUser { | ||
export function shareSetParams(params: TShareParams): IShareSetParams { | ||
return { | ||
type: SHARE.SET_USER, | ||
user | ||
type: SHARE.SET_PARAMS, | ||
params | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.