Skip to content

Commit 02b69e1

Browse files
author
Yaqoob
committed
Sonic InApp
1 parent 550cdaf commit 02b69e1

File tree

9 files changed

+66
-23
lines changed

9 files changed

+66
-23
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/build.gradle

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99

1010
defaultConfig {
1111
applicationId "com.orbitalsonic.sonicinapppurchase"
12-
minSdk 23
12+
minSdk 21
1313
targetSdk 33
1414
versionCode 1
1515
versionName "1.0"
@@ -33,13 +33,20 @@ android {
3333
}
3434

3535
dependencies {
36-
implementation project(path: ':sonicinapp')
37-
38-
implementation 'androidx.core:core-ktx:1.9.0'
36+
implementation 'androidx.core:core-ktx:1.7.0'
3937
implementation 'androidx.appcompat:appcompat:1.5.1'
4038
implementation 'com.google.android.material:material:1.7.0'
4139
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
4240
testImplementation 'junit:junit:4.13.2'
4341
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
4442
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.0'
43+
44+
// Google Play Billing
45+
implementation "com.android.billingclient:billing-ktx:5.1.0"
46+
47+
// LiveData
48+
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.6.0-alpha03"
49+
50+
// SonicInApp Library
51+
implementation project(path: ':sonicinapp')
4552
}

app/src/main/java/com/orbitalsonic/sonicinapppurchase/MainActivity.kt

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,61 @@ package com.orbitalsonic.sonicinapppurchase
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.util.Log
6+
import android.widget.Button
7+
import android.widget.TextView
58
import android.widget.Toast
69
import com.orbitalsonic.sonicinapp.BillingManager
10+
import com.orbitalsonic.sonicinapp.status.State
711

812
class MainActivity : AppCompatActivity() {
9-
10-
1113
private val billingManager by lazy { BillingManager(this) }
14+
private lateinit var tvTitle:TextView
15+
16+
// mostly people use package name in their
17+
private val productId:String = "Paste your original Product ID"
1218

1319
override fun onCreate(savedInstanceState: Bundle?) {
1420
super.onCreate(savedInstanceState)
1521
setContentView(R.layout.activity_main)
1622

1723
initBilling()
24+
initObserver()
25+
26+
tvTitle = findViewById(R.id.tv_title)
27+
findViewById<Button>(R.id.btn_purchase).setOnClickListener {
28+
onPurchaseClick()
29+
}
30+
}
31+
32+
private fun initObserver() {
33+
State.billingState.observe(this) {
34+
Log.d("BillingManager", "initObserver: $it")
35+
tvTitle.text = it.toString()
36+
}
1837
}
1938

2039
private fun initBilling() {
21-
/*if (BuildConfig.DEBUG) {
22-
billingManager.startConnection(billingManager.getDebugProductIDList()) { connectionResult, message ->
23-
// binding.mbMakePurchase.isEnabled = connectionResult
24-
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
40+
if (BuildConfig.DEBUG) {
41+
billingManager.startConnection(billingManager.getDebugProductIDList()){ isConnectionEstablished, message ->
42+
showMessage(message)
2543
}
2644
} else {
27-
billingManager.startConnection(listOf(packageName)) { connectionResult, message ->
28-
// binding.mbMakePurchase.isEnabled = connectionResult
29-
Toast.makeText(this, message, Toast.LENGTH_LONG).show()
45+
billingManager.startConnection(listOf(productId)) { isConnectionEstablished, message ->
46+
showMessage(message)
3047
}
31-
}*/
48+
}
3249
}
3350

3451

3552
private fun onPurchaseClick() {
3653
billingManager.makePurchase { isSuccess, message ->
37-
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
38-
//binding.mbMakePurchase.isEnabled = !isSuccess
54+
showMessage(message)
3955
}
4056
}
4157

58+
private fun showMessage(message:String){
59+
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
60+
}
61+
4262
}

app/src/main/res/layout/activity_main.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,27 @@
77
tools:context=".MainActivity">
88

99
<TextView
10+
android:id="@+id/tv_title"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
1213
android:text="Hello World!"
14+
android:padding="10dp"
15+
android:layout_marginHorizontal="10dp"
1316
app:layout_constraintBottom_toBottomOf="parent"
1417
app:layout_constraintEnd_toEndOf="parent"
1518
app:layout_constraintStart_toStartOf="parent"
1619
app:layout_constraintTop_toTopOf="parent" />
1720

21+
<Button
22+
android:id="@+id/btn_purchase"
23+
android:layout_width="match_parent"
24+
android:layout_height="wrap_content"
25+
android:text="Purchase"
26+
android:layout_marginHorizontal="20dp"
27+
android:layout_marginBottom="20dp"
28+
app:layout_constraintStart_toStartOf="parent"
29+
app:layout_constraintEnd_toEndOf="parent"
30+
app:layout_constraintBottom_toBottomOf="parent"
31+
/>
32+
1833
</androidx.constraintlayout.widget.ConstraintLayout>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_name">SonicInAppPurchase</string>
2+
<string name="app_name">Sonic InApp Purchase</string>
33
</resources>

sonicinapp/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ android {
88
compileSdk 33
99

1010
defaultConfig {
11-
minSdk 23
11+
minSdk 21
1212
targetSdk 33
1313

1414
consumerProguardFiles "consumer-rules.pro"
@@ -30,7 +30,7 @@ android {
3030
}
3131

3232
dependencies {
33-
implementation 'androidx.core:core-ktx:1.9.0'
33+
implementation 'androidx.core:core-ktx:1.7.0'
3434
implementation 'androidx.appcompat:appcompat:1.5.1'
3535

3636
// Google Play Billing

sonicinapp/src/main/java/com/orbitalsonic/sonicinapp/BillingManager.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import com.orbitalsonic.sonicinapp.helper.BillingHelper
99
*/
1010
class BillingManager(private val activity: Activity) : BillingHelper(activity) {
1111

12-
override fun startConnection(productIdsList: List<String>, callback: (connectionResult: Boolean, message: String) -> Unit) = startBillingConnection(productIdsList, callback)
12+
override fun startConnection(productIdsList: List<String>, callback: (isConnectionEstablished: Boolean, message: String) -> Unit) = startBillingConnection(productIdsList, callback)
1313

1414
fun makePurchase(callback: (isPurchased: Boolean, message: String) -> Unit) = purchase(callback)
1515

sonicinapp/src/main/java/com/orbitalsonic/sonicinapp/helper/BillingHelper.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ abstract class BillingHelper(private val activity: Activity) {
6969
if (isBillingReady) {
7070
setBillingState(BillingState.CONNECTION_ESTABLISHED)
7171
queryForAvailableProducts()
72+
Handler(Looper.getMainLooper()).post { callback.invoke(true, BillingState.CONNECTION_ESTABLISHED.message) }
7273
} else {
7374
setBillingState(BillingState.CONNECTION_FAILED)
75+
Handler(Looper.getMainLooper()).post { callback.invoke(false, billingResult.debugMessage) }
7476
}
75-
Handler(Looper.getMainLooper()).post { callback.invoke(isBillingReady, billingResult.debugMessage) }
7677
}
7778
})
7879
}

0 commit comments

Comments
 (0)