Skip to content

Commit b115371

Browse files
committed
Add open Google Play Store logic
1 parent cced527 commit b115371

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ buildscript {
99
'compileSdk' : 28,
1010
'minSdk' : 21,
1111
'targetSdk' : 28,
12+
'annotation' : '1.1.0',
13+
'core_ktx' : '1.1.0',
1214
'appcompat' : '1.1.0',
1315
'constraintLayout': '1.1.3'
1416
]

studyplus-android-sdk/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ dependencies {
4444
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_cotoutines_version"
4545
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_cotoutines_version"
4646

47-
implementation 'androidx.annotation:annotation:1.1.0'
47+
implementation "androidx.annotation:annotation:$versions.annotation"
48+
implementation "androidx.core:core-ktx:$versions.core_ktx"
4849

4950
def okhttp = "3.14.2"
5051
implementation "com.squareup.okhttp3:okhttp:$okhttp"

studyplus-android-sdk/src/main/java/jp/studyplus/android/sdk/Studyplus.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class Studyplus private constructor() {
4040
* @since 2.0.0
4141
*/
4242
fun startAuth(activity: Activity, requestCode: Int) {
43-
if (consumerKey == null || consumerSecret == null) {
44-
throw IllegalStateException("Please call setup method before this method call.")
45-
}
43+
check(!(consumerKey == null || consumerSecret == null)) { "Please call setup method before this method call." }
4644

4745
AuthTransit(consumerKey!!, consumerSecret!!).start(activity, requestCode)
4846
}
@@ -67,9 +65,7 @@ class Studyplus private constructor() {
6765
* @since 2.0.0
6866
*/
6967
fun postRecord(context: Context, studyRecord: StudyRecord, listener: OnPostRecordListener?) {
70-
if (!isAuthenticated(context)) {
71-
throw IllegalStateException("Please check your application's authentication before this method call.")
72-
}
68+
check(isAuthenticated(context)) { "Please check your application's authentication before this method call." }
7369

7470
runBlocking {
7571
try {
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package jp.studyplus.android.sdk.internal.auth
22

33
import android.app.Activity
4+
import android.content.Context
45
import android.content.Intent
6+
import android.content.pm.PackageManager
57
import android.net.Uri
8+
import androidx.core.net.toUri
69
import jp.studyplus.android.sdk.R
710

811
internal class AuthTransit
@@ -11,20 +14,37 @@ internal class AuthTransit
1114
* @param consumerSecret for API
1215
*/
1316
constructor(
14-
private val consumerKey: String,
15-
private val consumerSecret: String
17+
private val consumerKey: String,
18+
private val consumerSecret: String
1619
) {
1720
companion object {
1821
private const val EXTRA_CONSUMER_KEY = "consumer_key"
1922
private const val EXTRA_CONSUMER_SECRET = "consumer_secret"
23+
24+
private val URI_STORE = "market://details?id=jp.studyplus.android.app".toUri()
25+
private val INTENT_STORE = Intent(Intent.ACTION_VIEW, URI_STORE)
2026
}
2127

2228
fun start(activity: Activity, requestCode: Int) {
29+
if (isInstalledStudyplus(activity).not()) {
30+
openGooglePlayStore(activity)
31+
return
32+
}
33+
2334
val intent = Intent()
2435
intent.action = Intent.ACTION_VIEW
2536
intent.putExtra(EXTRA_CONSUMER_KEY, consumerKey)
2637
intent.putExtra(EXTRA_CONSUMER_SECRET, consumerSecret)
2738
intent.data = Uri.parse(activity.getString(R.string.app_custom_scheme))
2839
activity.startActivityForResult(intent, requestCode)
2940
}
41+
42+
private fun openGooglePlayStore(context: Context) {
43+
context.startActivity(INTENT_STORE)
44+
}
45+
46+
private fun isInstalledStudyplus(context: Context): Boolean {
47+
val packages = context.packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
48+
return packages.any { it.packageName == "jp.studyplus.android.app" }
49+
}
3050
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<resources>
2-
<string name="app_custom_scheme">studyplus://auth/external/start</string>
2+
<string name="app_custom_scheme" translatable="false">studyplus://auth/external/start</string>
33
</resources>

0 commit comments

Comments
 (0)