Skip to content

Commit

Permalink
[messages] add sim rotation feature
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Dec 9, 2024
1 parent b589f5b commit 3ad4f7a
Show file tree
Hide file tree
Showing 17 changed files with 152 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ interface MessagesDao {
fun selectLast(): LiveData<List<Message>>

@Transaction
@Query("SELECT * FROM message WHERE state = 'Pending' ORDER BY createdAt DESC LIMIT 100")
@Query("SELECT *, `rowid` FROM message WHERE state = 'Pending' ORDER BY createdAt DESC LIMIT 100")
fun selectPending(): List<MessageWithRecipients>

@Transaction
@Query("SELECT * FROM message WHERE id = :id")
@Query("SELECT *, `rowid` FROM message WHERE id = :id")
fun get(id: String): MessageWithRecipients?

@Insert
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.capcom.smsgateway.data.entities

import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Relation

Expand All @@ -14,7 +15,9 @@ data class MessageWithRecipients(
parentColumn = "id",
entityColumn = "messageId",
)
val states: List<MessageState> = emptyList()
val states: List<MessageState> = emptyList(),
@ColumnInfo(name = "rowid")
val rowId: Long = 0,
) {
val state: me.capcom.smsgateway.domain.ProcessingState
get() = when {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.capcom.smsgateway.helpers

import android.Manifest
import android.annotation.SuppressLint
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
Expand All @@ -15,12 +16,23 @@ object SubscriptionsHelper {
else -> context.getSystemService(SubscriptionManager::class.java)
}

@SuppressLint("MissingPermission")
fun selectAvailableSimSlots(context: Context): Set<Int>? {
if (!hasPhoneStatePermission(context)) {
return null
}

val subscriptionManager = getSubscriptionsManager(context) ?: return null
return when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1 -> subscriptionManager.activeSubscriptionInfoList.map { it.simSlotIndex }
.toSet()
else -> null
}
}

@SuppressLint("MissingPermission")
fun getSimSlotIndex(context: Context, subscriptionId: Int): Int? {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.READ_PHONE_STATE
) != PackageManager.PERMISSION_GRANTED
) {
if (!hasPhoneStatePermission(context)) {
return null
}

Expand All @@ -33,4 +45,11 @@ object SubscriptionsHelper {
null
}
}

fun hasPhoneStatePermission(context: Context): Boolean {
return ActivityCompat.checkSelfPermission(
context,
Manifest.permission.READ_PHONE_STATE
) == PackageManager.PERMISSION_GRANTED
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,28 @@ class MessagesService(
)
}

private fun selectSimNumber(request: MessageWithRecipients): Int? {
if (request.message.simNumber != null) {
return request.message.simNumber - 1
}

val simSlots = SubscriptionsHelper.selectAvailableSimSlots(context)?.sorted() ?: return null
if (simSlots.isEmpty()) {
throw RuntimeException("No SIMs found")
}

return when (settings.simSelectionMode) {
MessagesSettings.SimSelectionMode.OSDefault -> null
MessagesSettings.SimSelectionMode.RoundRobin -> simSlots[(request.rowId % simSlots.size).toInt()]
MessagesSettings.SimSelectionMode.Random -> simSlots.random()
}
}

private suspend fun sendSMS(request: MessageWithRecipients) {
val message = request.message
val id = message.id

val smsManager: SmsManager = getSmsManager(message.simNumber?.let { it - 1 })
val smsManager: SmsManager = getSmsManager(selectSimNumber(request))

@Suppress("NAME_SHADOWING")
val messageText = when (message.isEncrypted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class MessagesSettings(
PerDay(86400000L),
}

enum class SimSelectionMode {
OSDefault,
RoundRobin,
Random
}

private var version: Int
get() = storage.get<Int>(VERSION) ?: 0
set(value) = storage.set(VERSION, value)
Expand All @@ -39,6 +45,9 @@ class MessagesSettings(
val limitValue: Int
get() = storage.get(LIMIT_VALUE) ?: 0

val simSelectionMode: SimSelectionMode
get() = storage.get<SimSelectionMode>(SIM_SELECTION_MODE) ?: SimSelectionMode.OSDefault

val logLifetimeDays: Int?
get() = storage.get<Int?>(LOG_LIFETIME_DAYS)?.takeIf { it > 0 }

Expand Down Expand Up @@ -70,6 +79,8 @@ class MessagesSettings(
private const val LIMIT_PERIOD = "limit_period"
private const val LIMIT_VALUE = "limit_value"

private const val SIM_SELECTION_MODE = "sim_selection_mode"

private const val LOG_LIFETIME_DAYS = "log_lifetime_days"
}
}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable-notnight/ic_cloud_server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/black" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96z"/>

</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable-notnight/ic_local_server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<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/black"
android:pathData="M17,1.01L7,1c-1.1,0 -2,0.9 -2,2v18c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2V3c0,-1.1 -0.9,-1.99 -2,-1.99zM17,19H7V5h10v14z" />

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable-notnight/ic_messages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/black" android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM18,14L6,14v-2h12v2zM18,11L6,11L6,9h12v2zM18,8L6,8L6,6h12v2z"/>

</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable-notnight/ic_sim_selection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<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/black"
android:pathData="M19.99,4c0,-1.1 -0.89,-2 -1.99,-2h-8L4,8v12c0,1.1 0.9,2 2,2h12.01c1.1,0 1.99,-0.9 1.99,-2l-0.01,-16zM9,19L7,19v-2h2v2zM17,19h-2v-2h2v2zM9,15L7,15v-4h2v4zM13,19h-2v-4h2v4zM13,13h-2v-2h2v2zM17,15h-2v-4h2v4z" />

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_cloud_server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96z"/>

</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_local_server.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<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="M17,1.01L7,1c-1.1,0 -2,0.9 -2,2v18c0,1.1 0.9,2 2,2h10c1.1,0 2,-0.9 2,-2V3c0,-1.1 -0.9,-1.99 -2,-1.99zM17,19H7V5h10v14z" />

</vector>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_messages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillColor="@android:color/white" android:pathData="M20,2L4,2c-1.1,0 -1.99,0.9 -1.99,2L2,22l4,-4h14c1.1,0 2,-0.9 2,-2L22,4c0,-1.1 -0.9,-2 -2,-2zM18,14L6,14v-2h12v2zM18,11L6,11L6,9h12v2zM18,8L6,8L6,6h12v2z"/>

</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_sim_selection.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<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.99,4c0,-1.1 -0.89,-2 -1.99,-2h-8L4,8v12c0,1.1 0.9,2 2,2h12.01c1.1,0 1.99,-0.9 1.99,-2l-0.01,-16zM9,19L7,19v-2h2v2zM17,19h-2v-2h2v2zM9,15L7,15v-4h2v4zM13,19h-2v-4h2v4zM13,13h-2v-2h2v2zM17,15h-2v-4h2v4z" />

</vector>
10 changes: 10 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@
<item>PerHour</item>
<item>PerDay</item>
</string-array>
<string-array name="sim_selection_modes_titles">
<item>OS default</item>
<item>Round robin</item>
<item>Random</item>
</string-array>
<string-array name="sim_selection_modes_values">
<item>OSDefault</item>
<item>RoundRobin</item>
<item>Random</item>
</string-array>
</resources>
4 changes: 3 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<string name="cloud_server">Cloud server</string>
<string name="cloud_server_dotdotdot">Cloud server…</string>
<string name="copied_to_clipboard">Copied</string>
<string name="delays_limits_etc">Delays, Limits, etc.</string>
<string name="delays_limits_etc">Delays, limits, etc.</string>
<string name="delays_seconds">Delays, seconds</string>
<string name="delete_after_days">Delete after, days</string>
<string name="disable_battery_optimizations">Disable battery optimizations</string>
Expand Down Expand Up @@ -81,4 +81,6 @@
<string name="password_must_be_at_least_14_characters">Password must be at least 14 characters</string>
<string name="password_changed_successfully">Password changed successfully</string>
<string name="failed_to_change_password">Failed to change password: %1$s</string>
<string name="messages">Messages…</string>
<string name="if_sim_number_is_not_specified_use">If SIM number is not specified, use</string>
</resources>
10 changes: 8 additions & 2 deletions app/src/main/res/xml/messages_preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,19 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceCategory app:title="@string/messages_header">
<DropDownPreference
android:icon="@drawable/ic_sim_selection"
android:defaultValue="OSDefault"
android:entries="@array/sim_selection_modes_titles"
android:entryValues="@array/sim_selection_modes_values"
android:key="messages.limit_period"
app:title="@string/if_sim_number_is_not_specified_use"
app:useSimpleSummaryProvider="true" />
<EditTextPreference
app:icon="@drawable/ic_log_lifetime"
app:key="messages.log_lifetime_days"
app:useSimpleSummaryProvider="true"
app:title="@string/delete_after_days" />


</PreferenceCategory>

<PreferenceCategory
Expand Down
15 changes: 8 additions & 7 deletions app/src/main/res/xml/root_preferences.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<Preference
android:icon="@drawable/ic_local_server"
app:fragment="me.capcom.smsgateway.ui.settings.LocalServerSettingsFragment"
app:summary="@string/port_credentials_etc"
app:title="@string/local_server_dotdotdot" />
<Preference
android:icon="@drawable/ic_cloud_server"
app:fragment="me.capcom.smsgateway.ui.settings.CloudServerSettingsFragment"
app:summary="@string/api_url_private_token_credentials_etc"
app:title="@string/cloud_server_dotdotdot" />

<Preference
android:icon="@drawable/ic_messages"
app:fragment="me.capcom.smsgateway.ui.settings.MessagesSettingsFragment"
app:title="@string/messages"
app:summary="@string/delays_limits_etc"/>

<PreferenceCategory app:title="@string/encryption">
<EditTextPreference
app:icon="@drawable/ic_encryption"
Expand All @@ -17,13 +25,6 @@
app:title="@string/passphrase" />
</PreferenceCategory>

<PreferenceCategory app:title="@string/messages_header">
<Preference
app:fragment="me.capcom.smsgateway.ui.settings.MessagesSettingsFragment"
app:title="@string/more_settings"
app:summary="@string/delays_limits_etc"/>
</PreferenceCategory>

<PreferenceCategory app:title="@string/webhooks">
<SwitchPreference
android:defaultValue="true"
Expand Down

0 comments on commit 3ad4f7a

Please sign in to comment.