Skip to content

Commit

Permalink
android: Move sharing logic to MainActivity.
Browse files Browse the repository at this point in the history
Deletes 'ReceiveSharingActivity' and moves the 'handle receiving
shares from other apps' logic to MainActivity, as this is a better,
simpler logic and we don't have to maintain the dummy component
'SharingRoot' and an extra activity to show that.

Also deletes the now unsued component 'SharingRoot'.
  • Loading branch information
agrawal-d committed Jun 29, 2020
1 parent e0df137 commit b8b1462
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 131 deletions.
2 changes: 1 addition & 1 deletion android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

<!-- Disabled while the feature is experimental. See #117 and #4124.
<activity
android:name=".sharing.ReceiveShareActivity"
android:name=".MainActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:label="@string/app_name"
android:launchMode="singleTask"
Expand Down
89 changes: 88 additions & 1 deletion android/app/src/main/java/com/zulipmobile/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
package com.zulipmobile

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.webkit.WebView
import androidx.annotation.Nullable
import com.facebook.react.ReactActivity
import com.facebook.react.ReactApplication
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.ReactContext
import com.facebook.react.bridge.WritableMap
import com.zulipmobile.notifications.*
import com.zulipmobile.sharing.SharingModule

const val TAG = "MainActivityShare"


open class MainActivity : ReactActivity() {
/**
Expand All @@ -13,8 +27,81 @@ open class MainActivity : ReactActivity() {
return "ZulipMobile"
}

private fun sendEvent(reactContext: ReactContext,
eventName: String,
@Nullable params: WritableMap) {
Log.d(TAG, "Sending event with shared data")
emit(reactContext, eventName, params)
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
WebView.setWebContentsDebuggingEnabled(true)

if (intent?.action == Intent.ACTION_SEND) {
handleSend(intent)
}
// TODO also handle ACTION_SEND_MULTIPLE?
// See: https://developer.android.com/training/sharing/receive#receiving-data-activity
}

private fun handleSend(intent: Intent) {
val params: WritableMap
try {
params = getParamsFromIntent(intent)
} catch (e: ShareParamsParseException) {
Log.w(TAG, "Ignoring malformed share Intent: ${e.message}")
return
}

// TODO deduplicate this with notifyReact.
// Until then, keep in sync when changing.
val application = application as ReactApplication
val host = application.reactNativeHost
val reactContext = host.tryGetReactInstanceManager()?.currentReactContext
val appStatus = reactContext?.appStatus
Log.d(TAG, "app status is $appStatus")
when (appStatus) {
null, ReactAppStatus.NOT_RUNNING ->
// Either there's no JS environment running, or we haven't yet reached
// foreground. Expect the app to check initialSharedData on launch.
{
SharingModule.initialSharedData = params
Log.d(TAG, "Sharing data as initialSharedData")
}

ReactAppStatus.BACKGROUND, ReactAppStatus.FOREGROUND ->
// JS is running and has already reached foreground. It won't check
// initialSharedData again, but it will see a shareReceived event.
sendEvent(reactContext, "shareReceived", params)
}
}
}

private fun getParamsFromIntent(intent: Intent): WritableMap {
// For documentation of what fields to expect here, see:
// https://developer.android.com/reference/android/content/Intent#ACTION_SEND
val params = Arguments.createMap()
when {
"text/plain" == intent.type -> {
val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT)
params.putString("type", "text")
params.putString("sharedText", sharedText)
}
intent.type?.startsWith("image/") == true -> {
val url = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
?: throw ShareParamsParseException("Could not extract URL from Image Intent")
params.putString("type", "image")
params.putString("sharedImageUrl", url.toString())
}
else -> {
val url = intent.getParcelableExtra<Uri>(Intent.EXTRA_STREAM)
?: throw ShareParamsParseException("Could not extract URL from File Intent")
params.putString("type", "file")
params.putString("sharedFileUrl", url.toString())
}
}
return params
}
}

class ShareParamsParseException(errorMessage: String) : RuntimeException(errorMessage)

This file was deleted.

2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
/* @flow strict-local */
import { AppRegistry } from 'react-native';
import ZulipMobile from './src/ZulipMobile';
import SharingRoot from './src/sharing/SharingRoot';

AppRegistry.registerComponent('ZulipMobile', () => ZulipMobile);
AppRegistry.registerComponent('SharingRoot', () => SharingRoot);
18 changes: 0 additions & 18 deletions src/sharing/SharingRoot.js

This file was deleted.

0 comments on commit b8b1462

Please sign in to comment.