Skip to content

Commit

Permalink
//xplat/js/react-native-github/packages/react-native/ReactAndroid/src…
Browse files Browse the repository at this point in the history
…/main/java/com/facebook/react/modules/share:shareAndroid (#43977)

Summary:
Pull Request resolved: #43977

Changelog: [Internal]

Reviewed By: cortinico

Differential Revision: D55725721

fbshipit-source-id: 21f190043da053197bd5ba047122af79635f56ba
  • Loading branch information
andrewdacenko authored and facebook-github-bot committed Apr 8, 2024
1 parent 5156946 commit 8c804de
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 76 deletions.
7 changes: 6 additions & 1 deletion packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3479,11 +3479,16 @@ public class com/facebook/react/modules/permissions/PermissionsModule : com/face
public fun shouldShowRequestPermissionRationale (Ljava/lang/String;Lcom/facebook/react/bridge/Promise;)V
}

public class com/facebook/react/modules/share/ShareModule : com/facebook/fbreact/specs/NativeShareModuleSpec {
public final class com/facebook/react/modules/share/ShareModule : com/facebook/fbreact/specs/NativeShareModuleSpec {
public static final field Companion Lcom/facebook/react/modules/share/ShareModule$Companion;
public static final field ERROR_INVALID_CONTENT Ljava/lang/String;
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
public fun share (Lcom/facebook/react/bridge/ReadableMap;Ljava/lang/String;Lcom/facebook/react/bridge/Promise;)V
}

public final class com/facebook/react/modules/share/ShareModule$Companion {
}

public final class com/facebook/react/modules/sound/SoundManagerModule : com/facebook/fbreact/specs/NativeSoundManagerSpec {
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
public fun playTouchSound ()V
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.share

import android.content.Intent
import com.facebook.fbreact.specs.NativeShareModuleSpec
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.annotations.ReactModule

/** Intent module. Launch other activities or open URLs. */
@ReactModule(name = NativeShareModuleSpec.NAME)
public class ShareModule(reactContext: ReactApplicationContext) :
NativeShareModuleSpec(reactContext) {

/**
* Open a chooser dialog to send text content to other apps.
*
* Refer http://developer.android.com/intl/ko/training/sharing/send.html
*
* @param content the data to send
* @param dialogTitle the title of the chooser dialog
*/
public override fun share(content: ReadableMap?, dialogTitle: String?, promise: Promise) {
if (content == null) {
promise.reject(ERROR_INVALID_CONTENT, "Content cannot be null")
return
}
try {
val intent = Intent(Intent.ACTION_SEND)
intent.setTypeAndNormalize("text/plain")
if (content.hasKey("title")) {
intent.putExtra(Intent.EXTRA_SUBJECT, content.getString("title"))
}
if (content.hasKey("message")) {
intent.putExtra(Intent.EXTRA_TEXT, content.getString("message"))
}
val chooser = Intent.createChooser(intent, dialogTitle)
chooser.addCategory(Intent.CATEGORY_DEFAULT)
val currentActivity = getCurrentActivity()
if (currentActivity != null) {
currentActivity.startActivity(chooser)
} else {
getReactApplicationContext().startActivity(chooser)
}
val result = Arguments.createMap()
result.putString("action", ACTION_SHARED)
promise.resolve(result)
} catch (e: Exception) {
promise.reject(ERROR_UNABLE_TO_OPEN_DIALOG, "Failed to open share dialog")
}
}

public companion object {
const private val ACTION_SHARED: String = "sharedAction"
const public val ERROR_INVALID_CONTENT: String = "E_INVALID_CONTENT"
const private val ERROR_UNABLE_TO_OPEN_DIALOG: String = "E_UNABLE_TO_OPEN_DIALOG"
}
}

0 comments on commit 8c804de

Please sign in to comment.