Skip to content

Commit

Permalink
#1256 Provide backup and repair steps
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Niedermann <[email protected]>
  • Loading branch information
stefan-niedermann committed Jun 15, 2021
1 parent ca59964 commit b06c109
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.view.ActionMode;
import androidx.appcompat.widget.SearchView;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
Expand Down Expand Up @@ -50,9 +52,11 @@
import java.net.HttpURLConnection;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Function;
import java.util.stream.Collectors;

import it.niedermann.owncloud.notes.LockedActivity;
Expand Down Expand Up @@ -90,6 +94,7 @@
import it.niedermann.owncloud.notes.shared.model.NoteClickListener;
import it.niedermann.owncloud.notes.shared.util.CustomAppGlideModule;
import it.niedermann.owncloud.notes.shared.util.NoteUtil;
import it.niedermann.owncloud.notes.shared.util.ShareUtil;

import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O;
Expand Down Expand Up @@ -175,25 +180,52 @@ protected void onCreate(Bundle savedInstanceState) {
try {
final Account account = mainViewModel.getLocalAccountByAccountName(SingleAccountHelper.getCurrentSingleSignOnAccount(getApplicationContext()).name);
runOnUiThread(() -> mainViewModel.postCurrentAccount(account));
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
} catch (NextcloudFilesAppAccountNotFoundException e) {
// Verbose log output for https://github.com/stefan-niedermann/nextcloud-notes/issues/1256
final Throwable exception;
if (e instanceof NextcloudFilesAppAccountNotFoundException) {
final SharedPreferences ssoPreferences = AccountImporter.getSharedPreferences(getApplicationContext());
final StringBuilder ssoPreferencesString = new StringBuilder()
.append("Current SSO account: ").append(ssoPreferences.getString("PREF_CURRENT_ACCOUNT_STRING", null)).append("\n")
.append("\n")
.append("SSO SharedPreferences: ").append("\n");
for (Map.Entry<String, ?> entry : ssoPreferences.getAll().entrySet()) {
ssoPreferencesString.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
ssoPreferencesString.append("\n")
.append("Available accounts in DB: ").append(TextUtils.join(", ", mainViewModel.getAccounts().stream().map(Account::getAccountName).collect(Collectors.toList())));
exception = new RuntimeException(((NextcloudFilesAppAccountNotFoundException) e).getMessage(), new RuntimeException(ssoPreferencesString.toString(), e));
} else {
exception = e;
}
runOnUiThread(() -> ExceptionDialogFragment.newInstance(exception).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
runOnUiThread(() -> new AlertDialog.Builder(this)
.setTitle(NextcloudFilesAppAccountNotFoundException.class.getSimpleName())
.setMessage(R.string.backup_and_repair)
.setPositiveButton(R.string.simple_repair, (a, b) -> {
executor.submit(() -> {
for (Account account : mainViewModel.getAccounts()) {
SingleAccountHelper.setCurrentAccount(this, account.getAccountName());
runOnUiThread(this::recreate);
break;
}
});
})
.setNegativeButton(R.string.simple_backup, (a, b) -> {
executor.submit(() -> {
final List<Note> modifiedNotes = new LinkedList<>();
for (Account account : mainViewModel.getAccounts()) {
modifiedNotes.addAll(mainViewModel.getLocalModifiedNotes(account.getId()));
}
if (modifiedNotes.size() == 1) {
final Note note = modifiedNotes.get(0);
ShareUtil.openShareDialog(this, note.getTitle(), note.getContent());
} else {
ShareUtil.openShareDialog(this,
getResources().getQuantityString(R.plurals.share_multiple, modifiedNotes.size(), modifiedNotes.size()),
mainViewModel.collectNoteContents(modifiedNotes.stream().map(Note::getId).collect(Collectors.toList())));
}
});
})
.setNeutralButton(android.R.string.cancel, (a, b) -> {
final SharedPreferences ssoPreferences = AccountImporter.getSharedPreferences(getApplicationContext());
final StringBuilder ssoPreferencesString = new StringBuilder()
.append("Current SSO account: ").append(ssoPreferences.getString("PREF_CURRENT_ACCOUNT_STRING", null)).append("\n")
.append("\n")
.append("SSO SharedPreferences: ").append("\n");
for (Map.Entry<String, ?> entry : ssoPreferences.getAll().entrySet()) {
ssoPreferencesString.append(entry.getKey()).append(": ").append(entry.getValue()).append("\n");
}
ssoPreferencesString.append("\n")
.append("Available accounts in DB: ").append(TextUtils.join(", ", mainViewModel.getAccounts().stream().map(Account::getAccountName).collect(Collectors.toList())));
runOnUiThread(() -> ExceptionDialogFragment.newInstance(new RuntimeException(e.getMessage(), new RuntimeException(ssoPreferencesString.toString(), e))).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
})
.show());
} catch (NoCurrentAccountSelectedException e) {
runOnUiThread(() -> ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,10 @@ public void createOrUpdateSingleNoteWidgetData(SingleNoteWidgetData data) {
repo.createOrUpdateSingleNoteWidgetData(data);
}

public List<Note> getLocalModifiedNotes(long accountId) {
return repo.getLocalModifiedNotes(accountId);
}

public LiveData<Integer> getAccountsCount() {
return repo.countAccounts$();
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,7 @@
<string name="you_have_to_be_connected_to_the_internet_in_order_to_add_an_account">You have to be connected to the internet in order to add an account.</string>
<string name="simple_next">Next</string>
<string name="simple_prev">Previous</string>
<string name="simple_backup">Backup</string>
<string name="simple_repair">Repair</string>
<string name="backup_and_repair">We recommend to backup all unsynchronized notes and then try to repair the setup.</string>
</resources>

0 comments on commit b06c109

Please sign in to comment.