Skip to content

Commit

Permalink
InApMessagingにopenUrlを追加 (#181)
Browse files Browse the repository at this point in the history
* コメント追加
* openUrlの引数のContextを必須に変更
* contextがnullの時はNEW_TASKフラグをつけてapplicationから起動
* booleanを返すようにした
* CHANGELOG追記&bump version

Co-authored-by: kota.fujiwara <[email protected]>
Co-authored-by: A Aida <[email protected]>
  • Loading branch information
3 people authored Apr 4, 2022
1 parent 20c2000 commit 6882d2f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 20 deletions.
15 changes: 13 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,24 @@

| モジュール/プラグイン名 | Description | 最新のバージョン |
| :-- | :-- | :-- |
| core | イベントトラッキング機能を提供します。 | 2.16.0 |
| inappmessaging | アプリ内メッセージ機能を提供します。 | 2.11.0 |
| core | イベントトラッキング機能を提供します。 | 2.17.0 |
| inappmessaging | アプリ内メッセージ機能を提供します。 | 2.12.0 |
| notifications | プッシュ通知の受信および効果測定機能を提供します。 | 2.8.0 |
| variables | 設定値配信機能を提供します。 | 2.2.0 |
| visualtracking | ビジュアルトラッキング機能を提供します。| 2.6.0 |
| Karte Gradle Plugin | ビジュアルトラッキング機能に必要なプラグインです。| 2.4.0 |

# Releases - xxxx.xx.xx

### Core 2.17.0
** 🎉 FEATURE**
- KARTE SDKでURLを開くためのAPIを追加しました。このAPIはSDK内部での利用を想定しており、通常のSDK利用で使用することはありません。

### InAppMessaging 2.12.0
** 🔨 CHANGED**
- Core 2.17.0で追加されたAPIを利用するように内部処理を修正しました。
- 挙動の変更はありません。

# Releases - 2022.03.31

### Core 2.16.0
Expand Down
29 changes: 29 additions & 0 deletions core/src/main/java/io/karte/android/KarteApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package io.karte.android

import android.app.Activity
import android.app.Application
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
Expand Down Expand Up @@ -429,5 +430,33 @@ class KarteApp private constructor() : ActivityLifecycleCallback() {
fun onNewIntent(intent: Intent?) {
intent?.let { self.handleDeeplink(it) }
}

/**
* URLを開きます。
* **SDK内部で利用するために用意している機能であり、通常利用で使用することはありません。**
*
* @param [uri] 対象のURI
* @param [context] [Context]
*/
@JvmStatic
fun openUrl(uri: Uri, context: Context?): Boolean {
try {
var intent =
self.executeCommand(uri).filterIsInstance<Intent>().firstOrNull()
if (intent == null) {
intent = Intent(Intent.ACTION_VIEW).apply { data = uri }
}
if (context != null) {
context.startActivity(intent)
} else {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
self.application.startActivity(intent)
}
} catch (e: ActivityNotFoundException) {
Logger.e(LOG_TAG, "Failed to open url.", e)
return false
}
return true
}
}
}
37 changes: 37 additions & 0 deletions core/src/test/java/io/karte/android/integration/DeepLinkTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package io.karte.android.integration
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import com.google.common.truth.Truth.assertThat
import io.karte.android.KarteApp
import io.karte.android.TrackerRequestDispatcher
Expand All @@ -30,6 +31,7 @@ import org.junit.After
import org.junit.Before
import org.junit.Test
import org.robolectric.Robolectric
import org.robolectric.Shadows
import org.robolectric.android.controller.ActivityController

class NewIntentActivity : Activity() {
Expand Down Expand Up @@ -255,3 +257,38 @@ class DeepLinkEventTest : DeepLinkTestCase() {
assertNoEvent()
}
}

class OpenUrl処理 : TrackerTestCase() {
@Test
fun KarteがハンドルしないURLの場合そのURLIntentが投げられること() {
val activity = Robolectric.buildActivity(Activity::class.java).create().get()
KarteApp.openUrl(Uri.parse("test://hoge"), activity)

val nextActivity = Shadows.shadowOf(application).nextStartedActivity
assertThat(nextActivity).isNotNull()
assertThat(nextActivity.action).isEqualTo(Intent.ACTION_VIEW)
assertThat(nextActivity.dataString).isEqualTo("test://hoge")
}

@Test
fun hostopensettingsの場合アプリ設定のIntentが投げられること() {
val activity = Robolectric.buildActivity(Activity::class.java).create().get()
KarteApp.openUrl(Uri.parse("krt-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx://open-settings"), activity)

val nextActivity = Shadows.shadowOf(application).nextStartedActivity
assertThat(nextActivity).isNotNull()
assertThat(nextActivity.action).isEqualTo(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
assertThat(nextActivity.dataString).startsWith("package:")
}

@Test
fun hostopenstoreの場合ストアのIntentが投げられること() {
val activity = Robolectric.buildActivity(Activity::class.java).create().get()
KarteApp.openUrl(Uri.parse("krt-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx://open-store"), activity)

val nextActivity = Shadows.shadowOf(application).nextStartedActivity
assertThat(nextActivity).isNotNull()
assertThat(nextActivity.action).isEqualTo(Intent.ACTION_VIEW)
assertThat(nextActivity.dataString).startsWith("market:")
}
}
2 changes: 1 addition & 1 deletion core/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.16.0
2.17.0
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import android.app.Activity
import android.app.AlertDialog
import android.app.Dialog
import android.app.DialogFragment
import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
Expand All @@ -35,6 +33,7 @@ import android.webkit.ValueCallback
import android.webkit.WebChromeClient
import android.webkit.WebView
import androidx.fragment.app.FragmentActivity
import io.karte.android.KarteApp
import io.karte.android.core.logger.Logger
import io.karte.android.inappmessaging.InAppMessaging
import io.karte.android.inappmessaging.internal.view.WindowView
Expand Down Expand Up @@ -129,21 +128,10 @@ internal class IAMWindow(
}

override fun openUrl(uri: Uri, withReset: Boolean) {
Logger.d(LOG_TAG, "Opening url: $uri")
try {
var intent =
InAppMessaging.self?.app?.executeCommand(uri)?.filterIsInstance<Intent>()
?.firstOrNull()
if (intent == null) {
intent = Intent(Intent.ACTION_VIEW).apply { data = uri }
}
if (!withReset) {
(context as? Activity)?.let { InAppMessaging.self?.enablePreventRelayFlag(it) }
}
context.startActivity(intent)
} catch (e: ActivityNotFoundException) {
Logger.e(LOG_TAG, "Failed to open url.", e)
if (!withReset) {
(context as? Activity)?.let { InAppMessaging.self?.enablePreventRelayFlag(it) }
}
KarteApp.openUrl(uri, context)
}

override fun errorOccurred() {
Expand Down
2 changes: 1 addition & 1 deletion inappmessaging/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.11.0
2.12.0

0 comments on commit 6882d2f

Please sign in to comment.