|
| 1 | +package org.leanflutter.plugins.protocol_handler; |
| 2 | + |
| 3 | +import android.content.Intent; |
| 4 | + |
| 5 | +import androidx.annotation.NonNull; |
| 6 | + |
| 7 | +import java.util.HashMap; |
| 8 | +import java.util.Map; |
| 9 | + |
| 10 | +import io.flutter.embedding.engine.plugins.FlutterPlugin; |
| 11 | +import io.flutter.embedding.engine.plugins.activity.ActivityAware; |
| 12 | +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; |
| 13 | +import io.flutter.plugin.common.MethodCall; |
| 14 | +import io.flutter.plugin.common.MethodChannel; |
| 15 | +import io.flutter.plugin.common.MethodChannel.MethodCallHandler; |
| 16 | +import io.flutter.plugin.common.MethodChannel.Result; |
| 17 | +import io.flutter.plugin.common.PluginRegistry; |
| 18 | + |
| 19 | +/** |
| 20 | + * ProtocolHandlerPlugin |
| 21 | + */ |
| 22 | +public class ProtocolHandlerPlugin implements FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.NewIntentListener { |
| 23 | + /// The MethodChannel that will the communication between Flutter and native Android |
| 24 | + /// |
| 25 | + /// This local reference serves to register the plugin with the Flutter Engine and unregister it |
| 26 | + /// when the Flutter Engine is detached from the Activity |
| 27 | + private MethodChannel channel; |
| 28 | + |
| 29 | + private String initialUrl = ""; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) { |
| 33 | + channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "protocol_handler"); |
| 34 | + channel.setMethodCallHandler(this); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) { |
| 39 | + if (call.method.equals("getInitialUrl")) { |
| 40 | + result.success(initialUrl); |
| 41 | + } else { |
| 42 | + result.notImplemented(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { |
| 48 | + channel.setMethodCallHandler(null); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean onNewIntent(Intent intent) { |
| 53 | + this.handleIntent(intent, true); |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { |
| 59 | + binding.addOnNewIntentListener(this); |
| 60 | + this.handleIntent(binding.getActivity().getIntent(), true); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public void onDetachedFromActivityForConfigChanges() { } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { |
| 68 | + this.handleIntent(binding.getActivity().getIntent(), false); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onDetachedFromActivity() { } |
| 73 | + |
| 74 | + private void handleIntent(Intent intent, boolean isFromOnNewIntent) { |
| 75 | + String action = intent.getAction(); |
| 76 | + String dataString = intent.getDataString(); |
| 77 | + |
| 78 | + if (Intent.ACTION_VIEW.equals(action)) { |
| 79 | + if (isFromOnNewIntent) { |
| 80 | + this.initialUrl = dataString; |
| 81 | + } |
| 82 | + notifyUrlReceived(dataString); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void notifyUrlReceived(String url) { |
| 87 | + Map<String, String> args = new HashMap<>(); |
| 88 | + args.put("url", url); |
| 89 | + channel.invokeMethod("onProtocolUrlReceived", args); |
| 90 | + } |
| 91 | +} |
0 commit comments