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

Supporting the new Android plugins APIs & compatibility with flutter 3 #62

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
99 changes: 62 additions & 37 deletions android/src/main/kotlin/com/drenther/upi_pay/UpiPayPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.drenther.upi_pay

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
Expand All @@ -8,21 +9,29 @@ import android.graphics.drawable.Drawable
import android.net.Uri
import android.util.Base64
import android.util.Log
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.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry.ActivityResultListener
import io.flutter.plugin.common.PluginRegistry.Registrar
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.PluginRegistry
import java.io.ByteArrayOutputStream

class UpiPayPlugin internal constructor(registrar: Registrar, channel: MethodChannel) : MethodCallHandler, ActivityResultListener {
private val activity = registrar.activity()
class UpiPayPlugin: FlutterPlugin, MethodCallHandler, ActivityAware, PluginRegistry.ActivityResultListener {
/// The MethodChannel that will the communication between Flutter and native Android
///
/// This local reference serves to register the plugin with the Flutter Engine and unregister it
/// when the Flutter Engine is detached from the Activity
private lateinit var channel : MethodChannel
private var activity: Activity? = null

private var result: Result? = null
private var requestCodeNumber = 201119

var hasResponded = false
private var hasResponded = false

override fun onMethodCall(call: MethodCall, result: Result) {
hasResponded = false
Expand Down Expand Up @@ -75,12 +84,13 @@ class UpiPayPlugin internal constructor(registrar: Registrar, channel: MethodCha
val intent = Intent(Intent.ACTION_VIEW, uri)
intent.setPackage(app)

if (intent.resolveActivity(activity.packageManager) == null) {
if (activity?.let { intent.resolveActivity(it.packageManager) } == null) {
this.success("activity_unavailable")
return
}

activity.startActivityForResult(intent, requestCodeNumber)
activity?.startActivityForResult(intent, requestCodeNumber)

} catch (ex: Exception) {
Log.e("upi_pay", ex.toString())
this.success("failed_to_open_app")
Expand All @@ -94,29 +104,25 @@ class UpiPayPlugin internal constructor(registrar: Registrar, channel: MethodCha
val uri = uriBuilder.build()
val intent = Intent(Intent.ACTION_VIEW, uri)

val packageManager = activity.packageManager
val packageManager = activity?.packageManager

try {
val activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
val activities = packageManager?.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)

// Convert the activities into a response that can be transferred over the channel.
val activityResponse = activities.map {
val packageName = it.activityInfo.packageName
val drawable = packageManager.getApplicationIcon(packageName)

val bitmap = getBitmapFromDrawable(drawable)
val icon = if (bitmap != null) {
encodeToBase64(bitmap)
} else {
null
}
Comment on lines -108 to -112
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was the null check guard removed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this change will take a look maybe it got reformatted in Android studio


mapOf(
"packageName" to packageName,
"icon" to icon,
"priority" to it.priority,
"preferredOrder" to it.preferredOrder
)
val activityResponse = activities?.map {
val packageName = it.activityInfo.packageName
val drawable = packageManager.getApplicationIcon(packageName)

val bitmap = getBitmapFromDrawable(drawable)
val icon = encodeToBase64(bitmap)

mapOf(
"packageName" to packageName,
"icon" to icon,
"priority" to it.priority,
"preferredOrder" to it.preferredOrder
)
}

result?.success(activityResponse)
Expand All @@ -132,10 +138,10 @@ class UpiPayPlugin internal constructor(registrar: Registrar, channel: MethodCha
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.NO_WRAP)
}

private fun getBitmapFromDrawable(drawable: Drawable): Bitmap? {
private fun getBitmapFromDrawable(drawable: Drawable): Bitmap {
val bmp: Bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bmp)
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight())
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
return bmp
}
Expand All @@ -147,6 +153,7 @@ class UpiPayPlugin internal constructor(registrar: Registrar, channel: MethodCha
}
}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
if (requestCodeNumber == requestCode && result != null) {
if (data != null) {
Expand All @@ -163,13 +170,31 @@ class UpiPayPlugin internal constructor(registrar: Registrar, channel: MethodCha
return true
}

companion object {
@JvmStatic
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "upi_pay")
val plugin = UpiPayPlugin(registrar, channel)
registrar.addActivityResultListener(plugin)
channel.setMethodCallHandler(plugin)
}

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "upi_pay")
channel.setMethodCallHandler(this)
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

override fun onAttachedToActivity(binding: ActivityPluginBinding) {
activity = binding.activity
binding.addActivityResultListener(this)
}

override fun onDetachedFromActivityForConfigChanges() {
onDetachedFromActivity()
}

override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
activity = binding.activity
}

override fun onDetachedFromActivity() {
activity = null
}

}
18 changes: 15 additions & 3 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,17 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 30
compileSdkVersion flutter.compileSdkVersion
ndkVersion flutter.ndkVersion

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = '1.8'
}
drenther marked this conversation as resolved.
Show resolved Hide resolved

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -35,8 +45,10 @@ android {
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.drenther.upi_pay_example"
minSdkVersion 16
targetSdkVersion 30
// You can update the following values to match your application needs.
// For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-build-configuration.
minSdkVersion flutter.minSdkVersion
targetSdkVersion flutter.targetSdkVersion
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
1 change: 1 addition & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:exported="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
Expand Down
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.6.10'
repositories {
google()
jcenter()
Expand Down
33 changes: 20 additions & 13 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.6.1"
version: "2.8.2"
boolean_selector:
dependency: transitive
description:
Expand All @@ -28,14 +28,14 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.2.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
clock:
dependency: transitive
description:
Expand All @@ -49,7 +49,7 @@ packages:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -77,7 +77,7 @@ packages:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.0"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -94,21 +94,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.4"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "1.7.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
platform:
dependency: transitive
description:
Expand All @@ -134,7 +141,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.8.2"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -169,7 +176,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.0"
version: "0.4.9"
typed_data:
dependency: transitive
description:
Expand All @@ -190,14 +197,14 @@ packages:
path: ".."
relative: true
source: path
version: "1.0.0"
version: "2.0.0"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.2"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
flutter: ">2.0.0"
Loading