@@ -9,6 +9,7 @@ import android.os.Bundle
99import android.telephony.PhoneNumberUtils
1010import android.telephony.TelephonyManager
1111import android.view.View
12+ import android.webkit.URLUtil
1213import androidx.appcompat.app.AppCompatActivity
1314import androidx.core.app.ActivityCompat
1415import androidx.lifecycle.MutableLiveData
@@ -17,6 +18,7 @@ import com.google.android.material.progressindicator.LinearProgressIndicator
1718import com.google.android.material.textfield.TextInputEditText
1819import com.google.android.material.textfield.TextInputLayout
1920import timber.log.Timber
21+ import java.net.URI
2022
2123class LoginActivity : AppCompatActivity () {
2224 override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -25,6 +27,7 @@ class LoginActivity : AppCompatActivity() {
2527 setContentView(R .layout.activity_login)
2628 registerListeners()
2729 setPhoneNumber()
30+ setServerURL()
2831 }
2932
3033 private fun registerListeners () {
@@ -43,6 +46,12 @@ class LoginActivity : AppCompatActivity() {
4346 Timber .d(" phone number [$phoneNumber ] set successfully" )
4447 }
4548
49+ private fun setServerURL () {
50+ val serverUrlInput = findViewById<TextInputEditText >(R .id.loginServerUrlInput)
51+ serverUrlInput.setText(getString(R .string.default_server_url))
52+ Timber .d(" default server url [${serverUrlInput.text.toString()} ] set successfully" )
53+ }
54+
4655 @SuppressLint(" HardwareIds" )
4756 private fun getPhoneNumber (context : Context ): String? {
4857 val telephonyManager = this .getSystemService(Context .TELEPHONY_SERVICE ) as TelephonyManager
@@ -81,6 +90,12 @@ class LoginActivity : AppCompatActivity() {
8190 val apiKey = findViewById<TextInputEditText >(R .id.loginApiKeyTextInput)
8291 apiKey.isEnabled = false
8392
93+ val serverUrlLayout = findViewById<TextInputLayout >(R .id.loginServerUrlLayout)
94+ serverUrlLayout.error = null
95+
96+ val serverUrl = findViewById<TextInputEditText >(R .id.loginServerUrlInput)
97+ serverUrl.isEnabled = false
98+
8499 val phoneNumberLayout = findViewById<TextInputLayout >(R .id.loginPhoneNumberLayout)
85100 phoneNumberLayout.error = null
86101
@@ -89,6 +104,7 @@ class LoginActivity : AppCompatActivity() {
89104
90105 val resetView = fun () {
91106 apiKey.isEnabled = true
107+ serverUrl.isEnabled = true
92108 progressBar.visibility = View .INVISIBLE
93109 phoneNumber.isEnabled = true
94110 loginButton().isEnabled = true
@@ -104,17 +120,38 @@ class LoginActivity : AppCompatActivity() {
104120 return
105121 }
106122
107- val liveData = MutableLiveData <String ?>()
123+ if (! URLUtil .isValidUrl(serverUrl.text.toString().trim())) {
124+ Timber .e(" url number [${serverUrl.text.toString()} ] is not a valid URL" )
125+ resetView()
126+ serverUrlLayout.error = " Server URL [${serverUrl.text.toString()} ] is invalid"
127+ return
128+ }
129+
130+ if (! URLUtil .isHttpsUrl(serverUrl.text.toString().trim())) {
131+ Timber .e(" url number [${serverUrl.text.toString()} ] is not an https URL" )
132+ resetView()
133+ serverUrlLayout.error = " Server URL [${serverUrl.text.toString()} ] must be HTTPS"
134+ return
135+ }
136+
137+ val liveData = MutableLiveData <Pair <String ?, String ?>>()
108138 liveData.observe(this ) { authResult ->
109139 run {
110140 progressBar.visibility = View .INVISIBLE
111- if (authResult != null ) {
141+ if (authResult.first != null ) {
142+ resetView()
143+ apiKeyLayout.error = authResult.first
144+ return @run
145+ }
146+
147+ if (authResult.second != null ) {
112148 resetView()
113- apiKeyLayout .error = authResult
149+ serverUrlLayout .error = authResult.second
114150 return @run
115151 }
116152
117153 Settings .setApiKeyAsync(this , apiKey.text.toString())
154+ Settings .setServerUrlAsync(this , serverUrl.text.toString().trim())
118155
119156 val e164PhoneNumber = PhoneNumberUtils .formatNumberToE164(
120157 phoneNumber.text.toString(),
@@ -128,9 +165,9 @@ class LoginActivity : AppCompatActivity() {
128165 }
129166
130167 Thread {
131- val error = HttpSmsApiService (apiKey.text.toString()).validateApiKey()
168+ val error = HttpSmsApiService (apiKey.text.toString(), URI (serverUrl.text.toString().trim()) ).validateApiKey()
132169 liveData.postValue(error)
133- Timber .i( " login successful " )
170+ Timber .d( " finished validating api URL " )
134171 }.start()
135172 }
136173
0 commit comments