1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 11:42:16 -05:00

Working version of moving accounts within the Accounts view.

This commit is contained in:
danapple 2011-09-05 12:38:03 -05:00
parent c67ac4735f
commit 06cabdbde1
6 changed files with 179 additions and 42 deletions

View File

@ -21,6 +21,10 @@
android:title="@string/remove_account_action" /> android:title="@string/remove_account_action" />
<item android:id="@+id/clear_pending" <item android:id="@+id/clear_pending"
android:title="@string/clear_pending_action" /> android:title="@string/clear_pending_action" />
<item android:id="@+id/move_up"
android:title="@string/manage_accounts_move_up_action" />
<item android:id="@+id/move_down"
android:title="@string/manage_accounts_move_down_action" />
</menu> </menu>
</item> </item>
</menu> </menu>

View File

@ -1073,4 +1073,7 @@ Welcome to K-9 Mail setup. K-9 is an open source mail client for Android origin
<string name="attachment_save_title">Save attachment</string> <string name="attachment_save_title">Save attachment</string>
<string name="attachment_save_desc">No file browser found. Where would you like to save this attachment?</string> <string name="attachment_save_desc">No file browser found. Where would you like to save this attachment?</string>
<string name="manage_accounts_move_up_action">Move up</string>
<string name="manage_accounts_move_down_action">Move down</string>
</resources> </resources>

View File

@ -21,7 +21,9 @@ import com.fsck.k9.view.ColorChip;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Calendar; import java.util.Calendar;
import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
@ -377,19 +379,29 @@ public class Account implements BaseAccount {
mCryptoAutoSignature = prefs.getBoolean(mUuid + ".cryptoAutoSignature", false); mCryptoAutoSignature = prefs.getBoolean(mUuid + ".cryptoAutoSignature", false);
} }
private String combineUuids(String[] uuids) {
protected synchronized void delete(Preferences preferences) {
String[] uuids = preferences.getPreferences().getString("accountUuids", "").split(",");
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
for (int i = 0, length = uuids.length; i < length; i++) { for (int i = 0, length = uuids.length; i < length; i++) {
if (!uuids[i].equals(mUuid)) {
if (sb.length() > 0) { if (sb.length() > 0) {
sb.append(','); sb.append(',');
} }
sb.append(uuids[i]); sb.append(uuids[i]);
} }
}
String accountUuids = sb.toString(); String accountUuids = sb.toString();
return accountUuids;
}
protected synchronized void delete(Preferences preferences) {
String[] uuids = preferences.getPreferences().getString("accountUuids", "").split(",");
String[] newUuids = new String[uuids.length - 1];
int i = 0;
for (String uuid : uuids) {
if (uuid.equals(mUuid) == false) {
newUuids[i++] = uuid;
}
}
String accountUuids = combineUuids(newUuids);
SharedPreferences.Editor editor = preferences.getPreferences().edit(); SharedPreferences.Editor editor = preferences.getPreferences().edit();
editor.putString("accountUuids", accountUuids); editor.putString("accountUuids", accountUuids);
@ -455,6 +467,64 @@ public class Account implements BaseAccount {
editor.commit(); editor.commit();
} }
public static int findNewAccountNumber(List<Integer> accountNumbers) {
int newAccountNumber = -1;
Collections.sort(accountNumbers);
for (int accountNumber : accountNumbers) {
if (accountNumber > newAccountNumber + 1) {
break;
}
newAccountNumber = accountNumber;
}
newAccountNumber++;
return newAccountNumber;
}
public static List<Integer> getExistingAccountNumbers(Preferences preferences) {
Account[] accounts = preferences.getAccounts();
List<Integer> accountNumbers = new LinkedList<Integer>();
for (int i = 0; i < accounts.length; i++) {
accountNumbers.add(accounts[i].getAccountNumber());
}
return accountNumbers;
}
public static int generateAccountNumber(Preferences preferences) {
List<Integer> accountNumbers = getExistingAccountNumbers(preferences);
return findNewAccountNumber(accountNumbers);
}
public void move(Preferences preferences, boolean moveUp) {
String[] uuids = preferences.getPreferences().getString("accountUuids", "").split(",");
SharedPreferences.Editor editor = preferences.getPreferences().edit();
String[] newUuids = new String[uuids.length];
if (moveUp) {
for (int i = 0; i < uuids.length; i++) {
if (i > 0 && uuids[i].equals(mUuid)) {
newUuids[i] = newUuids[i-1];
newUuids[i-1] = mUuid;
}
else {
newUuids[i] = uuids[i];
}
}
}
else {
for (int i = uuids.length - 1; i >= 0; i--) {
if (i < uuids.length - 1 && uuids[i].equals(mUuid)) {
newUuids[i] = newUuids[i+1];
newUuids[i+1] = mUuid;
}
else {
newUuids[i] = uuids[i];
}
}
}
String accountUuids = combineUuids(newUuids);
editor.putString("accountUuids", accountUuids);
editor.commit();
preferences.refreshAccounts();
}
public synchronized void save(Preferences preferences) { public synchronized void save(Preferences preferences) {
SharedPreferences.Editor editor = preferences.getPreferences().edit(); SharedPreferences.Editor editor = preferences.getPreferences().edit();

View File

@ -3,7 +3,11 @@ package com.fsck.k9;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.util.Config; import android.util.Config;
@ -29,7 +33,8 @@ public class Preferences {
private Storage mStorage; private Storage mStorage;
private List<Account> accounts; private Map<String, Account> accounts = null;
private List<Account> accountsInOrder = null;
private Account newAccount; private Account newAccount;
private Context mContext; private Context mContext;
@ -45,17 +50,36 @@ public class Preferences {
} }
private synchronized void loadAccounts() { private synchronized void loadAccounts() {
accounts = new HashMap<String, Account>();
refreshAccounts();
}
public synchronized void refreshAccounts() {
Map<String, Account> newAccountMap = new HashMap<String, Account>();
accountsInOrder = new LinkedList<Account>();
String accountUuids = getPreferences().getString("accountUuids", null); String accountUuids = getPreferences().getString("accountUuids", null);
if ((accountUuids != null) && (accountUuids.length() != 0)) { if ((accountUuids != null) && (accountUuids.length() != 0)) {
String[] uuids = accountUuids.split(","); String[] uuids = accountUuids.split(",");
accounts = new ArrayList<Account>(uuids.length);
for (String uuid : uuids) { for (String uuid : uuids) {
accounts.add(new Account(this, uuid)); Account account = accounts.get(uuid);
} if (account != null) {
newAccountMap.put(uuid, account);
accountsInOrder.add(account);
} else { } else {
accounts = new ArrayList<Account>(); Account newAccount = new Account(this, uuid);
newAccountMap.put(uuid, newAccount);
accountsInOrder.add(newAccount);
} }
} }
}
if ((newAccount != null) && newAccount.getAccountNumber() != -1) {
newAccountMap.put(newAccount.getUuid(), newAccount);
accountsInOrder.add(newAccount);
newAccount = null;
}
accounts = newAccountMap;
}
/** /**
* Returns an array of the accounts on the system. If no accounts are * Returns an array of the accounts on the system. If no accounts are
@ -67,12 +91,7 @@ public class Preferences {
loadAccounts(); loadAccounts();
} }
if ((newAccount != null) && newAccount.getAccountNumber() != -1) { return accountsInOrder.toArray(EMPTY_ACCOUNT_ARRAY);
accounts.add(newAccount);
newAccount = null;
}
return accounts.toArray(EMPTY_ACCOUNT_ARRAY);
} }
/** /**
@ -81,16 +100,9 @@ public class Preferences {
* @return all accounts with {@link Account#isAvailable(Context)} * @return all accounts with {@link Account#isAvailable(Context)}
*/ */
public synchronized Collection<Account> getAvailableAccounts() { public synchronized Collection<Account> getAvailableAccounts() {
if (accounts == null) { Account[] allAccounts = getAccounts();
loadAccounts();
}
if ((newAccount != null) && newAccount.getAccountNumber() != -1) {
accounts.add(newAccount);
newAccount = null;
}
Collection<Account> retval = new ArrayList<Account>(accounts.size()); Collection<Account> retval = new ArrayList<Account>(accounts.size());
for (Account account : accounts) { for (Account account : allAccounts) {
if (account.isAvailable(mContext)) { if (account.isAvailable(mContext)) {
retval.add(account); retval.add(account);
} }
@ -103,28 +115,22 @@ public class Preferences {
if (accounts == null) { if (accounts == null) {
loadAccounts(); loadAccounts();
} }
Account account = accounts.get(uuid);
for (Account account : accounts) {
if (account.getUuid().equals(uuid)) {
return account; return account;
} }
}
if ((newAccount != null) && newAccount.getUuid().equals(uuid)) {
return newAccount;
}
return null;
}
public synchronized Account newAccount() { public synchronized Account newAccount() {
newAccount = new Account(K9.app); newAccount = new Account(K9.app);
accounts.put(newAccount.getUuid(), newAccount);
accountsInOrder.add(newAccount);
return newAccount; return newAccount;
} }
public synchronized void deleteAccount(Account account) { public synchronized void deleteAccount(Account account) {
accounts.remove(account); accounts.remove(account.getUuid());
accountsInOrder.remove(account);
account.delete(this); account.delete(this);
if (newAccount == account) { if (newAccount == account) {

View File

@ -584,6 +584,12 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
case R.id.recreate: case R.id.recreate:
onRecreate(realAccount); onRecreate(realAccount);
break; break;
case R.id.move_up:
onMove(realAccount, true);
break;
case R.id.move_down:
onMove(realAccount, false);
break;
} }
return true; return true;
} }
@ -602,7 +608,22 @@ public class Accounts extends K9ListActivity implements OnItemClickListener, OnC
private void onRecreate(Account account) { private void onRecreate(Account account) {
showDialog(DIALOG_RECREATE_ACCOUNT); showDialog(DIALOG_RECREATE_ACCOUNT);
} }
private void onMove(final Account account, final boolean up) {
AsyncUIProcessor.getInstance(getApplication()).execute(
new Runnable()
{
@Override
public void run() {
account.move(Preferences.getPreferences(Accounts.this), up);
runOnUiThread(new Runnable() {
@Override
public void run() {
refresh();
}
});
}
});
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
BaseAccount account = (BaseAccount)parent.getItemAtPosition(position); BaseAccount account = (BaseAccount)parent.getItemAtPosition(position);

View File

@ -0,0 +1,33 @@
package com.fsck.k9.activity;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import android.app.Application;
/**
* The class should be used to run long-running processes invoked from the UI that
* do not affect the Stores. There are probably pieces of MessagingController
* that can be moved here. There is no wakelock used here. Any network activity, or
* true background activity, that is invoked from here should wakelock itself. UI-centric
* activity does not need to be wakelocked, as it will simply continue when the phone wakes
* without disruption.
*
*/
public class AsyncUIProcessor {
private final ExecutorService threadPool = Executors.newCachedThreadPool();
private static AsyncUIProcessor inst = null;
private AsyncUIProcessor() {
}
public synchronized static AsyncUIProcessor getInstance(Application application) {
if (inst == null) {
inst = new AsyncUIProcessor();
}
return inst;
}
public void execute(Runnable runnable) {
threadPool.execute(runnable);
}
}