Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 14 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
24 changes: 24 additions & 0 deletions packages/share/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,27 @@ android {
disable 'InvalidPackage'
}
}

afterEvaluate {
def containsEmbeddingDependencies = false
for (def configuration : configurations.all) {
for (def dependency : configuration.dependencies) {
if (dependency.group == 'io.flutter' &&
dependency.name.startsWith('flutter_embedding') &&
dependency.isTransitive())
{
containsEmbeddingDependencies = true
break
}
}
}
if (!containsEmbeddingDependencies) {
android {
dependencies {
def lifecycle_version = "2.1.0"
api "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
api "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.share;

import androidx.annotation.NonNull;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import java.util.Map;

/** Handles the method calls for the plugin. */
class MethodCallHandler implements MethodChannel.MethodCallHandler {

private Share share;

/** Constructs the MethodChannelHandler */
MethodCallHandler(@NonNull Share share) {
this.share = share;
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("share")) {
if (!(call.arguments instanceof Map)) {
throw new IllegalArgumentException("Map argument expected");
}
// Android does not support showing the share sheet at a particular point on screen.
share.share((String) call.argument("text"), (String) call.argument("subject"));
result.success(null);
} else {
result.notImplemented();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.share;

import android.app.Activity;
import android.content.Intent;
import androidx.annotation.Nullable;

/** Handles share intent. */
class Share {

private Activity activity;

/**
* Constructs a Share object. The {@code activity} is used to start the share intent. It might be
* null when constructing the {@link Share} object and set to non-null when an activity is
* available using {@link #setActivity(Activity)}.
*/
Share(@Nullable Activity activity) {
this.activity = activity;
}

/**
* Sets the activity when an activity is available. When the activity becomes unavailable, use
* this method to set it to null.
*/
void setActivity(@Nullable Activity activity) {
this.activity = activity;
}

void share(String text, String subject) {
if (text == null || text.isEmpty()) {
throw new IllegalArgumentException("Non-empty text expected");
}

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.setType("text/plain");
Intent chooserIntent = Intent.createChooser(shareIntent, null /* dialog title optional */);
if (activity != null) {
activity.startActivity(chooserIntent);
} else {
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(chooserIntent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,63 @@

package io.flutter.plugins.share;

import android.content.Intent;
import io.flutter.plugin.common.MethodCall;
import android.app.Activity;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import java.util.Map;

/** Plugin method host for presenting a share sheet via Intent */
public class SharePlugin implements MethodChannel.MethodCallHandler {
public class SharePlugin implements FlutterPlugin, ActivityAware {

private static final String CHANNEL = "plugins.flutter.io/share";
private MethodCallHandler handler;
private Activity activity;
private Share share;
private MethodChannel methodChannel;

public static void registerWith(Registrar registrar) {
MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);
SharePlugin instance = new SharePlugin(registrar);
channel.setMethodCallHandler(instance);
SharePlugin plugin = new SharePlugin();
plugin.setUpChannel(registrar.messenger());
}

private final Registrar mRegistrar;
@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
setUpChannel(binding.getFlutterEngine().getDartExecutor());
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we teardown the channel here?


private SharePlugin(Registrar registrar) {
this.mRegistrar = registrar;
@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
activity = binding.getActivity();
share.setActivity(activity);
}

@Override
public void onDetachedFromActivity() {
activity = null;
share.setActivity(null);
methodChannel.setMethodCallHandler(null);
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
onAttachedToActivity(binding);
}

@Override
public void onMethodCall(MethodCall call, MethodChannel.Result result) {
if (call.method.equals("share")) {
if (!(call.arguments instanceof Map)) {
throw new IllegalArgumentException("Map argument expected");
}
// Android does not support showing the share sheet at a particular point on screen.
share((String) call.argument("text"), (String) call.argument("subject"));
result.success(null);
} else {
result.notImplemented();
}
public void onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity();
}

private void share(String text, String subject) {
if (text == null || text.isEmpty()) {
throw new IllegalArgumentException("Non-empty text expected");
}

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
shareIntent.setType("text/plain");
Intent chooserIntent = Intent.createChooser(shareIntent, null /* dialog title optional */);
if (mRegistrar.activity() != null) {
mRegistrar.activity().startActivity(chooserIntent);
} else {
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mRegistrar.context().startActivity(chooserIntent);
}
private void setUpChannel(BinaryMessenger messenger) {
methodChannel = new MethodChannel(messenger, CHANNEL);
share = new Share(activity);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like activity is always null at this point. You can probably remove the local activity variable and exclusively use the setter method, instead.

handler = new MethodCallHandler(share);
methodChannel.setMethodCallHandler(handler);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
<uses-permission android:name="android.permission.INTERNET"/>

<application android:name="io.flutter.app.FlutterApplication" android:label="share_example" android:icon="@mipmap/ic_launcher">
<activity android:name=".MainActivity"
<activity android:name=".EmbeddingV1Activity"
android:launchMode="singleTop"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a comment about singleTop in the sensor PR.

android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
</activity>
<activity android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@android:style/Theme.Black.NoTitleBar"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.shareexample;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class EmbeddingV1Activity extends FlutterActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Copyright 2019 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package io.flutter.plugins.shareexample;

import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.share.SharePlugin;

public class MainActivity extends FlutterActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
public void configureFlutterEngine(FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
flutterEngine.getPlugins().add(new SharePlugin());
}
}
3 changes: 3 additions & 0 deletions packages/share/example/android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true