+
+ /**
+ * Create the list of accounts that has been added into the app
+ */
+ @SuppressWarnings("deprecation")
+ private void addAccountsCheckboxPreferences() {
+
+ // Remove accounts in case list is refreshing for avoiding to have
+ // duplicate items
+ if (mAccountsPrefCategory.getPreferenceCount() > 0) {
+ mAccountsPrefCategory.removeAll();
+ }
+
+ AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
+ Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
+ Account currentAccount = AccountUtils.getCurrentOwnCloudAccount(getApplicationContext());
+
+ if (am.getAccountsByType(MainApp.getAccountType()).length == 0) {
+ // Show create account screen if there isn't any account
+ am.addAccount(MainApp.getAccountType(), null, null, null, this,
+ null,
+ null);
+ }
+ else {
+
+ for (Account a : accounts) {
+ LongClickableCheckBoxPreference accountPreference = new LongClickableCheckBoxPreference(this);
+ accountPreference.setKey(a.name);
+ accountPreference.setTitle(a.name);
+ mAccountsPrefCategory.addPreference(accountPreference);
+
+ // Check the current account that is being used
+ if (a.name.equals(currentAccount.name)) {
+ accountPreference.setChecked(true);
+ } else {
+ accountPreference.setChecked(false);
+ }
+
+ accountPreference.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
+ @Override
+ public boolean onPreferenceChange(Preference preference, Object newValue) {
+ String key = preference.getKey();
+ AccountManager am = (AccountManager) getSystemService(ACCOUNT_SERVICE);
+ Account accounts[] = am.getAccountsByType(MainApp.getAccountType());
+ for (Account a : accounts) {
+ CheckBoxPreference p = (CheckBoxPreference) findPreference(a.name);
+ if (key.equals(a.name)) {
+ p.setChecked(true);
+ AccountUtils.setCurrentOwnCloudAccount(getApplicationContext(), a.name);
+ } else {
+ p.setChecked(false);
+ }
+ }
+ return (Boolean) newValue;
+ }
+ });
+
+ }
+
+ // Add Create Account preference at the end of account list if
+ // Multiaccount is enabled
+ if (getResources().getBoolean(R.bool.multiaccount_support)) {
+ createAddAccountPreference();
+ }
+
+ }
+ }
+
+ /**
+ * Create the preference for allow adding new accounts
+ */
+ private void createAddAccountPreference() {
+ Preference addAccountPref = new Preference(this);
+ addAccountPref.setKey("add_account");
+ addAccountPref.setTitle(getString(R.string.prefs_add_account));
+ mAccountsPrefCategory.addPreference(addAccountPref);
+
+ addAccountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
+ @Override
+ public boolean onPreferenceClick(Preference preference) {
+ AccountManager am = AccountManager.get(getApplicationContext());
+ am.addAccount(MainApp.getAccountType(), null, null, null, Preferences.this, null, null);
+ return true;
+ }
+ });
+
+ }
+
+ @Override
+ protected void onPause() {
+ if (this.isFinishing()) {
+ Account current = AccountUtils.getCurrentOwnCloudAccount(this);
+ if ((mPreviousAccount == null && current != null)
+ || (mPreviousAccount != null && !mPreviousAccount.equals(current))) {
+ // the account set as default changed since this activity was
+ // created
+
+ // restart the main activity
+ Intent i = new Intent(this, FileDisplayActivity.class);
+ i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
+ startActivity(i);
+ }
+ }
+ super.onPause();
+ }
+