[Android] Add support for the PlatformChannel "Share.invoke" command (flutter/engine#48265)
## Description This PR adds support for the PlatformChannel `Share.invoke` message on Android (before this PR it is only supported on iOS). ## Related Issue Engine side for https://github.com/flutter/flutter/issues/138728 ## Tests Adds 2 tests.
This commit is contained in:
@@ -187,6 +187,11 @@ public class PlatformChannel {
|
||||
result.success(response);
|
||||
break;
|
||||
}
|
||||
case "Share.invoke":
|
||||
String text = (String) arguments;
|
||||
platformMessageHandler.share(text);
|
||||
result.success(null);
|
||||
break;
|
||||
default:
|
||||
result.notImplemented();
|
||||
break;
|
||||
@@ -549,6 +554,13 @@ public class PlatformChannel {
|
||||
* can be pasted.
|
||||
*/
|
||||
boolean clipboardHasStrings();
|
||||
|
||||
/**
|
||||
* The Flutter application would like to share the given {@code text} using the Android standard
|
||||
* intent action named {@code Intent.ACTION_SEND}. See:
|
||||
* https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND
|
||||
*/
|
||||
void share(@NonNull String text);
|
||||
}
|
||||
|
||||
/** Types of sounds the Android OS can play on behalf of an application. */
|
||||
|
||||
@@ -11,6 +11,7 @@ import android.content.ClipData;
|
||||
import android.content.ClipDescription;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.os.Build;
|
||||
import android.view.HapticFeedbackConstants;
|
||||
@@ -145,6 +146,11 @@ public class PlatformPlugin {
|
||||
public boolean clipboardHasStrings() {
|
||||
return PlatformPlugin.this.clipboardHasStrings();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void share(@NonNull String text) {
|
||||
PlatformPlugin.this.share(text);
|
||||
}
|
||||
};
|
||||
|
||||
public PlatformPlugin(@NonNull Activity activity, @NonNull PlatformChannel platformChannel) {
|
||||
@@ -570,4 +576,13 @@ public class PlatformPlugin {
|
||||
}
|
||||
return description.hasMimeType("text/*");
|
||||
}
|
||||
|
||||
private void share(@NonNull String text) {
|
||||
Intent intent = new Intent();
|
||||
intent.setAction(Intent.ACTION_SEND);
|
||||
intent.setType("text/plain");
|
||||
intent.putExtra(Intent.EXTRA_TEXT, text);
|
||||
|
||||
activity.startActivity(Intent.createChooser(intent, null));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package io.flutter.embedding.engine.systemchannels;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.ArgumentMatchers.refEq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
@@ -15,6 +17,7 @@ import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@Config(manifest = Config.NONE)
|
||||
@@ -42,4 +45,26 @@ public class PlatformChannelTest {
|
||||
}
|
||||
verify(mockResult).success(refEq(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void platformChannel_shareInvokeMessage() {
|
||||
MethodChannel rawChannel = mock(MethodChannel.class);
|
||||
FlutterJNI mockFlutterJNI = mock(FlutterJNI.class);
|
||||
DartExecutor dartExecutor = new DartExecutor(mockFlutterJNI, mock(AssetManager.class));
|
||||
PlatformChannel fakePlatformChannel = new PlatformChannel(dartExecutor);
|
||||
PlatformChannel.PlatformMessageHandler mockMessageHandler =
|
||||
mock(PlatformChannel.PlatformMessageHandler.class);
|
||||
fakePlatformChannel.setPlatformMessageHandler(mockMessageHandler);
|
||||
|
||||
ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class);
|
||||
doNothing().when(mockMessageHandler).share(valueCapture.capture());
|
||||
|
||||
final String expectedContent = "Flutter";
|
||||
MethodCall methodCall = new MethodCall("Share.invoke", expectedContent);
|
||||
MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
|
||||
fakePlatformChannel.parsingMethodCallHandler.onMethodCall(methodCall, mockResult);
|
||||
|
||||
assertEquals(valueCapture.getValue(), expectedContent);
|
||||
verify(mockResult).success(null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,16 +6,19 @@ package io.flutter.plugin.platform;
|
||||
|
||||
import static android.view.WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS;
|
||||
import static android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.anyBoolean;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.mockStatic;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.times;
|
||||
@@ -28,6 +31,7 @@ import android.content.ClipDescription;
|
||||
import android.content.ClipboardManager;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.AssetFileDescriptor;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
@@ -47,6 +51,8 @@ import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedStatic;
|
||||
import org.robolectric.Robolectric;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.android.controller.ActivityController;
|
||||
@@ -629,4 +635,35 @@ public class PlatformPluginTest {
|
||||
|
||||
verify(mockActivity, times(1)).finish();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startChoosenActivityWhenSharingText() {
|
||||
Activity mockActivity = mock(Activity.class);
|
||||
PlatformChannel mockPlatformChannel = mock(PlatformChannel.class);
|
||||
PlatformPluginDelegate mockPlatformPluginDelegate = mock(PlatformPluginDelegate.class);
|
||||
PlatformPlugin platformPlugin =
|
||||
new PlatformPlugin(mockActivity, mockPlatformChannel, mockPlatformPluginDelegate);
|
||||
|
||||
// Mock Intent.createChooser (in real application it opens a chooser where the user can
|
||||
// select which application will be used to share the selected text).
|
||||
Intent choosenIntent = new Intent();
|
||||
MockedStatic<Intent> intentClass = mockStatic(Intent.class);
|
||||
ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
|
||||
intentClass
|
||||
.when(() -> Intent.createChooser(intentCaptor.capture(), any()))
|
||||
.thenReturn(choosenIntent);
|
||||
|
||||
final String expectedContent = "Flutter";
|
||||
platformPlugin.mPlatformMessageHandler.share(expectedContent);
|
||||
|
||||
// Activity.startActivity should have been called.
|
||||
verify(mockActivity, times(1)).startActivity(choosenIntent);
|
||||
|
||||
// The intent action created by the plugin and passed to Intent.createChooser should be
|
||||
// 'Intent.ACTION_SEND'.
|
||||
Intent sendToIntent = intentCaptor.getValue();
|
||||
assertEquals(sendToIntent.getAction(), Intent.ACTION_SEND);
|
||||
assertEquals(sendToIntent.getType(), "text/plain");
|
||||
assertEquals(sendToIntent.getStringExtra(Intent.EXTRA_TEXT), expectedContent);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user