Skip to content

Commit ae981f0

Browse files
authored
Merge pull request #76 from ngageoint/BUGFIX/MAGE-988-Random-Login-Issue
MAGE 988- Random Login Issue
2 parents 57424c3 + c9d1e04 commit ae981f0

File tree

4 files changed

+136
-100
lines changed

4 files changed

+136
-100
lines changed

mage/src/main/java/mil/nga/giat/mage/login/LoginActivity.kt

Lines changed: 91 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import androidx.fragment.app.Fragment
1818
import androidx.lifecycle.ViewModelProvider
1919
import androidx.work.WorkManager
2020
import com.google.android.gms.common.ConnectionResult
21-
import com.google.android.gms.common.GooglePlayServicesUtil
21+
import com.google.android.gms.common.GoogleApiAvailability
2222
import dagger.hilt.android.AndroidEntryPoint
2323
import kotlinx.coroutines.CoroutineScope
2424
import 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

mage/src/main/res/layout/activity_login.xml

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<LinearLayout
99
android:layout_width="match_parent"
1010
android:layout_height="match_parent"
11+
android:gravity="center"
1112
android:orientation="vertical">
1213

1314
<LinearLayout
@@ -18,19 +19,13 @@
1819
android:gravity="center"
1920
tools:ignore="UseCompoundDrawables">
2021

21-
<ImageView
22-
android:layout_width="56dp"
23-
android:layout_height="56dp"
24-
android:contentDescription="@string/sign_into_your_account"
25-
app:tint="@color/header_color"
26-
android:layout_marginBottom="8dp"
27-
android:src="@drawable/ic_wand_white_50dp"/>
28-
2922
<TextView
3023
android:layout_width="wrap_content"
3124
android:layout_height="wrap_content"
25+
android:text="@string/sign_in_account"
3226
android:textSize="30sp"
33-
android:text="@string/sign_into_your_account"/>
27+
android:translationY="30sp"
28+
app:drawableTopCompat="@drawable/ic_wand_white_50dp"/>
3429

3530
</LinearLayout>
3631

@@ -41,6 +36,38 @@
4136
android:gravity="center"
4237
android:orientation="horizontal" >
4338

39+
<ScrollView
40+
android:id="@+id/login_error"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:visibility="gone">
44+
45+
<LinearLayout
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:gravity="center"
49+
android:orientation="vertical">
50+
51+
<TextView
52+
android:id="@+id/error_text"
53+
android:layout_width="wrap_content"
54+
android:layout_height="0dp"
55+
android:layout_weight=".8"
56+
android:drawablePadding="3dp"
57+
android:gravity="center"
58+
android:paddingStart="3dp"
59+
android:paddingEnd="3dp"
60+
android:scaleX="0.8"
61+
android:scaleY="0.8"
62+
android:text="@string/login_error_message"
63+
android:textSize="20sp"
64+
android:visibility="visible"
65+
app:drawableLeftCompat="@drawable/ic_error_outline_white_24dp"
66+
app:drawableTint="@color/md_red_900" />
67+
68+
</LinearLayout>
69+
</ScrollView>
70+
4471
<ScrollView
4572
android:id="@+id/login"
4673
android:layout_width="match_parent"
@@ -62,7 +89,7 @@
6289
android:id="@+id/google_login_button"
6390
android:layout_width="match_parent"
6491
android:layout_height="wrap_content"
65-
android:text="@string/sign_in_with_google"
92+
android:text="@string/sign_in_google"
6693
android:background="@drawable/common_google_signin_btn_text_dark"
6794
android:textColor="@color/md_white_1000"
6895
android:textAlignment="gravity"

mage/src/main/res/layout/activity_signup.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@
271271
android:layout_width="0dp"
272272
android:layout_height="wrap_content"
273273
android:layout_weight="1"
274-
android:text="@string/signup" />
274+
android:text="@string/signup_here" />
275275

276276
</LinearLayout>
277277

mage/src/main/res/values/strings.xml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,6 @@
169169
<string name="avatar">avatar</string>
170170
<string name="noContent" />
171171
<string name="saveButton">save button</string>
172-
<string name="sign_into_your_account">Sign into your Account</string>
173-
<string name="sign_in_with_google">Sign in with Google</string>
174-
<string name="or">or</string>
175172
<string name="what_type_of_observation_would_you_like_to_create">What type of observation would you like to create?</string>
176173
<string name="closeButton">Close Button</string>
177174
<string name="locationIcon">Location Icon</string>
@@ -285,5 +282,11 @@
285282
<string name="latitude">Latitude</string>
286283
<string name="longitude">Longitude</string>
287284
<string name="search_button">Search Button</string>
288-
285+
<string name="login_error_message">An Error Has Occurred
286+
Please Contact Your Server Administrator
287+
or Try the Server URL Again</string>
288+
<string name="sign_in_account">Sign into your Account</string>
289+
<string name="or">or</string>
290+
<string name="wand_description">A Magic Wand With Sparkles</string>
291+
<string name="sign_in_google">Sign in with Google</string>
289292
</resources>

0 commit comments

Comments
 (0)