OSDN Git Service

Add list item to open tips & tricks.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / dashboard / SupportItemAdapter.java
index bb07a3b..d3862b4 100644 (file)
@@ -33,9 +33,9 @@ import com.android.settings.overlay.SupportFeatureProvider;
 
 import java.util.ArrayList;
 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.EMAIL;
 import static com.android.settings.overlay.SupportFeatureProvider.SupportType.PHONE;
 
 /**
@@ -45,28 +45,29 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
 
     private static final int TYPE_TITLE = R.layout.support_item_title;
     private static final int TYPE_SUBTITLE = R.layout.support_item_subtitle;
-    private static final int TYPE_ESCALATION_CARD = R.layout.support_escalation_card;
+    private static final int TYPE_ESCALATION_OPTIONS = R.layout.support_escalation_options;
     private static final int TYPE_SUPPORT_TILE = R.layout.support_tile;
     private static final int TYPE_SIGN_IN_BUTTON = R.layout.support_sign_in_button;
 
     private final Activity mActivity;
-    private final SignInPromoClickListener mSignInPromoClickListener;
+    private final EscalationClickListener mEscalationClickListener;
     private final SupportFeatureProvider mSupportFeatureProvider;
     private final View.OnClickListener mItemClickListener;
     private final List<SupportData> mSupportData;
 
     private boolean mHasInternet;
+    private Account mAccount;
 
     public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider,
             View.OnClickListener itemClickListener) {
         mActivity = activity;
         mSupportFeatureProvider = supportFeatureProvider;
         mItemClickListener = itemClickListener;
-        mSignInPromoClickListener = new SignInPromoClickListener();
+        mEscalationClickListener = new EscalationClickListener();
         mSupportData = new ArrayList<>();
         // Optimistically assume we have Internet access. It will be updated later to correct value.
         mHasInternet = true;
-        setHasStableIds(true);
+        setAccount(mSupportFeatureProvider.getSupportEligibleAccount(mActivity));
         refreshData();
     }
 
@@ -83,6 +84,9 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
             case TYPE_SIGN_IN_BUTTON:
                 bindSignInPromoTile(holder, data);
                 break;
+            case TYPE_ESCALATION_OPTIONS:
+                bindEscalationOptions(holder, data);
+                break;
             default:
                 bindSupportTile(holder, data);
                 break;
@@ -118,76 +122,116 @@ 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
      * destroyed and recreated.
      */
-    public void refreshData() {
+    private void refreshData() {
         mSupportData.clear();
-        final Account[] accounts = mSupportFeatureProvider.getSupportEligibleAccounts(mActivity);
-        if (accounts.length == 0) {
+        if (mAccount == null) {
             addSignInPromo();
         } else {
-            addEscalationCards(accounts[0]);
+            addEscalationCards();
         }
         addMoreHelpItems();
         notifyDataSetChanged();
     }
 
-    private void addEscalationCards(Account account) {
+    private void addEscalationCards() {
         if (mHasInternet) {
-            mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
-                    R.string.support_escalation_title, R.string.support_escalation_summary,
-                    null /* intent */));
+            mSupportData.add(new SupportData.Builder(TYPE_TITLE)
+                    .setText1(R.string.support_escalation_title)
+                    .setText2(R.string.support_escalation_summary)
+                    .build());
         } else {
-            mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
-                    R.string.support_offline_title, R.string.support_offline_summary,
-                    null /* intent */));
+            mSupportData.add(new SupportData.Builder(TYPE_TITLE)
+                    .setText1(R.string.support_offline_title)
+                    .setText2(R.string.support_offline_summary)
+                    .build());
         }
+        final SupportData.Builder builder = new SupportData.Builder(TYPE_ESCALATION_OPTIONS);
         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) {
-            mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_call_24dp,
-                    R.string.support_escalation_by_phone, 0 /* summary */,
-                    mSupportFeatureProvider.getSupportIntent(mActivity, account, PHONE)));
-        }
-        if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, EMAIL)) {
-            mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_mail_24dp,
-                    R.string.support_escalation_by_email, 0 /* summary */,
-                    mSupportFeatureProvider.getSupportIntent(mActivity, account, EMAIL)));
+            builder.setText1(R.string.support_escalation_by_phone);
+            builder.setSummary1(mSupportFeatureProvider.getEstimatedWaitTime(mActivity, PHONE));
         }
         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) {
-            mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_chat_24dp,
-                    R.string.support_escalation_by_chat, 0 /* summary */,
-                    mSupportFeatureProvider.getSupportIntent(mActivity, account, CHAT)));
+            builder.setText2(R.string.support_escalation_by_chat);
+            builder.setSummary2(mSupportFeatureProvider.getEstimatedWaitTime(mActivity, CHAT));
         }
+        mSupportData.add(builder.build());
     }
 
     private void addSignInPromo() {
-        mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
-                R.string.support_sign_in_required_title, R.string.support_sign_in_required_summary,
-                null /* intent */));
-        mSupportData.add(new SupportData(TYPE_SIGN_IN_BUTTON, 0 /* icon */,
-                R.string.support_sign_in_button_text, R.string.support_sign_in_required_help,
-                null /* intent */));
-
+        mSupportData.add(new SupportData.Builder(TYPE_TITLE)
+                .setText1(R.string.support_sign_in_required_title)
+                .setText2(R.string.support_sign_in_required_summary)
+                .build());
+        mSupportData.add(new SupportData.Builder(TYPE_SIGN_IN_BUTTON)
+                .setText1(R.string.support_sign_in_button_text)
+                .setText2(R.string.support_sign_in_required_help)
+                .build());
     }
 
     private void addMoreHelpItems() {
-        mSupportData.add(new SupportData(TYPE_SUBTITLE, 0 /* icon */,
-                R.string.support_more_help_title, 0 /* summary */, null /* intent */));
-        mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_forum_24dp,
-                R.string.support_forum_title, 0 /* summary */,
-                mSupportFeatureProvider.getForumIntent()));
-        mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_help_24dp,
-                R.string.support_articles_title, 0 /* summary */, null /*intent */));
-        mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_feedback_24dp,
-                R.string.support_feedback_title, 0 /* summary */, null /*intent */));
+        mSupportData.add(new SupportData.Builder(TYPE_SUBTITLE)
+                .setText1(R.string.support_more_help_title)
+                .build());
+        mSupportData.add(new SupportData.Builder(TYPE_SUPPORT_TILE)
+                .setIcon(R.drawable.ic_forum_24dp)
+                .setText1(R.string.support_forum_title)
+                .setIntent(mSupportFeatureProvider.getForumIntent())
+                .build());
+        mSupportData.add(new SupportData.Builder(TYPE_SUPPORT_TILE)
+                .setIcon(R.drawable.ic_lightbulb_outline_24)
+                .setText1(R.string.support_tips_and_tricks_title)
+                .setIntent(mSupportFeatureProvider.getTipsAndTricksIntent(mActivity))
+                .build());
+        mSupportData.add(new SupportData.Builder(TYPE_SUPPORT_TILE)
+                .setIcon(R.drawable.ic_help_24dp)
+                .setText1(R.string.help_feedback_label)
+                .setIntent(mSupportFeatureProvider.getHelpIntent(mActivity))
+                .build());
+    }
+
+    private void bindEscalationOptions(ViewHolder holder, SupportData data) {
+        if (data.text1 == 0) {
+            holder.text1View.setVisibility(View.GONE);
+        } else {
+            holder.text1View.setText(data.text1);
+            holder.text1View.setOnClickListener(mEscalationClickListener);
+            holder.text1View.setEnabled(mHasInternet);
+            holder.text1View.setVisibility(View.VISIBLE);
+        }
+        if (data.text2 == 0) {
+            holder.text2View.setVisibility(View.GONE);
+        } else {
+            holder.text2View.setText(data.text2);
+            holder.text2View.setOnClickListener(mEscalationClickListener);
+            holder.text2View.setEnabled(mHasInternet);
+            holder.text2View.setVisibility(View.VISIBLE);
+        }
+        if (holder.summary1View != null) {
+            holder.summary1View.setText(data.summary1);
+            holder.summary1View.setVisibility(mHasInternet ? View.VISIBLE : View.GONE);
+        }
+        if (holder.summary2View != null) {
+            holder.summary2View.setText(data.summary2);
+            holder.summary2View.setVisibility(mHasInternet ? View.VISIBLE : View.GONE);
+        }
     }
 
     private void bindSignInPromoTile(ViewHolder holder, SupportData data) {
         holder.text1View.setText(data.text1);
         holder.text2View.setText(data.text2);
-        holder.text1View.setOnClickListener(mSignInPromoClickListener);
-        holder.text2View.setOnClickListener(mSignInPromoClickListener);
+        holder.text1View.setOnClickListener(mEscalationClickListener);
+        holder.text2View.setOnClickListener(mEscalationClickListener);
     }
 
     private void bindSupportTile(ViewHolder holder, SupportData data) {
@@ -204,21 +248,26 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
     }
 
     /**
-     * Click handler for sign-in promo.
+     * Click handler for starting escalation options.
      */
-    private final class SignInPromoClickListener implements View.OnClickListener {
+    private final class EscalationClickListener implements View.OnClickListener {
         @Override
         public void onClick(View v) {
             switch (v.getId()) {
-                case android.R.id.text1:
-                    mActivity.startActivityForResult(
-                            mSupportFeatureProvider.getAccountLoginIntent(), 0 /* requestCode */);
+                case android.R.id.text1: {
+                    final Intent intent = mAccount == null
+                            ? mSupportFeatureProvider.getAccountLoginIntent()
+                            : mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, PHONE);
+                    mActivity.startActivityForResult(intent, 0 /* requestCode */);
                     break;
-                case android.R.id.text2:
-                    mActivity.startActivityForResult(
-                            mSupportFeatureProvider.getSignInHelpIntent(mActivity),
-                            0 /* requestCode */);
+                }
+                case android.R.id.text2: {
+                    final Intent intent = mAccount == null
+                            ? mSupportFeatureProvider.getSignInHelpIntent(mActivity)
+                            : mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, CHAT);
+                    mActivity.startActivityForResult(intent, 0 /* requestCode */);
                     break;
+                }
             }
         }
     }
@@ -231,12 +280,16 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
         final ImageView iconView;
         final TextView text1View;
         final TextView text2View;
+        final TextView summary1View;
+        final TextView summary2View;
 
         ViewHolder(View itemView) {
             super(itemView);
             iconView = (ImageView) itemView.findViewById(android.R.id.icon);
             text1View = (TextView) itemView.findViewById(android.R.id.text1);
             text2View = (TextView) itemView.findViewById(android.R.id.text2);
+            summary1View = (TextView) itemView.findViewById(R.id.summary1);
+            summary2View = (TextView) itemView.findViewById(R.id.summary2);
         }
     }
 
@@ -246,18 +299,73 @@ public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAd
     private static final class SupportData {
 
         final Intent intent;
-        @LayoutRes final int type;
-        @DrawableRes final int icon;
-        @StringRes final int text1;
-        @StringRes final int text2;
-
-        SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int text1,
-                @StringRes int text2, Intent intent) {
-            this.type = type;
-            this.icon = icon;
-            this.text1 = text1;
-            this.text2 = text2;
-            this.intent = intent;
+        @LayoutRes
+        final int type;
+        @DrawableRes
+        final int icon;
+        @StringRes
+        final int text1;
+        @StringRes
+        final int text2;
+        final String summary1;
+        final String summary2;
+
+        private SupportData(Builder builder) {
+            this.type = builder.mType;
+            this.icon = builder.mIcon;
+            this.text1 = builder.mText1;
+            this.text2 = builder.mText2;
+            this.summary1 = builder.mSummary1;
+            this.summary2 = builder.mSummary2;
+            this.intent = builder.mIntent;
+        }
+
+        static final class Builder {
+            @LayoutRes private final int mType;
+            @DrawableRes private int mIcon;
+            @StringRes private int mText1;
+            @StringRes private int mText2;
+            private String mSummary1;
+            private String mSummary2;
+            private Intent mIntent;
+
+            Builder(@LayoutRes int type) {
+                mType = type;
+            }
+
+            Builder setIcon(@DrawableRes int icon) {
+                mIcon = icon;
+                return this;
+            }
+
+            Builder setText1(@StringRes int text1) {
+                mText1 = text1;
+                return this;
+            }
+
+            Builder setSummary1(String summary1) {
+                mSummary1 = summary1;
+                return this;
+            }
+
+            Builder setText2(@StringRes int text2) {
+                mText2 = text2;
+                return this;
+            }
+
+            Builder setSummary2(String summary2) {
+                mSummary2 = summary2;
+                return this;
+            }
+
+            Builder setIntent(Intent intent) {
+                mIntent = intent;
+                return this;
+            }
+
+            SupportData build() {
+                return new SupportData(this);
+            }
         }
     }
 }