mirror of
https://github.com/nextcloud/android.git
synced 2024-11-28 02:17:43 +03:00
change detection for the drawer implementation
This commit is contained in:
parent
f7178fa837
commit
9c2fe3f4fd
2 changed files with 94 additions and 23 deletions
|
@ -55,6 +55,7 @@ import com.owncloud.android.utils.BitmapUtils;
|
|||
public abstract class DrawerActivity extends ToolbarActivity {
|
||||
private static final String TAG = DrawerActivity.class.getSimpleName();
|
||||
private static final String KEY_IS_ACCOUNT_CHOOSER_ACTIVE = "IS_ACCOUNT_CHOOSER_ACTIVE";
|
||||
private static final int ACTION_MANAGE_ACCOUNTS = 101;
|
||||
|
||||
/**
|
||||
* Reference to the drawer layout.
|
||||
|
@ -189,7 +190,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
|
|||
case R.id.drawer_menu_account_manage:
|
||||
Intent manageAccountsIntent = new Intent(getApplicationContext(),
|
||||
ManageAccountsActivity.class);
|
||||
startActivity(manageAccountsIntent);
|
||||
startActivityForResult(manageAccountsIntent, ACTION_MANAGE_ACCOUNTS);
|
||||
break;
|
||||
case Menu.NONE:
|
||||
// account clicked
|
||||
|
@ -265,12 +266,8 @@ public abstract class DrawerActivity extends ToolbarActivity {
|
|||
/**
|
||||
* updates the account list in the drawer.
|
||||
*/
|
||||
// TODO call updateAccountList() after n.o. accounts changed
|
||||
public void updateAccountList() {
|
||||
AccountManager am = (AccountManager) this.getSystemService(this.ACCOUNT_SERVICE);
|
||||
|
||||
// populate UI
|
||||
Account[] accounts = am.getAccountsByType(MainApp.getAccountType());
|
||||
Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
|
||||
if(accounts.length > 0) {
|
||||
repopulateAccountList(accounts);
|
||||
setUsernameInDrawer(AccountUtils.getCurrentOwnCloudAccount(this).name);
|
||||
|
@ -284,7 +281,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
|
|||
*/
|
||||
private void repopulateAccountList(Account[] accounts) {
|
||||
// remove all accounts from list
|
||||
mNavigationView.getMenu().removeItem(Menu.NONE);
|
||||
mNavigationView.getMenu().removeGroup(R.id.drawer_menu_accounts);
|
||||
|
||||
// add all accounts to list
|
||||
for (int i = 0; i < accounts.length; i++) {
|
||||
|
@ -292,14 +289,20 @@ public abstract class DrawerActivity extends ToolbarActivity {
|
|||
int[] rgb = BitmapUtils.calculateRGB(accounts[i].name);
|
||||
TextDrawable icon = new TextDrawable(accounts[i].name.substring(0, 1).toUpperCase()
|
||||
, rgb[0], rgb[1], rgb[2]);
|
||||
mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 0, accounts[i].name).setIcon(icon);
|
||||
mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 1, accounts[i].name).setIcon(icon);
|
||||
} catch (Exception e) {
|
||||
Log_OC.e(TAG, "Error calculating RGB value for account menu item.", e);
|
||||
mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 0, accounts[i].name).setIcon(R
|
||||
mNavigationView.getMenu().add(R.id.drawer_menu_accounts, Menu.NONE, 1, accounts[i].name).setIcon(R
|
||||
.drawable.ic_account_circle);
|
||||
}
|
||||
}
|
||||
|
||||
// re-add add-account and manage-accounts
|
||||
mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_add, 2,
|
||||
getResources().getString(R.string.prefs_add_account)).setIcon(R.drawable.ic_account_plus);
|
||||
mNavigationView.getMenu().add(R.id.drawer_menu_accounts, R.id.drawer_menu_account_manage, 2,
|
||||
getResources().getString(R.string.drawer_manage_accounts)).setIcon(R.drawable.ic_settings);
|
||||
|
||||
// adding sets menu group back to visible, so safety check and setting invisible
|
||||
showMenu();
|
||||
}
|
||||
|
@ -431,6 +434,27 @@ public abstract class DrawerActivity extends ToolbarActivity {
|
|||
super.onBackPressed();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
super.onActivityResult(requestCode, resultCode, data);
|
||||
|
||||
// update Account list and active account if Manage Account activity replies with
|
||||
// - ACCOUNT_LIST_CHANGED = true
|
||||
// - RESULT_OK
|
||||
if (requestCode == ACTION_MANAGE_ACCOUNTS
|
||||
&& resultCode == RESULT_OK
|
||||
&& data.getBooleanExtra(ManageAccountsActivity.KEY_ACCOUNT_LIST_CHANGED, false)) {
|
||||
|
||||
// current account has changed
|
||||
if(data.getBooleanExtra(ManageAccountsActivity.KEY_CURRENT_ACCOUNT_CHANGED, false)) {
|
||||
mCurrentAccount = AccountUtils.getCurrentOwnCloudAccount(this);
|
||||
restart();
|
||||
} else {
|
||||
updateAccountList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a view that was identified by the id attribute from the drawer header.
|
||||
*
|
||||
|
@ -441,6 +465,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
|
|||
return ((NavigationView) findViewById(R.id.nav_view)).getHeaderView(0).findViewById(id);
|
||||
}
|
||||
|
||||
// TODO call on current account changed
|
||||
public void restart() {
|
||||
Intent i = new Intent(this, FileDisplayActivity.class);
|
||||
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
|
@ -535,6 +560,7 @@ public abstract class DrawerActivity extends ToolbarActivity {
|
|||
}
|
||||
|
||||
DrawerActivity.this.updateAccountList();
|
||||
DrawerActivity.this.restart();
|
||||
} catch (OperationCanceledException e) {
|
||||
Log_OC.d(TAG, "Account creation canceled");
|
||||
|
||||
|
|
|
@ -31,10 +31,7 @@ import android.content.ServiceConnection;
|
|||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.ListView;
|
||||
|
||||
import com.owncloud.android.MainApp;
|
||||
|
@ -51,13 +48,17 @@ import com.owncloud.android.ui.adapter.AccountListAdapter;
|
|||
import com.owncloud.android.ui.adapter.AccountListItem;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Managing the accounts.
|
||||
* An Activity that allows the user to manage accounts.
|
||||
*/
|
||||
public class ManageAccountsActivity extends ToolbarActivity
|
||||
implements AccountListAdapter.AccountListAdapterListener, AccountManagerCallback<Boolean>, ComponentsGetter {
|
||||
private static final String TAG = ManageAccountsActivity.class.getSimpleName();
|
||||
public static final String KEY_ACCOUNT_LIST_CHANGED = "ACCOUNT_LIST_CHANGED";
|
||||
public static final String KEY_CURRENT_ACCOUNT_CHANGED = "CURRENT_ACCOUNT_CHANGED";
|
||||
|
||||
private ListView mListView;
|
||||
private final Handler mHandler = new Handler();
|
||||
|
@ -66,6 +67,8 @@ public class ManageAccountsActivity extends ToolbarActivity
|
|||
protected FileUploader.FileUploaderBinder mUploaderBinder = null;
|
||||
protected FileDownloader.FileDownloaderBinder mDownloaderBinder = null;
|
||||
private ServiceConnection mDownloadServiceConnection, mUploadServiceConnection = null;
|
||||
Set<String> mOriginalAccounts;
|
||||
String mOriginalCurrentAccount;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -78,6 +81,10 @@ public class ManageAccountsActivity extends ToolbarActivity
|
|||
setupToolbar();
|
||||
updateActionBarTitleAndHomeButtonByString(getResources().getString(R.string.prefs_manage_accounts));
|
||||
|
||||
Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
|
||||
mOriginalAccounts = toAccountNameSet(accountList);
|
||||
mOriginalCurrentAccount = AccountUtils.getCurrentOwnCloudAccount(this).name;
|
||||
|
||||
mAccountListAdapter = new AccountListAdapter(this, getAccountListItems());
|
||||
|
||||
mListView.setAdapter(mAccountListAdapter);
|
||||
|
@ -85,6 +92,52 @@ public class ManageAccountsActivity extends ToolbarActivity
|
|||
initializeComponentGetters();
|
||||
}
|
||||
|
||||
/**
|
||||
* converts an array of accounts into a set of account names.
|
||||
*
|
||||
* @param accountList the account array
|
||||
* @return set of account names
|
||||
*/
|
||||
private Set<String> toAccountNameSet(Account[] accountList) {
|
||||
Set<String> actualAccounts = new HashSet<String>(accountList.length);
|
||||
for (Account account : accountList) {
|
||||
actualAccounts.add(account.name);
|
||||
}
|
||||
return actualAccounts;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
Intent resultIntent = new Intent();
|
||||
resultIntent.putExtra(KEY_ACCOUNT_LIST_CHANGED, hasAccountListChanged());
|
||||
resultIntent.putExtra(KEY_CURRENT_ACCOUNT_CHANGED, hasCurrentAccountChanged());
|
||||
setResult(RESULT_OK, resultIntent);
|
||||
|
||||
finish();
|
||||
super.onBackPressed();
|
||||
}
|
||||
|
||||
/**
|
||||
* checks the set of actual accounts against the set of original accounts when the activity has been started.
|
||||
*
|
||||
* @return <code>true</code> if aacount list has changed, <code>false</code> if not
|
||||
*/
|
||||
private boolean hasAccountListChanged() {
|
||||
Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
|
||||
Set<String> actualAccounts = toAccountNameSet(accountList);
|
||||
return !mOriginalAccounts.equals(actualAccounts);
|
||||
}
|
||||
|
||||
/**
|
||||
* checks actual current account against current accounts when the activity has been started.
|
||||
*
|
||||
* @return <code>true</code> if aacount list has changed, <code>false</code> if not
|
||||
*/
|
||||
private boolean hasCurrentAccountChanged() {
|
||||
String currentAccount = AccountUtils.getCurrentOwnCloudAccount(this).name;
|
||||
return !mOriginalCurrentAccount.equals(currentAccount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize ComponentsGetters.
|
||||
*/
|
||||
|
@ -106,10 +159,8 @@ public class ManageAccountsActivity extends ToolbarActivity
|
|||
*
|
||||
* @return list of account list items
|
||||
*/
|
||||
@NonNull
|
||||
private ArrayList<AccountListItem> getAccountListItems() {
|
||||
AccountManager am = (AccountManager) this.getSystemService(this.ACCOUNT_SERVICE);
|
||||
Account[] accountList = am.getAccountsByType(MainApp.getAccountType());
|
||||
Account[] accountList = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
|
||||
ArrayList<AccountListItem> adapterAccountList = new ArrayList<AccountListItem>(accountList.length);
|
||||
for (Account account : accountList) {
|
||||
adapterAccountList.add(new AccountListItem(account));
|
||||
|
@ -206,8 +257,7 @@ public class ManageAccountsActivity extends ToolbarActivity
|
|||
Account a = AccountUtils.getCurrentOwnCloudAccount(this);
|
||||
String accountName = "";
|
||||
if (a == null) {
|
||||
Account[] accounts = AccountManager.get(this)
|
||||
.getAccountsByType(MainApp.getAccountType());
|
||||
Account[] accounts = AccountManager.get(this).getAccountsByType(MainApp.getAccountType());
|
||||
if (accounts.length != 0)
|
||||
accountName = accounts[0].name;
|
||||
AccountUtils.setCurrentOwnCloudAccount(this, accountName);
|
||||
|
@ -276,10 +326,7 @@ public class ManageAccountsActivity extends ToolbarActivity
|
|||
} else if (component.equals(new ComponentName(ManageAccountsActivity.this, FileUploader.class))) {
|
||||
Log_OC.d(TAG, "Upload service connected");
|
||||
mUploaderBinder = (FileUploader.FileUploaderBinder) service;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -293,6 +340,4 @@ public class ManageAccountsActivity extends ToolbarActivity
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue