|
| 1 | +package com.salesforce.android.smi.messaging.features.core.salesforceAuthentication |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.os.Build |
| 5 | +import android.widget.Toast |
| 6 | +import com.salesforce.android.smi.common.api.Result |
| 7 | +import com.salesforce.android.smi.common.api.data |
| 8 | +import com.salesforce.android.smi.core.CoreClient |
| 9 | +import com.salesforce.android.smi.core.UserVerificationToken |
| 10 | +import com.salesforce.android.smi.messaging.features.core.UserVerification |
| 11 | +import com.salesforce.androidsdk.app.SalesforceSDKManager |
| 12 | +import com.salesforce.androidsdk.rest.RestClient |
| 13 | +import com.salesforce.androidsdk.rest.RestRequest |
| 14 | +import com.salesforce.androidsdk.rest.RestRequest.RestMethod |
| 15 | +import com.salesforce.androidsdk.rest.RestResponse |
| 16 | +import com.salesforce.androidsdk.ui.LoginActivity |
| 17 | +import java.util.logging.Level |
| 18 | +import java.util.logging.Logger |
| 19 | +import kotlin.coroutines.resume |
| 20 | +import kotlin.coroutines.suspendCoroutine |
| 21 | + |
| 22 | +class SampleSalesforceSDKManager( |
| 23 | + private val activityProvider: () -> Activity, |
| 24 | + private val coreClient: CoreClient, |
| 25 | + loginActivity: Class<out Activity?> = LoginActivity::class.java |
| 26 | +) : SalesforceSDKManager(activityProvider().applicationContext, activityProvider()::class.java, loginActivity) { |
| 27 | + private val logger = Logger.getLogger(TAG) |
| 28 | + |
| 29 | + init { |
| 30 | + customTabBrowser = null |
| 31 | + } |
| 32 | + |
| 33 | + private val messagingTokenPath: String = coreClient.salesforceAuthenticationRequestPath |
| 34 | + |
| 35 | + fun login() = gateToAPI28 { restClient() } |
| 36 | + |
| 37 | + fun logout() = gateToAPI28 { logout(activityProvider(), false) } |
| 38 | + |
| 39 | + suspend fun fetchVersions() = makeRequest(RestRequest.getRequestForVersions()) |
| 40 | + |
| 41 | + suspend fun fetchResources() = makeRequest(RestRequest.getRequestForResources(API_VERSION)) |
| 42 | + |
| 43 | + suspend fun fetchMessagingToken(): UserVerificationToken = |
| 44 | + makeRequest(RestRequest(RestMethod.GET, messagingTokenPath)).data?.asJSONObject()?.getJSONObject("data")?.let { json -> |
| 45 | + UserVerificationToken.passthroughToken(json.getString("accessToken"), json.getString("lastEventId")) |
| 46 | + } ?: throw Exception("Failed to fetch messaging token.") |
| 47 | + |
| 48 | + private suspend fun makeRequest(request: RestRequest): Result<RestResponse?> = |
| 49 | + gateToAPI28<Result<RestResponse?>> { |
| 50 | + restClient()?.let { restClient -> |
| 51 | + suspendCoroutine { continuation -> |
| 52 | + val callback = object : RestClient.AsyncRequestCallback { |
| 53 | + override fun onSuccess( |
| 54 | + request: RestRequest?, |
| 55 | + response: RestResponse? |
| 56 | + ) { |
| 57 | + response?.consume() |
| 58 | + continuation.resume(Result.Success(response)) |
| 59 | + } |
| 60 | + |
| 61 | + override fun onError(exception: Exception?) { |
| 62 | + continuation.resume(Result.Error(exception ?: REST_UNKNOWN_ERROR)) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + restClient.sendAsync(request, callback) |
| 67 | + } |
| 68 | + } ?: Result.Error(REST_UNAVAILABLE_ERROR) |
| 69 | + } ?: Result.Error(API_28_ERROR) |
| 70 | + |
| 71 | + private fun restClient(): RestClient? { |
| 72 | + var restClient: RestClient? = null |
| 73 | + clientManager.getRestClient(activityProvider()) { |
| 74 | + restClient = it |
| 75 | + } |
| 76 | + |
| 77 | + return restClient |
| 78 | + } |
| 79 | + |
| 80 | + private inline fun <T> gateToAPI28(block: () -> T): T? = |
| 81 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { |
| 82 | + block() |
| 83 | + } else { |
| 84 | + logger.log(Level.WARNING, API_28_ERROR.message) |
| 85 | + Toast.makeText(activityProvider(), API_28_ERROR.message, Toast.LENGTH_SHORT).show() |
| 86 | + null |
| 87 | + } |
| 88 | + |
| 89 | + companion object { |
| 90 | + fun getInstance( |
| 91 | + coreClient: CoreClient, |
| 92 | + activityProvider: () -> Activity |
| 93 | + ): SampleSalesforceSDKManager { |
| 94 | + if (!hasInstance() || activityProvider()::class.java != getInstance().mainActivityClass) { |
| 95 | + setInstance(SampleSalesforceSDKManager(activityProvider, coreClient)) |
| 96 | + } |
| 97 | + initInternal(activityProvider().applicationContext) |
| 98 | + return SalesforceSDKManager.Companion.getInstance() as SampleSalesforceSDKManager |
| 99 | + } |
| 100 | + |
| 101 | + private val REST_UNAVAILABLE_ERROR = Exception("Rest client not yet available.") |
| 102 | + private val REST_UNKNOWN_ERROR = Exception("Rest client not yet available.") |
| 103 | + private val API_28_ERROR = Exception("API 28+ only") |
| 104 | + private const val TAG = "SampleSalesforceSDKManager" |
| 105 | + private const val API_VERSION = "v65.0" |
| 106 | + } |
| 107 | +} |
0 commit comments