-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
// swift-tools-version:5.9 | ||
// swift-tools-version:6.0 | ||
|
||
// Copyright Airship and Contributors | ||
|
||
|
121 changes: 121 additions & 0 deletions
121
...amework-proxy/src/main/java/com/urbanairship/android/framework/proxy/ui/MessageWebView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package com.urbanairship.android.framework.proxy.ui | ||
|
||
import android.content.Context | ||
import android.os.Bundle | ||
import android.util.AttributeSet | ||
import android.webkit.WebView | ||
import androidx.annotation.CallSuper | ||
import androidx.annotation.MainThread | ||
import androidx.annotation.RestrictTo | ||
import com.urbanairship.UALog | ||
import com.urbanairship.actions.ActionArguments | ||
import com.urbanairship.actions.ActionRunRequest | ||
import com.urbanairship.javascript.JavaScriptEnvironment | ||
import com.urbanairship.json.JsonMap | ||
import com.urbanairship.json.JsonValue | ||
import com.urbanairship.messagecenter.Message | ||
import com.urbanairship.messagecenter.MessageCenter | ||
import com.urbanairship.webkit.AirshipWebViewClient | ||
import com.urbanairship.webkit.NestedScrollAirshipWebView | ||
import java.text.SimpleDateFormat | ||
import java.util.Locale | ||
import java.util.TimeZone | ||
import kotlinx.coroutines.runBlocking | ||
|
||
// Copy of https://github.com/urbanairship/android-library-dev/blob/main/urbanairship-message-center/src/main/java/com/urbanairship/messagecenter/ui/widget/MessageWebView.kt until its | ||
// public again | ||
|
||
/** Base WebView configured for Airship Message Center content. */ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
public class MessageWebView @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
defStyle: Int = 0, | ||
defResStyle: Int = 0 | ||
): NestedScrollAirshipWebView(context, attrs, defStyle, defResStyle) { | ||
|
||
/** | ||
* Loads the web view with the [Message]. | ||
* | ||
* @param message The message that will be displayed. | ||
*/ | ||
public fun loadMessage(message: Message) { | ||
UALog.v { "Loading message: ${message.id}" } | ||
val user = MessageCenter.shared().user | ||
|
||
// Send authorization in the headers if the web view supports it | ||
val headers = HashMap<String, String>() | ||
|
||
// Set the auth | ||
val (userId, password) = user.id to user.password | ||
if (userId != null && password != null) { | ||
setClientAuthRequest(message.bodyUrl, userId, password) | ||
headers["Authorization"] = createBasicAuth(userId, password) | ||
} | ||
UALog.v { "Load URL: ${message.bodyUrl}" } | ||
loadUrl(message.bodyUrl, headers) | ||
} | ||
} | ||
|
||
/** A `WebViewClient` that enables the Airship Native Bridge for Message Center. */ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
public open class MessageWebViewClient : AirshipWebViewClient() { | ||
|
||
/** | ||
* @hide | ||
*/ | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
override fun extendActionRequest( | ||
request: ActionRunRequest, | ||
webView: WebView | ||
): ActionRunRequest { | ||
val metadata = Bundle() | ||
val message = getMessage(webView) | ||
if (message != null) { | ||
metadata.putString(ActionArguments.RICH_PUSH_ID_METADATA, message.id) | ||
} | ||
request.setMetadata(metadata) | ||
return request | ||
} | ||
|
||
/** | ||
* @hide | ||
*/ | ||
@CallSuper | ||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
override fun extendJavascriptEnvironment( | ||
builder: JavaScriptEnvironment.Builder, | ||
webView: WebView | ||
): JavaScriptEnvironment.Builder { | ||
val message = getMessage(webView) | ||
val extras = message?.extras?.let { JsonValue.wrapOpt(it).optMap() } ?: JsonMap.EMPTY_MAP | ||
val formattedSentDate = message?.sentDate?.let { DATE_FORMATTER.format(it) } | ||
|
||
return super.extendJavascriptEnvironment(builder, webView) | ||
.addGetter("getMessageSentDateMS", message?.sentDate?.time ?: -1) | ||
.addGetter("getMessageId", message?.id) | ||
.addGetter("getMessageTitle", message?.title) | ||
.addGetter("getMessageSentDate", formattedSentDate) | ||
.addGetter("getUserId", MessageCenter.shared().user.id) | ||
.addGetter("getMessageExtras", extras) | ||
} | ||
|
||
/** | ||
* Helper method to get the RichPushMessage from the web view. | ||
* | ||
* @param webView The web view. | ||
* @return The rich push message, or null if the web view does not have an associated message. | ||
* @note This method should only be called from the main thread. | ||
*/ | ||
@MainThread | ||
private fun getMessage(webView: WebView): Message? = runBlocking { | ||
val url = webView.url | ||
MessageCenter.shared().inbox.getMessageByUrl(url) | ||
} | ||
|
||
private companion object { | ||
private val DATE_FORMATTER = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ", Locale.US).apply { | ||
timeZone = TimeZone.getTimeZone("UTC") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[versions] | ||
|
||
# Airship | ||
airshipProxy = '12.0.0' | ||
airshipProxy = '12.1.0' | ||
airship = '19.0.0' | ||
|
||
# Gradle plugins | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters