Skip to content

Commit

Permalink
[Android] Extract the PluginRegistry from the FlutterActivity (flutte…
Browse files Browse the repository at this point in the history
  • Loading branch information
zanderso authored Nov 30, 2017
1 parent 77d8acb commit 619f452
Show file tree
Hide file tree
Showing 7 changed files with 256 additions and 136 deletions.
1 change: 1 addition & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ java_library("flutter_shell_java") {
"io/flutter/app/FlutterActivityEvents.java",
"io/flutter/app/FlutterApplication.java",
"io/flutter/app/FlutterFragmentActivity.java",
"io/flutter/app/FlutterPluginRegistry.java",
"io/flutter/plugin/common/ActivityLifecycleListener.java",
"io/flutter/plugin/common/BasicMessageChannel.java",
"io/flutter/plugin/common/BinaryCodec.java",
Expand Down
131 changes: 13 additions & 118 deletions shell/platform/android/io/flutter/app/FlutterActivityDelegate.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@ public interface ViewFactory {

private final Activity activity;
private final ViewFactory viewFactory;
private final Map<String, Object> pluginMap = new LinkedHashMap<>(0);
private final List<RequestPermissionResultListener> requestPermissionResultListeners = new ArrayList<>(0);
private final List<ActivityResultListener> activityResultListeners = new ArrayList<>(0);
private final List<NewIntentListener> newIntentListeners = new ArrayList<>(0);
private final List<UserLeaveHintListener> userLeaveHintListeners = new ArrayList<>(0);
private final List<ViewDestroyListener> viewDestroyListeners = new ArrayList<>(0);

private FlutterView flutterView;
private View launchView;

Expand All @@ -111,45 +104,32 @@ public FlutterView getFlutterView() {
return flutterView;
}

// The implementation of PluginRegistry forwards to flutterView.
@Override
public boolean hasPlugin(String key) {
return pluginMap.containsKey(key);
return flutterView.getPluginRegistry().hasPlugin(key);
}

@Override
@SuppressWarnings("unchecked")
public <T> T valuePublishedByPlugin(String pluginKey) {
return (T) pluginMap.get(pluginKey);
return (T) flutterView.getPluginRegistry().valuePublishedByPlugin(pluginKey);
}

@Override
public Registrar registrarFor(String pluginKey) {
if (pluginMap.containsKey(pluginKey)) {
throw new IllegalStateException("Plugin key " + pluginKey + " is already in use");
}
pluginMap.put(pluginKey, null);
return new FlutterRegistrar(pluginKey);
return flutterView.getPluginRegistry().registrarFor(pluginKey);
}

@Override
public boolean onRequestPermissionResult(
int requestCode, String[] permissions, int[] grantResults) {
for (RequestPermissionResultListener listener : requestPermissionResultListeners) {
if (listener.onRequestPermissionResult(requestCode, permissions, grantResults)) {
return true;
}
}
return false;
return flutterView.getPluginRegistry().onRequestPermissionResult(requestCode, permissions, grantResults);
}

@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
for (ActivityResultListener listener : activityResultListeners) {
if (listener.onActivityResult(requestCode, resultCode, data)) {
return true;
}
}
return false;
return flutterView.getPluginRegistry().onActivityResult(requestCode, resultCode, data);
}

@Override
Expand Down Expand Up @@ -196,11 +176,7 @@ public void onNewIntent(Intent intent) {
// Only attempt to reload the Flutter Dart code during development. Use
// the debuggable flag as an indicator that we are in development mode.
if (!isDebuggable() || !loadIntent(intent)) {
for (NewIntentListener listener : newIntentListeners) {
if (listener.onNewIntent(intent)) {
return;
}
}
flutterView.getPluginRegistry().onNewIntent(intent);
}
}

Expand Down Expand Up @@ -253,18 +229,14 @@ public void onDestroy() {
}
}
if (flutterView != null) {
boolean destroy = true;
for (ViewDestroyListener listener : viewDestroyListeners) {
if (listener.onViewDestroy(flutterView.getFlutterNativeView())) {
destroy = false;
}
}
if (destroy) {
flutterView.destroy();
} else {
final boolean detach =
flutterView.getPluginRegistry().onViewDestroy(flutterView.getFlutterNativeView());
if (detach) {
// Detach, but do not destroy the FlutterView if a plugin
// expressed interest in its FlutterNativeView.
flutterView.detach();
} else {
flutterView.destroy();
}
}
}
Expand All @@ -280,6 +252,7 @@ public boolean onBackPressed() {

@Override
public void onUserLeaveHint() {
flutterView.getPluginRegistry().onUserLeaveHint();
}

@Override
Expand Down Expand Up @@ -463,82 +436,4 @@ public void onAnimationEnd(Animator animation) {
// We can make this configurable if users want it.
activity.setTheme(android.R.style.Theme_Black_NoTitleBar);
}

private class FlutterRegistrar implements Registrar {
private final String pluginKey;

FlutterRegistrar(String pluginKey) {
this.pluginKey = pluginKey;
}

@Override
public Activity activity() {
return activity;
}

@Override
public BinaryMessenger messenger() {
return flutterView;
}

@Override
public TextureRegistry textures() {
return flutterView;
}

@Override
public FlutterView view() {
return flutterView;
}

/**
* Publishes a value associated with the plugin being registered.
*
* <p>The published value is available to interested clients via
* {@link PluginRegistry#valuePublishedByPlugin(String)}.</p>
*
* <p>Publication should be done only when there is an interesting value
* to be shared with other code. This would typically be an instance of
* the plugin's main class itself that must be wired up to receive
* notifications or events from an Android API.
*
* <p>Overwrites any previously published value.</p>
*/
@Override
public Registrar publish(Object value) {
pluginMap.put(pluginKey, value);
return this;
}

@Override
public Registrar addRequestPermissionResultListener(
RequestPermissionResultListener listener) {
requestPermissionResultListeners.add(listener);
return this;
}

@Override
public Registrar addActivityResultListener(ActivityResultListener listener) {
activityResultListeners.add(listener);
return this;
}

@Override
public Registrar addNewIntentListener(NewIntentListener listener) {
newIntentListeners.add(listener);
return this;
}

@Override
public Registrar addUserLeaveHintListener(UserLeaveHintListener listener) {
userLeaveHintListeners.add(listener);
return this;
}

@Override
public Registrar addViewDestroyListener(ViewDestroyListener listener) {
viewDestroyListeners.add(listener);
return this;
}
}
}
194 changes: 194 additions & 0 deletions shell/platform/android/io/flutter/app/FlutterPluginRegistry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// 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.app;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import io.flutter.plugin.common.*;
import io.flutter.view.FlutterNativeView;
import io.flutter.view.FlutterView;
import io.flutter.view.TextureRegistry;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class FlutterPluginRegistry
implements PluginRegistry,
PluginRegistry.RequestPermissionResultListener,
PluginRegistry.ActivityResultListener,
PluginRegistry.NewIntentListener,
PluginRegistry.UserLeaveHintListener,
PluginRegistry.ViewDestroyListener {
private static final String TAG = "FlutterPluginRegistry";

private Activity mActivity;
private Context mAppContext;
private FlutterNativeView mNativeView;
private FlutterView mFlutterView;

private final Map<String, Object> mPluginMap = new LinkedHashMap<>(0);
private final List<RequestPermissionResultListener> mRequestPermissionResultListeners = new ArrayList<>(0);
private final List<ActivityResultListener> mActivityResultListeners = new ArrayList<>(0);
private final List<NewIntentListener> mNewIntentListeners = new ArrayList<>(0);
private final List<UserLeaveHintListener> mUserLeaveHintListeners = new ArrayList<>(0);
private final List<ViewDestroyListener> mViewDestroyListeners = new ArrayList<>(0);

public FlutterPluginRegistry(FlutterNativeView nativeView, Context context) {
mNativeView = nativeView;
mAppContext = context;
}

@Override
public boolean hasPlugin(String key) {
return mPluginMap.containsKey(key);
}

@Override
@SuppressWarnings("unchecked")
public <T> T valuePublishedByPlugin(String pluginKey) {
return (T) mPluginMap.get(pluginKey);
}

@Override
public Registrar registrarFor(String pluginKey) {
if (mPluginMap.containsKey(pluginKey)) {
throw new IllegalStateException("Plugin key " + pluginKey + " is already in use");
}
mPluginMap.put(pluginKey, null);
return new FlutterRegistrar(pluginKey);
}

public void attach(FlutterView flutterView, Activity activity) {
mFlutterView = flutterView;
mActivity = activity;
}

public void detach() {
mFlutterView = null;
mActivity = null;
}

private class FlutterRegistrar implements Registrar {
private final String pluginKey;

FlutterRegistrar(String pluginKey) {
this.pluginKey = pluginKey;
}

@Override
public Activity activity() {
return mActivity;
}

@Override
public Context context() {
return mAppContext;
}

@Override
public BinaryMessenger messenger() {
return mNativeView;
}

@Override
public TextureRegistry textures() {
return mFlutterView;
}

@Override
public FlutterView view() {
return mFlutterView;
}

@Override
public Registrar publish(Object value) {
mPluginMap.put(pluginKey, value);
return this;
}

@Override
public Registrar addRequestPermissionResultListener(
RequestPermissionResultListener listener) {
mRequestPermissionResultListeners.add(listener);
return this;
}

@Override
public Registrar addActivityResultListener(ActivityResultListener listener) {
mActivityResultListeners.add(listener);
return this;
}

@Override
public Registrar addNewIntentListener(NewIntentListener listener) {
mNewIntentListeners.add(listener);
return this;
}

@Override
public Registrar addUserLeaveHintListener(UserLeaveHintListener listener) {
mUserLeaveHintListeners.add(listener);
return this;
}

@Override
public Registrar addViewDestroyListener(ViewDestroyListener listener) {
mViewDestroyListeners.add(listener);
return this;
}
}

@Override
public boolean onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
for (RequestPermissionResultListener listener : mRequestPermissionResultListeners) {
if (listener.onRequestPermissionResult(requestCode, permissions, grantResults)) {
return true;
}
}
return false;
}

@Override
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
for (ActivityResultListener listener : mActivityResultListeners) {
if (listener.onActivityResult(requestCode, resultCode, data)) {
return true;
}
}
return false;
}

@Override
public boolean onNewIntent(Intent intent) {
for (NewIntentListener listener : mNewIntentListeners) {
if (listener.onNewIntent(intent)) {
return true;
}
}
return false;
}

@Override
public void onUserLeaveHint() {
for (UserLeaveHintListener listener : mUserLeaveHintListeners) {
listener.onUserLeaveHint();
}
}

@Override
public boolean onViewDestroy(FlutterNativeView view) {
boolean handled = false;
for (ViewDestroyListener listener : mViewDestroyListeners) {
if (listener.onViewDestroy(view)) {
handled = true;
}
}
return handled;
}
}
Loading

0 comments on commit 619f452

Please sign in to comment.