Skip to content

Commit afef801

Browse files
committed
Support android platform
1 parent 1b401c0 commit afef801

File tree

10 files changed

+167
-1
lines changed

10 files changed

+167
-1
lines changed

README-ZH.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
- [protocol_handler](#protocol_handler)
2323
- [平台支持](#平台支持)
24+
- [桌面端](#桌面端)
25+
- [移动端](#移动端)
2426
- [快速开始](#快速开始)
2527
- [安装](#安装)
2628
- [用法](#用法)
@@ -34,10 +36,17 @@
3436

3537
## 平台支持
3638

39+
### 桌面端
40+
3741
| Linux | macOS | Windows |
3842
| :---: | :---: | :-----: |
3943
|| ✔️ | ✔️ |
4044

45+
### 移动端
46+
47+
| Android | iOS |
48+
| :-----: | :---: |
49+
| ✔️ | ✔️ |
4150
## 快速开始
4251

4352
### 安装

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ English | [简体中文](./README-ZH.md)
4646

4747
| Android | iOS |
4848
| :-----: | :---: |
49-
| | ✔️ |
49+
| ✔️ | ✔️ |
5050

5151
## Quick Start
5252

android/.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/workspace.xml
5+
/.idea/libraries
6+
.DS_Store
7+
/build
8+
/captures

android/build.gradle

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
group 'org.leanflutter.plugins.protocol_handler'
2+
version '1.0'
3+
4+
buildscript {
5+
repositories {
6+
google()
7+
mavenCentral()
8+
}
9+
10+
dependencies {
11+
classpath 'com.android.tools.build:gradle:4.1.0'
12+
}
13+
}
14+
15+
rootProject.allprojects {
16+
repositories {
17+
google()
18+
mavenCentral()
19+
}
20+
}
21+
22+
apply plugin: 'com.android.library'
23+
24+
android {
25+
compileSdkVersion 31
26+
27+
compileOptions {
28+
sourceCompatibility JavaVersion.VERSION_1_8
29+
targetCompatibility JavaVersion.VERSION_1_8
30+
}
31+
32+
defaultConfig {
33+
minSdkVersion 16
34+
}
35+
}

android/settings.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rootProject.name = 'protocol_handler'

android/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="org.leanflutter.plugins.protocol_handler">
3+
</manifest>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

lib/src/protocol_registrar.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import 'dart:io';
22

3+
import 'protocol_registrar_impl_android.dart';
34
import 'protocol_registrar_impl_ios.dart';
45
import 'protocol_registrar_impl_macos.dart';
56
import 'protocol_registrar_impl_windows.dart';
67

78
class ProtocolRegistrar {
89
/// The shared instance of [ProtocolRegistrar].
910
static ProtocolRegistrar get instance {
11+
if (Platform.isAndroid) return ProtocolRegistrarImplAndroid.instance;
1012
if (Platform.isIOS) return ProtocolRegistrarImplIOS.instance;
1113
if (Platform.isMacOS) return ProtocolRegistrarImplMacOS.instance;
1214
if (Platform.isWindows) return ProtocolRegistrarImplWindows.instance;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import 'protocol_registrar.dart';
2+
3+
class ProtocolRegistrarImplAndroid extends ProtocolRegistrar {
4+
ProtocolRegistrarImplAndroid._();
5+
6+
/// The shared instance of [ProtocolRegistrarImplAndroid].
7+
static final ProtocolRegistrarImplAndroid instance =
8+
ProtocolRegistrarImplAndroid._();
9+
10+
@override
11+
Future<void> register(String protocol) async {
12+
// Skip
13+
}
14+
}

pubspec.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ dev_dependencies:
2626
flutter:
2727
plugin:
2828
platforms:
29+
android:
30+
package: org.leanflutter.plugins.protocol_handler
31+
pluginClass: ProtocolHandlerPlugin
2932
ios:
3033
pluginClass: ProtocolHandlerPlugin
3134
macos:

0 commit comments

Comments
 (0)