Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added changes for multisim, added tests #1080

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@
<!-- 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" />

<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
32 changes: 27 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,10 @@ private void checkDefaultSmsApp() {
}
}

private void requestPhoneStatePermission() {
ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.READ_PHONE_STATE }, REQUEST_PERMISSIONS_PHONE);
}

private void requestPermissionsIfNeeded() {
final Intent intent = getIntent();
if (intent != null && intent.hasExtra(EXTRA_PERMISSIONS)) {
Expand All @@ -472,6 +487,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