Search in sources :

Example 6 with NextcloudFilesAppAccountNotFoundException

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);
}
Also used : Note(it.niedermann.owncloud.notes.persistence.entity.Note) NoCurrentAccountSelectedException(com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)

Example 7 with NextcloudFilesAppAccountNotFoundException

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");
                }
            }
        }
    }
}
Also used : SingleSignOnAccount(com.nextcloud.android.sso.model.SingleSignOnAccount) ISyncCallback(it.niedermann.owncloud.notes.shared.model.ISyncCallback) SyncResultStatus(it.niedermann.owncloud.notes.shared.model.SyncResultStatus) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)

Example 8 with NextcloudFilesAppAccountNotFoundException

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());
    }
}
Also used : AttrRes(androidx.annotation.AttrRes) Bundle(android.os.Bundle) AlertDialog(androidx.appcompat.app.AlertDialog) LockedActivity(it.niedermann.owncloud.notes.LockedActivity) ProgressBar(android.widget.ProgressBar) NonNull(androidx.annotation.NonNull) FrameLayout(android.widget.FrameLayout) Response(retrofit2.Response) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException) SDK_INT(android.os.Build.VERSION.SDK_INT) Px(androidx.annotation.Px) LOLLIPOP_MR1(android.os.Build.VERSION_CODES.LOLLIPOP_MR1) ActivityManageAccountsBinding(it.niedermann.owncloud.notes.databinding.ActivityManageAccountsBinding) Toast(android.widget.Toast) NotesSettings(it.niedermann.owncloud.notes.shared.model.NotesSettings) IResponseCallback(it.niedermann.owncloud.notes.shared.model.IResponseCallback) ViewModelProvider(androidx.lifecycle.ViewModelProvider) R(it.niedermann.owncloud.notes.R) ViewGroup(android.view.ViewGroup) Spinner(android.widget.Spinner) ExceptionDialogFragment(it.niedermann.owncloud.notes.exception.ExceptionDialogFragment) NotesRepository(it.niedermann.owncloud.notes.persistence.NotesRepository) NetworkErrorException(android.accounts.NetworkErrorException) ArrayAdapter(android.widget.ArrayAdapter) AccountImporter(com.nextcloud.android.sso.AccountImporter) BrandedAlertDialogBuilder(it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder) Nullable(androidx.annotation.Nullable) TypedValue(android.util.TypedValue) Callback(retrofit2.Callback) ApiVersionUtil.getPreferredApiVersion(it.niedermann.owncloud.notes.shared.util.ApiVersionUtil.getPreferredApiVersion) BrandedDeleteAlertDialogBuilder(it.niedermann.owncloud.notes.branding.BrandedDeleteAlertDialogBuilder) Call(retrofit2.Call) EditText(android.widget.EditText) Account(it.niedermann.owncloud.notes.persistence.entity.Account) Call(retrofit2.Call) Spinner(android.widget.Spinner) NotesSettings(it.niedermann.owncloud.notes.shared.model.NotesSettings) BrandedAlertDialogBuilder(it.niedermann.owncloud.notes.branding.BrandedAlertDialogBuilder) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException) NetworkErrorException(android.accounts.NetworkErrorException) Response(retrofit2.Response) IResponseCallback(it.niedermann.owncloud.notes.shared.model.IResponseCallback) Callback(retrofit2.Callback) NonNull(androidx.annotation.NonNull) NextcloudFilesAppAccountNotFoundException(com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)

Aggregations

NextcloudFilesAppAccountNotFoundException (com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException)8 NetworkErrorException (android.accounts.NetworkErrorException)5 Account (it.niedermann.owncloud.notes.persistence.entity.Account)5 Bundle (android.os.Bundle)4 Nullable (androidx.annotation.Nullable)4 ViewModelProvider (androidx.lifecycle.ViewModelProvider)4 NoCurrentAccountSelectedException (com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException)4 LockedActivity (it.niedermann.owncloud.notes.LockedActivity)4 R (it.niedermann.owncloud.notes.R)4 SDK_INT (android.os.Build.VERSION.SDK_INT)3 Toast (android.widget.Toast)3 NonNull (androidx.annotation.NonNull)3 AlertDialog (androidx.appcompat.app.AlertDialog)3 AccountImporter (com.nextcloud.android.sso.AccountImporter)3 ExceptionDialogFragment (it.niedermann.owncloud.notes.exception.ExceptionDialogFragment)3 NotesRepository (it.niedermann.owncloud.notes.persistence.NotesRepository)3 IResponseCallback (it.niedermann.owncloud.notes.shared.model.IResponseCallback)3 Intent (android.content.Intent)2 LOLLIPOP_MR1 (android.os.Build.VERSION_CODES.LOLLIPOP_MR1)2 Log (android.util.Log)2