2 * Copyright (C) 2016 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.settings.dashboard;
18 import android.accounts.Account;
19 import android.annotation.DrawableRes;
20 import android.annotation.LayoutRes;
21 import android.annotation.StringRes;
22 import android.app.Activity;
23 import android.content.Intent;
24 import android.support.v7.widget.RecyclerView;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.ImageView;
29 import android.widget.TextView;
31 import com.android.settings.R;
32 import com.android.settings.overlay.SupportFeatureProvider;
34 import java.util.ArrayList;
35 import java.util.List;
36 import java.util.Objects;
38 import static com.android.settings.overlay.SupportFeatureProvider.SupportType.CHAT;
39 import static com.android.settings.overlay.SupportFeatureProvider.SupportType.PHONE;
42 * Item adapter for support tiles.
44 public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAdapter.ViewHolder> {
46 private static final int TYPE_TITLE = R.layout.support_item_title;
47 private static final int TYPE_SUBTITLE = R.layout.support_item_subtitle;
48 private static final int TYPE_ESCALATION_OPTIONS = R.layout.support_escalation_options;
49 private static final int TYPE_SUPPORT_TILE = R.layout.support_tile;
50 private static final int TYPE_SIGN_IN_BUTTON = R.layout.support_sign_in_button;
52 private final Activity mActivity;
53 private final EscalationClickListener mEscalationClickListener;
54 private final SupportFeatureProvider mSupportFeatureProvider;
55 private final View.OnClickListener mItemClickListener;
56 private final List<SupportData> mSupportData;
58 private boolean mHasInternet;
59 private Account mAccount;
61 public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider,
62 View.OnClickListener itemClickListener) {
64 mSupportFeatureProvider = supportFeatureProvider;
65 mItemClickListener = itemClickListener;
66 mEscalationClickListener = new EscalationClickListener();
67 mSupportData = new ArrayList<>();
68 // Optimistically assume we have Internet access. It will be updated later to correct value.
70 setAccount(mSupportFeatureProvider.getSupportEligibleAccount(mActivity));
75 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
76 return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
77 viewType, parent, false));
81 public void onBindViewHolder(ViewHolder holder, int position) {
82 final SupportData data = mSupportData.get(position);
83 switch (holder.getItemViewType()) {
84 case TYPE_SIGN_IN_BUTTON:
85 bindSignInPromoTile(holder, data);
87 case TYPE_ESCALATION_OPTIONS:
88 bindEscalationOptions(holder, data);
91 bindSupportTile(holder, data);
97 public int getItemViewType(int position) {
98 return mSupportData.get(position).type;
102 public int getItemCount() {
103 return mSupportData.size();
107 * Called when a support item is clicked.
109 public void onItemClicked(int position) {
110 if (position >= 0 && position < mSupportData.size()) {
111 final SupportData data = mSupportData.get(position);
112 if (data.intent != null) {
113 mActivity.startActivityForResult(data.intent, 0);
118 public void setHasInternet(boolean hasInternet) {
119 if (mHasInternet != hasInternet) {
120 mHasInternet = hasInternet;
125 public void setAccount(Account account) {
126 if (!Objects.equals(mAccount, account)) {
133 * Create data for the adapter. If there is already data in the adapter, they will be
134 * destroyed and recreated.
136 private void refreshData() {
137 mSupportData.clear();
138 if (mAccount == null) {
141 addEscalationCards();
144 notifyDataSetChanged();
147 private void addEscalationCards() {
149 mSupportData.add(new SupportData.Builder(TYPE_TITLE)
150 .setText1(R.string.support_escalation_title)
151 .setText2(R.string.support_escalation_summary)
154 mSupportData.add(new SupportData.Builder(TYPE_TITLE)
155 .setText1(R.string.support_offline_title)
156 .setText2(R.string.support_offline_summary)
159 final SupportData.Builder builder = new SupportData.Builder(TYPE_ESCALATION_OPTIONS);
160 if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) {
161 builder.setText1(R.string.support_escalation_by_phone);
162 builder.setSummary1(mSupportFeatureProvider.getEstimatedWaitTime(mActivity, PHONE));
164 if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) {
165 builder.setText2(R.string.support_escalation_by_chat);
166 builder.setSummary2(mSupportFeatureProvider.getEstimatedWaitTime(mActivity, CHAT));
168 mSupportData.add(builder.build());
171 private void addSignInPromo() {
172 mSupportData.add(new SupportData.Builder(TYPE_TITLE)
173 .setText1(R.string.support_sign_in_required_title)
174 .setText2(R.string.support_sign_in_required_summary)
176 mSupportData.add(new SupportData.Builder(TYPE_SIGN_IN_BUTTON)
177 .setText1(R.string.support_sign_in_button_text)
178 .setText2(R.string.support_sign_in_required_help)
182 private void addMoreHelpItems() {
183 mSupportData.add(new SupportData.Builder(TYPE_SUBTITLE)
184 .setText1(R.string.support_more_help_title)
186 mSupportData.add(new SupportData.Builder(TYPE_SUPPORT_TILE)
187 .setIcon(R.drawable.ic_forum_24dp)
188 .setText1(R.string.support_forum_title)
189 .setIntent(mSupportFeatureProvider.getForumIntent())
191 mSupportData.add(new SupportData.Builder(TYPE_SUPPORT_TILE)
192 .setIcon(R.drawable.ic_help_24dp)
193 .setText1(R.string.help_feedback_label)
194 .setIntent(mSupportFeatureProvider.getHelpIntent(mActivity))
198 private void bindEscalationOptions(ViewHolder holder, SupportData data) {
199 if (data.text1 == 0) {
200 holder.text1View.setVisibility(View.GONE);
202 holder.text1View.setText(data.text1);
203 holder.text1View.setOnClickListener(mEscalationClickListener);
204 holder.text1View.setEnabled(mHasInternet);
205 holder.text1View.setVisibility(View.VISIBLE);
207 if (data.text2 == 0) {
208 holder.text2View.setVisibility(View.GONE);
210 holder.text2View.setText(data.text2);
211 holder.text2View.setOnClickListener(mEscalationClickListener);
212 holder.text2View.setEnabled(mHasInternet);
213 holder.text2View.setVisibility(View.VISIBLE);
215 if (holder.summary1View != null) {
216 holder.summary1View.setText(data.summary1);
217 holder.summary1View.setVisibility(mHasInternet ? View.VISIBLE : View.GONE);
219 if (holder.summary2View != null) {
220 holder.summary2View.setText(data.summary2);
221 holder.summary2View.setVisibility(mHasInternet ? View.VISIBLE : View.GONE);
225 private void bindSignInPromoTile(ViewHolder holder, SupportData data) {
226 holder.text1View.setText(data.text1);
227 holder.text2View.setText(data.text2);
228 holder.text1View.setOnClickListener(mEscalationClickListener);
229 holder.text2View.setOnClickListener(mEscalationClickListener);
232 private void bindSupportTile(ViewHolder holder, SupportData data) {
233 if (holder.iconView != null) {
234 holder.iconView.setImageResource(data.icon);
236 if (holder.text1View != null) {
237 holder.text1View.setText(data.text1);
239 if (holder.text2View != null) {
240 holder.text2View.setText(data.text2);
242 holder.itemView.setOnClickListener(mItemClickListener);
246 * Click handler for starting escalation options.
248 private final class EscalationClickListener implements View.OnClickListener {
250 public void onClick(View v) {
252 case android.R.id.text1: {
253 final Intent intent = mAccount == null
254 ? mSupportFeatureProvider.getAccountLoginIntent()
255 : mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, PHONE);
256 mActivity.startActivityForResult(intent, 0 /* requestCode */);
259 case android.R.id.text2: {
260 final Intent intent = mAccount == null
261 ? mSupportFeatureProvider.getSignInHelpIntent(mActivity)
262 : mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, CHAT);
263 mActivity.startActivityForResult(intent, 0 /* requestCode */);
271 * {@link RecyclerView.ViewHolder} for support items.
273 static final class ViewHolder extends RecyclerView.ViewHolder {
275 final ImageView iconView;
276 final TextView text1View;
277 final TextView text2View;
278 final TextView summary1View;
279 final TextView summary2View;
281 ViewHolder(View itemView) {
283 iconView = (ImageView) itemView.findViewById(android.R.id.icon);
284 text1View = (TextView) itemView.findViewById(android.R.id.text1);
285 text2View = (TextView) itemView.findViewById(android.R.id.text2);
286 summary1View = (TextView) itemView.findViewById(R.id.summary1);
287 summary2View = (TextView) itemView.findViewById(R.id.summary2);
292 * Data for a single support item.
294 private static final class SupportData {
305 final String summary1;
306 final String summary2;
308 private SupportData(Builder builder) {
309 this.type = builder.mType;
310 this.icon = builder.mIcon;
311 this.text1 = builder.mText1;
312 this.text2 = builder.mText2;
313 this.summary1 = builder.mSummary1;
314 this.summary2 = builder.mSummary2;
315 this.intent = builder.mIntent;
318 static final class Builder {
319 @LayoutRes private final int mType;
320 @DrawableRes private int mIcon;
321 @StringRes private int mText1;
322 @StringRes private int mText2;
323 private String mSummary1;
324 private String mSummary2;
325 private Intent mIntent;
327 Builder(@LayoutRes int type) {
331 Builder setIcon(@DrawableRes int icon) {
336 Builder setText1(@StringRes int text1) {
341 Builder setSummary1(String summary1) {
342 mSummary1 = summary1;
346 Builder setText2(@StringRes int text2) {
351 Builder setSummary2(String summary2) {
352 mSummary2 = summary2;
356 Builder setIntent(Intent intent) {
361 SupportData build() {
362 return new SupportData(this);