use of com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException in project nextcloud-notes by stefan-niedermann.
the class BaseNoteFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
executor.submit(() -> {
try {
final var ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(requireContext().getApplicationContext());
this.localAccount = repo.getAccountByName(ssoAccount.name);
if (savedInstanceState == null) {
final long id = requireArguments().getLong(PARAM_NOTE_ID);
if (id > 0) {
final long accountId = requireArguments().getLong(PARAM_ACCOUNT_ID);
if (accountId > 0) {
/* Switch account if account id has been provided */
this.localAccount = repo.getAccountById(accountId);
SingleAccountHelper.setCurrentAccount(requireContext().getApplicationContext(), localAccount.getAccountName());
}
isNew = false;
note = originalNote = repo.getNoteById(id);
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
requireActivity().invalidateOptionsMenu();
} else {
final var paramNote = (Note) requireArguments().getSerializable(PARAM_NEWNOTE);
final var content = requireArguments().getString(PARAM_CONTENT);
if (paramNote == null) {
if (content == null) {
throw new IllegalArgumentException(PARAM_NOTE_ID + " is not given, argument " + PARAM_NEWNOTE + " is missing and " + PARAM_CONTENT + " is missing.");
} else {
note = new Note(-1, null, Calendar.getInstance(), NoteUtil.generateNoteTitle(content), content, getString(R.string.category_readonly), false, null, DBStatus.VOID, -1, "", 0);
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
requireActivity().invalidateOptionsMenu();
}
} else {
paramNote.setStatus(DBStatus.LOCAL_EDITED);
note = repo.addNote(localAccount.getId(), paramNote);
originalNote = null;
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
requireActivity().invalidateOptionsMenu();
}
}
} else {
note = (Note) savedInstanceState.getSerializable(SAVEDKEY_NOTE);
originalNote = (Note) savedInstanceState.getSerializable(SAVEDKEY_ORIGINAL_NOTE);
requireActivity().runOnUiThread(() -> onNoteLoaded(note));
requireActivity().invalidateOptionsMenu();
}
} catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
e.printStackTrace();
}
});
setHasOptionsMenu(true);
}
use of com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException in project nextcloud-notes by stefan-niedermann.
the class NotesRepository method scheduleSync.
/**
* Schedules a synchronization and start it directly, if the network is connected and no
* synchronization is currently running.
*
* @param onlyLocalChanges Whether to only push local changes to the server or to also load the whole list of notes from the server.
*/
public synchronized void scheduleSync(@Nullable Account account, boolean onlyLocalChanges) {
if (account == null) {
Log.i(TAG, SingleSignOnAccount.class.getSimpleName() + " is null. Is this a local account?");
} else {
if (syncActive.get(account.getId()) == null) {
syncActive.put(account.getId(), false);
}
Log.d(TAG, "Sync requested (" + (onlyLocalChanges ? "onlyLocalChanges" : "full") + "; " + (Boolean.TRUE.equals(syncActive.get(account.getId())) ? "sync active" : "sync NOT active") + ") ...");
if (isSyncPossible() && (!Boolean.TRUE.equals(syncActive.get(account.getId())) || onlyLocalChanges)) {
syncActive.put(account.getId(), true);
try {
Log.d(TAG, "... starting now");
final NotesServerSyncTask syncTask = new NotesServerSyncTask(context, this, account, onlyLocalChanges, apiProvider) {
@Override
void onPreExecute() {
syncStatus.postValue(true);
if (!syncScheduled.containsKey(localAccount.getId()) || syncScheduled.get(localAccount.getId()) == null) {
syncScheduled.put(localAccount.getId(), false);
}
if (!onlyLocalChanges && Boolean.TRUE.equals(syncScheduled.get(localAccount.getId()))) {
syncScheduled.put(localAccount.getId(), false);
}
}
@Override
void onPostExecute(SyncResultStatus status) {
for (Throwable e : exceptions) {
Log.e(TAG, e.getMessage(), e);
}
if (!status.pullSuccessful || !status.pushSuccessful) {
syncErrors.postValue(exceptions);
}
syncActive.put(localAccount.getId(), false);
// notify callbacks
if (callbacks.containsKey(localAccount.getId()) && callbacks.get(localAccount.getId()) != null) {
for (ISyncCallback callback : Objects.requireNonNull(callbacks.get(localAccount.getId()))) {
callback.onFinish();
}
}
notifyWidgets();
updateDynamicShortcuts(localAccount.getId());
// start next sync if scheduled meanwhile
if (syncScheduled.containsKey(localAccount.getId()) && syncScheduled.get(localAccount.getId()) != null && Boolean.TRUE.equals(syncScheduled.get(localAccount.getId()))) {
scheduleSync(localAccount, false);
}
syncStatus.postValue(false);
}
};
syncTask.addCallbacks(account, callbacksPush.get(account.getId()));
callbacksPush.put(account.getId(), new ArrayList<>());
if (!onlyLocalChanges) {
syncTask.addCallbacks(account, callbacksPull.get(account.getId()));
callbacksPull.put(account.getId(), new ArrayList<>());
}
syncExecutor.submit(syncTask);
} catch (NextcloudFilesAppAccountNotFoundException e) {
Log.e(TAG, "... Could not find " + SingleSignOnAccount.class.getSimpleName() + " for account name " + account.getAccountName());
e.printStackTrace();
}
} else if (!onlyLocalChanges) {
Log.d(TAG, "... scheduled");
syncScheduled.put(account.getId(), true);
if (callbacksPush.containsKey(account.getId()) && callbacksPush.get(account.getId()) != null) {
final var callbacks = callbacksPush.get(account.getId());
if (callbacks != null) {
for (final var callback : callbacks) {
callback.onScheduled();
}
} else {
Log.w(TAG, "List of push-callbacks was set for account \"" + account.getAccountName() + "\" but it was null");
}
}
} else {
Log.d(TAG, "... do nothing");
if (callbacksPush.containsKey(account.getId()) && callbacksPush.get(account.getId()) != null) {
final var callbacks = callbacksPush.get(account.getId());
if (callbacks != null) {
for (final var callback : callbacks) {
callback.onScheduled();
}
} else {
Log.w(TAG, "List of push-callbacks was set for account \"" + account.getAccountName() + "\" but it was null");
}
}
}
}
}
use of com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException in project nextcloud-notes by stefan-niedermann.
the class ManageAccountsActivity method onChangeFileSuffix.
private void onChangeFileSuffix(@NonNull Account localAccount) {
final var repository = NotesRepository.getInstance(getApplicationContext());
final var spinner = new Spinner(this);
final var wrapper = createDialogViewWrapper();
final var adapter = ArrayAdapter.createFromResource(this, R.array.settings_file_suffixes, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
final var dialog = new BrandedAlertDialogBuilder(this).setTitle(R.string.settings_file_suffix).setMessage(R.string.settings_file_suffix_description).setView(wrapper).setNeutralButton(android.R.string.cancel, null).setPositiveButton(R.string.action_edit_save, (v, d) -> new Thread(() -> {
try {
final Call<NotesSettings> putSettingsCall = repository.putServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()), new NotesSettings(null, spinner.getSelectedItem().toString()), getPreferredApiVersion(localAccount.getApiVersion()));
putSettingsCall.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<NotesSettings> call, @NonNull Response<NotesSettings> response) {
final var body = response.body();
if (response.isSuccessful() && body != null) {
runOnUiThread(() -> Toast.makeText(ManageAccountsActivity.this, getString(R.string.settings_file_suffix_success, body.getNotesPath()), Toast.LENGTH_LONG).show());
} else {
runOnUiThread(() -> Toast.makeText(ManageAccountsActivity.this, getString(R.string.http_status_code, response.code()), Toast.LENGTH_LONG).show());
}
}
@Override
public void onFailure(@NonNull Call<NotesSettings> call, @NonNull Throwable t) {
runOnUiThread(() -> ExceptionDialogFragment.newInstance(t).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
}
});
} catch (NextcloudFilesAppAccountNotFoundException e) {
runOnUiThread(() -> ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()));
}
}).start()).show();
try {
repository.getServerSettings(AccountImporter.getSingleSignOnAccount(this, localAccount.getAccountName()), getPreferredApiVersion(localAccount.getApiVersion())).enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<NotesSettings> call, @NonNull Response<NotesSettings> response) {
final NotesSettings body = response.body();
runOnUiThread(() -> {
if (response.isSuccessful() && body != null) {
for (int i = 0; i < adapter.getCount(); i++) {
if (adapter.getItem(i).equals(body.getFileSuffix())) {
spinner.setSelection(i);
break;
}
}
wrapper.removeAllViews();
wrapper.addView(spinner);
} else {
dialog.dismiss();
ExceptionDialogFragment.newInstance(new Exception(getString(R.string.http_status_code, response.code()))).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}
});
}
@Override
public void onFailure(@NonNull Call<NotesSettings> call, @NonNull Throwable t) {
runOnUiThread(() -> {
dialog.dismiss();
ExceptionDialogFragment.newInstance(t).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
});
}
});
} catch (NextcloudFilesAppAccountNotFoundException e) {
dialog.dismiss();
ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
}
}
Aggregations