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
Changes from chat discussion
1. Move all classes into `dev`.
2. Clean up the MethodChannel based on the engine lifecycle handlers.
  • Loading branch information
Michael Klimushyn committed Oct 11, 2019
commit a5caa9f68f9cea46a36c3b25b5725b6b0a0aff2d
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@
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.MethodChannel;

/**
* Plugin implementation that uses the new {@code io.flutter.embedding} package.
*
* <p>This can be included in an add to app scenario to gracefully handle activity and context
* changes, unlike the previous {@link io.flutter.plugins.androidintent.AndroidIntentPlugin}.
* <p>Instantiate this in an add to app scenario to gracefully handle activity and context changes.
*/
public final class AndroidIntentPlugin implements FlutterPlugin, ActivityAware {
private final IntentSender sender;
private final MethodCallHandlerImpl impl;

/**
* Initialize this within the {@code #configureFlutterEngine} of a Flutter activity or fragment.
Expand All @@ -22,22 +21,21 @@ public final class AndroidIntentPlugin implements FlutterPlugin, ActivityAware {
*/
public AndroidIntentPlugin() {
sender = new IntentSender(/*activity=*/ null, /*context=*/ null);
impl = new MethodCallHandlerImpl(sender);
}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
sender.setApplicationContext(binding.getApplicationContext());
sender.setActivity(null);
MethodChannel channel =
new MethodChannel(
binding.getFlutterEngine().getDartExecutor(), "plugins.flutter.io/android_intent");
channel.setMethodCallHandler(new MethodCallHandlerImpl(sender));
impl.startListening(binding.getFlutterEngine().getDartExecutor());
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
sender.setApplicationContext(null);
sender.setActivity(null);
impl.stopListening();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// 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 dev.flutter.plugins.androidintent;

import io.flutter.plugin.common.PluginRegistry.Registrar;

/**
* Automatically registers a plugin implementation that relies on the stable {@code
* io.flutter.plugin.common} package.
*/
public final class AndroidIntentPluginRegistrar {
private AndroidIntentPluginRegistrar() {}

/**
* Registers a plugin implementation that uses the stable {@code io.flutter.plugin.common}
* package.
*
* <p>Calling this automatically initializes the plugin. However plugins initialized this way
* won't react to changes in activity or context, unlike {@link AndroidIntentPlugin}.
*/
public static void registerWith(Registrar registrar) {
IntentSender sender = new IntentSender(registrar.activity(), registrar.context());
MethodCallHandlerImpl impl = new MethodCallHandlerImpl(sender);
impl.startListening(registrar.messenger());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,65 @@
import android.os.Bundle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import java.util.ArrayList;
import java.util.Map;

/** Forwards incoming {@link MethodCall}s to {@link IntentSender#send}. */
public final class MethodCallHandlerImpl implements MethodCallHandler {
private static final String TAG = "MethodCallHandlerImpl";
private final IntentSender sender;
private @Nullable MethodChannel methodChannel;

/**
* Uses the given {@code sender} for all incoming calls.
*
* <p>This assumes that the sender's context and activity state are managed elsewhere and
* correctly initialized before being sent here.
*/
public MethodCallHandlerImpl(IntentSender sender) {
MethodCallHandlerImpl(IntentSender sender) {
this.sender = sender;
}

/**
* Registers this instance as a method call handler on the given {@code messenger}.
*
* <p>Stops any previously started and unstopped calls.
*
* <p>This should be cleaned with {@link #stopListening} once the messenger is disposed of.
*/
void startListening(BinaryMessenger messenger) {
if (methodChannel != null) {
Log.wtf(TAG, "Setting a method call handler before the last was disposed.");
stopListening();
}

methodChannel = new MethodChannel(messenger, "plugins.flutter.io/android_intent");
methodChannel.setMethodCallHandler(this);
}

/**
* Clears this instance from listening to method calls.
*
* <p>Does nothing is {@link #startListening} hasn't been called, or if we're already stopped.
*/
void stopListening() {
if (methodChannel == null) {
Log.d(TAG, "Tried to stop listening when no methodChannel had been initialized.");
return;
}

methodChannel.setMethodCallHandler(null);
methodChannel = null;
}

/**
* Parses the incoming call and forwards it to the cached {@link IntentSender}.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.robolectric.Shadows.shadowOf;
Expand All @@ -14,6 +17,8 @@
import android.content.Intent;
import android.net.Uri;
import androidx.test.core.app.ApplicationProvider;
import io.flutter.plugin.common.BinaryMessenger;
import io.flutter.plugin.common.BinaryMessenger.BinaryMessageHandler;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel.Result;
import java.util.HashMap;
Expand All @@ -25,6 +30,7 @@

@RunWith(RobolectricTestRunner.class)
public class MethodCallHandlerImplTest {
private static final String CHANNEL_NAME = "plugins.flutter.io/android_intent";
private Context context;
private IntentSender sender;
private MethodCallHandlerImpl methodCallHandler;
Expand All @@ -36,6 +42,49 @@ public void setUp() {
methodCallHandler = new MethodCallHandlerImpl(sender);
}

@Test
public void startListening_registersChannel() {
BinaryMessenger messenger = mock(BinaryMessenger.class);

methodCallHandler.startListening(messenger);

verify(messenger, times(1))
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
}

@Test
public void startListening_unregistersExistingChannel() {
BinaryMessenger firstMessenger = mock(BinaryMessenger.class);
BinaryMessenger secondMessenger = mock(BinaryMessenger.class);
methodCallHandler.startListening(firstMessenger);

methodCallHandler.startListening(secondMessenger);

// Unregisters the first and then registers the second.
verify(firstMessenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
verify(secondMessenger, times(1))
.setMessageHandler(eq(CHANNEL_NAME), any(BinaryMessageHandler.class));
}

@Test
public void stopListening_unregistersExistingChannel() {
BinaryMessenger messenger = mock(BinaryMessenger.class);
methodCallHandler.startListening(messenger);

methodCallHandler.stopListening();

verify(messenger, times(1)).setMessageHandler(CHANNEL_NAME, null);
}

@Test
public void stopListening_doesNothingWhenUnset() {
BinaryMessenger messenger = mock(BinaryMessenger.class);

methodCallHandler.stopListening();

verify(messenger, never()).setMessageHandler(CHANNEL_NAME, null);
}

@Test
public void onMethodCall_doesNothingWhenContextIsNull() {
Result result = mock(Result.class);
Expand Down
4 changes: 2 additions & 2 deletions packages/android_intent/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ version: 0.3.3+3

flutter:
plugin:
androidPackage: io.flutter.plugins.androidintent
androidPackage: dev.flutter.plugins.androidintent
iosPrefix: FLT
pluginClass: AndroidIntentPlugin
pluginClass: AndroidIntentPluginRegistrar

dependencies:
flutter:
Expand Down