diff --git a/README.md b/README.md index 9e64c64..292cfe1 100644 --- a/README.md +++ b/README.md @@ -31,13 +31,32 @@ allprojects { val response = Verify.isPurchased(this, PACKAGE_ID) val userName = response.second val isPaid = response.first + val isInsallApklis = response.third ``` + * Check paid Java ```java - Pair response = Verify.Companion.isPurchased(this, PACKAGE_ID); + Triple response = Verify.Companion.isPurchased(this, PACKAGE_ID); String userName = response.getSecond(); Boolean isPaid = response.getFirst(); + Boolean isInsallApklis = response.getThird(); +``` + +* Show alert Kotlin +```kotlin + // ACTION: 1 Apklis is not install, 2 User not logged in, 3 App not purchased + Verify.showAlert(this, ACTION, PACKAGE_ID, title, message) + + Verify.showAlert(this, 1, PACKAGE_ID, "Usuario no logueado", "Para poder usar APP_NAME debe iniciar sección en Apklis, luego de iniciar sección puede continuar con el uso de APP_NAME") +``` + +* Show alert Java +```java + // ACTION: 1 Apklis is not install, 2 User not logged in, 3 App not purchased + Verify.showAlert(this, ACTION, PACKAGE_ID, title, message); + + Verify.showAlert(this, 1, PACKAGE_ID, "Usuario no logueado", "Para poder usar APP_NAME debe iniciar sección en Apklis, luego de iniciar sección puede continuar con el uso de APP_NAME"); ``` ### Contributing All contributions are welcome!!! diff --git a/apklischeckpayment/build.gradle b/apklischeckpayment/build.gradle index d866ade..5aa5244 100644 --- a/apklischeckpayment/build.gradle +++ b/apklischeckpayment/build.gradle @@ -8,10 +8,10 @@ android { buildToolsVersion "30.0.2" defaultConfig { - minSdkVersion 19 + minSdkVersion 14 targetSdkVersion 30 - versionCode 2 - versionName "1.1" + versionCode 3 + versionName "1.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles "consumer-rules.pro" @@ -41,4 +41,4 @@ dependencies { testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' -} \ No newline at end of file +} diff --git a/apklischeckpayment/src/main/java/cu/uci/apklischeckpayment/Verify.kt b/apklischeckpayment/src/main/java/cu/uci/apklischeckpayment/Verify.kt index 544e144..c0370cf 100644 --- a/apklischeckpayment/src/main/java/cu/uci/apklischeckpayment/Verify.kt +++ b/apklischeckpayment/src/main/java/cu/uci/apklischeckpayment/Verify.kt @@ -1,6 +1,10 @@ package cu.uci.apklischeckpayment +import android.app.AlertDialog import android.content.Context +import android.content.DialogInterface +import android.content.Intent +import android.content.pm.PackageManager import android.net.Uri import android.os.RemoteException @@ -11,29 +15,74 @@ class Verify { private const val APKLIS_PAID = "paid" private const val APKLIS_USER_NAME = "user_name" - fun isPurchased(context: Context, packageId: String): Pair { + fun isPurchased(context: Context, packageId: String): Triple { var paid = false + var install = false var userName: String? = null - val providerURI: Uri = Uri.parse("$APKLIS_PROVIDER$packageId") + + val packageManager: PackageManager = context.packageManager try { - val contentResolver = context.contentResolver.acquireContentProviderClient(providerURI) - val cursor = contentResolver?.query(providerURI, null, null, null, null) - cursor?.let { - if (it.moveToFirst()) { - paid = it.getInt(it.getColumnIndex(APKLIS_PAID)) > 0 - userName = it.getString(it.getColumnIndex(APKLIS_USER_NAME)) + install = true + packageManager.getPackageInfo("cu.uci.android.apklis", PackageManager.GET_ACTIVITIES) + val providerURI: Uri = Uri.parse("$APKLIS_PROVIDER$packageId") + try { + val contentResolver = context.contentResolver.acquireContentProviderClient(providerURI) + val cursor = contentResolver?.query(providerURI, null, null, null, null) + cursor?.let { + if (it.moveToFirst()) { + paid = it.getInt(it.getColumnIndex(APKLIS_PAID)) > 0 + userName = it.getString(it.getColumnIndex(APKLIS_USER_NAME)) + } } + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { + contentResolver?.close() + } else { + contentResolver?.release() + } + cursor?.close() + } catch (e: RemoteException) { + e.printStackTrace() } - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { - contentResolver?.close() - } else { - contentResolver?.release() - } - cursor?.close() - } catch (e: RemoteException) { + } catch (e: PackageManager.NameNotFoundException) { e.printStackTrace() + install = false } - return Pair(paid, userName) + return Triple(paid, userName, install) + } + + fun showAlert(context: Context, action: Int, packageId: String, title: String, message: String) { + /* + action = 0 | Install apklis + action = 1 | Login + action = 2 | Purchase + */ + val dialog = AlertDialog.Builder(context) + .setTitle(title) + .setMessage(message) + .setCancelable(false) + .setPositiveButton("Ir a Apklis") { _, _ -> + when (action) { + 0 -> { + val url = "https://www.apklis.cu/application/cu.uci.android.apklis" + val intent = Intent(Intent.ACTION_VIEW) + intent.data = Uri.parse(url) + context.startActivity(intent) + } + 1 -> { + val intent: Intent? = context.packageManager.getLaunchIntentForPackage("cu.uci.android.apklis") + intent!!.action = Intent.ACTION_VIEW + intent.addCategory(Intent.CATEGORY_LAUNCHER) + context.startActivity(intent) + } + 2 -> { + val url = "https://www.apklis.cu/application/$packageId" + val intent = Intent(Intent.ACTION_VIEW) + intent.data = Uri.parse(url) + context.startActivity(intent) + } + } + } + .show() } } -} \ No newline at end of file +} diff --git a/app/build.gradle b/app/build.gradle index 9428d45..c06e89f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -9,10 +9,10 @@ android { defaultConfig { applicationId "cu.uci.example.check.paid" - minSdkVersion 19 + minSdkVersion 14 targetSdkVersion 30 - versionCode 2 - versionName "1.1" + versionCode 3 + versionName "1.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } @@ -42,6 +42,6 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' - //implementation project(path: ':apklischeckpayment') - implementation 'com.github.Z17-CU:apklischeckpayment:0.0.2' -} \ No newline at end of file + implementation project(path: ':apklischeckpayment') +// implementation 'com.github.Z17-CU:apklischeckpayment:0.0.2' +} diff --git a/app/src/main/java/cu/uci/example/check/paid/MainActivity.kt b/app/src/main/java/cu/uci/example/check/paid/MainActivity.kt index 78dd7f4..204be64 100644 --- a/app/src/main/java/cu/uci/example/check/paid/MainActivity.kt +++ b/app/src/main/java/cu/uci/example/check/paid/MainActivity.kt @@ -1,8 +1,9 @@ package cu.uci.example.check.paid -import androidx.appcompat.app.AppCompatActivity +import android.content.pm.PackageManager import android.os.Bundle import android.widget.Toast +import androidx.appcompat.app.AppCompatActivity import cu.uci.apklischeckpayment.Verify class MainActivity : AppCompatActivity() { @@ -14,7 +15,63 @@ class MainActivity : AppCompatActivity() { val userName = response.second val isPaid = response.first + val install = response.third + + /* + action: + 1 - Install apklis + 2 - Login in Apklis + 3 - Purchase app + */ + + if (install) { + // Apklis instalada comprobar compra y login + + if (userName == null) { + // Usuario no logueado + + Verify.showAlert( + this, + 1, + "com.example.nova.prosalud", + "Usuario no logueado", + "Para poder usar ${getString(R.string.app_name)} debe iniciar sección en Apklis, luego de iniciar sección puede continuar con el uso de ${ + getString(R.string.app_name) + }" + ) + } else { + // Usuario loguado, comprobar compra + + if (isPaid) { + // Aplicación comprada, su lógica aquí + + Toast.makeText( + applicationContext, + "Todo esta bien, programe sus siguientes acciones", + Toast.LENGTH_LONG + ).show() + } else { + // Aplicación no comprada + + Verify.showAlert( + this, + 2, + "com.example.nova.prosalud", + "Aplicación no comprada", + "Para poder usar ${getString(R.string.app_name)} debe comprar nuestra aplicación, luego de que la compra sea confirmada puede continuar el uso de nuestra aplicación" + ) + } + } + } else { + // Apklis no instalado - Toast.makeText(this, "User $userName ${if (isPaid) "paid" else "does not paid"}", Toast.LENGTH_LONG).show() + Verify.showAlert( + this, + 0, + "com.example.nova.prosalud", + "Apklis no instalada", + "Para poder usar ${getString(R.string.app_name)} debe instalar Apklis y luego iniciar sección dentro de esta" + ) + } } -} \ No newline at end of file +}