Skip to content

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

Add button to enable SMS permission

Acho Arnold52a6b113f85c
diff --git a/android/app/src/main/java/com/httpsms/MainActivity.kt b/android/app/src/main/java/com/httpsms/MainActivity.kt
index 94332f6f..363e7c19 100644
--- a/android/app/src/main/java/com/httpsms/MainActivity.kt
+++ b/android/app/src/main/java/com/httpsms/MainActivity.kt
@@ -6,6 +6,7 @@ import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
+import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
@@ -59,6 +60,7 @@ class MainActivity : AppCompatActivity() {
scheduleHeartbeatWorker(this)
setVersion()
setHeartbeatListener(this)
+ setSmsPermissionListener()
setBatteryOptimizationListener()
}
@@ -73,6 +75,7 @@ class MainActivity : AppCompatActivity() {
redirectToLogin()
refreshToken(this)
setCardContent(this)
+ setSmsPermissionListener()
setBatteryOptimizationListener()
}
@@ -113,6 +116,7 @@ class MainActivity : AppCompatActivity() {
Settings.setIncomingCallEventsEnabled(context, Constants.SIM2, false)
}
}
+ setSmsPermissionListener()
}
var permissions = arrayOf(
@@ -282,8 +286,9 @@ class MainActivity : AppCompatActivity() {
@SuppressLint("BatteryLife")
private fun setBatteryOptimizationListener() {
val pm = getSystemService(POWER_SERVICE) as PowerManager
+ val button = findViewById<MaterialButton>(R.id.batteryOptimizationButtonButton)
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
- val button = findViewById<MaterialButton>(R.id.batteryOptimizationButtonButton)
+ button.visibility = View.VISIBLE
button.setOnClickListener {
val intent = Intent()
intent.action = ProviderSettings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
@@ -291,8 +296,43 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}
} else {
- val layout = findViewById<LinearLayout>(R.id.batteryOptimizationLinearLayout)
+ button.visibility = View.GONE
+ }
+ updatePermissionLayoutVisibility()
+ }
+
+ private fun setSmsPermissionListener() {
+ val smsPermissions = arrayOf(
+ Manifest.permission.SEND_SMS,
+ Manifest.permission.RECEIVE_SMS,
+ Manifest.permission.READ_SMS
+ )
+ val allGranted = smsPermissions.all {
+ checkSelfPermission(it) == PackageManager.PERMISSION_GRANTED
+ }
+
+ val button = findViewById<MaterialButton>(R.id.smsPermissionButton)
+ if (!allGranted) {
+ button.visibility = View.VISIBLE
+ button.setOnClickListener {
+ val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://httpsms.com/blog/grant-send-and-read-sms-permissions-on-android"))
+ startActivity(intent)
+ }
+ } else {
+ button.visibility = View.GONE
+ }
+ updatePermissionLayoutVisibility()
+ }
+
+ private fun updatePermissionLayoutVisibility() {
+ val smsButton = findViewById<MaterialButton>(R.id.smsPermissionButton)
+ val batteryButton = findViewById<MaterialButton>(R.id.batteryOptimizationButtonButton)
+ val layout = findViewById<LinearLayout>(R.id.batteryOptimizationLinearLayout)
+
+ if (smsButton.visibility == View.GONE && batteryButton.visibility == View.GONE) {
layout.visibility = View.GONE
+ } else {
+ layout.visibility = View.VISIBLE
}
}
diff --git a/android/app/src/main/res/drawable/open_in_new_24.xml b/android/app/src/main/res/drawable/open_in_new_24.xml
new file mode 100644
index 00000000..b257c344
--- /dev/null
+++ b/android/app/src/main/res/drawable/open_in_new_24.xml
@@ -0,0 +1,9 @@
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp"
+ android:height="24dp"
+ android:viewportWidth="24"
+ android:viewportHeight="24">
+ <path
+ android:fillColor="@android:color/white"
+ 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"/>
+</vector>
diff --git a/android/app/src/main/res/layout/activity_login.xml b/android/app/src/main/res/layout/activity_login.xml
index cfd86db6..25a19468 100644
--- a/android/app/src/main/res/layout/activity_login.xml
+++ b/android/app/src/main/res/layout/activity_login.xml
@@ -4,8 +4,9 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
- android:paddingLeft="16dp"
- android:paddingRight="16dp">
+ android:fitsSystemWindows="true"
+ android:paddingStart="16dp"
+ android:paddingEnd="16dp">
<ImageView
android:id="@+id/imageView"
@@ -22,7 +23,7 @@
<TextView
android:id="@+id/textView"
- android:layout_width="wrap_content"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:autoLink="web"
@@ -32,14 +33,13 @@
android:textSize="20sp"
app:layout_constraintBottom_toTopOf="@+id/loginApiKeyTextInputLayout"
app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/loginApiKeyTextInputLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/text_area_api_key"
app:errorEnabled="true"
@@ -47,8 +47,9 @@
app:endIconDrawable="@android:drawable/ic_menu_camera"
app:endIconContentDescription="cameraButton"
app:layout_constraintBottom_toTopOf="@+id/loginPhoneNumberLayoutSIM1"
- app:layout_constraintTop_toBottomOf="@+id/textView"
- tools:layout_editor_absoluteX="16dp">
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/textView">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/loginApiKeyTextInput"
@@ -63,17 +64,15 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/loginPhoneNumberLayoutSIM1"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
- android:layout_marginTop="-40dp"
- android:layout_marginBottom="224dp"
+ android:layout_marginTop="16dp"
android:hint="@string/login_phone_number_sim1"
app:errorEnabled="true"
app:placeholderText="@string/login_phone_number_hint"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/loginApiKeyTextInputLayout"
- app:layout_constraintVertical_bias="0.137"
- tools:layout_editor_absoluteX="16dp">
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/loginApiKeyTextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/loginPhoneNumberInputSIM1"
@@ -88,14 +87,15 @@
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/loginPhoneNumberLayoutSIM2"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
- app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM1"
- android:layout_marginBottom="224dp"
+ android:layout_marginTop="16dp"
android:hint="@string/login_phone_number_sim2"
app:errorEnabled="true"
app:placeholderText="@string/login_phone_number_hint"
- app:layout_constraintVertical_bias="0.137">
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM1">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/loginPhoneNumberInputSIM2"
@@ -110,9 +110,12 @@
<LinearLayout
android:id="@+id/loginServerUrlLayoutContainer"
android:orientation="vertical"
- app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
+ android:layout_width="0dp"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="16dp"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/loginPhoneNumberLayoutSIM2">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/loginServerUrlLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
@@ -120,9 +123,7 @@
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">
+ app:placeholderText="@string/login_server_url_hint">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/loginServerUrlInput"
@@ -137,9 +138,11 @@
<LinearLayout
android:id="@+id/linearLayout"
- android:layout_width="wrap_content"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
+ android:layout_marginTop="16dp"
android:orientation="vertical"
+ android:gravity="center"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/loginServerUrlLayoutContainer">
@@ -166,9 +169,7 @@
android:layout_marginTop="4dp"
android:indeterminate="true"
android:visibility="invisible"
- app:indicatorColor="@color/pink_500"
- app:layout_constraintTop_toBottomOf="@+id/loginButton"
- tools:layout_editor_absoluteX="16dp" />
+ app:indicatorColor="@color/pink_500" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
diff --git a/android/app/src/main/res/layout/activity_main.xml b/android/app/src/main/res/layout/activity_main.xml
index d04b9150..75849475 100644
--- a/android/app/src/main/res/layout/activity_main.xml
+++ b/android/app/src/main/res/layout/activity_main.xml
@@ -3,9 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
- android:paddingLeft="16dp"
- android:paddingRight="16dp"
android:layout_height="match_parent"
+ android:fitsSystemWindows="true"
tools:context=".MainActivity">
<ImageView
@@ -20,22 +19,14 @@
app:srcCompat="@drawable/logo_cropped"
tools:ignore="ImageContrastCheck" />
- <TextView
- android:id="@+id/mainAppVersion"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="16dp"
- android:text="@string/app_version"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
-
<com.google.android.material.card.MaterialCardView
android:id="@+id/mainPhoneCard"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="16dp"
android:layout_marginTop="24dp"
+ android:layout_marginStart="16dp"
+ android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView">
@@ -46,8 +37,6 @@
android:orientation="vertical"
android:padding="16dp">
- <!-- Title, secondary and supporting text -->
-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -66,7 +55,6 @@
android:textSize="28sp" />
</LinearLayout>
-
<TextView
android:id="@+id/cardRefreshTime"
android:layout_width="wrap_content"
@@ -82,10 +70,12 @@
<com.google.android.material.card.MaterialCardView
android:id="@+id/mainPhoneCardSIM2"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:elevation="16dp"
android:layout_marginTop="24dp"
+ android:layout_marginStart="16dp"
+ android:layout_marginEnd="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mainPhoneCard">
@@ -96,8 +86,6 @@
android:orientation="vertical"
android:padding="16dp">
- <!-- Title, secondary and supporting text -->
-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
@@ -117,7 +105,6 @@
</LinearLayout>
-
<TextView
android:id="@+id/cardRefreshTimeSIM2"
android:layout_width="wrap_content"
@@ -133,15 +120,34 @@
<LinearLayout
android:id="@+id/batteryOptimizationLinearLayout"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
- android:layout_weight="50"
+ android:layout_marginStart="16dp"
+ android:layout_marginEnd="16dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/mainPhoneCardSIM2">
+ <com.google.android.material.button.MaterialButton
+ android:id="@+id/smsPermissionButton"
+ style="@style/Widget.MaterialComponents.Button.Icon"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:layout_marginBottom="8dp"
+ android:backgroundTint="#4CAF50"
+ android:padding="10dp"
+ android:text="@string/enable_sms_permission"
+ android:textColor="@color/white"
+ android:textSize="16sp"
+ android:visibility="gone"
+ app:icon="@drawable/open_in_new_24"
+ app:iconGravity="textEnd"
+ app:iconPadding="8dp"
+ app:iconTint="@color/white"
+ tools:ignore="TextContrastCheck" />
+
<com.google.android.material.button.MaterialButton
android:id="@+id/batteryOptimizationButtonButton"
style="@style/Widget.MaterialComponents.Button.Icon"
@@ -160,10 +166,11 @@
<LinearLayout
android:id="@+id/linearLayout"
- android:layout_width="match_parent"
+ android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
- android:layout_weight="50"
+ android:layout_marginStart="16dp"
+ android:layout_marginEnd="16dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
@@ -190,11 +197,19 @@
android:layout_marginTop="4dp"
android:indeterminate="true"
android:visibility="invisible"
- app:indicatorColor="@color/pink_500"
- app:layout_constraintTop_toBottomOf="@+id/mainHeartbeatButton"
- tools:layout_editor_absoluteX="16dp" />
+ app:indicatorColor="@color/pink_500" />
</LinearLayout>
+ <TextView
+ android:id="@+id/mainAppVersion"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginTop="16dp"
+ android:text="@string/app_version"
+ app:layout_constraintEnd_toEndOf="parent"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
+
<com.google.android.material.button.MaterialButton
android:id="@+id/mainSettingsButton"
style="@style/Widget.MaterialComponents.Button.Icon"
diff --git a/android/app/src/main/res/layout/activity_settings.xml b/android/app/src/main/res/layout/activity_settings.xml
index c4e03daf..bfc078ca 100644
--- a/android/app/src/main/res/layout/activity_settings.xml
+++ b/android/app/src/main/res/layout/activity_settings.xml
@@ -3,7 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
- android:layout_height="match_parent">
+ android:layout_height="match_parent"
+ android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/settings_app_bar_layout"
@@ -17,12 +18,8 @@
style="@style/Widget.MaterialComponents.Toolbar.Primary"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
- android:layout_marginBottom="676dp"
android:background="@color/black"
app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintHorizontal_bias="0.0"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
app:navigationIcon="?attr/homeAsUpIndicator"
app:title="App Settings" />
</com.google.android.material.appbar.AppBarLayout>
@@ -30,8 +27,10 @@
<ScrollView
android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:fillViewport="true">
+ android:layout_height="0dp"
+ android:fillViewport="true"
+ app:layout_constraintBottom_toBottomOf="parent"
+ app:layout_constraintTop_toBottomOf="@+id/settings_app_bar_layout">
<LinearLayout
android:id="@+id/linearLayout2"
@@ -40,8 +39,7 @@
android:layout_marginTop="16dp"
android:orientation="vertical"
android:paddingLeft="16dp"
- android:paddingRight="16dp"
- app:layout_constraintTop_toBottomOf="@+id/settings_app_bar_layout">
+ android:paddingRight="16dp">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/settingsSIM1"
@@ -50,8 +48,7 @@
android:layout_height="wrap_content"
android:hint="@string/settings_sim1"
app:errorEnabled="true"
- android:layout_marginTop="48dp"
- app:layout_constraintTop_toTopOf="parent"
+ android:layout_marginTop="16dp"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
@@ -101,7 +98,6 @@
android:layout_height="wrap_content"
android:hint="@string/settings_sim_2"
app:errorEnabled="true"
- app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
@@ -150,7 +146,6 @@
android:layout_height="wrap_content"
android:hint="@string/encryption_key"
app:errorEnabled="true"
- app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="16dp">
<com.google.android.material.textfield.TextInputEditText
diff --git a/android/app/src/main/res/values-night/themes.xml b/android/app/src/main/res/values-night/themes.xml
index 8e80d527..dd456a18 100644
--- a/android/app/src/main/res/values-night/themes.xml
+++ b/android/app/src/main/res/values-night/themes.xml
@@ -12,6 +12,6 @@
<!-- Status bar color. -->
<item name="android:statusBarColor">#121212</item>
<!-- Customize your theme here. -->
- <item name="android:windowOptOutEdgeToEdgeEnforcement" tools:targetApi="35">true</item>
+ <item name="android:windowOptOutEdgeToEdgeEnforcement" tools:targetApi="35">false</item>
</style>
</resources>
diff --git a/android/app/src/main/res/values/strings.xml b/android/app/src/main/res/values/strings.xml
index 24e9609d..81a0f4f8 100644
--- a/android/app/src/main/res/values/strings.xml
+++ b/android/app/src/main/res/values/strings.xml
@@ -17,6 +17,7 @@
<string name="default_server_url">https://api.httpsms.com</string>
<string name="app_version">httpsms.com - %s</string>
<string name="disable_battery_optimization">Disable Battery Optimization</string>
+ <string name="enable_sms_permission">Enable SMS Permission</string>
<string name="main_app_settings">App Settings</string>
<string name="settings_sim1">SIM1</string>
<string name="settings_sim_2">SIM2</string>
diff --git a/android/app/src/main/res/values/themes.xml b/android/app/src/main/res/values/themes.xml
index 538ca49c..5914accc 100644
--- a/android/app/src/main/res/values/themes.xml
+++ b/android/app/src/main/res/values/themes.xml
@@ -13,7 +13,7 @@
<item name="android:statusBarColor">#121212</item>
<!-- Customize your theme here. -->
- <item name="android:windowOptOutEdgeToEdgeEnforcement" tools:targetApi="35">true</item>
+ <item name="android:windowOptOutEdgeToEdgeEnforcement" tools:targetApi="35">false</item>
</style>
</resources>