- Fix #1475 Detect missing Notes app on server when importing an account

Signed-off-by: Stefan Niedermann <info@niedermann.it>
This commit is contained in:
Stefan Niedermann 2022-03-25 13:11:25 +01:00
parent 5b06023a51
commit 828ba1c2ee
4 changed files with 60 additions and 42 deletions

View file

@ -73,11 +73,7 @@ public class TipsAdapter extends RecyclerView.Adapter<TipsViewHolder> {
} else if (throwable instanceof NextcloudFilesAppNotSupportedException) {
add(R.string.error_dialog_tip_files_outdated);
} else if (throwable instanceof NextcloudApiNotRespondingException) {
if (VERSION.SDK_INT >= VERSION_CODES.M) {
add(R.string.error_dialog_tip_disable_battery_optimizations, new Intent().setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_open_battery_settings));
} else {
add(R.string.error_dialog_tip_disable_battery_optimizations);
}
add(R.string.error_dialog_tip_disable_battery_optimizations, new Intent().setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS).putExtra(INTENT_EXTRA_BUTTON_TEXT, R.string.error_action_open_battery_settings));
add(R.string.error_dialog_tip_files_force_stop);
add(R.string.error_dialog_tip_files_delete_storage);
final var intent = new Intent(ACTION_APPLICATION_DETAILS_SETTINGS)

View file

@ -118,7 +118,7 @@ public class ImportAccountActivity extends AppCompatActivity {
@Override
public void onError(@NonNull Throwable t) {
runOnUiThread(() -> {
binding.addButton.setEnabled(true);
restoreCleanState();
ExceptionDialogFragment.newInstance(t).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
});
}

View file

@ -53,30 +53,41 @@ public class NotesImportTask {
executor.submit(() -> {
Log.i(TAG, "… Fetching notes IDs");
final var status = new ImportStatus();
final var remoteIds = notesAPI.getNotesIDs().blockingSingle();
status.total = remoteIds.size();
status$.postValue(status);
Log.i(TAG, "… Total count: " + remoteIds.size());
final var latch = new CountDownLatch(remoteIds.size());
for (long id : remoteIds) {
fetchExecutor.submit(() -> {
try {
repo.addNote(localAccount.getId(), notesAPI.getNote(id).blockingSingle().getResponse());
} catch (Throwable t) {
Log.w(TAG, "Could not import note with remoteId " + id + ": " + t.getMessage());
status.warnings.add(t);
}
status.count++;
status$.postValue(status);
latch.countDown();
});
}
try {
latch.await();
Log.i(TAG, "IMPORT FINISHED");
callback.onSuccess(null);
} catch (InterruptedException e) {
callback.onError(e);
final var remoteIds = notesAPI.getNotesIDs().blockingSingle();
status.total = remoteIds.size();
status$.postValue(status);
Log.i(TAG, "… Total count: " + remoteIds.size());
final var latch = new CountDownLatch(remoteIds.size());
for (long id : remoteIds) {
fetchExecutor.submit(() -> {
try {
repo.addNote(localAccount.getId(), notesAPI.getNote(id).blockingSingle().getResponse());
} catch (Throwable t) {
Log.w(TAG, "Could not import note with remoteId " + id + ": " + t.getMessage());
status.warnings.add(t);
}
status.count++;
status$.postValue(status);
latch.countDown();
});
}
try {
latch.await();
Log.i(TAG, "IMPORT FINISHED");
callback.onSuccess(null);
} catch (InterruptedException e) {
callback.onError(e);
}
} catch (Throwable t) {
final Throwable cause = t.getCause();
if (t.getClass() == RuntimeException.class && cause != null) {
Log.e(TAG, "Could not fetch list of note IDs: " + cause.getMessage());
callback.onError(cause);
} else {
Log.e(TAG, "Could not fetch list of note IDs: " + t.getMessage());
callback.onError(t);
}
}
});
return status$;

View file

@ -1,5 +1,15 @@
package it.niedermann.owncloud.notes.persistence;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O;
import static androidx.lifecycle.Transformations.distinctUntilChanged;
import static androidx.lifecycle.Transformations.map;
import static java.util.stream.Collectors.toMap;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets;
import android.accounts.NetworkErrorException;
import android.content.BroadcastReceiver;
import android.content.Context;
@ -31,7 +41,6 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@ -66,16 +75,6 @@ import it.niedermann.owncloud.notes.shared.util.NoteUtil;
import it.niedermann.owncloud.notes.shared.util.SSOUtil;
import retrofit2.Call;
import static android.os.Build.VERSION.SDK_INT;
import static android.os.Build.VERSION_CODES.O;
import static androidx.lifecycle.Transformations.distinctUntilChanged;
import static androidx.lifecycle.Transformations.map;
import static it.niedermann.owncloud.notes.edit.EditNoteActivity.ACTION_SHORTCUT;
import static it.niedermann.owncloud.notes.shared.util.NoteUtil.generateNoteExcerpt;
import static it.niedermann.owncloud.notes.widget.notelist.NoteListWidget.updateNoteListWidgets;
import static it.niedermann.owncloud.notes.widget.singlenote.SingleNoteWidget.updateSingleNoteWidgets;
import static java.util.stream.Collectors.toMap;
@SuppressWarnings("UnusedReturnValue")
public class NotesRepository {
@ -186,15 +185,27 @@ public class NotesRepository {
@Override
public void onError(@NonNull Throwable t) {
Log.e(TAG, "… Error while importing " + account.getAccountName() + ": " + t.getMessage());
deleteAccount(account);
SingleAccountHelper.setCurrentAccount(context, null);
callback.onError(t);
}
});
} catch (NextcloudFilesAppAccountNotFoundException e) {
Log.e(TAG, "… Could not find " + SingleSignOnAccount.class.getSimpleName() + " for account name " + account.getAccountName());
callback.onError(e);
importExecutor.submit(() -> {
deleteAccount(account);
SingleAccountHelper.setCurrentAccount(context, null);
callback.onError(e);
});
}
} else {
callback.onError(new NetworkErrorException());
Log.e(TAG, "… No network connection available to import " + account.getAccountName());
importExecutor.submit(() -> {
deleteAccount(account);
SingleAccountHelper.setCurrentAccount(context, null);
callback.onError(new NetworkErrorException());
});
}
}
return new MutableLiveData<>(new ImportStatus());