Skip to content

Commit

Permalink
Fix #81 Single Sign-on not working with Android 7
Browse files Browse the repository at this point in the history
- move login logic into drawer activity
  • Loading branch information
stefan-niedermann committed Jun 14, 2019
1 parent 1ae4aab commit 3b23e98
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 134 deletions.
133 changes: 76 additions & 57 deletions app/src/main/java/it/niedermann/nextcloud/deck/ui/DrawerActivity.java
Expand Up @@ -5,6 +5,7 @@
import android.database.sqlite.SQLiteConstraintException;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -23,8 +24,12 @@

import com.google.android.material.navigation.NavigationView;
import com.google.android.material.snackbar.Snackbar;
import com.nextcloud.android.sso.AccountImporter;
import com.nextcloud.android.sso.exceptions.AndroidGetAccountsPermissionNotGranted;
import com.nextcloud.android.sso.exceptions.NextcloudFilesAppNotInstalledException;
import com.nextcloud.android.sso.helper.SingleAccountHelper;
import com.nextcloud.android.sso.model.SingleSignOnAccount;
import com.nextcloud.android.sso.ui.UiExceptionManager;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,7 +44,6 @@
import it.niedermann.nextcloud.deck.persistence.sync.adapters.db.util.WrappedLiveData;
import it.niedermann.nextcloud.deck.ui.board.EditBoardDialogFragment;
import it.niedermann.nextcloud.deck.ui.exception.ExceptionHandler;
import it.niedermann.nextcloud.deck.ui.login.LoginDialogFragment;
import it.niedermann.nextcloud.deck.util.ViewUtil;

public abstract class DrawerActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
Expand All @@ -63,11 +67,44 @@ public abstract class DrawerActivity extends AppCompatActivity implements Naviga
protected List<Account> accountsList = new ArrayList<>();
protected Account account;
protected boolean accountChooserActive = false;
private LoginDialogFragment loginDialogFragment;
protected SyncManager syncManager;
protected SharedPreferences sharedPreferences;
private HeaderViewHolder headerViewHolder;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
AccountImporter.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

AccountImporter.onActivityResult(requestCode, resultCode, data, this, (SingleSignOnAccount account) -> {
Account acc = new Account();
acc.setName(account.name);
acc.setUserName(account.userId);
acc.setUrl(account.url);
final WrappedLiveData<Account> accountLiveData = this.syncManager.createAccount(acc);
accountLiveData.observe(this, (Account ac) -> {
if (accountLiveData.hasError()) {
try {
accountLiveData.throwError();
} catch (SQLiteConstraintException ex) {
Snackbar.make(coordinatorLayout, "Account bereits hinzugefügt", Snackbar.LENGTH_SHORT).show();
}
} else {
Snackbar.make(coordinatorLayout, "Account hinzugefügt", Snackbar.LENGTH_SHORT).show();
}
});

SingleAccountHelper.setCurrentAccount(getApplicationContext(), account.name);
});
}


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -94,35 +131,33 @@ protected void onCreate(Bundle savedInstanceState) {
long lastAccountId = sharedPreferences.getLong(getString(R.string.shared_preference_last_account), NO_ACCOUNTS);
DeckLog.log("--- Read: shared_preference_last_account" + " | " + lastAccountId);

if (accounts.size() >= lastAccountId) {
if (lastAccountId == NO_ACCOUNTS || accountsList.size() == 1) {
this.account = accountsList.get(0);
} else {
for (Account account : accounts) {
if (lastAccountId == account.getId()) {
this.account = account;
break;
}
}
}
for (Account account : accounts) {
if (lastAccountId == account.getId() || lastAccountId == NO_ACCOUNTS) {
this.account = account;
SingleAccountHelper.setCurrentAccount(getApplicationContext(), this.account.getName());
setHeaderView();
syncManager = new SyncManager(this);
ViewUtil.addAvatar(this, headerViewHolder.currentAccountAvatar, this.account.getUrl(), this.account.getUserName());
// TODO show spinner
syncManager.synchronize(new IResponseCallback<Boolean>(this.account) {
@Override
public void onResponse(Boolean response) {
//nothing
}
});
accountSet(this.account);

SingleAccountHelper.setCurrentAccount(getApplicationContext(), this.account.getName());
setHeaderView();
syncManager = new SyncManager(this);
ViewUtil.addAvatar(this, headerViewHolder.currentAccountAvatar, this.account.getUrl(), this.account.getUserName());
// TODO show spinner
syncManager.synchronize(new IResponseCallback<Boolean>(this.account) {
@Override
public void onResponse(Boolean response) {
//nothing
// Remember last account
SharedPreferences.Editor editor = sharedPreferences.edit();
DeckLog.log("--- Write: shared_preference_last_account" + " | " + this.account.getId());
editor.putLong(getString(R.string.shared_preference_last_account), this.account.getId());
editor.apply();
break;
}
});
accountSet(this.account);
}
}
});
} else {
loginDialogFragment = new LoginDialogFragment();
loginDialogFragment.show(this.getSupportFragmentManager(), "NoticeDialogFragment");
showAccountPicker();
}
});

Expand All @@ -136,6 +171,19 @@ public void onResponse(Boolean response) {
});
}

private void showAccountPicker() {
try {
AccountImporter.pickNewAccount(this);
} catch (NextcloudFilesAppNotInstalledException e) {
UiExceptionManager.showDialogForException(this, e);
Log.w("Deck", "=============================================================");
Log.w("Deck", "Nextcloud app is not installed. Cannot choose account");
e.printStackTrace();
} catch (AndroidGetAccountsPermissionNotGranted e) {
AccountImporter.requestAndroidAccountPermissionsAndPickAccount(this);
}
}

@Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
Expand All @@ -152,8 +200,7 @@ public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (accountChooserActive) {
switch (item.getItemId()) {
case MENU_ID_ADD_ACCOUNT:
loginDialogFragment = new LoginDialogFragment();
loginDialogFragment.show(this.getSupportFragmentManager(), "NoticeDialogFragment");
showAccountPicker();
break;
default:
this.account = accountsList.get(item.getItemId());
Expand Down Expand Up @@ -192,34 +239,6 @@ protected void setHeaderView() {
((TextView) navigationView.getHeaderView(0).findViewById(R.id.drawer_username_full)).setText(account.getName());
}

public void onAccountChoose(SingleSignOnAccount account) {
getSupportFragmentManager().beginTransaction().remove(loginDialogFragment).commit();
Account acc = new Account();
acc.setName(account.name);
acc.setUserName(account.userId);
acc.setUrl(account.url);
final WrappedLiveData<Account> accountLiveData = this.syncManager.createAccount(acc);
accountLiveData.observe(this, (Account ac) -> {
if (accountLiveData.hasError()) {
try {
accountLiveData.throwError();
} catch (SQLiteConstraintException ex) {
Snackbar.make(coordinatorLayout, "Account bereits hinzugefügt", Snackbar.LENGTH_SHORT).show();
}
} else {
syncManager.synchronize(new IResponseCallback<Boolean>(ac) {
@Override
public void onResponse(Boolean response) {

}
});
Snackbar.make(coordinatorLayout, "Account hinzugefügt", Snackbar.LENGTH_SHORT).show();
}
});

SingleAccountHelper.setCurrentAccount(getApplicationContext(), account.name);
}

private void buildSidenavAccountChooser() {
Menu menu = navigationView.getMenu();
menu.clear();
Expand Down
60 changes: 31 additions & 29 deletions app/src/main/java/it/niedermann/nextcloud/deck/ui/MainActivity.java
Expand Up @@ -80,7 +80,7 @@ protected void onCreate(Bundle savedInstanceState) {
dragAndDrop.register(viewPager);
dragAndDrop.addCardMovedByDragListener((movedCard, stackId, position) -> {
//FIXME: implement me por favour!
DeckLog.log("Card \""+movedCard.getCard().getTitle()+"\" was moved to Stack "+stackId+" on position "+position);
DeckLog.log("Card \"" + movedCard.getCard().getTitle() + "\" was moved to Stack " + stackId + " on position " + position);
});

fab.setOnClickListener((View view) -> {
Expand Down Expand Up @@ -203,35 +203,37 @@ protected void buildSidenavMenu() {
Menu menu = navigationView.getMenu();
menu.clear();
SubMenu boardsMenu = menu.addSubMenu(getString(R.string.simple_boards));
int index = 0;
for (Board board : boardsList) {
MenuItem m = boardsMenu.add(Menu.NONE, index++, Menu.NONE, board.getTitle()).setIcon(ViewUtil.getTintedImageView(this, R.drawable.circle_grey600_36dp, "#" + board.getColor()));
AppCompatImageButton contextMenu = new AppCompatImageButton(this);
contextMenu.setBackgroundDrawable(null);
contextMenu.setImageDrawable(ViewUtil.getTintedImageView(this, R.drawable.ic_menu, R.color.grey600));
contextMenu.setOnClickListener((v) -> {
PopupMenu popup = new PopupMenu(MainActivity.this, contextMenu);
popup.getMenuInflater()
.inflate(R.menu.navigation_context_menu, popup.getMenu());
popup.setOnMenuItemClickListener((MenuItem item) -> {
switch (item.getItemId()) {
case R.id.edit_board:
EditBoardDialogFragment.newInstance(account.getId(), board.getLocalId()).show(getSupportFragmentManager(), getString(R.string.edit_board));
break;
case R.id.archive_board:
// TODO implement
Snackbar.make(drawer, "Archiving boards is not yet supported.", Snackbar.LENGTH_LONG).show();
break;
case R.id.delete_board:
// TODO select next available board
syncManager.deleteBoard(board);
break;
}
return true;
if (boardsList != null) {
int index = 0;
for (Board board : boardsList) {
MenuItem m = boardsMenu.add(Menu.NONE, index++, Menu.NONE, board.getTitle()).setIcon(ViewUtil.getTintedImageView(this, R.drawable.circle_grey600_36dp, "#" + board.getColor()));
AppCompatImageButton contextMenu = new AppCompatImageButton(this);
contextMenu.setBackgroundDrawable(null);
contextMenu.setImageDrawable(ViewUtil.getTintedImageView(this, R.drawable.ic_menu, R.color.grey600));
contextMenu.setOnClickListener((v) -> {
PopupMenu popup = new PopupMenu(MainActivity.this, contextMenu);
popup.getMenuInflater()
.inflate(R.menu.navigation_context_menu, popup.getMenu());
popup.setOnMenuItemClickListener((MenuItem item) -> {
switch (item.getItemId()) {
case R.id.edit_board:
EditBoardDialogFragment.newInstance(account.getId(), board.getLocalId()).show(getSupportFragmentManager(), getString(R.string.edit_board));
break;
case R.id.archive_board:
// TODO implement
Snackbar.make(drawer, "Archiving boards is not yet supported.", Snackbar.LENGTH_LONG).show();
break;
case R.id.delete_board:
// TODO select next available board
syncManager.deleteBoard(board);
break;
}
return true;
});
popup.show();
});
popup.show();
});
m.setActionView(contextMenu);
m.setActionView(contextMenu);
}
}
boardsMenu.add(Menu.NONE, MENU_ID_ADD_BOARD, Menu.NONE, getString(R.string.add_board)).setIcon(R.drawable.ic_add_grey_24dp);
menu.add(Menu.NONE, MENU_ID_ABOUT, Menu.NONE, getString(R.string.about)).setIcon(R.drawable.ic_info_outline_grey_24dp);
Expand Down

This file was deleted.

0 comments on commit 3b23e98

Please sign in to comment.