Skip to content

Commit cad7b03

Browse files
authored
Merge pull request #22 from studyplus/feature/google_play_store
Google Play Storeを開く対応
2 parents cced527 + 2918a9a commit cad7b03

File tree

6 files changed

+32
-18
lines changed

6 files changed

+32
-18
lines changed

README.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Add it in your root build.gradle at the end of repositories:
2424

2525
```groovy
2626
dependencies {
27-
implementation 'com.github.studyplus:Studyplus-Android-SDK:2.5.1'
27+
implementation 'com.github.studyplus:Studyplus-Android-SDK:2.5.2'
2828
}
2929
```
3030

@@ -41,12 +41,7 @@ Studyplus.instance.setup("consumer_key", "consumer_secret")
4141
Open an Activity to connect with Studyplus.
4242

4343
```kotlin
44-
try {
45-
Studyplus.instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
46-
} catch (e: ActivityNotFoundException) {
47-
e.printStackTrace()
48-
Toast.makeText(context, "Need for Studyplus 5.0.0+", Toast.LENGTH_LONG).show()
49-
}
44+
Studyplus.instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
5045
```
5146

5247
Then save its result.

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: 3 additions & 7 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 {
@@ -90,4 +86,4 @@ class Studyplus private constructor() {
9086
@JvmStatic
9187
val instance by lazy { Studyplus() }
9288
}
93-
}
89+
}
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)