Skip to content
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

feat: receive sharing intents android #1178

Merged
merged 10 commits into from
Jan 15, 2024
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
5 changes: 5 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
<activity android:name="com.linusu.flutter_web_auth.CallbackActivity" android:exported="true">
<intent-filter android:label="flutter_web_auth">
Expand Down
28 changes: 28 additions & 0 deletions android/app/src/main/kotlin/com/rtirl/chat/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import java.util.UUID
import android.os.Bundle


class MainActivity : FlutterActivity() {

private var sharedData: String = ""

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleIntent()
}

override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
val ttsPlugin = TextToSpeechPlugin(this)
val ttsChannel = MethodChannel(
Expand Down Expand Up @@ -74,8 +83,27 @@ class MainActivity : FlutterActivity() {
}
}

MethodChannel(
flutterEngine.dartExecutor.binaryMessenger,
"com.rtirl.chat/share"
).setMethodCallHandler { call, result ->
if (call.method == "getSharedData") {
result.success(sharedData)
sharedData = ""
}
}

super.configureFlutterEngine(flutterEngine)
}

private fun handleIntent() {
// Handle the received text share intent
if (intent?.action == Intent.ACTION_SEND && intent.type == "text/plain") {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let { intentData ->
sharedData = intentData
}
}
}
}


Expand Down
15 changes: 15 additions & 0 deletions lib/components/message_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:rtchat/components/image/resilient_network_image.dart';
import 'package:rtchat/models/adapters/actions.dart';
import 'package:rtchat/models/channels.dart';
import 'package:rtchat/models/commands.dart';
import 'package:rtchat/share_channel.dart';

class MessageInputWidget extends StatefulWidget {
final Channel channel;
Expand Down Expand Up @@ -57,6 +58,20 @@ class _MessageInputWidgetState extends State<MessageInputWidget> {
_isKeyboardVisible = visible;
});
});

ShareChannel()
// Register a callback to handle any shared data while app is running
..onDataReceived = _handleSharedData
// Check to see if there is any shared data already via sharing
..getSharedText().then(_handleSharedData);
}

// Handles any shared data we may receive.
void _handleSharedData(String sharedData) {
debugPrint('Shared data received: $sharedData');
setState(() {
_textEditingController.text = sharedData;
});
}

@override
Expand Down
1 change: 0 additions & 1 deletion lib/screens/settings/qr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class QRDisplay extends StatelessWidget {
dataModuleShape: QrDataModuleShape.square,
color: Colors.black,
),
// Remove the embeddedImage property
),
CircleAvatar(
radius: 35,
Expand Down
27 changes: 27 additions & 0 deletions lib/share_channel.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:flutter/services.dart';

class ShareChannel {
void Function(String)? onDataReceived;

ShareChannel() {
// If sharing resumes the app we will check to see if we received any shared data
SystemChannels.lifecycle.setMessageHandler((msg) async {
if (msg?.contains("resumed") ?? false) {
getSharedText().then((String data) {
// Nothing was shared with us :(
if (data.isEmpty) {
return;
}
// We got something! Inform our listener.
onDataReceived?.call(data);
});
}
return;
});
}

static const _channel = MethodChannel('com.rtirl.chat/share');
Future<String> getSharedText() async {
return await _channel.invokeMethod<String>('getSharedData') ?? '';
}
}
Loading