13
13
import android .content .pm .PackageManager ;
14
14
import android .os .Build ;
15
15
import android .os .Bundle ;
16
+ import android .telephony .SubscriptionInfo ;
17
+ import android .telephony .SubscriptionManager ;
16
18
import android .util .Log ;
17
19
import android .view .View ;
18
20
import android .widget .Button ;
19
21
import android .widget .EditText ;
20
22
import android .widget .ImageButton ;
23
+ import android .widget .RadioButton ;
24
+ import android .widget .RadioGroup ;
21
25
import android .widget .Switch ;
22
26
import android .widget .TextView ;
23
27
import android .widget .Toast ;
33
37
import com .vernu .sms .dtos .RegisterDeviceResponseDTO ;
34
38
import com .vernu .sms .helpers .SharedPreferenceHelper ;
35
39
40
+ import java .util .ArrayList ;
41
+ import java .util .List ;
42
+
36
43
import retrofit2 .Call ;
37
44
import retrofit2 .Callback ;
38
45
import retrofit2 .Response ;
@@ -51,6 +58,8 @@ public class MainActivity extends AppCompatActivity {
51
58
private ImageButton copyDeviceIdImgBtn ;
52
59
private TextView deviceBrandAndModelTxt , deviceIdTxt ;
53
60
61
+ private RadioGroup defaultSimSlotRadioGroup ;
62
+
54
63
private static final int SEND_SMS_PERMISSION_REQUEST_CODE = 0 ;
55
64
private static final int SCAN_QR_REQUEST_CODE = 49374 ;
56
65
@@ -86,16 +95,29 @@ protected void onCreate(Bundle savedInstanceState) {
86
95
87
96
copyDeviceIdImgBtn = findViewById (R .id .copyDeviceIdImgBtn );
88
97
98
+ defaultSimSlotRadioGroup = findViewById (R .id .defaultSimSlotRadioGroup );
99
+
100
+ getAvailableSimSlots ().forEach (subscriptionInfo -> {
101
+ RadioButton radioButton = new RadioButton (mContext );
102
+ radioButton .setText (subscriptionInfo .getDisplayName ().toString ());
103
+ radioButton .setId (subscriptionInfo .getSubscriptionId ());
104
+ radioButton .setOnClickListener (view -> {
105
+ SharedPreferenceHelper .setSharedPreferenceInt (mContext , "PREFERED_SIM" , subscriptionInfo .getSubscriptionId ());
106
+ });
107
+ radioButton .setChecked (subscriptionInfo .getSubscriptionId () == SharedPreferenceHelper .getSharedPreferenceInt (mContext , "PREFERED_SIM" , 0 ));
108
+ defaultSimSlotRadioGroup .addView (radioButton );
109
+ });
110
+
89
111
deviceIdTxt .setText (deviceId );
90
112
deviceBrandAndModelTxt .setText (Build .BRAND + " " + Build .MODEL );
91
113
92
- if (deviceId == null || deviceId .isEmpty ()) {
114
+ if (deviceId == null || deviceId .isEmpty ()) {
93
115
registerDeviceBtn .setText ("Register" );
94
116
} else {
95
117
registerDeviceBtn .setText ("Update" );
96
118
}
97
119
98
- if (isSMSPermissionGranted (mContext )) {
120
+ if (isSMSPermissionGranted (mContext ) && isReadPhoneStatePermissionGranted ( mContext ) ) {
99
121
grantSMSPermissionBtn .setEnabled (false );
100
122
grantSMSPermissionBtn .setText ("SMS Permission Granted" );
101
123
} else {
@@ -157,6 +179,7 @@ public void onFailure(Call<RegisterDeviceResponseDTO> call, Throwable t) {
157
179
intentIntegrator .initiateScan ();
158
180
});
159
181
182
+ getAvailableSimSlots ();
160
183
161
184
}
162
185
@@ -236,17 +259,14 @@ public void onFailure(Call<RegisterDeviceResponseDTO> call, Throwable t) {
236
259
}
237
260
238
261
private void handleSMSRequestPermission (View view ) {
239
- if (isSMSPermissionGranted (mContext )) {
262
+ if (isSMSPermissionGranted (mContext ) && isReadPhoneStatePermissionGranted ( mContext ) ) {
240
263
Snackbar .make (view , "Already got permissions" , Snackbar .LENGTH_SHORT ).show ();
241
264
} else {
242
- if (ActivityCompat .shouldShowRequestPermissionRationale (MainActivity .this , Manifest .permission .SEND_SMS )) {
243
- Snackbar .make (view , "PERMISSION DENIED, Pls grant SMS Permission in app settings" , Snackbar .LENGTH_SHORT ).show ();
244
- } else {
245
- Snackbar .make (view , "Grant SMS Permissions to continue" , Snackbar .LENGTH_SHORT ).show ();
246
- ActivityCompat .requestPermissions (MainActivity .this ,
247
- new String []{Manifest .permission .SEND_SMS },
248
- SEND_SMS_PERMISSION_REQUEST_CODE );
249
- }
265
+ Snackbar .make (view , "Grant SMS Permissions to continue" , Snackbar .LENGTH_SHORT ).show ();
266
+ ActivityCompat .requestPermissions (MainActivity .this ,
267
+ new String []{Manifest .permission .SEND_SMS , Manifest .permission .READ_PHONE_STATE
268
+ }, SEND_SMS_PERMISSION_REQUEST_CODE );
269
+
250
270
}
251
271
}
252
272
@@ -272,4 +292,19 @@ protected void onActivityResult(int requestCode, int resultCode, @Nullable Inten
272
292
private boolean isSMSPermissionGranted (Context context ) {
273
293
return ContextCompat .checkSelfPermission (context , Manifest .permission .SEND_SMS ) == PackageManager .PERMISSION_GRANTED ;
274
294
}
295
+
296
+ private boolean isReadPhoneStatePermissionGranted (Context context ) {
297
+ return ContextCompat .checkSelfPermission (context , Manifest .permission .READ_PHONE_STATE ) == PackageManager .PERMISSION_GRANTED ;
298
+ }
299
+
300
+ private List <SubscriptionInfo > getAvailableSimSlots () {
301
+
302
+ SubscriptionManager subscriptionManager = SubscriptionManager .from (mContext );
303
+ if (ActivityCompat .checkSelfPermission (this , Manifest .permission .READ_PHONE_STATE ) != PackageManager .PERMISSION_GRANTED ) {
304
+ return new ArrayList <>();
305
+ }
306
+
307
+ return subscriptionManager .getActiveSubscriptionInfoList ();
308
+
309
+ }
275
310
}
0 commit comments