Skip to content

Commit 2252145

Browse files
committed
Add dual sim support for login page
1 parent 6f1070e commit 2252145

File tree

5 files changed

+131
-22
lines changed

5 files changed

+131
-22
lines changed

android/app/src/main/java/com/httpsms/LoginActivity.kt

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import android.telephony.PhoneNumberUtils
1010
import android.telephony.TelephonyManager
1111
import android.view.View
1212
import android.webkit.URLUtil
13+
import android.widget.LinearLayout
1314
import androidx.appcompat.app.AppCompatActivity
1415
import androidx.core.app.ActivityCompat
1516
import androidx.lifecycle.MutableLiveData
@@ -27,21 +28,31 @@ class LoginActivity : AppCompatActivity() {
2728
setContentView(R.layout.activity_login)
2829
registerListeners()
2930
setPhoneNumber()
31+
disableSim2()
3032
setServerURL()
3133
}
3234

3335
private fun registerListeners() {
3436
loginButton().setOnClickListener { onLoginClick() }
3537
}
3638

39+
private fun disableSim2() {
40+
if (SmsManagerService.isDualSIM(this)) {
41+
Timber.d("dual sim detected")
42+
return
43+
}
44+
val sim2Layout = findViewById<LinearLayout>(R.id.loginPhoneNumberLayoutSIM2)
45+
sim2Layout.visibility = View.GONE
46+
}
47+
3748
private fun setPhoneNumber() {
3849
val phoneNumber = getPhoneNumber(this)
3950
if(phoneNumber == null) {
4051
Timber.d("cannot get phone due to no permissions")
4152
return
4253
}
4354

44-
val phoneInput = findViewById<TextInputEditText>(R.id.loginPhoneNumberInput)
55+
val phoneInput = findViewById<TextInputEditText>(R.id.loginPhoneNumberInputSIM1)
4556
phoneInput.setText(phoneNumber)
4657
Timber.d("phone number [$phoneNumber] set successfully")
4758
}
@@ -96,17 +107,24 @@ class LoginActivity : AppCompatActivity() {
96107
val serverUrl = findViewById<TextInputEditText>(R.id.loginServerUrlInput)
97108
serverUrl.isEnabled = false
98109

99-
val phoneNumberLayout = findViewById<TextInputLayout>(R.id.loginPhoneNumberLayout)
110+
val phoneNumberLayout = findViewById<TextInputLayout>(R.id.loginPhoneNumberLayoutSIM1)
100111
phoneNumberLayout.error = null
101112

102-
val phoneNumber = findViewById<TextInputEditText>(R.id.loginPhoneNumberInput)
113+
val phoneNumber = findViewById<TextInputEditText>(R.id.loginPhoneNumberInputSIM1)
103114
phoneNumber.isEnabled = false
104115

116+
val phoneNumberLayoutSIM2 = findViewById<TextInputLayout>(R.id.loginPhoneNumberLayoutSIM2)
117+
phoneNumberLayoutSIM2.error = null
118+
119+
val phoneNumberSIM2 = findViewById<TextInputEditText>(R.id.loginPhoneNumberInputSIM2)
120+
phoneNumberSIM2.isEnabled = false
121+
105122
val resetView = fun () {
106123
apiKey.isEnabled = true
107124
serverUrl.isEnabled = true
108125
progressBar.visibility = View.INVISIBLE
109126
phoneNumber.isEnabled = true
127+
phoneNumberSIM2.isEnabled = true
110128
loginButton().isEnabled = true
111129
}
112130

@@ -120,6 +138,18 @@ class LoginActivity : AppCompatActivity() {
120138
return
121139
}
122140

141+
if (
142+
SmsManagerService.isDualSIM(this) && (
143+
!PhoneNumberUtils.isWellFormedSmsAddress(phoneNumberSIM2.text.toString()) ||
144+
!PhoneNumberUtils.isGlobalPhoneNumber(phoneNumberSIM2.text.toString())
145+
)
146+
) {
147+
Timber.e("phone number [${phoneNumberSIM2.text.toString()}] is not valid")
148+
resetView()
149+
phoneNumberLayoutSIM2.error = "Invalid E.164 phone number"
150+
return
151+
}
152+
123153
if(!URLUtil.isValidUrl(serverUrl.text.toString().trim())) {
124154
Timber.e("url number [${serverUrl.text.toString()}] is not a valid URL")
125155
resetView()
@@ -157,7 +187,15 @@ class LoginActivity : AppCompatActivity() {
157187
phoneNumber.text.toString(),
158188
this.resources.configuration.locales.get(0).country
159189
)
160-
Settings.setOwnerAsync(this, e164PhoneNumber)
190+
Settings.setSIM1PhoneNumber(this, e164PhoneNumber)
191+
192+
if(SmsManagerService.isDualSIM(this)) {
193+
val sim2PhoneNumber = PhoneNumberUtils.formatNumberToE164(
194+
phoneNumberSIM2.text.toString(),
195+
this.resources.configuration.locales.get(0).country
196+
)
197+
Settings.setSIM2PhoneNumber(this, sim2PhoneNumber)
198+
}
161199

162200
Timber.d("login successfully redirecting to main view")
163201
redirectToMain()

android/app/src/main/java/com/httpsms/Settings.kt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ object Settings {
99
private const val DEFAULT_PHONE_NUMBER = "NOT_FOUND"
1010

1111
private const val SETTINGS_OWNER = "SETTINGS_OWNER"
12+
private const val SETTINGS_SIM1_PHONE_NUMBER = "SETTINGS_SIM1_PHONE_NUMBER"
13+
private const val SETTINGS_SIM2_PHONE_NUMBER = "SETTINGS_SIM2_PHONE_NUMBER"
1214
private const val SETTINGS_ACTIVE = "SETTINGS_ACTIVE_STATUS"
1315
private const val SETTINGS_SIM1_ACTIVE = "SETTINGS_SIM1_ACTIVE_STATUS"
1416
private const val SETTINGS_SIM2_ACTIVE = "SETTINGS_SIM2_ACTIVE_STATUS"
@@ -75,6 +77,24 @@ object Settings {
7577
.apply()
7678
}
7779

80+
fun setSIM1PhoneNumber(context: Context, owner: String?) {
81+
Timber.d(Settings::setOwnerAsync.name)
82+
83+
PreferenceManager.getDefaultSharedPreferences(context)
84+
.edit()
85+
.putString(this.SETTINGS_OWNER, owner)
86+
.apply()
87+
}
88+
89+
fun setSIM2PhoneNumber(context: Context, owner: String?) {
90+
Timber.d(Settings::setOwnerAsync.name)
91+
92+
PreferenceManager.getDefaultSharedPreferences(context)
93+
.edit()
94+
.putString(this.SETTINGS_OWNER, owner)
95+
.apply()
96+
}
97+
7898
fun getActiveStatus(context: Context): Boolean {
7999
val activeStatus = PreferenceManager
80100
.getDefaultSharedPreferences(context)
@@ -97,6 +117,25 @@ object Settings {
97117
return activeStatus
98118
}
99119

120+
fun setIncomingActiveSIM1(context: Context, status: Boolean) {
121+
Timber.d(Settings::setIncomingActiveSIM1.name)
122+
123+
PreferenceManager.getDefaultSharedPreferences(context)
124+
.edit()
125+
.putBoolean(this.SETTINGS_SIM1_INCOMING_ACTIVE, status)
126+
.apply()
127+
}
128+
129+
fun setIncomingActiveSIM2(context: Context, status: Boolean) {
130+
Timber.d(Settings::setIncomingActiveSIM2.name)
131+
132+
PreferenceManager.getDefaultSharedPreferences(context)
133+
.edit()
134+
.putBoolean(this.SETTINGS_SIM2_INCOMING_ACTIVE, status)
135+
.apply()
136+
}
137+
138+
100139
fun getActiveStatus(context: Context, sim: String): Boolean {
101140
var setting = this.SETTINGS_SIM1_ACTIVE
102141
if (sim == Constants.SIM2) {

android/app/src/main/java/com/httpsms/SettingsActivity.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class SettingsActivity : AppCompatActivity() {
4747
Timber.d("logging out user")
4848
Settings.setApiKeyAsync(this, null)
4949
Settings.setOwnerAsync(this, null)
50+
Settings.setSIM1PhoneNumber(this, null)
51+
Settings.setSIM2PhoneNumber(this, null)
52+
Settings.setActiveStatusAsync(this, true)
53+
Settings.setIncomingActiveSIM1(this, true)
54+
Settings.setIncomingActiveSIM2(this, true)
5055
Settings.setFcmTokenLastUpdateTimestampAsync(this, 0)
5156
redirectToLogin()
5257
}

android/app/src/main/res/layout/activity_login.xml

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
<ImageView
1111
android:id="@+id/imageView"
12-
android:layout_width="145dp"
13-
android:layout_height="148dp"
12+
android:layout_width="100dp"
13+
android:layout_height="100dp"
1414
android:layout_marginTop="32dp"
1515
android:contentDescription="@string/img_http_sms_logo"
1616
app:layout_constraintEnd_toEndOf="parent"
@@ -33,7 +33,8 @@
3333
app:layout_constraintBottom_toTopOf="@+id/loginApiKeyTextInputLayout"
3434
app:layout_constraintEnd_toEndOf="parent"
3535
app:layout_constraintHorizontal_bias="0.498"
36-
app:layout_constraintStart_toStartOf="parent" />
36+
app:layout_constraintStart_toStartOf="parent"
37+
app:layout_constraintTop_toBottomOf="@+id/imageView" />
3738

3839
<com.google.android.material.textfield.TextInputLayout
3940
android:id="@+id/loginApiKeyTextInputLayout"
@@ -56,12 +57,12 @@
5657
</com.google.android.material.textfield.TextInputLayout>
5758

5859
<com.google.android.material.textfield.TextInputLayout
59-
android:id="@+id/loginPhoneNumberLayout"
60+
android:id="@+id/loginPhoneNumberLayoutSIM1"
6061
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
6162
android:layout_width="match_parent"
6263
android:layout_height="wrap_content"
6364
android:layout_marginBottom="224dp"
64-
android:hint="@string/phone_number"
65+
android:hint="@string/login_phone_number_sim1"
6566
app:errorEnabled="true"
6667
app:placeholderText="@string/login_phone_number_hint"
6768
app:layout_constraintBottom_toBottomOf="parent"
@@ -70,38 +71,62 @@
7071
tools:layout_editor_absoluteX="16dp">
7172

7273
<com.google.android.material.textfield.TextInputEditText
73-
android:id="@+id/loginPhoneNumberInput"
74+
android:id="@+id/loginPhoneNumberInputSIM1"
7475
android:layout_width="match_parent"
7576
android:layout_height="wrap_content"
7677
android:inputType="textMultiLine"
7778
tools:ignore="TextContrastCheck" />
7879

7980
</com.google.android.material.textfield.TextInputLayout>
8081

81-
8282
<com.google.android.material.textfield.TextInputLayout
83-
android:id="@+id/loginServerUrlLayout"
83+
android:id="@+id/loginPhoneNumberLayoutSIM2"
8484
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
8585
android:layout_width="match_parent"
8686
android:layout_height="wrap_content"
87-
android:layout_marginTop="-24dp"
88-
android:hint="@string/server_url"
87+
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM1"
88+
android:layout_marginBottom="224dp"
89+
android:hint="@string/login_phone_number_sim2"
8990
app:errorEnabled="true"
90-
app:placeholderText="@string/login_server_url_hint"
91-
app:layout_constraintBottom_toBottomOf="parent"
92-
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayout"
93-
app:layout_constraintVertical_bias="0.137"
94-
tools:layout_editor_absoluteX="16dp">
91+
app:placeholderText="@string/login_phone_number_hint"
92+
app:layout_constraintVertical_bias="0.137">
9593

9694
<com.google.android.material.textfield.TextInputEditText
97-
android:id="@+id/loginServerUrlInput"
95+
android:id="@+id/loginPhoneNumberInputSIM2"
9896
android:layout_width="match_parent"
9997
android:layout_height="wrap_content"
10098
android:inputType="textMultiLine"
10199
tools:ignore="TextContrastCheck" />
102100

103101
</com.google.android.material.textfield.TextInputLayout>
104102

103+
<LinearLayout
104+
android:id="@+id/loginServerUrlLayoutContainer"
105+
android:orientation="vertical"
106+
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM2"
107+
android:layout_width="match_parent"
108+
android:layout_height="wrap_content">
109+
<com.google.android.material.textfield.TextInputLayout
110+
android:id="@+id/loginServerUrlLayout"
111+
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
112+
android:layout_width="match_parent"
113+
android:layout_height="wrap_content"
114+
android:hint="@string/server_url"
115+
app:errorEnabled="true"
116+
app:placeholderText="@string/login_server_url_hint"
117+
app:layout_constraintBottom_toBottomOf="parent"
118+
app:layout_constraintVertical_bias="0.137">
119+
120+
<com.google.android.material.textfield.TextInputEditText
121+
android:id="@+id/loginServerUrlInput"
122+
android:layout_width="match_parent"
123+
android:layout_height="wrap_content"
124+
android:inputType="textMultiLine"
125+
tools:ignore="TextContrastCheck" />
126+
127+
</com.google.android.material.textfield.TextInputLayout>
128+
</LinearLayout>
129+
105130
<LinearLayout
106131
android:id="@+id/linearLayout"
107132
android:layout_width="wrap_content"
@@ -110,7 +135,7 @@
110135
android:orientation="vertical"
111136
app:layout_constraintEnd_toEndOf="parent"
112137
app:layout_constraintStart_toStartOf="parent"
113-
app:layout_constraintTop_toBottomOf="@+id/loginServerUrlLayout">
138+
app:layout_constraintTop_toBottomOf="@+id/loginServerUrlLayoutContainer">
114139

115140
<com.google.android.material.button.MaterialButton
116141
android:id="@+id/loginButton"

android/app/src/main/res/values/strings.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<string name="img_http_sms_logo">HTTP Sms Logo</string>
1010
<string name="get_your_api_key">Open\nhttpsms.com/settings\nto get your API key</string>
1111
<string name="main_log_out">Log Out</string>
12-
<string name="login_phone_number_hint">International phone number in E.164 format</string>
12+
<string name="login_phone_number_hint">e.g +18005550199 (international format)</string>
1313
<string name="login_server_url_hint">e.g https://api.httpsms.com</string>
1414
<string name="phone_number">Phone Number</string>
1515
<string name="send_heartbeat">Send Heartbeat</string>
@@ -22,4 +22,6 @@
2222
<string name="settings_sim_2">SIM2</string>
2323
<string name="settings_incoming_messages_sim1">Incoming Messages (SIM1)</string>
2424
<string name="settings_incoming_messages_sim2">Incoming Messages (SIM2)</string>
25+
<string name="login_phone_number_sim1">Phone Number (SIM1)</string>
26+
<string name="login_phone_number_sim2">Phone Number (SIM2)</string>
2527
</resources>

0 commit comments

Comments
 (0)