Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9a1c7bb authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

Handle already existing account

parent 4c2a01c6
Loading
Loading
Loading
Loading
+60 −60
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import android.animation.AnimatorInflater;
import android.annotation.SuppressLint;
import android.app.SearchManager;
import android.content.Intent;
import android.database.sqlite.SQLiteConstraintException;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
@@ -37,6 +36,7 @@ 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.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

@@ -665,8 +665,8 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Check which request we're responding to
        if (requestCode == create_note_cmd) {
        switch (requestCode) {
            case create_note_cmd: {
                // Make sure the request was successful
                if (resultCode == RESULT_OK) {
                    //not need because of db.synchronisation in createActivity
@@ -684,15 +684,21 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
                    }
                }
                listView.scrollToPosition(0);
        } else if (requestCode == server_settings) {
                break;
            }
            case server_settings: {
                // Recreate activity completely, because theme switching makes problems when only invalidating the views.
                // @see https://github.com/stefan-niedermann/nextcloud-notes/issues/529
                recreate();
        } else if (requestCode == manage_account) {
                break;
            }
            case manage_account: {
                if (resultCode == RESULT_FIRST_USER) {
                    selectAccount(null);
                }
        } else {
                break;
            }
            default: {
                try {
                    AccountImporter.onActivityResult(requestCode, resultCode, data, this, (ssoAccount) -> {
                        CapabilitiesWorker.update(this);
@@ -703,29 +709,22 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
                                final Capabilities capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, null);
                                db.addAccount(ssoAccount.url, ssoAccount.userId, ssoAccount.name, capabilities);
                                Log.i(TAG, capabilities.toString());
                                runOnUiThread(() -> selectAccount(ssoAccount.name));
                            } catch (Exception e) {
                                if (e instanceof TokenMismatchException && db.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(() -> {
                                        selectAccount(ssoAccount.name);
                                binding.accountNavigation.setVisibility(VISIBLE);
                                binding.drawerLayout.closeDrawer(GravityCompat.START);
                            });
                        } catch (SQLiteConstraintException e) {
                            if (db.getAccounts().size() > 1) { // TODO ideally only show snackbar when this is a not migrated account
                                runOnUiThread(() -> {
                                    BrandedSnackbar.make(coordinatorLayout, R.string.account_already_imported, Snackbar.LENGTH_LONG).show();
                                    selectAccount(ssoAccount.name);
                                    binding.accountNavigation.setVisibility(VISIBLE);
                                    binding.drawerLayout.closeDrawer(GravityCompat.START);
                                        coordinatorLayout.post(() -> BrandedSnackbar.make(coordinatorLayout, R.string.account_already_imported, Snackbar.LENGTH_LONG).show());
                                    });
                            }
                        } catch (Exception e) {
                                } else {
                                    e.printStackTrace();
                                    runOnUiThread(() -> {
                                binding.accountNavigation.setVisibility(GONE);
                                binding.drawerLayout.openDrawer(GravityCompat.START);
                                        binding.activityNotesListView.progressCircular.setVisibility(GONE);
                                        ExceptionDialogFragment.newInstance(e).show(getSupportFragmentManager(), ExceptionDialogFragment.class.getSimpleName());
                                    });
                                }
                            }
                        }).start();
                    });
                } catch (AccountImportCancelledException e) {
@@ -733,6 +732,7 @@ public class NotesListViewActivity extends LockedActivity implements NoteClickLi
                }
            }
        }
    }

    private void updateCurrentAccountAvatar() {
        try {
+1 −3
Original line number Diff line number Diff line
@@ -45,9 +45,7 @@ public class SSOUtil {
        try {
            SingleAccountHelper.getCurrentSingleSignOnAccount(context);
            return true;
        } catch (NextcloudFilesAppAccountNotFoundException e) {
            return false;
        } catch (NoCurrentAccountSelectedException e) {
        } catch (NextcloudFilesAppAccountNotFoundException | NoCurrentAccountSelectedException e) {
            return false;
        }
    }
+16 −26
Original line number Diff line number Diff line
@@ -59,12 +59,6 @@
                        android:textSize="24sp" />
                </RelativeLayout>

                <LinearLayout
                    android:id="@+id/accountNavigation"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/navigationList"
                    android:layout_width="match_parent"
@@ -85,10 +79,6 @@
                    tools:itemCount="3"
                    tools:listitem="@layout/item_navigation" />
            </LinearLayout>
            </LinearLayout>

        </androidx.core.widget.NestedScrollView>

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>
 No newline at end of file