Only show AccountSyncSettings when we have an account

Also removes useless list of accounts

Bug: 16295803
Change-Id: Ida3c1846327874af9f97695a485b12ec689fd343
This commit is contained in:
Alexandra Gherghina
2014-07-14 20:09:22 +01:00
parent cfc7f9d960
commit defdfc54bb

View File

@@ -17,7 +17,6 @@
package com.android.settings.accounts; package com.android.settings.accounts;
import com.google.android.collect.Lists; import com.google.android.collect.Lists;
import com.google.android.collect.Maps;
import android.accounts.Account; import android.accounts.Account;
import android.accounts.AccountManager; import android.accounts.AccountManager;
@@ -59,7 +58,6 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
public class AccountSyncSettings extends AccountPreferenceBase { public class AccountSyncSettings extends AccountPreferenceBase {
@@ -76,9 +74,6 @@ public class AccountSyncSettings extends AccountPreferenceBase {
private ImageView mProviderIcon; private ImageView mProviderIcon;
private TextView mErrorInfoView; private TextView mErrorInfoView;
private Account mAccount; private Account mAccount;
// List of all accounts, updated when accounts are added/removed
// We need to re-scan the accounts on sync events, in case sync state changes.
private Account[] mAccounts;
private ArrayList<SyncStateCheckBoxPreference> mCheckBoxes = private ArrayList<SyncStateCheckBoxPreference> mCheckBoxes =
new ArrayList<SyncStateCheckBoxPreference>(); new ArrayList<SyncStateCheckBoxPreference>();
private ArrayList<SyncAdapterType> mInvisibleAdapters = Lists.newArrayList(); private ArrayList<SyncAdapterType> mInvisibleAdapters = Lists.newArrayList();
@@ -185,11 +180,16 @@ public class AccountSyncSettings extends AccountPreferenceBase {
return; return;
} }
mAccount = (Account) arguments.getParcelable(ACCOUNT_KEY); mAccount = (Account) arguments.getParcelable(ACCOUNT_KEY);
if (mAccount != null) { if (!accountExists(mAccount)) {
if (Log.isLoggable(TAG, Log.VERBOSE)) Log.v(TAG, "Got account: " + mAccount); Log.e(TAG, "Account provided does not exist: " + mAccount);
mUserId.setText(mAccount.name); finish();
mProviderId.setText(mAccount.type); return;
} }
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Got account: " + mAccount);
}
mUserId.setText(mAccount.name);
mProviderId.setText(mAccount.type);
} }
@Override @Override
@@ -336,10 +336,7 @@ public class AccountSyncSettings extends AccountPreferenceBase {
// plus whatever the system needs to sync, e.g., invisible sync adapters // plus whatever the system needs to sync, e.g., invisible sync adapters
if (mAccount != null) { if (mAccount != null) {
for (SyncAdapterType syncAdapter : mInvisibleAdapters) { for (SyncAdapterType syncAdapter : mInvisibleAdapters) {
// invisible sync adapters' account type should be same as current account type requestOrCancelSync(mAccount, syncAdapter.authority, startSync);
if (syncAdapter.accountType.equals(mAccount.type)) {
requestOrCancelSync(mAccount, syncAdapter.authority, startSync);
}
} }
} }
} }
@@ -378,7 +375,7 @@ public class AccountSyncSettings extends AccountPreferenceBase {
boolean syncIsFailing = false; boolean syncIsFailing = false;
// Refresh the sync status checkboxes - some syncs may have become active. // Refresh the sync status checkboxes - some syncs may have become active.
updateAccountCheckboxes(mAccounts); updateAccountCheckboxes();
for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) { for (int i = 0, count = getPreferenceScreen().getPreferenceCount(); i < count; i++) {
Preference pref = getPreferenceScreen().getPreference(i); Preference pref = getPreferenceScreen().getPreference(i);
@@ -446,29 +443,42 @@ public class AccountSyncSettings extends AccountPreferenceBase {
@Override @Override
public void onAccountsUpdate(final UserHandle userHandle) { public void onAccountsUpdate(final UserHandle userHandle) {
super.onAccountsUpdate(userHandle); super.onAccountsUpdate(userHandle);
mAccounts = AccountManager.get(getActivity()).getAccountsAsUser( if (!accountExists(mAccount)) {
mUserHandle.getIdentifier()); // The account was deleted
updateAccountCheckboxes(mAccounts); finish();
return;
}
updateAccountCheckboxes();
onSyncStateUpdated(); onSyncStateUpdated();
} }
private void updateAccountCheckboxes(Account[] accounts) { private boolean accountExists(Account account) {
if (account == null) return false;
Account[] accounts = AccountManager.get(getActivity()).getAccountsByTypeAsUser(
account.type, mUserHandle);
final int count = accounts.length;
for (int i = 0; i < count; i++) {
if (accounts[i].equals(account)) {
return true;
}
}
return false;
}
private void updateAccountCheckboxes() {
mInvisibleAdapters.clear(); mInvisibleAdapters.clear();
SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser( SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypesAsUser(
mUserHandle.getIdentifier()); mUserHandle.getIdentifier());
HashMap<String, ArrayList<String>> accountTypeToAuthorities = ArrayList<String> authorities = new ArrayList<String>();
Maps.newHashMap();
for (int i = 0, n = syncAdapters.length; i < n; i++) { for (int i = 0, n = syncAdapters.length; i < n; i++) {
final SyncAdapterType sa = syncAdapters[i]; final SyncAdapterType sa = syncAdapters[i];
// Only keep track of sync adapters for this account
if (!sa.accountType.equals(mAccount.type)) continue;
if (sa.isUserVisible()) { if (sa.isUserVisible()) {
ArrayList<String> authorities = accountTypeToAuthorities.get(sa.accountType);
if (authorities == null) {
authorities = new ArrayList<String>();
accountTypeToAuthorities.put(sa.accountType, authorities);
}
if (Log.isLoggable(TAG, Log.VERBOSE)) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.d(TAG, "onAccountUpdated: added authority " + sa.authority Log.d(TAG, "updateAccountCheckboxes: added authority " + sa.authority
+ " to accountType " + sa.accountType); + " to accountType " + sa.accountType);
} }
authorities.add(sa.authority); authorities.add(sa.authority);
@@ -484,25 +494,19 @@ public class AccountSyncSettings extends AccountPreferenceBase {
} }
mCheckBoxes.clear(); mCheckBoxes.clear();
for (int i = 0, n = accounts.length; i < n; i++) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
final Account account = accounts[i]; Log.d(TAG, "looking for sync adapters that match account " + mAccount);
}
for (int j = 0, m = authorities.size(); j < m; j++) {
final String authority = authorities.get(j);
// We could check services here....
int syncState = ContentResolver.getIsSyncableAsUser(mAccount, authority,
mUserHandle.getIdentifier());
if (Log.isLoggable(TAG, Log.VERBOSE)) { if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.d(TAG, "looking for sync adapters that match account " + account); Log.d(TAG, " found authority " + authority + " " + syncState);
} }
final ArrayList<String> authorities = accountTypeToAuthorities.get(account.type); if (syncState > 0) {
if (authorities != null && (mAccount == null || mAccount.equals(account))) { addSyncStateCheckBox(mAccount, authority);
for (int j = 0, m = authorities.size(); j < m; j++) {
final String authority = authorities.get(j);
// We could check services here....
int syncState = ContentResolver.getIsSyncableAsUser(account, authority,
mUserHandle.getIdentifier());
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.d(TAG, " found authority " + authority + " " + syncState);
}
if (syncState > 0) {
addSyncStateCheckBox(account, authority);
}
}
} }
} }