Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compatibility with embedding v2 #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 56 additions & 8 deletions android/src/main/java/com/smaho/eng/esptouch/EsptouchPlugin.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
package com.smaho.eng.esptouch;

import android.app.Activity;
import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;

import com.espressif.iot.esptouch.task.EsptouchTaskParameter;

import java.util.Map;

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.BinaryMessenger;
import io.flutter.plugin.common.EventChannel;
import io.flutter.plugin.common.PluginRegistry.Registrar;


/** EsptouchPlugin */
public class EsptouchPlugin implements EventChannel.StreamHandler {
public class EsptouchPlugin implements EventChannel.StreamHandler, FlutterPlugin, ActivityAware {
private static final String TAG = "EsptouchPlugin";
private static final String CHANNEL_NAME= "eng.smaho.com/esptouch_plugin/results";

/** backward compatibility with embedding v1 **/
@SuppressWarnings("deprecation") // Registrar deprecated (v1 plugin embedding).
public static void registerWith(Registrar registrar) {
final EventChannel eventChannel = new EventChannel(registrar.messenger(), CHANNEL_NAME);
eventChannel.setStreamHandler(new EsptouchPlugin(registrar.context()));
EsptouchPlugin plugin = new EsptouchPlugin();
plugin.activity = registrar.activity();
plugin.context = registrar.context();
plugin.setupMethodChannel(registrar.messenger());
}

private final Context context;
private EventChannel channel;
private Activity activity;
private Context context;
private EspTouchTaskUtil taskUtil;

private EsptouchPlugin(Context context) {
this.context = context;
}

private EsptouchTaskParameter buildTaskParameter(Map<String, Integer> m) {
EsptouchTaskParameter.Builder b = new EsptouchTaskParameter.Builder();
b.setIntervalGuideCodeMillisecond(m.get("intervalGuideCodeMillisecond"));
Expand All @@ -47,6 +57,44 @@ private EsptouchTaskParameter buildTaskParameter(Map<String, Integer> m) {
return new EsptouchTaskParameter(b);
}


@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
context = binding.getApplicationContext();
setupMethodChannel(binding.getBinaryMessenger());
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel.setStreamHandler(null);
channel = null;
}

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
activity = binding.getActivity();
}

@Override
public void onDetachedFromActivityForConfigChanges() {
activity = null;
}

@Override
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
activity = binding.getActivity();
}

@Override
public void onDetachedFromActivity() {
activity = null;
}

private void setupMethodChannel(BinaryMessenger messenger) {
channel = new EventChannel(messenger, CHANNEL_NAME);
channel.setStreamHandler(this);
}

@Override
@SuppressWarnings("unchecked")
public void onListen(Object o, EventChannel.EventSink eventSink) {
Expand Down
6 changes: 5 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:name="${applicationName}"
android:label="esptouch_example"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -37,5 +37,9 @@
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<meta-data
android:name="flutterEmbedding"
android:value="2" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import io.flutter.app.FlutterActivity;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
private static final String CHANNEL = "eng.smaho.com/esptouch_plugin/example";
Expand All @@ -24,20 +24,19 @@ public class MainActivity extends FlutterActivity {
private MethodChannel.Result result;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GeneratedPluginRegistrant.registerWith(this);
new MethodChannel(getFlutterView(), CHANNEL).setMethodCallHandler(
(call, result) -> {
this.call = call;
this.result = result;
// https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-hardware-id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
handlePermissions();
} else {
handleMethodCall();
}
}
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL).setMethodCallHandler(
(call, result) -> {
this.call = call;
this.result = result;
// https://developer.android.com/about/versions/marshmallow/android-6.0-changes#behavior-hardware-id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
handlePermissions();
} else {
handleMethodCall();
}
}
);
}

Expand Down