From 921f0b9f2124b1cda96f85b7b699a4ca02b37fd8 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 12 Oct 2016 13:27:12 -0700 Subject: [PATCH] Add PlatformPlugin.java (flutter/engine#3121) The PlatformPlugin is the first of the built-in plugins that will replace the mojom platform services. --- engine/src/flutter/lib/ui/window.dart | 14 +++--- .../flutter/shell/platform/android/BUILD.gn | 2 + .../plugin/common/JSONMessageListener.java | 31 +++++++++++++ .../plugin/platform/PlatformPlugin.java | 45 +++++++++++++++++++ .../android/io/flutter/view/FlutterView.java | 10 +++-- .../platform/android/platform_view_android.cc | 5 ++- 6 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 engine/src/flutter/shell/platform/android/io/flutter/plugin/common/JSONMessageListener.java create mode 100644 engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java diff --git a/engine/src/flutter/lib/ui/window.dart b/engine/src/flutter/lib/ui/window.dart index 201f4fded2..c2b364465e 100644 --- a/engine/src/flutter/lib/ui/window.dart +++ b/engine/src/flutter/lib/ui/window.dart @@ -215,14 +215,14 @@ class Window { /// semantics update cannot be used further. void updateSemantics(SemanticsUpdate update) native "Window_updateSemantics"; - void sendPlatformMesssage(String name, - ByteData data, - PlatformMessageResponseCallback callback) { - _sendPlatformMesssage(name, callback, data); + void sendPlatformMessage(String name, + ByteData data, + PlatformMessageResponseCallback callback) { + _sendPlatformMessage(name, callback, data); } - void _sendPlatformMesssage(String name, - PlatformMessageResponseCallback callback, - ByteData data) native "Window_sendPlatformMesssage"; + void _sendPlatformMessage(String name, + PlatformMessageResponseCallback callback, + ByteData data) native "Window_sendPlatformMessage"; } diff --git a/engine/src/flutter/shell/platform/android/BUILD.gn b/engine/src/flutter/shell/platform/android/BUILD.gn index df4e004f52..ebf4ad59fa 100644 --- a/engine/src/flutter/shell/platform/android/BUILD.gn +++ b/engine/src/flutter/shell/platform/android/BUILD.gn @@ -64,6 +64,8 @@ android_library("java") { visibility = [ ":*" ] java_files = [ + "io/flutter/plugin/common/JSONMessageListener.java", + "io/flutter/plugin/platform/PlatformPlugin.java", "io/flutter/view/AccessibilityBridge.java", "io/flutter/view/FlutterMain.java", "io/flutter/view/FlutterView.java", diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/JSONMessageListener.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/JSONMessageListener.java new file mode 100644 index 0000000000..09fc1cca52 --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/JSONMessageListener.java @@ -0,0 +1,31 @@ +// Copyright 2016 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.plugin.common; + +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +import io.flutter.view.FlutterView; + +public abstract class JSONMessageListener implements FlutterView.OnMessageListener { + static final String TAG = "FlutterView"; + + @Override + public String onMessage(FlutterView view, String message) { + try { + JSONObject response = onJSONMessage(view, new JSONObject(message)); + if (response == null) + return null; + return response.toString(); + } catch (JSONException e) { + Log.e(TAG, "JSON exception", e); + return null; + } + } + + public abstract JSONObject onJSONMessage(FlutterView view, JSONObject message) throws JSONException; +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java new file mode 100644 index 0000000000..f487d7ccae --- /dev/null +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -0,0 +1,45 @@ +// Copyright 2016 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.plugin.platform; + +import android.app.Activity; +import android.view.SoundEffectConstants; +import android.view.View; +import android.util.Log; + +import org.json.JSONException; +import org.json.JSONObject; + +import io.flutter.plugin.common.JSONMessageListener; +import io.flutter.view.FlutterView; + +/** + * Android implementation of the platform plugin. + */ +public class PlatformPlugin extends JSONMessageListener { + private final Activity mActivity; + + public PlatformPlugin(Activity activity) { + mActivity = activity; + } + + @Override + public JSONObject onJSONMessage(FlutterView view, JSONObject message) throws JSONException { + if (message.getString("method").equals("SystemSound.play")) { + playSystemSound(message.getJSONArray("args").getString(0)); + return null; + } + // TODO(abarth): We should throw an exception here that gets + // transmitted back to Dart. + return null; + } + + void playSystemSound(String soundType) { + if (soundType.equals("SystemSoundType.click")) { + View view = mActivity.getWindow().getDecorView(); + view.playSoundEffect(SoundEffectConstants.CLICK); + } + } +} diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index 070d30c186..9b6e7fa3b8 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -60,6 +60,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import io.flutter.plugin.platform.PlatformPlugin; + import org.domokit.common.ActivityLifecycleListener; import org.domokit.activity.ActivityImpl; import org.domokit.editing.KeyboardImpl; @@ -163,6 +165,8 @@ public class FlutterView extends SurfaceView setLocale(getResources().getConfiguration().locale); + mOnMessageListeners.put("flutter/platform", new PlatformPlugin((Activity)getContext())); + if ((context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { discoveryReceiver = new DiscoveryReceiver(); context.registerReceiver(discoveryReceiver, new IntentFilter(ACTION_DISCOVER)); @@ -585,14 +589,14 @@ public class FlutterView extends SurfaceView private static native void nativeInvokePlatformMessageResponseCallback(long nativePlatformViewAndroid, int responseId, String buffer); @CalledByNative - private void handlePlatformMessage(String messageName, String message, final int responseId) { - OnMessageListener listener = mOnMessageListeners.get(messageName); + private void handlePlatformMessage(String name, String message, final int responseId) { + OnMessageListener listener = mOnMessageListeners.get(name); if (listener != null) { nativeInvokePlatformMessageResponseCallback(mNativePlatformView, responseId, listener.onMessage(this, message)); return; } - OnMessageListenerAsync asyncListener = mAsyncOnMessageListeners.get(messageName); + OnMessageListenerAsync asyncListener = mAsyncOnMessageListeners.get(name); if (asyncListener != null) { asyncListener.onMessage(this, message, new MessageResponse() { @Override diff --git a/engine/src/flutter/shell/platform/android/platform_view_android.cc b/engine/src/flutter/shell/platform/android/platform_view_android.cc index 095eb45aec..69d888f7fc 100644 --- a/engine/src/flutter/shell/platform/android/platform_view_android.cc +++ b/engine/src/flutter/shell/platform/android/platform_view_android.cc @@ -130,8 +130,9 @@ void PlatformViewAndroid::InvokePlatformMessageResponseCallback( auto it = pending_messages_.find(response_id); if (it == pending_messages_.end()) return; - std::string response = - base::android::ConvertJavaStringToUTF8(env, java_response); + std::string response; + if (java_response) + response = base::android::ConvertJavaStringToUTF8(env, java_response); // TODO(abarth): There's an extra copy here. it->second->InvokeCallback( std::vector(response.data(), response.data() + response.size()));