diff --git a/app/build.gradle b/app/build.gradle index 3b7e8fa..0b7d3e7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -101,6 +101,7 @@ dependencies { implementation "org.radarbase:radar-android-weather:$radar_commons_android_version" implementation "org.radarbase:radar-android-audio:$radar_commons_android_version" implementation "org.radarbase:radar-android-faros:$radar_commons_android_version" + implementation "org.radarbase:radar-android-login-oauth2:$radar_commons_android_version" implementation "org.radarbase:radar-android-polar:$radar_commons_android_version" implementation "org.radarbase:radar-android-google-sleep:$radar_commons_android_version" implementation "org.radarbase:radar-android-google-activity:$radar_commons_android_version" diff --git a/app/src/main/java/org/radarcns/detail/AuthServiceImpl.kt b/app/src/main/java/org/radarcns/detail/AuthServiceImpl.kt index 44024c9..1bc3f2b 100644 --- a/app/src/main/java/org/radarcns/detail/AuthServiceImpl.kt +++ b/app/src/main/java/org/radarcns/detail/AuthServiceImpl.kt @@ -3,11 +3,13 @@ package org.radarcns.detail import org.radarbase.android.auth.AppAuthState import org.radarbase.android.auth.AuthService import org.radarbase.android.auth.LoginManager +import org.radarbase.android.auth.oauth2.OAuth2LoginManager import org.radarbase.android.auth.portal.ManagementPortalLoginManager class AuthServiceImpl : AuthService() { override fun createLoginManagers(appAuth: AppAuthState): List = listOf( ManagementPortalLoginManager(this, appAuth), + OAuth2LoginManager(this, appAuth) ) override fun showLoginNotification() = Unit diff --git a/app/src/main/java/org/radarcns/detail/LoginActivityImpl.kt b/app/src/main/java/org/radarcns/detail/LoginActivityImpl.kt index 4f653b9..26f608a 100644 --- a/app/src/main/java/org/radarcns/detail/LoginActivityImpl.kt +++ b/app/src/main/java/org/radarcns/detail/LoginActivityImpl.kt @@ -37,6 +37,7 @@ import org.json.JSONObject import org.radarbase.android.RadarApplication.Companion.radarConfig import org.radarbase.android.RadarConfiguration.Companion.BASE_URL_KEY import org.radarbase.android.auth.* +import org.radarbase.android.auth.oauth2.OAuth2LoginManager import org.radarbase.android.auth.portal.ManagementPortalLoginManager import org.radarbase.android.util.Boast import org.radarbase.android.util.NetworkConnectedReceiver @@ -61,7 +62,8 @@ class LoginActivityImpl : LoginActivity(), NetworkConnectedReceiver.NetworkConne private lateinit var binding: ActivityLoginBinding - private lateinit var qrCodeScanner: QrCodeScanner + private lateinit var mpQrCodeScanner: QrCodeScanner + private lateinit var sepQrCodeScanner: QrCodeScanner private lateinit var dialog: Dialog private lateinit var mainHandler: Handler @@ -76,13 +78,18 @@ class LoginActivityImpl : LoginActivity(), NetworkConnectedReceiver.NetworkConne networkReceiver = NetworkConnectedReceiver(this, this) didCreate = true - qrCodeScanner = QrCodeScanner(this) { value -> + mpQrCodeScanner = QrCodeScanner(this) { value -> value?.takeTrimmedIfNotEmpty() - ?.also { parseQrCode(it) } + ?.also { parseMpQrCode(it) } + } + sepQrCodeScanner = QrCodeScanner(this) { value -> + value?.takeTrimmedIfNotEmpty() + ?.also { parseSepQrCode(it) } } with(binding) { - scanButton.setOnClickListener { v -> scan(v) } + mpScanButton.setOnClickListener { v -> scanQrFromMP(v) } + sepScanButton.setOnClickListener { scanQrFromSEP() } enterCredentialsButton.setOnClickListener { v -> enterCredentials(v) } loader.visibility = View.GONE } @@ -102,7 +109,7 @@ class LoginActivityImpl : LoginActivity(), NetworkConnectedReceiver.NetworkConne networkReceiver.run { unregister() } } - private fun parseQrCode(qrCode: String) { + private fun parseMpQrCode(qrCode: String) { onProcessing() logger.info("Read tokenUrl: {}", qrCode) @@ -142,6 +149,33 @@ class LoginActivityImpl : LoginActivity(), NetworkConnectedReceiver.NetworkConne } } + private fun parseSepQrCode(qrData: String) { + onProcessing() + logger.debug("Processing data from QR code {}", qrData) + + if (qrData.isEmpty()) { + loginFailed(null, QrException("QR code is empty. Please retry")) + logger.error("QR code is empty. Please retry") + return + } + + applyOAuthManager { binder, oAuthManager, authState -> + if (qrData.startsWith('{')) { + try { + oAuthManager.parsePreLoginQr(authState, qrData) + } catch (ex: Exception) { + loginFailed(oAuthManager, ex) + return@applyOAuthManager + } + } else { + // QR code should have data in JSON format if scanning from SEP UI + loginFailed(oAuthManager, QrException("QR code doesn't contains the data in JSON format")) + return@applyOAuthManager + } + binder.update(oAuthManager) + } + } + private fun applyMpManager(callback: (AuthService.AuthServiceBinder, ManagementPortalLoginManager, AppAuthState) -> Unit) { authConnection.applyBinder { val manager = managers.find { it is ManagementPortalLoginManager } @@ -152,6 +186,16 @@ class LoginActivityImpl : LoginActivity(), NetworkConnectedReceiver.NetworkConne } } + private fun applyOAuthManager(callback: (AuthService.AuthServiceBinder, OAuth2LoginManager, AppAuthState) -> Unit) { + authConnection.applyBinder { + val oauthManager = managers.find { it is OAuth2LoginManager } + as? OAuth2LoginManager ?: return@applyBinder + applyState { + callback(this@applyBinder, oauthManager, this) + } + } + } + private fun onProcessing() { setLoader(true) } @@ -171,20 +215,29 @@ class LoginActivityImpl : LoginActivity(), NetworkConnectedReceiver.NetworkConne private fun checkNetworkConnection() = with(binding) { if (networkIsConnected) { - scanButton.isEnabled = true + mpScanButton.isEnabled = true + sepScanButton.isEnabled = true enterCredentialsButton.isEnabled = true messageText.text = "" } else { - scanButton.isEnabled = false + mpScanButton.isEnabled = false + sepScanButton.isEnabled = false enterCredentialsButton.isEnabled = false messageText.setText(R.string.no_connection) } } - private fun scan(@Suppress("UNUSED_PARAMETER") view: View) { + private fun scanQrFromMP(@Suppress("UNUSED_PARAMETER") view: View) { + if (canLogin) { + canLogin = false + mpQrCodeScanner.start() + } + } + + private fun scanQrFromSEP() { if (canLogin) { canLogin = false - qrCodeScanner.start() + sepQrCodeScanner.start() } } @@ -330,11 +383,13 @@ class LoginActivityImpl : LoginActivity(), NetworkConnectedReceiver.NetworkConne private fun setLoader(show: Boolean) = with(binding) { if (show) { - scanButton.visibility = View.GONE + mpScanButton.visibility = View.GONE + sepScanButton.visibility = View.GONE enterCredentialsButton.visibility = View.GONE loader.visibility = View.VISIBLE } else { - scanButton.visibility = View.VISIBLE + mpScanButton.visibility = View.VISIBLE + sepScanButton.visibility = View.VISIBLE enterCredentialsButton.visibility = View.VISIBLE loader.visibility = View.GONE } diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 2e56d6c..1285f13 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -45,24 +45,25 @@ android:padding="@dimen/size_medium"> + + + + + + + Download and Install Error in package name! - Log in to pRMT App with the QR Code or Token provided to you. - Login + Log in to pRMT App with the QR Code or Token provided to you by Management Portal. + Log in to pRMT App with the QR Code provided to you by Self Enrolment Portal. + Login With Management Portal + Login With SEP-UI ItemDetailHostActivity Items Item Detail