Skip to content

Commit 0816188

Browse files
committed
update: implemented the v2 Android embedding
1 parent 9aa055b commit 0816188

File tree

9 files changed

+122
-70
lines changed

9 files changed

+122
-70
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="co.paystack.flutterpaystack">
2+
package="co.paystack.flutterpaystack">
33

44
<application>
55
<activity
66
android:name=".AuthActivity"
7-
android:theme="@style/Paystack.Dialog"/>
7+
android:theme="@style/Paystack.Dialog" />
88
</application>
99
</manifest>
Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
11
package co.paystack.flutterpaystack
22

3-
import android.annotation.SuppressLint
4-
import android.content.Context
5-
import android.os.Build
6-
import android.provider.Settings
7-
import io.flutter.plugin.common.MethodCall
8-
import io.flutter.plugin.common.MethodChannel
9-
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
10-
import io.flutter.plugin.common.MethodChannel.Result
11-
import io.flutter.plugin.common.PluginRegistry.Registrar
12-
13-
class FlutterPaystackPlugin(val appContext: Context, val authDelegate: AuthDelegate) : MethodCallHandler {
14-
companion object {
15-
@JvmStatic
16-
fun registerWith(registrar: Registrar) {
17-
val activity = registrar.activity() ?: return
18-
val channel = MethodChannel(registrar.messenger(), "flutter_paystack")
19-
val authDelegate = AuthDelegate(activity = activity)
20-
val plugin = FlutterPaystackPlugin(appContext = registrar.context(), authDelegate = authDelegate)
21-
channel.setMethodCallHandler(plugin)
3+
import android.app.Activity
4+
import io.flutter.embedding.engine.plugins.FlutterPlugin
5+
import io.flutter.embedding.engine.plugins.activity.ActivityAware
6+
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
7+
import io.flutter.plugin.common.BinaryMessenger
8+
import io.flutter.plugin.common.PluginRegistry
9+
10+
11+
class FlutterPaystackPlugin : FlutterPlugin, ActivityAware {
12+
13+
private var pluginBinding: FlutterPlugin.FlutterPluginBinding? = null
14+
private var methodCallHandler: MethodCallHandlerImpl? = null
15+
16+
override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
17+
pluginBinding = binding
2218
}
23-
}
24-
25-
@SuppressLint("HardwareIds")
26-
override fun onMethodCall(call: MethodCall, result: Result) {
27-
28-
when (call.method) {
29-
"getDeviceId" -> {
30-
result.success("androidsdk_" + Settings.Secure.getString(appContext.contentResolver,
31-
Settings.Secure.ANDROID_ID))
32-
}
33-
"getUserAgent" -> {
34-
result.success("Android_" + Build.VERSION.SDK_INT + "_Paystack_" + BuildConfig.VERSION_NAME)
35-
}
36-
37-
"getVersionCode" -> {
38-
result.success(BuildConfig.VERSION_CODE.toString())
39-
}
40-
41-
"getAuthorization" -> {
42-
authDelegate.handleAuthorization(result, call)
43-
}
44-
"getEncryptedData" -> {
45-
val encryptedData = Crypto.encrypt(call.argument<String>("stringData").toString())
46-
result.success(encryptedData)
47-
}
48-
49-
else -> result.notImplemented()
19+
20+
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
21+
pluginBinding = null
5022
}
5123

52-
}
24+
private fun setupMethodHandler(messenger: BinaryMessenger?, activity: Activity) {
25+
methodCallHandler = MethodCallHandlerImpl(messenger, activity)
26+
}
27+
28+
29+
override fun onDetachedFromActivity() {
30+
methodCallHandler?.disposeHandler()
31+
methodCallHandler = null
32+
}
33+
34+
35+
override fun onAttachedToActivity(binding: ActivityPluginBinding) {
36+
setupMethodHandler(pluginBinding?.binaryMessenger, binding.activity)
37+
}
38+
39+
override fun onDetachedFromActivityForConfigChanges() = onDetachedFromActivity()
40+
41+
override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) = onAttachedToActivity(binding)
42+
43+
companion object {
44+
45+
@JvmStatic
46+
fun registerWith(registrar: PluginRegistry.Registrar) {
47+
val plugin = FlutterPaystackPlugin()
48+
plugin.setupMethodHandler(registrar.messenger(), registrar.activity())
49+
}
50+
}
5351

5452
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package co.paystack.flutterpaystack
2+
3+
import android.annotation.SuppressLint
4+
import android.app.Activity
5+
import android.os.Build
6+
import android.provider.Settings
7+
import io.flutter.plugin.common.BinaryMessenger
8+
import io.flutter.plugin.common.MethodCall
9+
import io.flutter.plugin.common.MethodChannel
10+
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
11+
12+
class MethodCallHandlerImpl(messenger: BinaryMessenger?, private val activity: Activity?) : MethodCallHandler {
13+
private var channel: MethodChannel? = null
14+
private var authDelegate: AuthDelegate? = null
15+
16+
init {
17+
activity!!.let {
18+
authDelegate = AuthDelegate(it)
19+
channel = MethodChannel(messenger, channelName)
20+
channel?.setMethodCallHandler(this)
21+
}
22+
}
23+
24+
@SuppressLint("HardwareIds")
25+
override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
26+
when (call.method) {
27+
"getDeviceId" -> {
28+
val deviceId = Settings.Secure.getString(activity?.contentResolver, Settings.Secure.ANDROID_ID)
29+
result.success("androidsdk_$deviceId")
30+
}
31+
"getUserAgent" -> {
32+
result.success("Android_" + Build.VERSION.SDK_INT + "_Paystack_" + BuildConfig.VERSION_NAME)
33+
}
34+
35+
"getVersionCode" -> {
36+
result.success(BuildConfig.VERSION_CODE.toString())
37+
}
38+
39+
"getAuthorization" -> {
40+
authDelegate?.handleAuthorization(result, call)
41+
}
42+
"getEncryptedData" -> {
43+
val encryptedData = Crypto.encrypt(call.argument<String>("stringData").toString())
44+
result.success(encryptedData)
45+
}
46+
47+
else -> result.notImplemented()
48+
}
49+
}
50+
51+
fun disposeHandler() {
52+
channel?.setMethodCallHandler(null)
53+
channel = null
54+
}
55+
}
56+
57+
private const val channelName = "plugins.wilburt/flutter_paystack"

example/android/app/src/main/AndroidManifest.xml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
flutter needs it to communicate with the running application
66
to allow setting breakpoints, to provide hot reload, etc.
77
-->
8-
<uses-permission android:name="android.permission.INTERNET"/>
8+
<uses-permission android:name="android.permission.INTERNET" />
99

1010
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
1111
calls FlutterMain.startInitialization(this); in its onCreate method.
@@ -14,14 +14,14 @@
1414
FlutterApplication and put your custom class here. -->
1515
<application
1616
android:name="io.flutter.app.FlutterApplication"
17-
android:label="Flutter Paystack"
18-
android:icon="@mipmap/ic_launcher">
17+
android:icon="@mipmap/ic_launcher"
18+
android:label="Flutter Paystack">
1919
<activity
2020
android:name=".MainActivity"
21-
android:launchMode="singleTop"
22-
android:theme="@style/LaunchTheme"
2321
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density"
2422
android:hardwareAccelerated="true"
23+
android:launchMode="singleTop"
24+
android:theme="@style/LaunchTheme"
2525
android:windowSoftInputMode="adjustResize">
2626
<!-- This keeps the window background of the activity showing
2727
until Flutter renders its first frame. It can be removed if
@@ -31,9 +31,13 @@
3131
android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
3232
android:value="true" />
3333
<intent-filter>
34-
<action android:name="android.intent.action.MAIN"/>
35-
<category android:name="android.intent.category.LAUNCHER"/>
34+
<action android:name="android.intent.action.MAIN" />
35+
<category android:name="android.intent.category.LAUNCHER" />
3636
</intent-filter>
3737
</activity>
38+
39+
<meta-data
40+
android:name="flutterEmbedding"
41+
android:value="2" />
3842
</application>
3943
</manifest>
Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
package co.paystack.flutterpaystackexample
22

3-
import android.os.Bundle
3+
import io.flutter.embedding.android.FlutterActivity
44

5-
import io.flutter.app.FlutterActivity
6-
import io.flutter.plugins.GeneratedPluginRegistrant
75

8-
class MainActivity(): FlutterActivity() {
9-
override fun onCreate(savedInstanceState: Bundle?) {
10-
super.onCreate(savedInstanceState)
11-
GeneratedPluginRegistrant.registerWith(this)
12-
}
13-
}
6+
class MainActivity: FlutterActivity()

example/android/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
buildscript {
2-
ext.kotlin_version = '1.3.61'
2+
ext.kotlin_version = '1.3.72'
33
repositories {
44
google()
55
jcenter()
66
}
77

88
dependencies {
9-
classpath 'com.android.tools.build:gradle:4.0.0'
9+
classpath 'com.android.tools.build:gradle:4.0.1'
1010
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1111
}
1212
}

ios/Classes/FlutterPaystackPlugin.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ @implementation FlutterPaystackPlugin {
99
}
1010
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
1111
FlutterMethodChannel* channel = [FlutterMethodChannel
12-
methodChannelWithName:@"flutter_paystack"
12+
methodChannelWithName:@"plugins.wilburt/flutter_paystack"
1313
binaryMessenger:[registrar messenger]];
1414
UIViewController *viewController =
1515
[UIApplication sharedApplication].delegate.window.rootViewController;

lib/src/common/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import 'package:flutter_paystack/src/models/charge.dart';
66
import 'package:intl/intl.dart';
77

88
class Utils {
9-
static const MethodChannel channel = const MethodChannel('flutter_paystack');
9+
static const MethodChannel channel = const MethodChannel('plugins.wilburt/flutter_paystack');
1010

1111
static validateSdkInitialized() {
1212
if (!PaystackPlugin.sdkInitialized) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ flutter:
3535

3636
environment:
3737
sdk: ">=2.2.2 <3.0.0"
38-
flutter: ">=1.10.0 <2.0.0"
38+
flutter: ">=1.20.2 <2.0.0"

0 commit comments

Comments
 (0)