提交 e93d2c27 编写于 作者: A Adam Barth 提交者: GitHub

Add PlatformPlugin.java (#3121)

The PlatformPlugin is the first of the built-in plugins that will replace the
mojom platform services.
上级 119b1a99
......@@ -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";
}
......
......@@ -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",
......
// 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;
}
// 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);
}
}
}
......@@ -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
......
......@@ -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<char>(response.data(), response.data() + response.size()));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册