@@ -70,28 +70,19 @@ class LoginActivity : WearableActivity()
70
70
// hiding the password using dots
71
71
nc_password_input.transformationMethod = PasswordTransformationMethod .getInstance()
72
72
73
- if (isNetworkAvailable()) {
74
- if (userInstance != null ) {
75
- // There is an instance of a user on the watch, retrieve data
76
- val userJson = JSONObject (userInstance)
77
- requestNcStatus(
78
- userJson.get(" serverURL" ).toString(),
79
- userJson.get(" username" ).toString(),
80
- userJson.get(" password" ).toString()
81
- )
82
- }
83
- } else {
84
- Toast .makeText(this ,
85
- " Internet connection not available" , Toast .LENGTH_SHORT ).show()
73
+ if (userInstance != null ) {
74
+ // There is an instance of a user on the watch, retrieve data
75
+ val userJson = JSONObject (userInstance)
76
+ requestNcStatus(
77
+ userJson.get(" serverURL" ).toString(),
78
+ userJson.get(" username" ).toString(),
79
+ userJson.get(" password" ).toString(),
80
+ this
81
+ )
86
82
}
83
+
87
84
checkAndRequestPermissions()
88
- btn_next.setOnClickListener {
89
- if (isNetworkAvailable())
90
- onNextClicked()
91
- else
92
- Toast .makeText(this ,
93
- " Internet connection not available" , Toast .LENGTH_SHORT ).show()
94
- }
85
+ btn_next.setOnClickListener { onNextClicked() }
95
86
}
96
87
97
88
/* This method is used to request permissions
@@ -148,7 +139,7 @@ class LoginActivity : WearableActivity()
148
139
* NC instance. Then, if the result code is 200_OK, results
149
140
* are passed to the next activity.
150
141
*/
151
- private fun requestNcStatus (serverURL : String , username : String , password : String )
142
+ private fun requestNcStatus (serverURL : String , username : String , password : String , context : Context )
152
143
{
153
144
showProgressBar()
154
145
val credentials = basic(username, password)
@@ -186,14 +177,29 @@ class LoginActivity : WearableActivity()
186
177
navigateToResults(responseData)
187
178
}
188
179
} catch (e: JSONException ) {
189
- Log .d (" requestNcStatus" ," JsonException!!" )
180
+ Log .e (" requestNcStatus" ," JsonException!!" )
190
181
e.printStackTrace()
182
+ runOnUiThread {
183
+ Toast .makeText(
184
+ context,
185
+ " Check the URL, maybe it's not a NC instance" , Toast .LENGTH_SHORT
186
+ ).show()
187
+ }
188
+ showLoginFields()
191
189
}
192
190
}
193
191
194
192
override fun onFailure (call : Call ? , e : IOException ? ) {
195
193
// todo: handle it showing info to the user
196
194
showLoginFields()
195
+ if (e != null ) {
196
+ runOnUiThread {
197
+ Toast .makeText(
198
+ context,
199
+ e.message + " \n Check your internet connectivity" , Toast .LENGTH_SHORT
200
+ ).show()
201
+ }
202
+ }
197
203
}
198
204
})
199
205
}
@@ -220,7 +226,8 @@ class LoginActivity : WearableActivity()
220
226
serverURL + = SERVERINFO_API
221
227
requestNcStatus(serverURL,
222
228
nc_username_input.text.toString(),
223
- nc_password_input.text.toString())
229
+ nc_password_input.text.toString(),
230
+ this )
224
231
} else {
225
232
Toast .makeText(this ,
226
233
" All fields are mandatory" , Toast .LENGTH_SHORT ).show()
@@ -239,14 +246,4 @@ class LoginActivity : WearableActivity()
239
246
}
240
247
}
241
248
242
- /* This method is used to test the connectivity
243
- * https://developer.android.com/training/monitoring-device-state/connectivity-status-type
244
- */
245
- private fun isNetworkAvailable (): Boolean {
246
- val connectivityMgr =
247
- getSystemService(Context .CONNECTIVITY_SERVICE ) as ConnectivityManager
248
- val networkInfo: NetworkInfo ? = connectivityMgr.activeNetworkInfo
249
- // if no network is available networkInfo will be null
250
- return networkInfo?.isConnectedOrConnecting == true
251
- }
252
249
}
0 commit comments