Skip to content

Commit

Permalink
Add support for Multi-SIM, with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyber1000 authored and kurahaupo committed Sep 6, 2022
1 parent b52e564 commit 9ed193f
Show file tree
Hide file tree
Showing 43 changed files with 1,000 additions and 384 deletions.
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
<!-- Apps wanting to use foreground services must now request the FOREGROUND_SERVICE permission first. -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

<!-- Get Information about simcards (needed for multisim) -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"
android:maxSdkVersion="29" />
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />

<uses-feature android:name="android.hardware.telephony" android:required="false"/>
<uses-feature android:name="android.hardware.wifi" android:required="false"/>

Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/zegoggles/smssync/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import com.zegoggles.smssync.receiver.BootReceiver;
import com.zegoggles.smssync.receiver.SmsBroadcastReceiver;
import com.zegoggles.smssync.service.BackupJobs;
import com.zegoggles.smssync.utils.SimCard;
import com.zegoggles.smssync.utils.SimCardHelper;

import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
Expand All @@ -60,6 +62,8 @@ public class App extends Application {
/** Google Play Services present on this device? */
public static boolean gcmAvailable;

public static SimCard[] SimCards;

private Preferences preferences;
private BackupJobs backupJobs;

Expand All @@ -68,6 +72,7 @@ public void onCreate() {
super.onCreate();
setupStrictMode();
gcmAvailable = GooglePlayServices.isAvailable(this);
SimCards = SimCardHelper.getSimCards(getApplicationContext());
preferences = new Preferences(this);
preferences.migrate();

Expand Down
40 changes: 35 additions & 5 deletions app/src/main/java/com/zegoggles/smssync/activity/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import android.Manifest;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
Expand Down Expand Up @@ -116,6 +117,8 @@ public class MainActivity extends ThemeActivity implements
private static final int REQUEST_PERMISSIONS_BACKUP_MANUAL = 4;
private static final int REQUEST_PERMISSIONS_BACKUP_MANUAL_SKIP = 5;
private static final int REQUEST_PERMISSIONS_BACKUP_SERVICE = 6;
private static final int REQUEST_PERMISSIONS_PHONE = 7;


public static final String EXTRA_PERMISSIONS = "permissions";
private static final String SCREEN_TITLE_RES = "titleRes";
Expand All @@ -134,7 +137,8 @@ public void onCreate(Bundle bundle) {
setSupportActionBar(toolbar);
getSupportFragmentManager().addOnBackStackChangedListener(this);

authPreferences = new AuthPreferences(this);
//XOAuth2 is legacy and therefore not considered for multi-sim (authPreferences only used in this context here)
authPreferences = new AuthPreferences(this, 0);
oauth2Client = new OAuth2Client(authPreferences.getOAuth2ClientId());
fallbackAuthIntent = new Intent(this, OAuth2WebAuthActivity.class).setData(oauth2Client.requestUrl());
preferenceTitles = new PreferenceTitles(getResources(), R.xml.preferences);
Expand All @@ -147,6 +151,7 @@ public void onCreate(Bundle bundle) {
}
checkDefaultSmsApp();
requestPermissionsIfNeeded();
requestPhoneStatePermission();
}

@Override
Expand Down Expand Up @@ -309,7 +314,9 @@ public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, Preferen

@Override public void onBackStackChanged() {
if (getSupportActionBar() == null) return;
getSupportActionBar().setSubtitle(getCurrentTitle());
@StringRes Integer title = getCurrentTitle();
if (title == null) return;
getSupportActionBar().setSubtitle(title);
getSupportActionBar().setDisplayHomeAsUpEnabled(getSupportFragmentManager().getBackStackEntryCount() > 0);
}

Expand All @@ -318,21 +325,25 @@ public boolean onPreferenceStartScreen(PreferenceFragmentCompat caller, Preferen
onBackStackChanged();
}

private @StringRes int getCurrentTitle() {
private @StringRes Integer getCurrentTitle() {
final int entryCount = getSupportFragmentManager().getBackStackEntryCount();
final List<Fragment> fragments = getSupportFragmentManager().getFragments();
if (entryCount == 0) {
return 0;
} else if (fragments.size() > 0 && fragments.get(fragments.size() - 1)
instanceof com.zegoggles.smssync.activity.fragments.AdvancedSettings.Server) {
return null;
} else {
final FragmentManager.BackStackEntry entry = getSupportFragmentManager().getBackStackEntryAt(entryCount - 1);
return entry.getBreadCrumbTitleRes();
}
}

@Subscribe public void performAction(PerformAction action) {
if (authPreferences.isLoginInformationSet()) {
if (atLeastOneLoginInformationSet(this)) {
if (action.confirm) {
showDialog(CONFIRM_ACTION, new BundleBuilder().putString(ACTION, action.action.name()).build());
} else if (preferences.isFirstBackup() && action.action == Backup) {
} else if (preferences.isFirstBackup(0) && action.action == Backup) {
showDialog(FIRST_SYNC);
} else {
doPerform(action.action);
Expand Down Expand Up @@ -463,6 +474,18 @@ private void checkDefaultSmsApp() {
}
}

private void requestPhoneStatePermission() {
String phonePermission;
if (Build.VERSION.SDK_INT > 29) {
phonePermission=Manifest.permission.READ_PHONE_NUMBERS;
}
else
{
phonePermission=Manifest.permission.READ_PHONE_STATE;
}
ActivityCompat.requestPermissions(this, new String[]{phonePermission}, REQUEST_PERMISSIONS_PHONE);
}

private void requestPermissionsIfNeeded() {
final Intent intent = getIntent();
if (intent != null && intent.hasExtra(EXTRA_PERMISSIONS)) {
Expand All @@ -472,6 +495,13 @@ private void requestPermissionsIfNeeded() {
}
}

private boolean atLeastOneLoginInformationSet(Context context) {
for (Integer settingsId = 0; settingsId < App.SimCards.length; settingsId++) {
if (new AuthPreferences(context, settingsId).isLoginInformationSet()) return true;
}
return false;
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ private void onRestore() {
private void onAuthFailed() {
statusLabel.setText(R.string.status_auth_failure);

if (new AuthPreferences(getContext()).useXOAuth()) {
////XOAuth2 is legacy and therefore not considered for multi-sim
if (new AuthPreferences(getContext(), 0).useXOAuth()) {
syncDetailsLabel.setText(R.string.status_auth_failure_details_xoauth);
} else {
syncDetailsLabel.setText(R.string.status_auth_failure_details_plain);
Expand Down Expand Up @@ -276,7 +277,12 @@ private void finishedRestore(RestoreState newState) {
}

private void idle() {
syncDetailsLabel.setText(getLastSyncText(preferences.getDataTypePreferences().getMostRecentSyncedDate()));
long mostRecentSyncedDate = 0;
for (Integer settingsId = 0; settingsId < App.SimCards.length; settingsId++) {
long mostRecentSyncedDateForSettingsId = preferences.getDataTypePreferences().getMostRecentSyncedDate(settingsId);
if (mostRecentSyncedDateForSettingsId>mostRecentSyncedDate) mostRecentSyncedDate = mostRecentSyncedDateForSettingsId;
}
syncDetailsLabel.setText(getLastSyncText(mostRecentSyncedDate));
statusLabel.setText(R.string.status_idle);
statusLabel.setTextColor(idleColor);
statusIcon.setImageDrawable(idle);
Expand Down
Loading

0 comments on commit 9ed193f

Please sign in to comment.