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 1 commit
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
Prev Previous commit
Next Next commit
refactor
  • Loading branch information
Chris Yang committed Oct 4, 2019
commit d372d3d8744311d1bf214cff13884f3d6ac02876
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.share.MethodChannelHandler;
import io.flutter.plugins.share.MethodCallHandler;
import io.flutter.plugins.share.Share;

/**
* Plugin implementation that uses the new {@code io.flutter.embedding} package.
Expand All @@ -15,14 +16,16 @@
public class SharePlugin implements FlutterPlugin, ActivityAware {

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

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
final MethodChannel methodChannel =
new MethodChannel(binding.getFlutterEngine().getDartExecutor(), CHANNEL);
handler = new MethodChannelHandler(activity);
share = new Share(activity);
handler = new MethodCallHandler(share);
methodChannel.setMethodCallHandler(handler);
}

Expand All @@ -32,13 +35,13 @@ public void onDetachedFromEngine(FlutterPluginBinding binding) {}
@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
activity = binding.getActivity();
handler.setActivity(activity);
share.setActivity(activity);
}

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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 channel for the plugin. */
public class MethodCallHandler implements MethodChannel.MethodCallHandler {

private Share share;

/** Constructs the MethodChannelHandler */
public 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();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.flutter.plugins.share;

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

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

private Activity activity;

/**
* Constructs a Share object.
*
* @param activity the activity used to fire share intent.
*/
public Share(@Nullable Activity activity) {
this.activity = activity;
}

/**
* Sets the activity.
*
* @param activity if the activity becomes unavailable, call this with a null parameter to set it
* to null.
*/
public 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 @@ -14,8 +14,8 @@ public class SharePlugin {

public static void registerWith(Registrar registrar) {
MethodChannel channel = new MethodChannel(registrar.messenger(), CHANNEL);

MethodChannelHandler handler = new MethodChannelHandler(registrar.activity());
Share share = new Share(registrar.activity());
MethodCallHandler handler = new MethodCallHandler(share);
channel.setMethodCallHandler(handler);
}
}