Skip to content
Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.branding

import android.content.Context
import android.view.View
import androidx.annotation.ColorInt
import androidx.annotation.StringRes
import androidx.fragment.app.FragmentActivity
import com.google.android.material.snackbar.BaseTransientBottomBar
import com.google.android.material.snackbar.Snackbar
import com.nextcloud.android.sso.AccountImporter
import com.nextcloud.android.sso.exceptions.SSOException
import com.nextcloud.android.sso.helper.SingleAccountHelper
import it.niedermann.owncloud.notes.R
import it.niedermann.owncloud.notes.exception.ExceptionDialogFragment
import it.niedermann.owncloud.notes.main.SyncErrorMessage
import it.niedermann.owncloud.notes.persistence.FilesAppAccount
import it.niedermann.owncloud.notes.persistence.SyncFailureReason
import it.niedermann.owncloud.notes.persistence.entity.Account
import java.lang.ref.WeakReference

object BrandedSnackbar {
private const val SYNC_ERROR_MAX_LINES = 4

private var syncErrorSnackbar: WeakReference<Snackbar>? = null
private var currentAccount: Account? = null

@JvmStatic
fun make(
view: View,
text: CharSequence,
@BaseTransientBottomBar.Duration duration: Int
): Snackbar {
@ColorInt val color = BrandingUtil.readBrandMainColor(view.context)
val snackbar = Snackbar.make(view, text, duration)
val utils = BrandingUtil.of(color, view.context)

utils.material.themeSnackbar(snackbar)

return snackbar
}

@JvmStatic
fun make(
view: View,
@StringRes resId: Int,
@BaseTransientBottomBar.Duration duration: Int
): Snackbar {
return make(view, view.resources.getText(resId), duration)
}

@JvmStatic
fun showSyncError(activity: FragmentActivity, view: View, anchor: View, exceptions: Collection<Throwable>) {
val reason = SyncErrorMessage(exceptions).reasonOf()
if (reason == SyncFailureReason.ACCOUNT_REMOVED && isCurrentAccountAvailable(activity)) return

val snackbar = make(view, reason.text(view.context), reason.snackbarDuration())
.setTextMaxLines(SYNC_ERROR_MAX_LINES)
.setAnchorView(anchor)
when (reason) {
SyncFailureReason.TOKEN_MISMATCH ->
snackbar.setAction(R.string.error_action_grant_access) { requestAccessAgain(activity) }

SyncFailureReason.ACCOUNT_REMOVED ->
snackbar.setAction(R.string.choose_account) { chooseAccount(activity) }

SyncFailureReason.NO_NETWORK, SyncFailureReason.MAINTENANCE -> Unit

else -> snackbar.setAction(R.string.simple_more) { showDetails(activity, exceptions) }
}

syncErrorSnackbar = WeakReference(snackbar)
snackbar.show()
}

@JvmStatic
fun onCurrentAccountChanged(account: Account) {
currentAccount = account
syncErrorSnackbar?.get()?.dismiss()
syncErrorSnackbar = null
}

@JvmStatic
fun showSyncError(activity: FragmentActivity, view: View, anchor: View, exception: Throwable) =
showSyncError(activity, view, anchor, listOf(exception))

private fun SyncFailureReason.text(context: Context): CharSequence = when (this) {
SyncFailureReason.ACCOUNT_REMOVED -> context.getString(message, currentAccount?.userName)
else -> context.getText(message)
}

private fun SyncFailureReason.snackbarDuration(): Int = when (this) {
SyncFailureReason.TOKEN_MISMATCH, SyncFailureReason.ACCOUNT_REMOVED -> Snackbar.LENGTH_INDEFINITE
else -> Snackbar.LENGTH_LONG
}

private fun isCurrentAccountAvailable(activity: FragmentActivity): Boolean =
currentAccount?.let { FilesAppAccount.exists(activity, it.accountName) } == true

private fun requestAccessAgain(activity: FragmentActivity) {
try {
val ssoAccount = SingleAccountHelper.getCurrentSingleSignOnAccount(activity)
AccountImporter.authenticateSingleSignAccount(activity, ssoAccount)
} catch (e: SSOException) {
showDetails(activity, listOf(e))
}
}

private fun chooseAccount(activity: FragmentActivity) {
try {
AccountImporter.pickNewAccount(activity)
} catch (e: SSOException) {
showDetails(activity, listOf(e))
}
}

private fun showDetails(activity: FragmentActivity, exceptions: Collection<Throwable>) {
ExceptionDialogFragment.newInstance(ArrayList(exceptions))
.show(activity.supportFragmentManager, ExceptionDialogFragment::class.java.simpleName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static it.niedermann.owncloud.notes.shared.model.ENavigationCategoryType.UNCATEGORIZED;
import static it.niedermann.owncloud.notes.shared.util.SSOUtil.askForNewAccount;

import android.accounts.NetworkErrorException;
import android.animation.AnimatorInflater;
import android.app.SearchManager;
import android.content.Context;
Expand Down Expand Up @@ -71,16 +70,13 @@
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.AccountImportCancelledException;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException;
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException;
import com.nextcloud.android.sso.exceptions.NoCurrentAccountSelectedException;
import com.nextcloud.android.sso.exceptions.TokenMismatchException;
import com.nextcloud.android.sso.exceptions.UnknownErrorException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.owncloud.android.lib.common.utils.Log_OC;

import org.jetbrains.annotations.NotNull;

import java.net.HttpURLConnection;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -169,6 +165,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, A

boolean canMoveNoteToAnotherAccounts = false;


@Override
protected void onCreate(Bundle savedInstanceState) {
SplashScreen.installSplashScreen(this);
Expand Down Expand Up @@ -223,11 +220,7 @@ protected void onCreate(Bundle savedInstanceState) {
mainViewModel.getSyncStatus().observe(this, syncStatus -> swipeRefreshLayout.setRefreshing(syncStatus));
mainViewModel.getSyncErrors().observe(this, exceptions -> {
if (mainViewModel.containsNonInfrastructureRelatedItems(exceptions)) {
BrandedSnackbar.make(coordinatorLayout, SyncErrorMessage.of(exceptions), Snackbar.LENGTH_LONG)
.setAnchorView(binding.activityNotesListView.fabCreate)
.setAction(R.string.simple_more, v -> ExceptionDialogFragment.newInstance(exceptions)
.show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()))
.show();
BrandedSnackbar.showSyncError(this, coordinatorLayout, fabCreate, exceptions);
}
});
mainViewModel.getSelectedCategory().observe(this, (selectedCategory) -> {
Expand Down Expand Up @@ -307,6 +300,7 @@ protected void onCreate(Bundle savedInstanceState) {
});
mainViewModel.getNavigationCategories().observe(this, navigationItems -> this.adapterCategories.setItems(navigationItems));
mainViewModel.getCurrentAccount().observe(this, (nextAccount) -> {
BrandedSnackbar.onCurrentAccountChanged(nextAccount);
fabCreate.hide();
Glide
.with(this)
Expand All @@ -327,17 +321,10 @@ public void onError(@NonNull Throwable t) {
runOnUiThread(() -> {
if (t instanceof IntendedOfflineException) {
Log.i(TAG, "Capabilities and notes not updated because " + nextAccount.getAccountName() + " is offline by intention.");
} else if (t instanceof NetworkErrorException) {
BrandedSnackbar.make(coordinatorLayout, getString(R.string.error_sync, getString(R.string.error_no_network)), Snackbar.LENGTH_LONG)
.setAnchorView(binding.activityNotesListView.fabCreate)
.show();
} else {
BrandedSnackbar.make(coordinatorLayout, SyncErrorMessage.of(t), Snackbar.LENGTH_LONG)
.setAction(R.string.simple_more, v -> ExceptionDialogFragment.newInstance(t)
.show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()))
.setAnchorView(binding.activityNotesListView.fabCreate)
.show();
return;
}

BrandedSnackbar.showSyncError(MainActivity.this, coordinatorLayout, fabCreate, t);
});
}
});
Expand Down Expand Up @@ -618,21 +605,10 @@ public void onError(@NonNull Throwable t) {
swipeRefreshLayout.setRefreshing(false);
if (t instanceof IntendedOfflineException) {
Log.i(TAG, "Capabilities and notes not updated because " + currentAccount.getAccountName() + " is offline by intention.");
} else if (t instanceof NextcloudHttpRequestFailedException && ((NextcloudHttpRequestFailedException) t).getStatusCode() == HttpURLConnection.HTTP_UNAVAILABLE) {
BrandedSnackbar.make(coordinatorLayout, R.string.error_maintenance_mode, Snackbar.LENGTH_LONG)
.setAnchorView(binding.activityNotesListView.fabCreate)
.show();
} else if (t instanceof NetworkErrorException) {
BrandedSnackbar.make(coordinatorLayout, getString(R.string.error_sync, getString(R.string.error_no_network)), Snackbar.LENGTH_LONG)
.setAnchorView(binding.activityNotesListView.fabCreate)
.show();
} else {
BrandedSnackbar.make(coordinatorLayout, SyncErrorMessage.of(t), Snackbar.LENGTH_LONG)
.setAction(R.string.simple_more, v -> ExceptionDialogFragment.newInstance(t)
.show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName()))
.setAnchorView(binding.activityNotesListView.fabCreate)
.show();
return;
}

BrandedSnackbar.showSyncError(MainActivity.this, coordinatorLayout, fabCreate, t);
});
}
});
Expand Down Expand Up @@ -840,8 +816,20 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
default -> {
try {
AccountImporter.onActivityResult(requestCode, resultCode, data, this, (ssoAccount) -> {
ApiProvider.getInstance().invalidateAPICache(ssoAccount);
Comment thread
alperozturk96 marked this conversation as resolved.
Resolved
CapabilitiesWorker.update(this);
executor.submit(() -> {
final var existingAccount = mainViewModel.getLocalAccountByAccountName(ssoAccount.name);
if (existingAccount != null) {
runOnUiThread(() -> {
mainViewModel.postCurrentAccount(existingAccount);
BrandedSnackbar
.make(coordinatorLayout, R.string.account_access_renewed, Snackbar.LENGTH_LONG)
.setAnchorView(binding.activityNotesListView.fabCreate)
.show();
});
return;
}
final var importSnackbar = BrandedSnackbar.make(coordinatorLayout, R.string.progress_import_indeterminate, Snackbar.LENGTH_INDEFINITE)
.setAnchorView(binding.activityNotesListView.fabCreate);
Log.i(TAG, "Added account: " + "name:" + ssoAccount.name + ", " + ssoAccount.url + ", userId" + ssoAccount.userId);
Expand Down Expand Up @@ -879,19 +867,9 @@ public void onError(@NonNull Throwable t) {
} catch (Throwable e) {
importSnackbar.dismiss();
ApiProvider.getInstance().invalidateAPICache(ssoAccount);
// Happens when importing an already existing account the second time
if (e instanceof TokenMismatchException && mainViewModel.getLocalAccountByAccountName(ssoAccount.name) != null) {
Log.w(TAG, "Received " + TokenMismatchException.class.getSimpleName() + " and the given ssoAccount.name (" + ssoAccount.name + ") does already exist in the database. Assume that this account has already been imported.");
runOnUiThread(() -> {
mainViewModel.postCurrentAccount(mainViewModel.getLocalAccountByAccountName(ssoAccount.name));
// TODO there is already a sync in progress and results in displaying a TokenMissMatchException snackbar which conflicts with this one
coordinatorLayout.post(() -> BrandedSnackbar.make(coordinatorLayout, R.string.account_already_imported, Snackbar.LENGTH_LONG)
.setAnchorView(binding.activityNotesListView.fabCreate)
.show());
});
} else if (e instanceof UnknownErrorException && e.getMessage() != null && e.getMessage().contains("No address associated with hostname")) {
if (e instanceof UnknownErrorException && e.getMessage() != null && e.getMessage().contains("No address associated with hostname")) {
// https://github.com/nextcloud/notes-android/issues/1014
runOnUiThread(() -> Snackbar.make(coordinatorLayout, R.string.you_have_to_be_connected_to_the_internet_in_order_to_add_an_account, Snackbar.LENGTH_LONG)
runOnUiThread(() -> BrandedSnackbar.make(coordinatorLayout, R.string.you_have_to_be_connected_to_the_internet_in_order_to_add_an_account, Snackbar.LENGTH_LONG)
.setAnchorView(binding.activityNotesListView.fabCreate)
.show());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,18 @@
*/
package it.niedermann.owncloud.notes.main

import androidx.annotation.StringRes
import android.accounts.NetworkErrorException
import com.nextcloud.android.sso.exceptions.NextcloudApiNotRespondingException
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException
import com.nextcloud.android.sso.exceptions.NextcloudHttpRequestFailedException
import com.nextcloud.android.sso.exceptions.NextcloudNetworkException
import com.nextcloud.android.sso.exceptions.TokenMismatchException
import it.niedermann.owncloud.notes.persistence.NoteSyncException
import it.niedermann.owncloud.notes.persistence.SyncFailureReason

object SyncErrorMessage {
@JvmStatic
@StringRes
fun of(exceptions: Collection<Throwable>): Int =
(exceptions.map { it.syncFailureReason() }.distinct().singleOrNull() ?: SyncFailureReason.UNKNOWN).message

@JvmStatic
@StringRes
fun of(exception: Throwable): Int = exception.syncFailureReason().message
class SyncErrorMessage(private val exceptions: Collection<Throwable>) {
fun reasonOf(): SyncFailureReason =
exceptions.map { it.syncFailureReason() }.distinct().singleOrNull() ?: SyncFailureReason.UNKNOWN

private fun Throwable.syncFailureReason(): SyncFailureReason =
generateSequence(this) { it.cause }.firstNotNullOfOrNull { it.directSyncFailureReason() }
Expand All @@ -33,6 +28,8 @@ object SyncErrorMessage {
is NextcloudHttpRequestFailedException -> SyncFailureReason.fromStatusCode(statusCode)
is NextcloudNetworkException, is NextcloudApiNotRespondingException -> SyncFailureReason.SERVER_UNREACHABLE
is TokenMismatchException -> SyncFailureReason.TOKEN_MISMATCH
is NextcloudFilesAppAccountNotFoundException -> SyncFailureReason.ACCOUNT_REMOVED
is NetworkErrorException -> SyncFailureReason.NO_NETWORK
else -> null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class CapabilitiesClient {

@WorkerThread
public static Capabilities getCapabilities(@NonNull Context context, @NonNull SingleSignOnAccount ssoAccount, @Nullable String lastETag, @NonNull ApiProvider apiProvider) throws Throwable {
FilesAppAccount.ensureExists(context, ssoAccount.name);
final var ocsAPI = apiProvider.getOcsAPI(context, ssoAccount);
try {
final var response = ocsAPI.getCapabilities(lastETag).blockingSingle();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
package it.niedermann.owncloud.notes.persistence

import android.content.Context
import com.nextcloud.android.sso.AccountImporter
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppAccountNotFoundException
import com.nextcloud.android.sso.model.SingleSignOnAccount

object FilesAppAccount {
@JvmStatic
@Throws(NextcloudFilesAppAccountNotFoundException::class)
fun require(context: Context, accountName: String): SingleSignOnAccount {
ensureExists(context, accountName)

return AccountImporter.getSingleSignOnAccount(context, accountName)
}

@JvmStatic
@Throws(NextcloudFilesAppAccountNotFoundException::class)
fun ensureExists(context: Context, accountName: String) {
if (!exists(context, accountName)) {
throw NextcloudFilesAppAccountNotFoundException(context, accountName)
}
}

fun exists(context: Context, accountName: String): Boolean =
AccountImporter.getAccountForName(context, accountName) != null
}
Loading
Loading