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

feat(android_intent_plus): support for intent as URI #2970

Merged
merged 12 commits into from
May 31, 2024
4 changes: 4 additions & 0 deletions packages/android_intent_plus/CHANGELOG.md
jaumard marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 5.0.3

- **FIX**(android_intent_plus): Add support for parking intent URI ([#2164](https://github.com/fluttercommunity/plus_plugins/issues/2164)).

## 5.0.2

- **REFACTOR**(android_intent_plus): Migrate Android example to use the new plugins declaration ([#2773](https://github.com/fluttercommunity/plus_plugins/issues/2773)). ([7c2de04d](https://github.com/fluttercommunity/plus_plugins/commit/7c2de04deffa1dc788d93c12e5cee1fd98821514))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.text.TextUtils;
import android.util.Log;
import androidx.annotation.Nullable;
import java.net.URISyntaxException;

/** Forms and launches intents. */
public final class IntentSender {
Expand Down Expand Up @@ -175,4 +176,8 @@ Intent buildIntent(

return intent;
}

public Intent parse(String uri) throws URISyntaxException {
return Intent.parseUri(uri, Intent.URI_INTENT_SCHEME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;

Expand Down Expand Up @@ -91,7 +92,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
sender.buildIntent(
action, flags, category, data, arguments, packageName, componentName, type);

if ("launch".equalsIgnoreCase(call.method)) {
if ("parseAndLaunch".equalsIgnoreCase(call.method)) {
try {
intent = sender.parse(call.argument("uri"));
sender.send(intent);
} catch (URISyntaxException e) {
result.error("WRONG_URI", "Uri's syntax is wrong", null);
jaumard marked this conversation as resolved.
Show resolved Hide resolved
}
} else if ("launch".equalsIgnoreCase(call.method)) {

if (intent != null && !sender.canResolveActivity(intent)) {
Log.i(TAG, "Cannot resolve explicit intent, falling back to implicit");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void main() {
(Widget widget) =>
widget is Text && widget.data!.startsWith('Tap here'),
),
findsNWidgets(4),
findsNWidgets(5),
);
} else {
expect(
Expand Down
10 changes: 10 additions & 0 deletions packages/android_intent_plus/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class MyApp extends StatelessWidget {
/// Holds the different intent widgets.
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});
static const _intent =
'intent://payment#Intent;action=ch.twint.action.TWINT_PAYMENT;scheme=twint;S.code=98815;S.startingOrigin=EXTERNAL_WEB_BROWSER;S.browser_fallback_url=;end';
jaumard marked this conversation as resolved.
Show resolved Hide resolved

void _createAlarm() {
const intent = AndroidIntent(
Expand Down Expand Up @@ -69,6 +71,10 @@ class MyHomePage extends StatelessWidget {
child: const Text(
'Tap here to set an alarm\non weekdays at 9:30pm.'),
),
ElevatedButton(
onPressed: _parseAndLaunch,
child: const Text('Tap here to parse and launch intent $_intent'),
),
ElevatedButton(
onPressed: _openChooser,
child: const Text('Tap here to launch Intent with Chooser'),
Expand Down Expand Up @@ -111,6 +117,10 @@ class MyHomePage extends StatelessWidget {
);
intent.sendBroadcast();
}

void _parseAndLaunch() {
AndroidIntent.parseAndLaunch(_intent);
}
}

/// Launches intents to specific Android activities.
Expand Down
3 changes: 2 additions & 1 deletion packages/android_intent_plus/example/pubspec.yaml
jaumard marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ dependencies:
flutter:
sdk: flutter
platform: ^3.1.0
android_intent_plus: ^5.0.2
android_intent_plus: #^5.0.2
path: ../

dev_dependencies:
flutter_driver:
Expand Down
13 changes: 13 additions & 0 deletions packages/android_intent_plus/lib/android_intent.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,19 @@ class AndroidIntent {
await _channel.invokeMethod<void>('launch', _buildArguments());
}

/// Parse and Launch the intent in format.
///
/// Equivalent of native android Intent.parseUri(URI, Intent.URI_INTENT_SCHEME)
/// This works only on Android platforms.
static Future<void> parseAndLaunch(String uri) async {
if (!const LocalPlatform().isAndroid) {
return;
}

await const MethodChannel(_kChannelName)
.invokeMethod<void>('parseAndLaunch', {'uri': uri});
}

/// Launch the intent with 'createChooser(intent, title)'.
///
/// This works only on Android platforms.
Expand Down
2 changes: 1 addition & 1 deletion packages/android_intent_plus/pubspec.yaml
jaumard marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: android_intent_plus
description: Flutter plugin for launching Android Intents. Not supported on iOS.
version: 5.0.2
version: 5.0.3
homepage: https://plus.fluttercommunity.dev/
repository: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/android_intent_plus
issue_tracker: https://github.com/fluttercommunity/plus_plugins/labels/android_intent_plus
Expand Down
Loading