-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d8b59df
Showing
133 changed files
with
4,837 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,9 @@ | ||
.DS_Store | ||
.dart_tool/ | ||
.idea | ||
|
||
.packages | ||
.pub/ | ||
|
||
build/ | ||
.flutter-plugins |
This file contains 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,10 @@ | ||
# This file tracks properties of this Flutter project. | ||
# Used by Flutter tool to assess capabilities and perform upgrades etc. | ||
# | ||
# This file should be version controlled and should not be manually edited. | ||
|
||
version: | ||
revision: bbfbf1770cca2da7c82e887e4e4af910034800b6 | ||
channel: stable | ||
|
||
project_type: plugin |
This file contains 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,20 @@ | ||
{ | ||
// Use IntelliSense to learn about possible attributes. | ||
// Hover to view descriptions of existing attributes. | ||
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Flutter: Attach to Device", | ||
"type": "dart", | ||
"request": "attach", | ||
"program": "example/lib/main.dart" | ||
}, | ||
{ | ||
"name": "Flutter", | ||
"program": "example/lib/main.dart", | ||
"request": "launch", | ||
"type": "dart" | ||
} | ||
] | ||
} |
This file contains 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,3 @@ | ||
## 0.0.1 | ||
|
||
* TODO: Describe initial release. |
This file contains 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 @@ | ||
TODO: Add your license here. |
This file contains 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,23 @@ | ||
# g_faraday | ||
|
||
## 快速集成 | ||
|
||
### 创建一个 flutter module 项目 | ||
|
||
``` | ||
flutter create -t module #{your_project_name} | ||
``` | ||
|
||
### 在module目录下创建一个 flutter plugin 项目 | ||
|
||
``` | ||
cd #{your_project_name} | ||
flutter create -t plugin --platforms ios,android faraday_helper | ||
``` | ||
|
||
### 在flutter module项目中依赖g_faraday | ||
``` | ||
``` |
This file contains 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,8 @@ | ||
*.iml | ||
.gradle | ||
/local.properties | ||
/.idea/workspace.xml | ||
/.idea/libraries | ||
.DS_Store | ||
/build | ||
/captures |
25 changes: 25 additions & 0 deletions
25
android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
This file contains 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,25 @@ | ||
package io.flutter.plugins; | ||
|
||
import io.flutter.plugin.common.PluginRegistry; | ||
import com.yuxiaor.flutter.g_faraday.GFaradayPlugin; | ||
|
||
/** | ||
* Generated file. Do not edit. | ||
*/ | ||
public final class GeneratedPluginRegistrant { | ||
public static void registerWith(PluginRegistry registry) { | ||
if (alreadyRegisteredWith(registry)) { | ||
return; | ||
} | ||
GFaradayPlugin.registerWith(registry.registrarFor("com.yuxiaor.flutter.g_faraday.GFaradayPlugin")); | ||
} | ||
|
||
private static boolean alreadyRegisteredWith(PluginRegistry registry) { | ||
final String key = GeneratedPluginRegistrant.class.getCanonicalName(); | ||
if (registry.hasPlugin(key)) { | ||
return true; | ||
} | ||
registry.registrarFor(key); | ||
return false; | ||
} | ||
} |
This file contains 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,43 @@ | ||
group 'com.yuxiaor.flutter.g_faraday' | ||
version '1.0-SNAPSHOT' | ||
|
||
buildscript { | ||
ext.kotlin_version = '1.3.72' | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath 'com.android.tools.build:gradle:4.0.1' | ||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | ||
} | ||
} | ||
|
||
rootProject.allprojects { | ||
repositories { | ||
google() | ||
jcenter() | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'kotlin-android' | ||
|
||
android { | ||
compileSdkVersion 28 | ||
|
||
sourceSets { | ||
main.java.srcDirs += 'src/main/kotlin' | ||
} | ||
defaultConfig { | ||
minSdkVersion 16 | ||
} | ||
lintOptions { | ||
disable 'InvalidPackage' | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" | ||
} |
This file contains 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,4 @@ | ||
org.gradle.jvmargs=-Xmx1536M | ||
android.enableR8=true | ||
android.useAndroidX=true | ||
android.enableJetifier=true |
This file contains 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,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip |
This file contains 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 @@ | ||
rootProject.name = 'g_faraday' |
This file contains 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,23 @@ | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
package="com.yuxiaor.flutter.g_faraday"> | ||
|
||
<application tools:node="merge"> | ||
<provider | ||
android:name=".utils.NativeContextProvider" | ||
android:authorities="${applicationId}.app.provider" | ||
android:exported="false" /> | ||
|
||
<activity | ||
android:name=".FaradayActivity" | ||
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode" | ||
android:hardwareAccelerated="true" | ||
android:launchMode="singleTop" | ||
android:windowSoftInputMode="adjustResize" /> | ||
|
||
<meta-data | ||
android:name="flutterEmbedding" | ||
android:value="2" /> | ||
|
||
</application> | ||
</manifest> |
158 changes: 158 additions & 0 deletions
158
android/src/main/kotlin/com/yuxiaor/flutter/g_faraday/Faraday.kt
This file contains 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,158 @@ | ||
package com.yuxiaor.flutter.g_faraday | ||
|
||
import android.app.Activity | ||
import android.content.Intent | ||
import androidx.fragment.app.FragmentActivity | ||
import com.yuxiaor.flutter.g_faraday.delegates.DefaultNavigatorDelegate | ||
import com.yuxiaor.flutter.g_faraday.delegates.FaradayNavigator | ||
import com.yuxiaor.flutter.g_faraday.plugins.ActivityAwarePlugin | ||
import com.yuxiaor.flutter.g_faraday.plugins.ResultListener | ||
import com.yuxiaor.flutter.g_faraday.utils.NativeContextProvider | ||
import com.yuxiaor.flutter.g_faraday.utils.getArgs | ||
import com.yuxiaor.flutter.g_faraday.utils.getFlutterArgs | ||
import com.yuxiaor.flutter.g_faraday.utils.startForResult | ||
import io.flutter.embedding.engine.FlutterEngine | ||
import io.flutter.embedding.engine.dart.DartExecutor | ||
import io.flutter.embedding.engine.plugins.FlutterPlugin | ||
import java.io.Serializable | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
/** | ||
* Author: Edward | ||
* Date: 2020-08-31 | ||
* Description: | ||
*/ | ||
object Faraday { | ||
|
||
//native -> flutter intent args key | ||
internal const val FLUTTER_ARGS_KEY = "_flutter_args_key" | ||
const val ARGS_KEY = "_args_key_" | ||
private val nextCode = AtomicInteger() | ||
var navigator: FaradayNavigator? = null | ||
|
||
private val activityAwarePlugin = ActivityAwarePlugin() | ||
private val faradayPlugin = GFaradayPlugin() | ||
|
||
@JvmStatic | ||
val engine by lazy { FlutterEngine(NativeContextProvider.context) } | ||
|
||
@JvmStatic | ||
fun startFlutterEngine(navigatorDelegate: FaradayNavigator? = null) { | ||
this.navigator = navigatorDelegate ?: DefaultNavigatorDelegate() | ||
//注册插件 | ||
registerPlugin(faradayPlugin) | ||
registerPlugin(activityAwarePlugin) | ||
//开始执行dart代码,启动引擎 | ||
engine.dartExecutor.executeDartEntrypoint(DartExecutor.DartEntrypoint.createDefault()) | ||
} | ||
|
||
@JvmStatic | ||
fun registerPlugin(plugin: FlutterPlugin) { | ||
engine.plugins.add(plugin) | ||
} | ||
|
||
@JvmStatic | ||
fun getCurrentActivity(): Activity? { | ||
return activityAwarePlugin.binding?.activity | ||
} | ||
|
||
/** | ||
* When jumped to native page form a flutter page with args,this method is provided to get the args. | ||
* Or you can also call [Intent.getFlutterArgs()] to get args if you are using kotlin. | ||
*/ | ||
@JvmStatic | ||
fun getFlutterArgs(intent: Intent): Serializable? { | ||
return intent.getFlutterArgs() | ||
} | ||
|
||
|
||
@JvmStatic | ||
fun openFlutter(activity: FragmentActivity, url: String, vararg params: Pair<String, Any>, onActivityResult: ((data: HashMap<String, Any?>?) -> Unit)? = null) { | ||
activity.openFlutter(url, *params) { | ||
onActivityResult?.invoke(it) | ||
} | ||
} | ||
|
||
@JvmStatic | ||
fun openFlutter(activity: Activity, url: String, vararg params: Pair<String, Any>) { | ||
activity.openFlutter(url, *params) | ||
} | ||
|
||
@JvmStatic | ||
fun openFlutter(activity: Activity, url: String, requestCode: Int, vararg params: Pair<String, Any>) { | ||
activity.openFlutter(url, requestCode, *params) | ||
} | ||
|
||
internal fun openActivityForResult(intent: Intent, callback: (result: HashMap<String, Any?>?) -> Unit) { | ||
val nextRequestCode = nextCode.getAndIncrement() | ||
getCurrentActivity()?.startActivityForResult(intent, nextRequestCode) | ||
ResultListener(activityAwarePlugin) { requestCode, resultCode, data -> | ||
if (requestCode == nextRequestCode && resultCode == Activity.RESULT_OK) { | ||
callback.invoke(data?.getArgs()) | ||
} | ||
} | ||
} | ||
|
||
|
||
internal fun onPageCreate(args: Any?, callback: (seqId: Int) -> Unit) { | ||
faradayPlugin.onPageCreate(args, callback) | ||
} | ||
|
||
internal fun onPageShow(seqId: Int) { | ||
faradayPlugin.onPageShow(seqId) | ||
} | ||
|
||
internal fun onPageHidden(seqId: Int) { | ||
faradayPlugin.onPageHidden(seqId) | ||
} | ||
|
||
internal fun onPageDealloc(seqId: Int) { | ||
faradayPlugin.onPageDealloc(seqId) | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Native to flutter | ||
* @param url flutter router | ||
* @param params params from native to flutter | ||
* @param onActivityResult callback for Activity Result | ||
*/ | ||
fun FragmentActivity.openFlutter(url: String, vararg params: Pair<String, Any>, onActivityResult: ((data: HashMap<String, Any?>?) -> Unit)? = null) { | ||
val args = hashMapOf(*params).apply { this["name"] = url } | ||
val intent = Intent(this, FaradayActivity::class.java).putExtra(Faraday.FLUTTER_ARGS_KEY, args) | ||
startForResult(intent) { resultCode, data -> | ||
if (resultCode == Activity.RESULT_OK) { | ||
onActivityResult?.invoke(data?.getArgs()) | ||
} | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Native to flutter | ||
* @param url flutter router | ||
* @param params params from native to flutter | ||
* | ||
* no Activity result | ||
*/ | ||
fun Activity.openFlutter(url: String, vararg params: Pair<String, Any>) { | ||
val args = hashMapOf(*params).apply { this["name"] = url } | ||
val intent = Intent(this, FaradayActivity::class.java).putExtra(Faraday.FLUTTER_ARGS_KEY, args) | ||
startActivity(intent) | ||
} | ||
|
||
|
||
/** | ||
* Native to flutter | ||
* @param url flutter router | ||
* @param params params from native to flutter | ||
* | ||
* you need to override [onActivityResult] in your Activity to get the result | ||
*/ | ||
fun Activity.openFlutter(url: String, requestCode: Int, vararg params: Pair<String, Any>) { | ||
val args = hashMapOf(*params).apply { this["name"] = url } | ||
val intent = Intent(this, FaradayActivity::class.java).putExtra(Faraday.FLUTTER_ARGS_KEY, args) | ||
startActivityForResult(intent, requestCode) | ||
} | ||
|
48 changes: 48 additions & 0 deletions
48
android/src/main/kotlin/com/yuxiaor/flutter/g_faraday/FaradayActivity.kt
This file contains 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,48 @@ | ||
package com.yuxiaor.flutter.g_faraday | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import io.flutter.embedding.android.FlutterActivity | ||
import io.flutter.embedding.engine.FlutterEngine | ||
|
||
/** | ||
* Author: Edward | ||
* Date: 2020-09-01 | ||
* Description: | ||
*/ | ||
class FaradayActivity : FlutterActivity() { | ||
|
||
private var seqId = 0 | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
//native -> flutter args | ||
val args = intent.getSerializableExtra(Faraday.FLUTTER_ARGS_KEY) | ||
Faraday.onPageCreate(args) { | ||
seqId = it | ||
} | ||
} | ||
|
||
override fun provideFlutterEngine(context: Context): FlutterEngine? { | ||
return Faraday.engine | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
Faraday.onPageShow(seqId) | ||
} | ||
|
||
|
||
override fun onPause() { | ||
super.onPause() | ||
Faraday.onPageHidden(seqId) | ||
|
||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
Faraday.onPageDealloc(seqId) | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.