Skip to content

Commit

Permalink
First commit. Added unit tests for all methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
recheej committed Jul 26, 2019
0 parents commit 34a23d8
Show file tree
Hide file tree
Showing 47 changed files with 1,265 additions and 0 deletions.
82 changes: 82 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Built application files
*.apk
*.ap_
*.aab

# Files for the ART/Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/
out/
release/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/

# Android Studio captures folder
captures/

# IntelliJ
*.iml
.idea/workspace.xml
.idea/tasks.xml
.idea/gradle.xml
.idea/assetWizardSettings.xml
.idea/dictionaries
.idea/libraries
# Android Studio 3 in .gitignore file.
.idea/caches
.idea/modules.xml
# Comment next line if keeping position of elements in Navigation Editor is relevant for you
.idea/navEditor.xml

# Keystore files
# Uncomment the following lines if you do not want to check your keystore files in.
#*.jks
#*.keystore

# External native build folder generated in Android Studio 2.2 and later
.externalNativeBuild

# Google Services (e.g. APIs or Firebase)
# google-services.json

# Freeline
freeline.py
freeline/
freeline_project_description.json

# fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output
fastlane/readme.md

# Version control
vcs.xml

# lint
lint/intermediates/
lint/generated/
lint/outputs/
lint/tmp/
# lint/reports/
10 changes: 10 additions & 0 deletions .idea/codeStyles/Project.xml

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

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

4 changes: 4 additions & 0 deletions .idea/encodings.xml

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

9 changes: 9 additions & 0 deletions .idea/misc.xml

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

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

1 change: 1 addition & 0 deletions billinglibrarycoroutines/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
53 changes: 53 additions & 0 deletions billinglibrarycoroutines/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 29
buildToolsVersion "29.0.1"


defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}

// Gradle automatically adds 'android.test.runner' as a dependency.
useLibrary 'android.test.runner'

useLibrary 'android.test.base'
useLibrary 'android.test.mock'

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'com.android.billingclient:billing:2.0.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0-RC'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0-RC'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.core:core-ktx:1.0.2'
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.23.0'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.0-RC'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:core:1.2.0'
}
repositories {
mavenCentral()
}
21 changes: 21 additions & 0 deletions billinglibrarycoroutines/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
9 changes: 9 additions & 0 deletions billinglibrarycoroutines/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.billinglibrarycoroutines">

<application>
<uses-library android:name="android.test.runner"
android:required="false"/>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.example.billinglibrarycoroutines

import com.android.billingclient.api.BillingResult

class BillingResultException(val billingResult: BillingResult) : Exception(billingResult.debugMessage)
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.example.billinglibrarycoroutines.extensions

import android.app.Activity
import com.android.billingclient.api.*
import com.example.billinglibrarycoroutines.BillingResultException
import kotlinx.coroutines.CancellableContinuation
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withContext
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

@Throws(BillingResultException::class)
suspend fun BillingClient.getSkuDetails(params: SkuDetailsParams): List<SkuDetails>? = suspendOnMainThread {
querySkuDetailsAsync(params) { billingResult: BillingResult, skuDetails: List<SkuDetails>? ->
if (billingResult.isOk()) {
it.resume(skuDetails)
} else {
it.resumeWithException(billingResult.toBillingException())
}
}
}

@Throws(BillingResultException::class)
suspend fun BillingClient.acknowledgePurchase(params: AcknowledgePurchaseParams) =
suspendOnMainThread<Unit> { continuation ->
acknowledgePurchase(params) {
if (it.isOk()) {
continuation.resume(Unit)
} else {
continuation.resumeWithException(it.toBillingException())
}
}
}

@Throws(BillingResultException::class)
suspend fun BillingClient.consume(params: ConsumeParams) = suspendOnMainThread<String> {
consumeAsync(params) { billingResult, purchaseToken ->
if (billingResult.isOk()) {
it.resume(purchaseToken)
} else {
it.resumeWithException(billingResult.toBillingException())
}
}
}

@Throws(BillingResultException::class)
suspend fun BillingClient.launchPriceChange(activity: Activity, params: PriceChangeFlowParams) =
suspendOnMainThread<Unit> { continuation ->
launchPriceChangeConfirmationFlow(activity, params) {
if (it.isOk()) {
continuation.resume(Unit)
} else {
continuation.resumeWithException(it.toBillingException())
}
}
}

@Throws(BillingResultException::class)
suspend fun BillingClient.loadRewardedSku(params: RewardLoadParams) =
suspendOnMainThread<Unit> { continuation ->
loadRewardedSku(params) {
if (it.isOk()) {
continuation.resume(Unit)
} else {
continuation.resumeWithException(it.toBillingException())
}
}
}

@Throws(BillingResultException::class)
suspend fun BillingClient.getPurchaseHistory(skuType: String) =
suspendCancellableCoroutine<List<PurchaseHistoryRecord>?> {
queryPurchaseHistoryAsync(skuType) { billingResult, purchaseHistoryRecordList ->
if (billingResult.isOk()) {
it.resume(purchaseHistoryRecordList)
} else {
it.resumeWithException(billingResult.toBillingException())
}
}
}

private suspend inline fun <T> suspendOnMainThread(crossinline block: (CancellableContinuation<T>) -> Unit): T =
withContext(Dispatchers.Main) {
suspendCancellableCoroutine(block)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.example.billinglibrarycoroutines.extensions

import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.BillingResult
import com.example.billinglibrarycoroutines.BillingResultException

internal fun BillingResult.toBillingException() = BillingResultException(this)

fun BillingResult.isOk() = responseCode == BillingClient.BillingResponseCode.OK
3 changes: 3 additions & 0 deletions billinglibrarycoroutines/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="app_name">BillingLibraryCoroutines</string>
</resources>
Loading

0 comments on commit 34a23d8

Please sign in to comment.