Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,13 @@
import androidx.annotation.NonNull;

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.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry;

public class UniLinksPlugin
implements FlutterPlugin,
MethodChannel.MethodCallHandler,
EventChannel.StreamHandler,
ActivityAware,
PluginRegistry.NewIntentListener {

public class UniLinksPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler, EventChannel.StreamHandler, PluginRegistry.NewIntentListener {
private static final String MESSAGES_CHANNEL = "uni_links/messages";
private static final String EVENTS_CHANNEL = "uni_links/events";

Expand All @@ -30,16 +22,14 @@ public class UniLinksPlugin
private String initialLink;
private String latestLink;
private Context context;
private boolean initialIntent = true;

private void handleIntent(Context context, Intent intent) {
String action = intent.getAction();
String dataString = intent.getDataString();

if (Intent.ACTION_VIEW.equals(action)) {
if (initialIntent) {
if (initialLink == null) {
initialLink = dataString;
initialIntent = false;
}
latestLink = dataString;
if (changeReceiver != null) changeReceiver.onReceive(context, intent);
Expand Down Expand Up @@ -67,44 +57,43 @@ public void onReceive(Context context, Intent intent) {
}

@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
public void onAttachedToEngine(@NonNull FlutterPlugin.FlutterPluginBinding flutterPluginBinding) {
this.context = flutterPluginBinding.getApplicationContext();
register(flutterPluginBinding.getBinaryMessenger(), this);
register(flutterPluginBinding.getBinaryMessenger());
}

private static void register(BinaryMessenger messenger, UniLinksPlugin plugin) {
private void register(BinaryMessenger messenger) {
final MethodChannel methodChannel = new MethodChannel(messenger, MESSAGES_CHANNEL);
methodChannel.setMethodCallHandler(plugin);
methodChannel.setMethodCallHandler(this);

final EventChannel eventChannel = new EventChannel(messenger, EVENTS_CHANNEL);
eventChannel.setStreamHandler(plugin);
eventChannel.setStreamHandler(this);
}

/** Plugin registration. */
public static void registerWith(@NonNull PluginRegistry.Registrar registrar) {
// Detect if we've been launched in background
public static void registerWith(PluginRegistry.Registrar registrar) {
// Detect if we've been launched in the background
if (registrar.activity() == null) {
return;
}

final UniLinksPlugin instance = new UniLinksPlugin();
instance.context = registrar.context();
register(registrar.messenger(), instance);
instance.register(registrar.messenger());

instance.handleIntent(registrar.context(), registrar.activity().getIntent());
registrar.addNewIntentListener(instance);
}

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {}
public void onDetachedFromEngine(@NonNull FlutterPlugin.FlutterPluginBinding binding) {}

@Override
public void onListen(Object o, EventChannel.EventSink eventSink) {
changeReceiver = createChangeReceiver(eventSink);
public void onListen(Object arguments, EventChannel.EventSink events) {
changeReceiver = createChangeReceiver(events);
}

@Override
public void onCancel(Object o) {
public void onCancel(Object arguments) {
changeReceiver = null;
}

Expand All @@ -121,26 +110,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result

@Override
public boolean onNewIntent(Intent intent) {
this.handleIntent(context, intent);
handleIntent(context, intent);
return false;
}

@Override
public void onAttachedToActivity(@NonNull ActivityPluginBinding activityPluginBinding) {
activityPluginBinding.addOnNewIntentListener(this);
this.handleIntent(this.context, activityPluginBinding.getActivity().getIntent());
}

@Override
public void onDetachedFromActivityForConfigChanges() {}

@Override
public void onReattachedToActivityForConfigChanges(
@NonNull ActivityPluginBinding activityPluginBinding) {
activityPluginBinding.addOnNewIntentListener(this);
this.handleIntent(this.context, activityPluginBinding.getActivity().getIntent());
}

@Override
public void onDetachedFromActivity() {}
}
4 changes: 2 additions & 2 deletions uni_links/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
'(tap on any of the above commands to print it to'
' the console/logger and copy to the device clipboard.)',
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.caption,
style: Theme.of(context).textTheme.bodySmall,
),
]
].expand((el) => el).toList(),
Expand All @@ -222,7 +222,7 @@ class _MyAppState extends State<MyApp> with SingleTickerProviderStateMixin {
}

void _showSnackBar(String msg) {
WidgetsBinding.instance?.addPostFrameCallback((_) {
WidgetsBinding.instance.addPostFrameCallback((_) {
final context = _scaffoldKey.currentContext;
if (context != null) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
Expand Down