Skip to content

Commit

Permalink
Merge pull request #22 from studyplus/feature/google_play_store
Browse files Browse the repository at this point in the history
Google Play Storeを開く対応
  • Loading branch information
koji-1009 authored Sep 25, 2019
2 parents cced527 + 2918a9a commit cad7b03
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 18 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Add it in your root build.gradle at the end of repositories:

```groovy
dependencies {
implementation 'com.github.studyplus:Studyplus-Android-SDK:2.5.1'
implementation 'com.github.studyplus:Studyplus-Android-SDK:2.5.2'
}
```

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

```kotlin
try {
Studyplus.instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
} catch (e: ActivityNotFoundException) {
e.printStackTrace()
Toast.makeText(context, "Need for Studyplus 5.0.0+", Toast.LENGTH_LONG).show()
}
Studyplus.instance.startAuth(this@MainActivity, REQUEST_CODE_AUTH)
```

Then save its result.
Expand Down
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ buildscript {
'compileSdk' : 28,
'minSdk' : 21,
'targetSdk' : 28,
'annotation' : '1.1.0',
'core_ktx' : '1.1.0',
'appcompat' : '1.1.0',
'constraintLayout': '1.1.3'
]
Expand Down
3 changes: 2 additions & 1 deletion studyplus-android-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_cotoutines_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlin_cotoutines_version"

implementation 'androidx.annotation:annotation:1.1.0'
implementation "androidx.annotation:annotation:$versions.annotation"
implementation "androidx.core:core-ktx:$versions.core_ktx"

def okhttp = "3.14.2"
implementation "com.squareup.okhttp3:okhttp:$okhttp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ class Studyplus private constructor() {
* @since 2.0.0
*/
fun startAuth(activity: Activity, requestCode: Int) {
if (consumerKey == null || consumerSecret == null) {
throw IllegalStateException("Please call setup method before this method call.")
}
check(consumerKey != null && consumerSecret != null) { "Please call setup method before this method call." }

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

runBlocking {
try {
Expand All @@ -90,4 +86,4 @@ class Studyplus private constructor() {
@JvmStatic
val instance by lazy { Studyplus() }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package jp.studyplus.android.sdk.internal.auth

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.core.net.toUri
import jp.studyplus.android.sdk.R

internal class AuthTransit
Expand All @@ -11,20 +14,37 @@ internal class AuthTransit
* @param consumerSecret for API
*/
constructor(
private val consumerKey: String,
private val consumerSecret: String
private val consumerKey: String,
private val consumerSecret: String
) {
companion object {
private const val EXTRA_CONSUMER_KEY = "consumer_key"
private const val EXTRA_CONSUMER_SECRET = "consumer_secret"

private val URI_STORE = "market://details?id=jp.studyplus.android.app".toUri()
private val INTENT_STORE = Intent(Intent.ACTION_VIEW, URI_STORE)
}

fun start(activity: Activity, requestCode: Int) {
if (isInstalledStudyplus(activity).not()) {
openGooglePlayStore(activity)
return
}

val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.putExtra(EXTRA_CONSUMER_KEY, consumerKey)
intent.putExtra(EXTRA_CONSUMER_SECRET, consumerSecret)
intent.data = Uri.parse(activity.getString(R.string.app_custom_scheme))
activity.startActivityForResult(intent, requestCode)
}

private fun openGooglePlayStore(context: Context) {
context.startActivity(INTENT_STORE)
}

private fun isInstalledStudyplus(context: Context): Boolean {
val packages = context.packageManager.getInstalledApplications(PackageManager.GET_META_DATA)
return packages.any { it.packageName == "jp.studyplus.android.app" }
}
}
2 changes: 1 addition & 1 deletion studyplus-android-sdk/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<resources>
<string name="app_custom_scheme">studyplus://auth/external/start</string>
<string name="app_custom_scheme" translatable="false">studyplus://auth/external/start</string>
</resources>

0 comments on commit cad7b03

Please sign in to comment.