Skip to content

Commit

Permalink
add webview window
Browse files Browse the repository at this point in the history
  • Loading branch information
bulanyurij committed May 24, 2021
1 parent ed38f54 commit fc3f6dd
Show file tree
Hide file tree
Showing 16 changed files with 271 additions and 113 deletions.
16 changes: 0 additions & 16 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions Liqpay/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
compileSdkVersion 30
buildToolsVersion "29.0.3"

defaultConfig {
minSdkVersion 21
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -27,8 +27,9 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
16 changes: 16 additions & 0 deletions Liqpay/src/main/java/ua/liqpay/Config.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ua.liqpay

/**
* Liqpay checkout end point.
*/
internal const val LIQPAY_URL_CHECKOUT = "https://www.liqpay.ua/api/3/checkout"

/**
* Privat24 application package name.
*/
internal const val PRIVAT24_APP_PACKAGE = "ua.privatbank.ap24"

/**
* Privat24 application deeplink path.
*/
internal const val PRIVAT24_APP_URI_SCHEME = "privat24:"
15 changes: 3 additions & 12 deletions Liqpay/src/main/java/ua/liqpay/LiqPay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ua.liqpay.utils.base64
import ua.liqpay.utils.getDeviceId
import ua.liqpay.utils.isNetworkAvailable
import ua.liqpay.utils.signature
import ua.liqpay.view.BUNDLE_LIQPAY_DATA
import ua.liqpay.view.LiqpayActivity
import java.io.IOException
import java.net.URLEncoder
Expand Down Expand Up @@ -48,7 +49,7 @@ class LiqPay(private val context: Context,
liqPay.eventReceiver,
IntentFilter(BROADCAST_RECEIVER_ACTION)
)
startLiqpayActivity(base64Data, signature)
LiqpayActivity.start(context, base64Data, signature)
}
}

Expand Down Expand Up @@ -83,7 +84,7 @@ class LiqPay(private val context: Context,
callback.onSuccess(resp)
} catch (e: IOException) {
e.printStackTrace()
callback.onError(ErrorCode.IO)
callback.onError(ErrorCode.OTHER)
}
}
}
Expand All @@ -101,14 +102,4 @@ class LiqPay(private val context: Context,
}
}
}

private fun startLiqpayActivity(data: String, signature: String) {
val intent = Intent(context, LiqpayActivity::class.java)
intent.putExtra(
LiqpayActivity.INTENT_POST_DATA, "data=" + URLEncoder.encode(data) +
"&signature=" + signature + "&hash_device=" + getDeviceId(context) + "&channel=android"
)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
}
4 changes: 2 additions & 2 deletions Liqpay/src/main/java/ua/liqpay/request/ErrorCode.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ua.liqpay.request

enum class ErrorCode {
IO,
FAIL_INTERNET_CONNECTION,
NEED_NON_UI_THREAD,
CHECKOUT_CANCELED
CHECKOUT_CANCELED,
OTHER
}
42 changes: 42 additions & 0 deletions Liqpay/src/main/java/ua/liqpay/utils/AppUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package ua.liqpay.utils

import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri



fun isInstallApp(context: Context, uri: String): Boolean {
val pm = context.packageManager
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES)
return true
} catch (e: PackageManager.NameNotFoundException) {
}
return false
}


fun handleAppLink(context: Context, url: String, app: String) {
if (isInstallApp(context, app)) {
context.startActivity(Intent(Intent.ACTION_VIEW, Uri.parse(url)))
return
}
try {
context.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=$app")
)
)
} catch (unused: ActivityNotFoundException) {
context.startActivity(
Intent(
Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$app")
)
)
}
}
24 changes: 24 additions & 0 deletions Liqpay/src/main/java/ua/liqpay/utils/JsonUtil.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ua.liqpay.utils

import org.json.JSONException
import org.json.JSONObject

/**
* Merge json objects.
*
* @param object1 [JSONObject]
*
* @param object2 [JSONObject]
*/
fun merge(object1: JSONObject, object2: JSONObject): JSONObject {
val iter = object2.keys() as Iterator<String>
while (iter.hasNext()) {
val key = iter.next()
try {
object1.put(key, object2[key])
} catch (e: JSONException) {
e.printStackTrace()
}
}
return object1
}
2 changes: 1 addition & 1 deletion Liqpay/src/main/java/ua/liqpay/utils/SignatureUtil.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package ua.liqpay.utils

/**
* Create Liqopay signature
* Create Liqpay signature
*
* @param data Base64 encoded data
* @param privateKey Liqpay private key
Expand Down
106 changes: 39 additions & 67 deletions Liqpay/src/main/java/ua/liqpay/view/LiqpayActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,44 @@ package ua.liqpay.view
import android.annotation.SuppressLint
import android.app.Activity
import android.app.ProgressDialog
import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.util.Base64
import android.webkit.WebView
import android.webkit.WebViewClient
import org.json.JSONObject
import ua.liqpay.utils.URLEncodeUtil
import java.net.URI
import ua.liqpay.LIQPAY_URL_CHECKOUT
import java.net.URLEncoder

class LiqpayActivity : Activity() {
const val BUNDLE_LIQPAY_DATA = "bundle_liqpay_data"

private lateinit var webView: WebView
internal class LiqpayActivity : Activity() {

public override fun onCreate(savedInstanceState: Bundle?) {
private lateinit var progressDialog: ProgressDialog

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
webView = WebView(this)
setContentView(webView)
initWebView()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
progressDialog = ProgressDialog(this).apply {
setMessage("Loading...")
show()
}
progressDialog.dismiss()
setContentView(createLiqpayView())
}

override fun onDestroy() {
webView.destroy()
sendEvent(null)
super.onDestroy()
progressDialog.dismiss()
}

/**
* Init web view and load web page
* Create liqpay view.
*/
@SuppressLint("SetJavaScriptEnabled")
private fun initWebView() {
val progressDialog = ProgressDialog(this)
progressDialog.setMessage("Loading...")
progressDialog.show()

webView.apply {
postUrl(LIQPAY_API_URL_CHECKOUT, sharedData())
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
progressDialog.cancel()
super.onPageFinished(view, url)
}

override fun onLoadResource(view: WebView, url: String) {
super.onLoadResource(view, url)
val startUrl = "/api/mob/webview"
if (url.contains(startUrl)) {
val data = parseUrl(url)
sendEvent(data.toString())
finish()
}
}
private fun createLiqpayView(): LiqpayView {
return LiqpayView(this).apply {
postUrl(LIQPAY_URL_CHECKOUT, sharedData())
webChromeClient = WindowChromeClient {
setContentView(createLiqpayView())
}
}
}
Expand All @@ -67,37 +49,27 @@ class LiqpayActivity : Activity() {
* Get shared data from intent
*/
private fun sharedData(): ByteArray {
return intent.getStringExtra(INTENT_POST_DATA)?.encodeToByteArray() ?: byteArrayOf()
return intent.getStringExtra(BUNDLE_LIQPAY_DATA)?.toByteArray() ?: byteArrayOf()
}

companion object {


/**
* Parse url data
*
* @param url Web url
*
* @return [JSONObject]
*/
private fun parseUrl(url: String?): JSONObject {
val data = JSONObject()
URLEncodeUtil.parse(URI(url), "UTF-8").forEach {
try {
if ("data" == it.first) {
val item = JSONObject(String(Base64.decode(it.second, 2)))
LiqPayUtil.addAll(data, item)
} else {
data.put(it.first, it.second)
}
} catch (e: Exception) {
e.printStackTrace()
/**
* Start liqpay activity.
*
* @param context [Context]
*
* @param data Liqpay data
*
* @param signature Liqpay signature
*/
@JvmStatic
internal fun start(context: Context, data: String, signature: String) {
val intent = Intent(context, LiqpayActivity::class.java).apply {
putExtra(BUNDLE_LIQPAY_DATA, "data=" + URLEncoder.encode(data) + "&signature=" + signature)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
}
return data
}


companion object {
const val INTENT_POST_DATA = "post_data"
}
}
Loading

0 comments on commit fc3f6dd

Please sign in to comment.