Skip to content

Commit 52a6b11

Browse files
committed
Add button to enable SMS permission
1 parent ec1da99 commit 52a6b11

File tree

8 files changed

+133
-72
lines changed

8 files changed

+133
-72
lines changed

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.app.NotificationChannel
66
import android.app.NotificationManager
77
import android.content.Context
88
import android.content.Intent
9+
import android.content.pm.PackageManager
910
import android.net.Uri
1011
import android.os.Build
1112
import android.os.Bundle
@@ -59,6 +60,7 @@ class MainActivity : AppCompatActivity() {
5960
scheduleHeartbeatWorker(this)
6061
setVersion()
6162
setHeartbeatListener(this)
63+
setSmsPermissionListener()
6264
setBatteryOptimizationListener()
6365
}
6466

@@ -73,6 +75,7 @@ class MainActivity : AppCompatActivity() {
7375
redirectToLogin()
7476
refreshToken(this)
7577
setCardContent(this)
78+
setSmsPermissionListener()
7679
setBatteryOptimizationListener()
7780
}
7881

@@ -113,6 +116,7 @@ class MainActivity : AppCompatActivity() {
113116
Settings.setIncomingCallEventsEnabled(context, Constants.SIM2, false)
114117
}
115118
}
119+
setSmsPermissionListener()
116120
}
117121

118122
var permissions = arrayOf(
@@ -282,17 +286,53 @@ class MainActivity : AppCompatActivity() {
282286
@SuppressLint("BatteryLife")
283287
private fun setBatteryOptimizationListener() {
284288
val pm = getSystemService(POWER_SERVICE) as PowerManager
289+
val button = findViewById<MaterialButton>(R.id.batteryOptimizationButtonButton)
285290
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
286-
val button = findViewById<MaterialButton>(R.id.batteryOptimizationButtonButton)
291+
button.visibility = View.VISIBLE
287292
button.setOnClickListener {
288293
val intent = Intent()
289294
intent.action = ProviderSettings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
290295
intent.data = Uri.parse("package:$packageName")
291296
startActivity(intent)
292297
}
293298
} else {
294-
val layout = findViewById<LinearLayout>(R.id.batteryOptimizationLinearLayout)
299+
button.visibility = View.GONE
300+
}
301+
updatePermissionLayoutVisibility()
302+
}
303+
304+
private fun setSmsPermissionListener() {
305+
val smsPermissions = arrayOf(
306+
Manifest.permission.SEND_SMS,
307+
Manifest.permission.RECEIVE_SMS,
308+
Manifest.permission.READ_SMS
309+
)
310+
val allGranted = smsPermissions.all {
311+
checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED
312+
}
313+
314+
val button = findViewById<MaterialButton>(R.id.smsPermissionButton)
315+
if (!allGranted) {
316+
button.visibility = View.VISIBLE
317+
button.setOnClickListener {
318+
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://httpsms.com/blog/grant-send-and-read-sms-permissions-on-android"))
319+
startActivity(intent)
320+
}
321+
} else {
322+
button.visibility = View.GONE
323+
}
324+
updatePermissionLayoutVisibility()
325+
}
326+
327+
private fun updatePermissionLayoutVisibility() {
328+
val smsButton = findViewById<MaterialButton>(R.id.smsPermissionButton)
329+
val batteryButton = findViewById<MaterialButton>(R.id.batteryOptimizationButtonButton)
330+
val layout = findViewById<LinearLayout>(R.id.batteryOptimizationLinearLayout)
331+
332+
if (smsButton.visibility == View.GONE && batteryButton.visibility == View.GONE) {
295333
layout.visibility = View.GONE
334+
} else {
335+
layout.visibility = View.VISIBLE
296336
}
297337
}
298338

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
2+
android:width="24dp"
3+
android:height="24dp"
4+
android:viewportWidth="24"
5+
android:viewportHeight="24">
6+
<path
7+
android:fillColor="@android:color/white"
8+
android:pathData="M19,19H5V5h7V3H5c-1.11,0 -2,0.9 -2,2v14c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2v-7h-2v7zM14,3v2h3.59l-9.83,9.83 1.41,1.41L19,6.41V10h2V3h-7z"/>
9+
</vector>

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
7-
android:paddingLeft="16dp"
8-
android:paddingRight="16dp">
7+
android:fitsSystemWindows="true"
8+
android:paddingStart="16dp"
9+
android:paddingEnd="16dp">
910

1011
<ImageView
1112
android:id="@+id/imageView"
@@ -22,7 +23,7 @@
2223

2324
<TextView
2425
android:id="@+id/textView"
25-
android:layout_width="wrap_content"
26+
android:layout_width="0dp"
2627
android:layout_height="wrap_content"
2728
android:layout_marginBottom="20dp"
2829
android:autoLink="web"
@@ -32,23 +33,23 @@
3233
android:textSize="20sp"
3334
app:layout_constraintBottom_toTopOf="@+id/loginApiKeyTextInputLayout"
3435
app:layout_constraintEnd_toEndOf="parent"
35-
app:layout_constraintHorizontal_bias="0.498"
3636
app:layout_constraintStart_toStartOf="parent"
3737
app:layout_constraintTop_toBottomOf="@+id/imageView" />
3838

3939
<com.google.android.material.textfield.TextInputLayout
4040
android:id="@+id/loginApiKeyTextInputLayout"
4141
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
42-
android:layout_width="match_parent"
42+
android:layout_width="0dp"
4343
android:layout_height="wrap_content"
4444
android:hint="@string/text_area_api_key"
4545
app:errorEnabled="true"
4646
app:endIconMode="custom"
4747
app:endIconDrawable="@android:drawable/ic_menu_camera"
4848
app:endIconContentDescription="cameraButton"
4949
app:layout_constraintBottom_toTopOf="@+id/loginPhoneNumberLayoutSIM1"
50-
app:layout_constraintTop_toBottomOf="@+id/textView"
51-
tools:layout_editor_absoluteX="16dp">
50+
app:layout_constraintEnd_toEndOf="parent"
51+
app:layout_constraintStart_toStartOf="parent"
52+
app:layout_constraintTop_toBottomOf="@+id/textView">
5253

5354
<com.google.android.material.textfield.TextInputEditText
5455
android:id="@+id/loginApiKeyTextInput"
@@ -63,17 +64,15 @@
6364
<com.google.android.material.textfield.TextInputLayout
6465
android:id="@+id/loginPhoneNumberLayoutSIM1"
6566
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
66-
android:layout_width="match_parent"
67+
android:layout_width="0dp"
6768
android:layout_height="wrap_content"
68-
android:layout_marginTop="-40dp"
69-
android:layout_marginBottom="224dp"
69+
android:layout_marginTop="16dp"
7070
android:hint="@string/login_phone_number_sim1"
7171
app:errorEnabled="true"
7272
app:placeholderText="@string/login_phone_number_hint"
73-
app:layout_constraintBottom_toBottomOf="parent"
74-
app:layout_constraintTop_toBottomOf="@+id/loginApiKeyTextInputLayout"
75-
app:layout_constraintVertical_bias="0.137"
76-
tools:layout_editor_absoluteX="16dp">
73+
app:layout_constraintEnd_toEndOf="parent"
74+
app:layout_constraintStart_toStartOf="parent"
75+
app:layout_constraintTop_toBottomOf="@+id/loginApiKeyTextInputLayout">
7776

7877
<com.google.android.material.textfield.TextInputEditText
7978
android:id="@+id/loginPhoneNumberInputSIM1"
@@ -88,14 +87,15 @@
8887
<com.google.android.material.textfield.TextInputLayout
8988
android:id="@+id/loginPhoneNumberLayoutSIM2"
9089
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
91-
android:layout_width="match_parent"
90+
android:layout_width="0dp"
9291
android:layout_height="wrap_content"
93-
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM1"
94-
android:layout_marginBottom="224dp"
92+
android:layout_marginTop="16dp"
9593
android:hint="@string/login_phone_number_sim2"
9694
app:errorEnabled="true"
9795
app:placeholderText="@string/login_phone_number_hint"
98-
app:layout_constraintVertical_bias="0.137">
96+
app:layout_constraintEnd_toEndOf="parent"
97+
app:layout_constraintStart_toStartOf="parent"
98+
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM1">
9999

100100
<com.google.android.material.textfield.TextInputEditText
101101
android:id="@+id/loginPhoneNumberInputSIM2"
@@ -110,19 +110,20 @@
110110
<LinearLayout
111111
android:id="@+id/loginServerUrlLayoutContainer"
112112
android:orientation="vertical"
113-
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM2"
114-
android:layout_width="match_parent"
115-
android:layout_height="wrap_content">
113+
android:layout_width="0dp"
114+
android:layout_height="wrap_content"
115+
android:layout_marginTop="16dp"
116+
app:layout_constraintEnd_toEndOf="parent"
117+
app:layout_constraintStart_toStartOf="parent"
118+
app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM2">
116119
<com.google.android.material.textfield.TextInputLayout
117120
android:id="@+id/loginServerUrlLayout"
118121
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
119122
android:layout_width="match_parent"
120123
android:layout_height="wrap_content"
121124
android:hint="@string/server_url"
122125
app:errorEnabled="true"
123-
app:placeholderText="@string/login_server_url_hint"
124-
app:layout_constraintBottom_toBottomOf="parent"
125-
app:layout_constraintVertical_bias="0.137">
126+
app:placeholderText="@string/login_server_url_hint">
126127

127128
<com.google.android.material.textfield.TextInputEditText
128129
android:id="@+id/loginServerUrlInput"
@@ -137,9 +138,11 @@
137138

138139
<LinearLayout
139140
android:id="@+id/linearLayout"
140-
android:layout_width="wrap_content"
141+
android:layout_width="0dp"
141142
android:layout_height="wrap_content"
143+
android:layout_marginTop="16dp"
142144
android:orientation="vertical"
145+
android:gravity="center"
143146
app:layout_constraintEnd_toEndOf="parent"
144147
app:layout_constraintStart_toStartOf="parent"
145148
app:layout_constraintTop_toBottomOf="@+id/loginServerUrlLayoutContainer">
@@ -166,9 +169,7 @@
166169
android:layout_marginTop="4dp"
167170
android:indeterminate="true"
168171
android:visibility="invisible"
169-
app:indicatorColor="@color/pink_500"
170-
app:layout_constraintTop_toBottomOf="@+id/loginButton"
171-
tools:layout_editor_absoluteX="16dp" />
172+
app:indicatorColor="@color/pink_500" />
172173
</LinearLayout>
173174

174175
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:layout_width="match_parent"
6-
android:paddingLeft="16dp"
7-
android:paddingRight="16dp"
86
android:layout_height="match_parent"
7+
android:fitsSystemWindows="true"
98
tools:context=".MainActivity">
109

1110
<ImageView
@@ -20,22 +19,14 @@
2019
app:srcCompat="@drawable/logo_cropped"
2120
tools:ignore="ImageContrastCheck" />
2221

23-
<TextView
24-
android:id="@+id/mainAppVersion"
25-
android:layout_width="wrap_content"
26-
android:layout_height="wrap_content"
27-
android:layout_marginTop="16dp"
28-
android:text="@string/app_version"
29-
app:layout_constraintEnd_toEndOf="parent"
30-
app:layout_constraintStart_toStartOf="parent"
31-
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
32-
3322
<com.google.android.material.card.MaterialCardView
3423
android:id="@+id/mainPhoneCard"
35-
android:layout_width="match_parent"
24+
android:layout_width="0dp"
3625
android:layout_height="wrap_content"
3726
android:elevation="16dp"
3827
android:layout_marginTop="24dp"
28+
android:layout_marginStart="16dp"
29+
android:layout_marginEnd="16dp"
3930
app:layout_constraintEnd_toEndOf="parent"
4031
app:layout_constraintStart_toStartOf="parent"
4132
app:layout_constraintTop_toBottomOf="@+id/imageView">
@@ -46,8 +37,6 @@
4637
android:orientation="vertical"
4738
android:padding="16dp">
4839

49-
<!-- Title, secondary and supporting text -->
50-
5140
<LinearLayout
5241
android:layout_width="match_parent"
5342
android:layout_height="match_parent"
@@ -66,7 +55,6 @@
6655
android:textSize="28sp" />
6756
</LinearLayout>
6857

69-
7058
<TextView
7159
android:id="@+id/cardRefreshTime"
7260
android:layout_width="wrap_content"
@@ -82,10 +70,12 @@
8270

8371
<com.google.android.material.card.MaterialCardView
8472
android:id="@+id/mainPhoneCardSIM2"
85-
android:layout_width="match_parent"
73+
android:layout_width="0dp"
8674
android:layout_height="wrap_content"
8775
android:elevation="16dp"
8876
android:layout_marginTop="24dp"
77+
android:layout_marginStart="16dp"
78+
android:layout_marginEnd="16dp"
8979
app:layout_constraintEnd_toEndOf="parent"
9080
app:layout_constraintStart_toStartOf="parent"
9181
app:layout_constraintTop_toBottomOf="@+id/mainPhoneCard">
@@ -96,8 +86,6 @@
9686
android:orientation="vertical"
9787
android:padding="16dp">
9888

99-
<!-- Title, secondary and supporting text -->
100-
10189
<LinearLayout
10290
android:layout_width="match_parent"
10391
android:layout_height="match_parent"
@@ -117,7 +105,6 @@
117105

118106
</LinearLayout>
119107

120-
121108
<TextView
122109
android:id="@+id/cardRefreshTimeSIM2"
123110
android:layout_width="wrap_content"
@@ -133,15 +120,34 @@
133120

134121
<LinearLayout
135122
android:id="@+id/batteryOptimizationLinearLayout"
136-
android:layout_width="match_parent"
123+
android:layout_width="0dp"
137124
android:layout_height="wrap_content"
138125
android:layout_marginTop="16dp"
139-
android:layout_weight="50"
126+
android:layout_marginStart="16dp"
127+
android:layout_marginEnd="16dp"
140128
android:orientation="vertical"
141129
app:layout_constraintEnd_toEndOf="parent"
142130
app:layout_constraintStart_toStartOf="parent"
143131
app:layout_constraintTop_toBottomOf="@+id/mainPhoneCardSIM2">
144132

133+
<com.google.android.material.button.MaterialButton
134+
android:id="@+id/smsPermissionButton"
135+
style="@style/Widget.MaterialComponents.Button.Icon"
136+
android:layout_width="match_parent"
137+
android:layout_height="wrap_content"
138+
android:layout_marginBottom="8dp"
139+
android:backgroundTint="#4CAF50"
140+
android:padding="10dp"
141+
android:text="@string/enable_sms_permission"
142+
android:textColor="@color/white"
143+
android:textSize="16sp"
144+
android:visibility="gone"
145+
app:icon="@drawable/open_in_new_24"
146+
app:iconGravity="textEnd"
147+
app:iconPadding="8dp"
148+
app:iconTint="@color/white"
149+
tools:ignore="TextContrastCheck" />
150+
145151
<com.google.android.material.button.MaterialButton
146152
android:id="@+id/batteryOptimizationButtonButton"
147153
style="@style/Widget.MaterialComponents.Button.Icon"
@@ -160,10 +166,11 @@
160166

161167
<LinearLayout
162168
android:id="@+id/linearLayout"
163-
android:layout_width="match_parent"
169+
android:layout_width="0dp"
164170
android:layout_height="wrap_content"
165171
android:layout_marginTop="16dp"
166-
android:layout_weight="50"
172+
android:layout_marginStart="16dp"
173+
android:layout_marginEnd="16dp"
167174
android:orientation="vertical"
168175
app:layout_constraintEnd_toEndOf="parent"
169176
app:layout_constraintStart_toStartOf="parent"
@@ -190,11 +197,19 @@
190197
android:layout_marginTop="4dp"
191198
android:indeterminate="true"
192199
android:visibility="invisible"
193-
app:indicatorColor="@color/pink_500"
194-
app:layout_constraintTop_toBottomOf="@+id/mainHeartbeatButton"
195-
tools:layout_editor_absoluteX="16dp" />
200+
app:indicatorColor="@color/pink_500" />
196201
</LinearLayout>
197202

203+
<TextView
204+
android:id="@+id/mainAppVersion"
205+
android:layout_width="wrap_content"
206+
android:layout_height="wrap_content"
207+
android:layout_marginTop="16dp"
208+
android:text="@string/app_version"
209+
app:layout_constraintEnd_toEndOf="parent"
210+
app:layout_constraintStart_toStartOf="parent"
211+
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
212+
198213
<com.google.android.material.button.MaterialButton
199214
android:id="@+id/mainSettingsButton"
200215
style="@style/Widget.MaterialComponents.Button.Icon"

0 commit comments

Comments
 (0)