@@ -18,7 +18,7 @@ import androidx.fragment.app.Fragment
1818import androidx.lifecycle.ViewModelProvider
1919import androidx.work.WorkManager
2020import com.google.android.gms.common.ConnectionResult
21- import com.google.android.gms.common.GooglePlayServicesUtil
21+ import com.google.android.gms.common.GoogleApiAvailability
2222import dagger.hilt.android.AndroidEntryPoint
2323import kotlinx.coroutines.CoroutineScope
2424import kotlinx.coroutines.Dispatchers
@@ -98,12 +98,12 @@ class LoginActivity : AppCompatActivity() {
9898 preferences.edit().putInt(getString(R .string.databaseVersionKey), MageSqliteOpenHelper .DATABASE_VERSION ).apply ()
9999
100100 // check google play services version
101- val isGooglePlayServicesAvailable = GooglePlayServicesUtil .isGooglePlayServicesAvailable(
101+ val isGooglePlayServicesAvailable = GoogleApiAvailability .getInstance() .isGooglePlayServicesAvailable(
102102 applicationContext
103103 )
104104 if (isGooglePlayServicesAvailable != ConnectionResult .SUCCESS ) {
105- if (GooglePlayServicesUtil .isUserRecoverableError (isGooglePlayServicesAvailable)) {
106- val dialog = GooglePlayServicesUtil . getErrorDialog(isGooglePlayServicesAvailable, this , 1 )
105+ if (GoogleApiAvailability .getInstance().isUserResolvableError (isGooglePlayServicesAvailable)) {
106+ val dialog = GoogleApiAvailability .getInstance(). getErrorDialog(this , 1 , isGooglePlayServicesAvailable )
107107 dialog?.setOnCancelListener { dialog1: DialogInterface ->
108108 dialog1.dismiss()
109109 finish()
@@ -193,67 +193,74 @@ class LoginActivity : AppCompatActivity() {
193193
194194 private fun observeAuthentication (authentication : Authentication ? ) {
195195 if (authentication == null ) return
196- val status = authentication.status
197- if (status is AuthenticationStatus .Success ) {
198- val token = status.token
199- viewModel.authorize(authentication.strategy, token)
200- } else if (status is AccountCreated ) {
201- val message = status.message
202- val dialog = ContactDialog (this , (preferences), " Account Created" , message)
203- dialog.setAuthenticationStrategy(authentication.strategy)
204- dialog.show(null )
205- } else if (status is Offline ) {
206- val message = status.message
207- val dialog = ContactDialog (this , (preferences), " Sign in Failed" , message)
208- dialog.setAuthenticationStrategy(authentication.strategy)
209- dialog.show { workOffline: Boolean ->
210- if (workOffline) {
211- loginComplete(false )
196+ when (val status = authentication.status){
197+ is AuthenticationStatus .Success -> {
198+ val token = status.token
199+ viewModel.authorize(authentication.strategy, token)
200+ }
201+ is AccountCreated -> {
202+ val message = status.message
203+ val dialog = ContactDialog (this , (preferences), " Account Created" , message)
204+ dialog.setAuthenticationStrategy(authentication.strategy)
205+ dialog.show(null )
206+ }
207+ is Offline -> {
208+ val message = status.message
209+ val dialog = ContactDialog (this , (preferences), " Sign in Failed" , message)
210+ dialog.setAuthenticationStrategy(authentication.strategy)
211+ dialog.show { workOffline: Boolean ->
212+ if (workOffline) {
213+ loginComplete(false )
214+ }
215+ viewModel.completeOffline(workOffline)
212216 }
213- viewModel.completeOffline(workOffline)
214217 }
215- } else if (status is AuthenticationStatus .Failure ) {
216- val message = status.message
217- val dialog = ContactDialog (this , (preferences), " Sign in Failed" , message)
218- dialog.setAuthenticationStrategy(authentication.strategy)
219- dialog.show(null )
218+ is AuthenticationStatus .Failure -> {
219+ val message = status.message
220+ val dialog = ContactDialog (this , (preferences), " Sign in Failed" , message)
221+ dialog.setAuthenticationStrategy(authentication.strategy)
222+ dialog.show(null )
223+ }
220224 }
221225 }
222226
223227 private fun observeAuthorization (authorization : Authorization ? ) {
224228 if (authorization == null ) return
225- val status = authorization.status
226- if (status is AuthorizationStatus .Success ) {
227- loginComplete(status.sessionChanged)
228- } else if (status is FailAuthorization ) {
229- val dialog = ContactDialog (
230- this ,
231- preferences,
232- " Registration Sent" ,
233- getString(R .string.device_registered_text)
234- )
235- val user = status.user
236- if (user != null ) {
237- dialog.username = user.username
229+ when (val status = authorization.status) {
230+ is AuthorizationStatus .Success -> {
231+ loginComplete(status.sessionChanged)
238232 }
239- dialog.show(null )
240- } else if (status is FailInvalidServer ) {
241- val dialog = ContactDialog (
242- this ,
243- preferences,
244- " Application Compatibility Error" ,
245- " MAGE is not compatible with this server, please ensure your application is up to date or contact your MAGE administrator."
246- )
247- dialog.show(null )
248- }
249- if (status is FailAuthentication ) {
250- val message = status.message
251- val dialog = ContactDialog (this , preferences, " Sign-in Failed" , message)
252- val user = status.user
253- if (user != null ) {
254- dialog.username = user.username
233+ is FailAuthorization -> {
234+ val dialog = ContactDialog (
235+ this ,
236+ preferences,
237+ " Registration Sent" ,
238+ getString(R .string.device_registered_text)
239+ )
240+ val user = status.user
241+ if (user != null ) {
242+ dialog.username = user.username
243+ }
244+ dialog.show(null )
245+ }
246+ is FailInvalidServer -> {
247+ val dialog = ContactDialog (
248+ this ,
249+ preferences,
250+ " Application Compatibility Error" ,
251+ " MAGE is not compatible with this server, please ensure your application is up to date or contact your MAGE administrator."
252+ )
253+ dialog.show(null )
254+ }
255+ is FailAuthentication -> {
256+ val message = status.message
257+ val dialog = ContactDialog (this , preferences, " Sign-in Failed" , message)
258+ val user = status.user
259+ if (user != null ) {
260+ dialog.username = user.username
261+ }
262+ dialog.show(null )
255263 }
256- dialog.show(null )
257264 }
258265 }
259266
@@ -277,41 +284,40 @@ class LoginActivity : AppCompatActivity() {
277284 Log .e(LOG_NAME , " Error parsing authentication strategy" , e)
278285 }
279286 }
280- if (strategies.size > 1 && strategies.containsKey(" local" )) {
281- findViewById<View >(R .id.or ).visibility = View .VISIBLE
282- } else {
283- findViewById<View >(R .id.or ).visibility = View .GONE
287+ when {
288+ (strategies.size > 1 ) -> {
289+ if (strategies.containsKey(" local" ))
290+ findViewById<View >(R .id.or ).visibility = View .VISIBLE
291+ }
292+ (strategies.size == 1 ) -> findViewById<View >(R .id.or ).visibility = View .GONE
293+ else -> {
294+ findViewById<View >(R .id.or ).visibility = View .GONE
295+ findViewById<View >(R .id.login_error).visibility = View .VISIBLE
296+ }
284297 }
285298 findViewById<View >(R .id.google_login_button).visibility = View .GONE
286299 for (entry: Map .Entry <String ?, JSONObject > in strategies.entries) {
287300 val authenticationName = entry.key
288301 val authenticationType = entry.value.optString(" type" )
289302 if (supportFragmentManager.findFragmentByTag(authenticationName) != null ) continue
290- if ((" local" == authenticationName)) {
291- val loginFragment: Fragment = MageLoginFragment .newInstance(
292- entry.key!! , entry.value
293- )
294- transaction.add(R .id.local_auth, loginFragment, authenticationName)
295- } else if ((" oauth" == authenticationType)) {
296- val loginFragment: Fragment = IdpLoginFragment .newInstance(
297- (entry.key)!! , entry.value
298- )
299- transaction.add(R .id.third_party_auth, loginFragment, authenticationName)
300- } else if ((" saml" == authenticationType)) {
301- val loginFragment: Fragment = IdpLoginFragment .newInstance(
302- (entry.key)!! , entry.value
303- )
304- transaction.add(R .id.third_party_auth, loginFragment, authenticationName)
305- } else if ((" ldap" == authenticationType)) {
306- val loginFragment: Fragment = LdapLoginFragment .newInstance(
307- (entry.key)!! , entry.value
308- )
309- transaction.add(R .id.third_party_auth, loginFragment, authenticationName)
310- } else {
311- val loginFragment: Fragment = IdpLoginFragment .newInstance(
312- (entry.key)!! , entry.value
313- )
314- transaction.add(R .id.third_party_auth, loginFragment, authenticationName)
303+ when (authenticationType) {
304+ " local" -> {
305+ val loginFragment: Fragment = MageLoginFragment .newInstance(
306+ entry.key!! , entry.value
307+ )
308+ transaction.add(R .id.local_auth, loginFragment, authenticationName)
309+ }
310+ " ldap" -> {
311+ val loginFragment: Fragment = LdapLoginFragment .newInstance(
312+ (entry.key)!! , entry.value
313+ )
314+ transaction.add(R .id.third_party_auth, loginFragment, authenticationName)
315+ } else -> { // saml, oauth, and overflow
316+ val loginFragment: Fragment = IdpLoginFragment .newInstance(
317+ (entry.key)!! , entry.value
318+ )
319+ transaction.add(R .id.third_party_auth, loginFragment, authenticationName)
320+ }
315321 }
316322 }
317323
0 commit comments