This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[android_intent] Migrate to the new embedding #2143
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5a82ef0
Refactor the existing logic
c26003e
Migrate the plugin to the new embedding
7cca6f9
Add a unit test.
dd6079c
Review feedback.
a5caa9f
Changes from chat discussion
0a0e3e3
Naming changes based on offline discussion.
5434ffb
Last migration fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 52 additions & 139 deletions
191
...id_intent/android/src/main/java/io/flutter/plugins/androidintent/AndroidIntentPlugin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,161 +1,74 @@ | ||
| // Copyright 2017 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.plugins.androidintent; | ||
|
|
||
| import android.content.ComponentName; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.net.Uri; | ||
| import android.os.Bundle; | ||
| import android.provider.Settings; | ||
| import android.util.Log; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import io.flutter.plugin.common.MethodChannel.MethodCallHandler; | ||
| import io.flutter.plugin.common.MethodChannel.Result; | ||
| 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.PluginRegistry.Registrar; | ||
| import java.util.ArrayList; | ||
| import java.util.Map; | ||
|
|
||
| /** AndroidIntentPlugin */ | ||
| @SuppressWarnings("unchecked") | ||
| public class AndroidIntentPlugin implements MethodCallHandler { | ||
| private static final String TAG = AndroidIntentPlugin.class.getCanonicalName(); | ||
| private final Registrar mRegistrar; | ||
| /** | ||
| * Plugin implementation that uses the new {@code io.flutter.embedding} package. | ||
| * | ||
| * <p>Instantiate this in an add to app scenario to gracefully handle activity and context changes. | ||
| */ | ||
| public final class AndroidIntentPlugin implements FlutterPlugin, ActivityAware { | ||
| private final IntentSender sender; | ||
| private final MethodCallHandlerImpl impl; | ||
|
|
||
| /** Plugin registration. */ | ||
| public static void registerWith(Registrar registrar) { | ||
| final MethodChannel channel = | ||
| new MethodChannel(registrar.messenger(), "plugins.flutter.io/android_intent"); | ||
| channel.setMethodCallHandler(new AndroidIntentPlugin(registrar)); | ||
| /** | ||
| * Initialize this within the {@code #configureFlutterEngine} of a Flutter activity or fragment. | ||
| * | ||
| * <p>See {@code io.flutter.plugins.androidintentexample.MainActivity} for an example. | ||
| */ | ||
| public AndroidIntentPlugin() { | ||
| sender = new IntentSender(/*activity=*/ null, /*context=*/ null); | ||
| impl = new MethodCallHandlerImpl(sender); | ||
| } | ||
|
|
||
| private AndroidIntentPlugin(Registrar registrar) { | ||
| this.mRegistrar = registrar; | ||
| /** | ||
| * Registers a plugin implementation that uses the stable {@code io.flutter.plugin.common} | ||
| * package. | ||
| * | ||
| * <p>Calling this automatically initializes the plugin. However plugins initialized this way | ||
| * won't react to changes in activity or context, unlike {@link AndroidIntentPlugin}. | ||
| */ | ||
| public static void registerWith(Registrar registrar) { | ||
| IntentSender sender = new IntentSender(registrar.activity(), registrar.context()); | ||
| MethodCallHandlerImpl impl = new MethodCallHandlerImpl(sender); | ||
| impl.startListening(registrar.messenger()); | ||
| } | ||
|
|
||
| private String convertAction(String action) { | ||
| switch (action) { | ||
| case "action_view": | ||
| return Intent.ACTION_VIEW; | ||
| case "action_voice": | ||
| return Intent.ACTION_VOICE_COMMAND; | ||
| case "settings": | ||
| return Settings.ACTION_SETTINGS; | ||
| case "action_location_source_settings": | ||
| return Settings.ACTION_LOCATION_SOURCE_SETTINGS; | ||
| case "action_application_details_settings": | ||
| return Settings.ACTION_APPLICATION_DETAILS_SETTINGS; | ||
| default: | ||
| return action; | ||
| } | ||
| @Override | ||
| public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) { | ||
| sender.setApplicationContext(binding.getApplicationContext()); | ||
| sender.setActivity(null); | ||
| impl.startListening(binding.getFlutterEngine().getDartExecutor()); | ||
| } | ||
|
|
||
| private Bundle convertArguments(Map<String, ?> arguments) { | ||
| Bundle bundle = new Bundle(); | ||
| for (String key : arguments.keySet()) { | ||
| Object value = arguments.get(key); | ||
| if (value instanceof Integer) { | ||
| bundle.putInt(key, (Integer) value); | ||
| } else if (value instanceof String) { | ||
| bundle.putString(key, (String) value); | ||
| } else if (value instanceof Boolean) { | ||
| bundle.putBoolean(key, (Boolean) value); | ||
| } else if (value instanceof Double) { | ||
| bundle.putDouble(key, (Double) value); | ||
| } else if (value instanceof Long) { | ||
| bundle.putLong(key, (Long) value); | ||
| } else if (value instanceof byte[]) { | ||
| bundle.putByteArray(key, (byte[]) value); | ||
| } else if (value instanceof int[]) { | ||
| bundle.putIntArray(key, (int[]) value); | ||
| } else if (value instanceof long[]) { | ||
| bundle.putLongArray(key, (long[]) value); | ||
| } else if (value instanceof double[]) { | ||
| bundle.putDoubleArray(key, (double[]) value); | ||
| } else if (isTypedArrayList(value, Integer.class)) { | ||
| bundle.putIntegerArrayList(key, (ArrayList<Integer>) value); | ||
| } else if (isTypedArrayList(value, String.class)) { | ||
| bundle.putStringArrayList(key, (ArrayList<String>) value); | ||
| } else if (isStringKeyedMap(value)) { | ||
| bundle.putBundle(key, convertArguments((Map<String, ?>) value)); | ||
| } else { | ||
| throw new UnsupportedOperationException("Unsupported type " + value); | ||
| } | ||
| } | ||
| return bundle; | ||
| @Override | ||
| public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) { | ||
| sender.setApplicationContext(null); | ||
| sender.setActivity(null); | ||
| impl.stopListening(); | ||
| } | ||
|
|
||
| private boolean isTypedArrayList(Object value, Class<?> type) { | ||
| if (!(value instanceof ArrayList)) { | ||
| return false; | ||
| } | ||
| ArrayList list = (ArrayList) value; | ||
| for (Object o : list) { | ||
| if (!(o == null || type.isInstance(o))) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| @Override | ||
| public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { | ||
| sender.setActivity(binding.getActivity()); | ||
| } | ||
|
|
||
| private boolean isStringKeyedMap(Object value) { | ||
| if (!(value instanceof Map)) { | ||
| return false; | ||
| } | ||
| Map map = (Map) value; | ||
| for (Object key : map.keySet()) { | ||
| if (!(key == null || key instanceof String)) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| @Override | ||
| public void onDetachedFromActivity() { | ||
| sender.setActivity(null); | ||
| } | ||
|
|
||
| private Context getActiveContext() { | ||
| return (mRegistrar.activity() != null) ? mRegistrar.activity() : mRegistrar.context(); | ||
| @Override | ||
| public void onDetachedFromActivityForConfigChanges() { | ||
| onDetachedFromActivity(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onMethodCall(MethodCall call, Result result) { | ||
| Context context = getActiveContext(); | ||
| String action = convertAction((String) call.argument("action")); | ||
|
|
||
| // Build intent | ||
| Intent intent = new Intent(action); | ||
| if (mRegistrar.activity() == null) { | ||
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
| } | ||
| if (call.argument("flag") != null) { | ||
| intent.addFlags((Integer) call.argument("flags")); | ||
| } | ||
| if (call.argument("category") != null) { | ||
| intent.addCategory((String) call.argument("category")); | ||
| } | ||
| if (call.argument("data") != null) { | ||
| intent.setData(Uri.parse((String) call.argument("data"))); | ||
| } | ||
| if (call.argument("arguments") != null) { | ||
| intent.putExtras(convertArguments((Map) call.argument("arguments"))); | ||
| } | ||
| if (call.argument("package") != null) { | ||
| String packageName = (String) call.argument("package"); | ||
| intent.setPackage(packageName); | ||
| if (call.argument("componentName") != null) { | ||
| intent.setComponent( | ||
| new ComponentName(packageName, (String) call.argument("componentName"))); | ||
| } | ||
| if (intent.resolveActivity(context.getPackageManager()) == null) { | ||
| Log.i(TAG, "Cannot resolve explicit intent - ignoring package"); | ||
| intent.setPackage(null); | ||
| } | ||
| } | ||
|
|
||
| Log.i(TAG, "Sending intent " + intent); | ||
| context.startActivity(intent); | ||
|
|
||
| result.success(null); | ||
| public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { | ||
| onAttachedToActivity(binding); | ||
| } | ||
| } | ||
110 changes: 110 additions & 0 deletions
110
...s/android_intent/android/src/main/java/io/flutter/plugins/androidintent/IntentSender.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| package io.flutter.plugins.androidintent; | ||
|
|
||
| import android.app.Activity; | ||
| import android.content.ComponentName; | ||
| import android.content.Context; | ||
| import android.content.Intent; | ||
| import android.net.Uri; | ||
| import android.os.Bundle; | ||
| import android.text.TextUtils; | ||
| import android.util.Log; | ||
| import androidx.annotation.Nullable; | ||
|
|
||
| /** Forms and launches intents. */ | ||
| public final class IntentSender { | ||
| private static final String TAG = "IntentSender"; | ||
|
|
||
| private @Nullable Activity activity; | ||
| private @Nullable Context applicationContext; | ||
|
|
||
| /** | ||
| * Caches the given {@code activity} and {@code applicationContext} to use for sending intents | ||
| * later. | ||
| * | ||
| * <p>Either may be null initially, but at least {@code applicationContext} should be set before | ||
| * calling {@link #send}. | ||
| * | ||
| * <p>See also {@link #setActivity}, {@link #setApplicationContext}, and {@link #send}. | ||
| */ | ||
| public IntentSender(@Nullable Activity activity, @Nullable Context applicationContext) { | ||
| this.activity = activity; | ||
| this.applicationContext = applicationContext; | ||
| } | ||
|
|
||
| /** | ||
| * Creates and launches an intent with the given params using the cached {@link Activity} and | ||
| * {@link Context}. | ||
| * | ||
| * <p>This will fail to create and send the intent if {@code applicationContext} hasn't been set | ||
| * at the time of calling. | ||
| * | ||
| * <p>This uses {@code activity} to start the intent whenever it's not null. Otherwise it falls | ||
| * back to {@code applicationContext} and adds {@link Intent#FLAG_ACTIVITY_NEW_TASK} to the intent | ||
| * before launching it. | ||
| * | ||
| * @param action the Intent action, such as {@code ACTION_VIEW}. | ||
| * @param flags forwarded to {@link Intent#addFlags(int)} if non-null. | ||
| * @param category forwarded to {@link Intent#addCategory(String)} if non-null. | ||
| * @param data forwarded to {@link Intent#setData(Uri)} if non-null. | ||
| * @param arguments forwarded to {@link Intent#putExtras(Bundle)} if non-null. | ||
| * @param packageName forwarded to {@link Intent#setPackage(String)} if non-null. This is forced | ||
| * to null if it can't be resolved. | ||
| * @param componentName forwarded to {@link Intent#setComponent(ComponentName)} if non-null. | ||
| */ | ||
| void send( | ||
| String action, | ||
| @Nullable Integer flags, | ||
| @Nullable String category, | ||
| @Nullable Uri data, | ||
| @Nullable Bundle arguments, | ||
| @Nullable String packageName, | ||
| @Nullable ComponentName componentName) { | ||
| if (applicationContext == null) { | ||
| Log.wtf(TAG, "Trying to send an intent before the applicationContext was initialized."); | ||
| return; | ||
| } | ||
|
|
||
| Intent intent = new Intent(action); | ||
|
|
||
| if (flags != null) { | ||
| intent.addFlags(flags); | ||
| } | ||
| if (!TextUtils.isEmpty(category)) { | ||
| intent.addCategory(category); | ||
| } | ||
| if (data != null) { | ||
| intent.setData(data); | ||
| } | ||
| if (arguments != null) { | ||
| intent.putExtras(arguments); | ||
| } | ||
| if (!TextUtils.isEmpty(packageName)) { | ||
| intent.setPackage(packageName); | ||
| if (intent.resolveActivity(applicationContext.getPackageManager()) == null) { | ||
| Log.i(TAG, "Cannot resolve explicit intent - ignoring package"); | ||
| intent.setPackage(null); | ||
| } | ||
| } | ||
| if (componentName != null) { | ||
| intent.setComponent(componentName); | ||
| } | ||
|
|
||
| Log.v(TAG, "Sending intent " + intent); | ||
| if (activity != null) { | ||
| activity.startActivity(intent); | ||
| } else { | ||
| intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | ||
| applicationContext.startActivity(intent); | ||
| } | ||
| } | ||
|
|
||
| /** Caches the given {@code activity} to use for {@link #send}. */ | ||
| void setActivity(@Nullable Activity activity) { | ||
| this.activity = activity; | ||
| } | ||
|
|
||
| /** Caches the given {@code applicationContext} to use for {@link #send}. */ | ||
| void setApplicationContext(@Nullable Context applicationContext) { | ||
| this.applicationContext = applicationContext; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make more senes to set activity to null in the appropriate
ActivityAwaremethods? The execution of this method doesn't really imply anything about the Activity...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would a user potentially be changing the plugin's engine, but expecting it to hold on to an existing activity reference? I was assuming that if the engine changes, the activity itself is also invalid. I also figured that the Activity callbacks should be called anyway so I am updating the Activity there too, but I wanted to add an extra check here just to guarantee that they never got out of state based on some callback issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can add an extra call, but if that extra call ever has an effect, it indicates a bug in the embedding. The Activity calls are supposed to be strictly between engine calls:
attach engine
attach activity
detach activity
detach engine