Skip to content

Commit

Permalink
feat(android): multi sim support
Browse files Browse the repository at this point in the history
  • Loading branch information
vernu committed Mar 3, 2024
1 parent ff63cdc commit 6784546
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 21 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.vernu.sms"
minSdk 24
targetSdk 32
versionCode 7
versionName "2.0"
versionCode 8
versionName "2.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.vernu.sms">

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
Expand All @@ -33,6 +37,9 @@
import com.vernu.sms.dtos.RegisterDeviceResponseDTO;
import com.vernu.sms.helpers.SharedPreferenceHelper;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
Expand All @@ -51,6 +58,8 @@ public class MainActivity extends AppCompatActivity {
private ImageButton copyDeviceIdImgBtn;
private TextView deviceBrandAndModelTxt, deviceIdTxt;

private RadioGroup defaultSimSlotRadioGroup;

private static final int SEND_SMS_PERMISSION_REQUEST_CODE = 0;
private static final int SCAN_QR_REQUEST_CODE = 49374;

Expand Down Expand Up @@ -86,16 +95,29 @@ protected void onCreate(Bundle savedInstanceState) {

copyDeviceIdImgBtn = findViewById(R.id.copyDeviceIdImgBtn);

defaultSimSlotRadioGroup = findViewById(R.id.defaultSimSlotRadioGroup);

getAvailableSimSlots().forEach(subscriptionInfo -> {
RadioButton radioButton = new RadioButton(mContext);
radioButton.setText(subscriptionInfo.getDisplayName().toString());
radioButton.setId(subscriptionInfo.getSubscriptionId());
radioButton.setOnClickListener(view -> {
SharedPreferenceHelper.setSharedPreferenceInt(mContext, "PREFERED_SIM", subscriptionInfo.getSubscriptionId());
});
radioButton.setChecked(subscriptionInfo.getSubscriptionId() == SharedPreferenceHelper.getSharedPreferenceInt(mContext, "PREFERED_SIM", 0));
defaultSimSlotRadioGroup.addView(radioButton);
});

deviceIdTxt.setText(deviceId);
deviceBrandAndModelTxt.setText(Build.BRAND + " " + Build.MODEL);

if(deviceId == null || deviceId.isEmpty()) {
if (deviceId == null || deviceId.isEmpty()) {
registerDeviceBtn.setText("Register");
} else {
registerDeviceBtn.setText("Update");
}

if (isSMSPermissionGranted(mContext)) {
if (isSMSPermissionGranted(mContext) && isReadPhoneStatePermissionGranted(mContext)) {
grantSMSPermissionBtn.setEnabled(false);
grantSMSPermissionBtn.setText("SMS Permission Granted");
} else {
Expand Down Expand Up @@ -157,6 +179,7 @@ public void onFailure(Call<RegisterDeviceResponseDTO> call, Throwable t) {
intentIntegrator.initiateScan();
});

getAvailableSimSlots();

}

Expand Down Expand Up @@ -236,17 +259,14 @@ public void onFailure(Call<RegisterDeviceResponseDTO> call, Throwable t) {
}

private void handleSMSRequestPermission(View view) {
if (isSMSPermissionGranted(mContext)) {
if (isSMSPermissionGranted(mContext) && isReadPhoneStatePermissionGranted(mContext)) {
Snackbar.make(view, "Already got permissions", Snackbar.LENGTH_SHORT).show();
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.SEND_SMS)) {
Snackbar.make(view, "PERMISSION DENIED, Pls grant SMS Permission in app settings", Snackbar.LENGTH_SHORT).show();
} else {
Snackbar.make(view, "Grant SMS Permissions to continue", Snackbar.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS},
SEND_SMS_PERMISSION_REQUEST_CODE);
}
Snackbar.make(view, "Grant SMS Permissions to continue", Snackbar.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS, Manifest.permission.READ_PHONE_STATE
}, SEND_SMS_PERMISSION_REQUEST_CODE);

}
}

Expand All @@ -272,4 +292,19 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
private boolean isSMSPermissionGranted(Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED;
}

private boolean isReadPhoneStatePermissionGranted(Context context) {
return ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED;
}

private List<SubscriptionInfo> getAvailableSimSlots() {

SubscriptionManager subscriptionManager = SubscriptionManager.from(mContext);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return new ArrayList<>();
}

return subscriptionManager.getActiveSubscriptionInfoList();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public static void sendSMS(String phoneNo, String message) {
}

}

public static void sendSMSFromSpecificSim(String phoneNo, String message, int simSlot) {
SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(simSlot);
smsManager.sendMultipartTextMessage(phoneNo, null, smsManager.divideMessage(message), null, null);
}
}
20 changes: 18 additions & 2 deletions android/app/src/main/java/com/vernu/sms/models/SMSPayload.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
package com.vernu.sms.models;

public class SMSPayload {
public String[] receivers;
public String smsBody;
private String[] receivers;
private String smsBody;

public SMSPayload(String[] receivers, String smsBody) {
this.receivers = receivers;
this.smsBody = smsBody;
}

public String[] getReceivers() {
return receivers;
}

public void setReceivers(String[] receivers) {
this.receivers = receivers;
}

public String getSmsBody() {
return smsBody;
}

public void setSmsBody(String smsBody) {
this.smsBody = smsBody;
}
}
16 changes: 10 additions & 6 deletions android/app/src/main/java/com/vernu/sms/services/FCMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.vernu.sms.R;
import com.vernu.sms.activities.MainActivity;
import com.vernu.sms.helpers.SMSHelper;
import com.vernu.sms.helpers.SharedPreferenceHelper;
import com.vernu.sms.models.SMSPayload;


Expand All @@ -36,16 +37,19 @@ public void onMessageReceived(RemoteMessage remoteMessage) {

// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
// sendNotification("data msg received ", remoteMessage.getData().toString());
int len = smsPayload.receivers.length;
if (len > 0) {
for (int i = 0; i < len; i++) {
SMSHelper.sendSMS(smsPayload.receivers[i], smsPayload.smsBody);

for (String receiver : smsPayload.getReceivers()) {
int preferedSim = SharedPreferenceHelper.getSharedPreferenceInt(this, "PREFERED_SIM", 0);
try {
SMSHelper.sendSMSFromSpecificSim(receiver, smsPayload.getSmsBody(), preferedSim);
} catch(Exception e) {
Log.d("SMS_SEND_ERROR", e.getMessage());
SMSHelper.sendSMS(receiver, smsPayload.getSmsBody());
}
}
}

// Check if message contains a notification payload.
// TODO: Handle FCM Notification Messages
if (remoteMessage.getNotification() != null) {
// sendNotification("notif msg", "msg body");
}
Expand Down
18 changes: 18 additions & 0 deletions android/app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@
android:layout_height="wrap_content"
android:text="Grant SMS Permission"
android:visibility="visible" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Default SIM"
android:textStyle="bold" />
<RadioGroup
android:id="@+id/defaultSimSlotRadioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</RadioGroup>
</LinearLayout>
</LinearLayout>


Expand Down

0 comments on commit 6784546

Please sign in to comment.