Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
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
11 changes: 9 additions & 2 deletions packages/android_intent/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 0.3.4

* Migrate the plugin to use the V2 Android engine embedding. This shouldn't
affect existing functionality. Plugin authors who use the V2 embedding can now
instantiate the plugin and expect that it correctly responds to app lifecycle
changes.

## 0.3.3+3

* Define clang module for iOS.
Expand All @@ -8,11 +15,11 @@

## 0.3.3+1

* Added "action_application_details_settings" action to open application info settings .
* Added "action_application_details_settings" action to open application info settings .

## 0.3.3

* Added "flags" option to call intent.addFlags(int) in native.
* Added "flags" option to call intent.addFlags(int) in native.

## 0.3.2

Expand Down
36 changes: 36 additions & 0 deletions packages/android_intent/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,40 @@ android {
lintOptions {
disable 'InvalidPackage'
}
testOptions {
unitTests.includeAndroidResources = true
}
}

dependencies {
compileOnly 'androidx.annotation:annotation:1.0.0'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:1.10.19'
testImplementation 'androidx.test:core:1.0.0'
testImplementation 'org.robolectric:robolectric:4.3'
}

// TODO(mklim): Remove this hack once androidx.lifecycle is included on stable. https://github.com/flutter/flutter/issues/42348
afterEvaluate {
def containsEmbeddingDependencies = false
for (def configuration : configurations.all) {
for (def dependency : configuration.dependencies) {
if (dependency.group == 'io.flutter' &&
dependency.name.startsWith('flutter_embedding') &&
dependency.isTransitive())
{
containsEmbeddingDependencies = true
break
}
}
}
if (!containsEmbeddingDependencies) {
android {
dependencies {
def lifecycle_version = "2.1.0"
api "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
api "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
}
}
}
}
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);
Copy link
Contributor

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 ActivityAware methods? The execution of this method doesn't really imply anything about the Activity...

Copy link
Contributor Author

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.

Copy link
Contributor

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

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);
}
}
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;
}
}
Loading