Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Add dual sim support for login page

Acho Arnold22521459e467
diff --git a/android/app/src/main/java/com/httpsms/LoginActivity.kt b/android/app/src/main/java/com/httpsms/LoginActivity.kt
index e57352a6..1e140e05 100644
--- a/android/app/src/main/java/com/httpsms/LoginActivity.kt
+++ b/android/app/src/main/java/com/httpsms/LoginActivity.kt
@@ -10,6 +10,7 @@ import android.telephony.PhoneNumberUtils
import android.telephony.TelephonyManager
import android.view.View
import android.webkit.URLUtil
+import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.lifecycle.MutableLiveData
@@ -27,6 +28,7 @@ class LoginActivity : AppCompatActivity() {
setContentView(R.layout.activity_login)
registerListeners()
setPhoneNumber()
+ disableSim2()
setServerURL()
}
@@ -34,6 +36,15 @@ class LoginActivity : AppCompatActivity() {
loginButton().setOnClickListener { onLoginClick() }
}
+ private fun disableSim2() {
+ if (SmsManagerService.isDualSIM(this)) {
+ Timber.d("dual sim detected")
+ return
+ }
+ val sim2Layout = findViewById<LinearLayout>(R.id.loginPhoneNumberLayoutSIM2)
+ sim2Layout.visibility = View.GONE
+ }
+
private fun setPhoneNumber() {
val phoneNumber = getPhoneNumber(this)
if(phoneNumber == null) {
@@ -41,7 +52,7 @@ class LoginActivity : AppCompatActivity() {
return
}
- val phoneInput = findViewById<TextInputEditText>(R.id.loginPhoneNumberInput)
+ val phoneInput = findViewById<TextInputEditText>(R.id.loginPhoneNumberInputSIM1)
phoneInput.setText(phoneNumber)
Timber.d("phone number [$phoneNumber] set successfully")
}
@@ -96,17 +107,24 @@ class LoginActivity : AppCompatActivity() {
val serverUrl = findViewById<TextInputEditText>(R.id.loginServerUrlInput)
serverUrl.isEnabled = false
- val phoneNumberLayout = findViewById<TextInputLayout>(R.id.loginPhoneNumberLayout)
+ val phoneNumberLayout = findViewById<TextInputLayout>(R.id.loginPhoneNumberLayoutSIM1)
phoneNumberLayout.error = null
- val phoneNumber = findViewById<TextInputEditText>(R.id.loginPhoneNumberInput)
+ val phoneNumber = findViewById<TextInputEditText>(R.id.loginPhoneNumberInputSIM1)
phoneNumber.isEnabled = false
+ val phoneNumberLayoutSIM2 = findViewById<TextInputLayout>(R.id.loginPhoneNumberLayoutSIM2)
+ phoneNumberLayoutSIM2.error = null
+
+ val phoneNumberSIM2 = findViewById<TextInputEditText>(R.id.loginPhoneNumberInputSIM2)
+ phoneNumberSIM2.isEnabled = false
+
val resetView = fun () {
apiKey.isEnabled = true
serverUrl.isEnabled = true
progressBar.visibility = View.INVISIBLE
phoneNumber.isEnabled = true
+ phoneNumberSIM2.isEnabled = true
loginButton().isEnabled = true
}
@@ -120,6 +138,18 @@ class LoginActivity : AppCompatActivity() {
return
}
+ if (
+ SmsManagerService.isDualSIM(this) && (
+ !PhoneNumberUtils.isWellFormedSmsAddress(phoneNumberSIM2.text.toString()) ||
+ !PhoneNumberUtils.isGlobalPhoneNumber(phoneNumberSIM2.text.toString())
+ )
+ ) {
+ Timber.e("phone number [${phoneNumberSIM2.text.toString()}] is not valid")
+ resetView()
+ phoneNumberLayoutSIM2.error = "Invalid E.164 phone number"
+ return
+ }
+
if(!URLUtil.isValidUrl(serverUrl.text.toString().trim())) {
Timber.e("url number [${serverUrl.text.toString()}] is not a valid URL")
resetView()
@@ -157,7 +187,15 @@ class LoginActivity : AppCompatActivity() {
phoneNumber.text.toString(),
this.resources.configuration.locales.get(0).country
)
- Settings.setOwnerAsync(this, e164PhoneNumber)
+ Settings.setSIM1PhoneNumber(this, e164PhoneNumber)
+
+ if(SmsManagerService.isDualSIM(this)) {
+ val sim2PhoneNumber = PhoneNumberUtils.formatNumberToE164(
+ phoneNumberSIM2.text.toString(),
+ this.resources.configuration.locales.get(0).country
+ )
+ Settings.setSIM2PhoneNumber(this, sim2PhoneNumber)
+ }
Timber.d("login successfully redirecting to main view")
redirectToMain()
diff --git a/android/app/src/main/java/com/httpsms/Settings.kt b/android/app/src/main/java/com/httpsms/Settings.kt
index c6043e5c..d2b8eb0b 100644
--- a/android/app/src/main/java/com/httpsms/Settings.kt
+++ b/android/app/src/main/java/com/httpsms/Settings.kt
@@ -9,6 +9,8 @@ object Settings {
private const val DEFAULT_PHONE_NUMBER = "NOT_FOUND"
private const val SETTINGS_OWNER = "SETTINGS_OWNER"
+ private const val SETTINGS_SIM1_PHONE_NUMBER = "SETTINGS_SIM1_PHONE_NUMBER"
+ private const val SETTINGS_SIM2_PHONE_NUMBER = "SETTINGS_SIM2_PHONE_NUMBER"
private const val SETTINGS_ACTIVE = "SETTINGS_ACTIVE_STATUS"
private const val SETTINGS_SIM1_ACTIVE = "SETTINGS_SIM1_ACTIVE_STATUS"
private const val SETTINGS_SIM2_ACTIVE = "SETTINGS_SIM2_ACTIVE_STATUS"
@@ -75,6 +77,24 @@ object Settings {
.apply()
}
+ fun setSIM1PhoneNumber(context: Context, owner: String?) {
+ Timber.d(Settings::setOwnerAsync.name)
+
+ PreferenceManager.getDefaultSharedPreferences(context)
+ .edit()
+ .putString(this.SETTINGS_OWNER, owner)
+ .apply()
+ }
+
+ fun setSIM2PhoneNumber(context: Context, owner: String?) {
+ Timber.d(Settings::setOwnerAsync.name)
+
+ PreferenceManager.getDefaultSharedPreferences(context)
+ .edit()
+ .putString(this.SETTINGS_OWNER, owner)
+ .apply()
+ }
+
fun getActiveStatus(context: Context): Boolean {
val activeStatus = PreferenceManager
.getDefaultSharedPreferences(context)
@@ -97,6 +117,25 @@ object Settings {
return activeStatus
}
+ fun setIncomingActiveSIM1(context: Context, status: Boolean) {
+ Timber.d(Settings::setIncomingActiveSIM1.name)
+
+ PreferenceManager.getDefaultSharedPreferences(context)
+ .edit()
+ .putBoolean(this.SETTINGS_SIM1_INCOMING_ACTIVE, status)
+ .apply()
+ }
+
+ fun setIncomingActiveSIM2(context: Context, status: Boolean) {
+ Timber.d(Settings::setIncomingActiveSIM2.name)
+
+ PreferenceManager.getDefaultSharedPreferences(context)
+ .edit()
+ .putBoolean(this.SETTINGS_SIM2_INCOMING_ACTIVE, status)
+ .apply()
+ }
+
+
fun getActiveStatus(context: Context, sim: String): Boolean {
var setting = this.SETTINGS_SIM1_ACTIVE
if (sim == Constants.SIM2) {
diff --git a/android/app/src/main/java/com/httpsms/SettingsActivity.kt b/android/app/src/main/java/com/httpsms/SettingsActivity.kt
index 06c9b488..4c15644a 100644
--- a/android/app/src/main/java/com/httpsms/SettingsActivity.kt
+++ b/android/app/src/main/java/com/httpsms/SettingsActivity.kt
@@ -47,6 +47,11 @@ class SettingsActivity : AppCompatActivity() {
Timber.d("logging out user")
Settings.setApiKeyAsync(this, null)
Settings.setOwnerAsync(this, null)
+ Settings.setSIM1PhoneNumber(this, null)
+ Settings.setSIM2PhoneNumber(this, null)
+ Settings.setActiveStatusAsync(this, true)
+ Settings.setIncomingActiveSIM1(this, true)
+ Settings.setIncomingActiveSIM2(this, true)
Settings.setFcmTokenLastUpdateTimestampAsync(this, 0)
redirectToLogin()
}
diff --git a/android/app/src/main/res/layout/activity_login.xml b/android/app/src/main/res/layout/activity_login.xml
index 1a54dc8f..0a1f046c 100644
--- a/android/app/src/main/res/layout/activity_login.xml
+++ b/android/app/src/main/res/layout/activity_login.xml
@@ -9,8 +9,8 @@
<ImageView
android:id="@+id/imageView"
- android:layout_width="145dp"
- android:layout_height="148dp"
+ android:layout_width="100dp"
+ android:layout_height="100dp"
android:layout_marginTop="32dp"
android:contentDescription="@string/img_http_sms_logo"
app:layout_constraintEnd_toEndOf="parent"
@@ -33,7 +33,8 @@
app:layout_constraintBottom_toTopOf="@+id/loginApiKeyTextInputLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
- app:layout_constraintStart_toStartOf="parent" />
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/imageView" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/loginApiKeyTextInputLayout"
@@ -56,12 +57,12 @@
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
- android:id="@+id/loginPhoneNumberLayout"
+ android:id="@+id/loginPhoneNumberLayoutSIM1"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="224dp"
- android:hint="@string/phone_number"
+ android:hint="@string/login_phone_number_sim1"
app:errorEnabled="true"
app:placeholderText="@string/login_phone_number_hint"
app:layout_constraintBottom_toBottomOf="parent"
@@ -70,7 +71,7 @@
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
- android:id="@+id/loginPhoneNumberInput"
+ android:id="@+id/loginPhoneNumberInputSIM1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
@@ -78,23 +79,20 @@
</com.google.android.material.textfield.TextInputLayout>
-
<com.google.android.material.textfield.TextInputLayout
- android:id="@+id/loginServerUrlLayout"
+ android:id="@+id/loginPhoneNumberLayoutSIM2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:layout_marginTop="-24dp"
- android:hint="@string/server_url"
+ app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM1"
+ android:layout_marginBottom="224dp"
+ android:hint="@string/login_phone_number_sim2"
app:errorEnabled="true"
- app:placeholderText="@string/login_server_url_hint"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayout"
- app:layout_constraintVertical_bias="0.137"
- tools:layout_editor_absoluteX="16dp">
+ app:placeholderText="@string/login_phone_number_hint"
+ app:layout_constraintVertical_bias="0.137">
<com.google.android.material.textfield.TextInputEditText
- android:id="@+id/loginServerUrlInput"
+ android:id="@+id/loginPhoneNumberInputSIM2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
@@ -102,6 +100,33 @@
</com.google.android.material.textfield.TextInputLayout>
+ <LinearLayout
+ android:id="@+id/loginServerUrlLayoutContainer"
+ android:orientation="vertical"
+ app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM2"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content">
+ <com.google.android.material.textfield.TextInputLayout
+ android:id="@+id/loginServerUrlLayout"
+ style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:hint="@string/server_url"
+ app:errorEnabled="true"
+ app:placeholderText="@string/login_server_url_hint"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintVertical_bias="0.137">
+
+ <com.google.android.material.textfield.TextInputEditText
+ android:id="@+id/loginServerUrlInput"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:inputType="textMultiLine"
+ tools:ignore="TextContrastCheck" />
+
+ </com.google.android.material.textfield.TextInputLayout>
+ </LinearLayout>
+
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="wrap_content"
@@ -110,7 +135,7 @@
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/loginServerUrlLayout">
+ app:layout_constraintTop_toBottomOf="@+id/loginServerUrlLayoutContainer">
<com.google.android.material.button.MaterialButton
android:id="@+id/loginButton"
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index e613a001..98ff3e67 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -9,7 +9,7 @@
<string name="img_http_sms_logo">HTTP Sms Logo</string>
<string name="get_your_api_key">Open\nhttpsms.com/settings\nto get your API key</string>
<string name="main_log_out">Log Out</string>
- <string name="login_phone_number_hint">International phone number in E.164 format</string>
+ <string name="login_phone_number_hint">e.g +18005550199 (international format)</string>
<string name="login_server_url_hint">e.g https://api.httpsms.com</string>
<string name="phone_number">Phone Number</string>
<string name="send_heartbeat">Send Heartbeat</string>
@@ -22,4 +22,6 @@
<string name="settings_sim_2">SIM2</string>
<string name="settings_incoming_messages_sim1">Incoming Messages (SIM1)</string>
<string name="settings_incoming_messages_sim2">Incoming Messages (SIM2)</string>
+ <string name="login_phone_number_sim1">Phone Number (SIM1)</string>
+ <string name="login_phone_number_sim2">Phone Number (SIM2)</string>
</resources>