Explicitly setAccount in the same way as setHasInternet.

Bug: 28141203

This frees adapter from updating account and internet internally, and
allows refreshData() be private.

Change-Id: I8ccb4311c48c66222ee19e2d01b1c4943acdb0ee
This commit is contained in:
Fan Zhang
2016-04-29 13:05:49 -07:00
parent fc63188c2c
commit 9502163f92
3 changed files with 28 additions and 16 deletions

View File

@@ -130,7 +130,8 @@ public final class SupportFragment extends InstrumentedFragment implements View.
@Override @Override
public void onAccountsUpdated(Account[] accounts) { public void onAccountsUpdated(Account[] accounts) {
// Account changed, update support items. // Account changed, update support items.
mSupportItemAdapter.refreshData(); mSupportItemAdapter.setAccount(
mSupportFeatureProvider.getSupportEligibleAccount(mActivity));
} }
@Override @Override

View File

@@ -33,6 +33,7 @@ import com.android.settings.overlay.SupportFeatureProvider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import static com.android.settings.overlay.SupportFeatureProvider.SupportType.CHAT; import static com.android.settings.overlay.SupportFeatureProvider.SupportType.CHAT;
import static com.android.settings.overlay.SupportFeatureProvider.SupportType.EMAIL; import static com.android.settings.overlay.SupportFeatureProvider.SupportType.EMAIL;
@@ -56,6 +57,7 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
private final List<SupportData> mSupportData; private final List<SupportData> mSupportData;
private boolean mHasInternet; private boolean mHasInternet;
private Account mAccount;
public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider, public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider,
View.OnClickListener itemClickListener) { View.OnClickListener itemClickListener) {
@@ -66,6 +68,7 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
mSupportData = new ArrayList<>(); mSupportData = new ArrayList<>();
// Optimistically assume we have Internet access. It will be updated later to correct value. // Optimistically assume we have Internet access. It will be updated later to correct value.
mHasInternet = true; mHasInternet = true;
setAccount(mSupportFeatureProvider.getSupportEligibleAccount(mActivity));
refreshData(); refreshData();
} }
@@ -117,23 +120,29 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
} }
} }
public void setAccount(Account account) {
if (!Objects.equals(mAccount, account)) {
mAccount = account;
refreshData();
}
}
/** /**
* Create data for the adapter. If there is already data in the adapter, they will be * Create data for the adapter. If there is already data in the adapter, they will be
* destroyed and recreated. * destroyed and recreated.
*/ */
public void refreshData() { private void refreshData() {
mSupportData.clear(); mSupportData.clear();
final Account[] accounts = mSupportFeatureProvider.getSupportEligibleAccounts(mActivity); if (mAccount == null) {
if (accounts.length == 0) {
addSignInPromo(); addSignInPromo();
} else { } else {
addEscalationCards(accounts[0]); addEscalationCards();
} }
addMoreHelpItems(); addMoreHelpItems();
notifyDataSetChanged(); notifyDataSetChanged();
} }
private void addEscalationCards(Account account) { private void addEscalationCards() {
if (mHasInternet) { if (mHasInternet) {
mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */, mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
R.string.support_escalation_title, R.string.support_escalation_summary, R.string.support_escalation_title, R.string.support_escalation_summary,
@@ -146,17 +155,17 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) { if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) {
mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_call_24dp, mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_call_24dp,
R.string.support_escalation_by_phone, 0 /* summary */, R.string.support_escalation_by_phone, 0 /* summary */,
mSupportFeatureProvider.getSupportIntent(mActivity, account, PHONE))); mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, PHONE)));
} }
if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, EMAIL)) { if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, EMAIL)) {
mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_mail_24dp, mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_mail_24dp,
R.string.support_escalation_by_email, 0 /* summary */, R.string.support_escalation_by_email, 0 /* summary */,
mSupportFeatureProvider.getSupportIntent(mActivity, account, EMAIL))); mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, EMAIL)));
} }
if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) { if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) {
mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_chat_24dp, mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_chat_24dp,
R.string.support_escalation_by_chat, 0 /* summary */, R.string.support_escalation_by_chat, 0 /* summary */,
mSupportFeatureProvider.getSupportIntent(mActivity, account, CHAT))); mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, CHAT)));
} }
} }
@@ -245,10 +254,14 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
private static final class SupportData { private static final class SupportData {
final Intent intent; final Intent intent;
@LayoutRes final int type; @LayoutRes
@DrawableRes final int icon; final int type;
@StringRes final int text1; @DrawableRes
@StringRes final int text2; final int icon;
@StringRes
final int text1;
@StringRes
final int text2;
SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int text1, SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int text1,
@StringRes int text2, Intent intent) { @StringRes int text2, Intent intent) {

View File

@@ -18,7 +18,6 @@ package com.android.settings.overlay;
import android.accounts.Account; import android.accounts.Account;
import android.annotation.IntDef; import android.annotation.IntDef;
import android.annotation.NonNull;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -51,8 +50,7 @@ public interface SupportFeatureProvider {
/** /**
* Returns an {@link Account} that's eligible for support options. * Returns an {@link Account} that's eligible for support options.
*/ */
@NonNull Account getSupportEligibleAccount(Context context);
Account[] getSupportEligibleAccounts(Context context);
/** /**
* Returns an {@link Intent} that opens email support for specified account. * Returns an {@link Intent} that opens email support for specified account.