11package jp.studyplus.android.sdk.internal.auth
22
33import android.app.Activity
4+ import android.content.Context
45import android.content.Intent
6+ import android.content.pm.PackageManager
57import android.net.Uri
8+ import androidx.core.net.toUri
69import jp.studyplus.android.sdk.R
710
811internal class AuthTransit
@@ -11,20 +14,37 @@ internal class AuthTransit
1114 * @param consumerSecret for API
1215 */
1316constructor (
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}
0 commit comments