OSDN Git Service

Merge "Prompt sign-in when there is no account for support options" into nyc-mr1-dev
[android-x86/packages-apps-Settings.git] / src / com / android / settings / dashboard / SupportItemAdapter.java
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.android.settings.dashboard;
17
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;
30
31 import com.android.settings.R;
32 import com.android.settings.overlay.SupportFeatureProvider;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import static com.android.settings.overlay.SupportFeatureProvider.SupportType.CHAT;
38 import static com.android.settings.overlay.SupportFeatureProvider.SupportType.EMAIL;
39 import static com.android.settings.overlay.SupportFeatureProvider.SupportType.PHONE;
40
41 /**
42  * Item adapter for support tiles.
43  */
44 public final class SupportItemAdapter extends RecyclerView.Adapter<SupportItemAdapter.ViewHolder> {
45
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_CARD = R.layout.support_escalation_card;
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;
51
52     private final Activity mActivity;
53     private final SignInPromoClickListener mSignInPromoClickListener;
54     private final SupportFeatureProvider mSupportFeatureProvider;
55     private final View.OnClickListener mItemClickListener;
56     private final List<SupportData> mSupportData;
57
58     private boolean mHasInternet;
59
60     public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider,
61             View.OnClickListener itemClickListener) {
62         mActivity = activity;
63         mSupportFeatureProvider = supportFeatureProvider;
64         mItemClickListener = itemClickListener;
65         mSignInPromoClickListener = new SignInPromoClickListener();
66         mSupportData = new ArrayList<>();
67         // Optimistically assume we have Internet access. It will be updated later to correct value.
68         mHasInternet = true;
69         setHasStableIds(true);
70         refreshData();
71     }
72
73     @Override
74     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
75         return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
76                 viewType, parent, false));
77     }
78
79     @Override
80     public void onBindViewHolder(ViewHolder holder, int position) {
81         final SupportData data = mSupportData.get(position);
82         switch (holder.getItemViewType()) {
83             case TYPE_SIGN_IN_BUTTON:
84                 bindSignInPromoTile(holder, data);
85                 break;
86             default:
87                 bindSupportTile(holder, data);
88                 break;
89         }
90     }
91
92     @Override
93     public int getItemViewType(int position) {
94         return mSupportData.get(position).type;
95     }
96
97     @Override
98     public int getItemCount() {
99         return mSupportData.size();
100     }
101
102     /**
103      * Called when a support item is clicked.
104      */
105     public void onItemClicked(int position) {
106         if (position >= 0 && position < mSupportData.size()) {
107             final SupportData data = mSupportData.get(position);
108             if (data.intent != null) {
109                 mActivity.startActivityForResult(data.intent, 0);
110             }
111         }
112     }
113
114     public void setHasInternet(boolean hasInternet) {
115         if (mHasInternet != hasInternet) {
116             mHasInternet = hasInternet;
117             refreshData();
118         }
119     }
120
121     /**
122      * Create data for the adapter. If there is already data in the adapter, they will be
123      * destroyed and recreated.
124      */
125     public void refreshData() {
126         mSupportData.clear();
127         final Account[] accounts = mSupportFeatureProvider.getSupportEligibleAccounts(mActivity);
128         if (accounts.length == 0) {
129             addSignInPromo();
130         } else {
131             addEscalationCards(accounts[0]);
132         }
133         addMoreHelpItems();
134         notifyDataSetChanged();
135     }
136
137     private void addEscalationCards(Account account) {
138         if (mHasInternet) {
139             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
140                     R.string.support_escalation_title, R.string.support_escalation_summary,
141                     null /* intent */));
142         } else {
143             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
144                     R.string.support_offline_title, R.string.support_offline_summary,
145                     null /* intent */));
146         }
147         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) {
148             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_call_24dp,
149                     R.string.support_escalation_by_phone, 0 /* summary */,
150                     mSupportFeatureProvider.getSupportIntent(mActivity, account, PHONE)));
151         }
152         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, EMAIL)) {
153             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_mail_24dp,
154                     R.string.support_escalation_by_email, 0 /* summary */,
155                     mSupportFeatureProvider.getSupportIntent(mActivity, account, EMAIL)));
156         }
157         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) {
158             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_chat_24dp,
159                     R.string.support_escalation_by_chat, 0 /* summary */,
160                     mSupportFeatureProvider.getSupportIntent(mActivity, account, CHAT)));
161         }
162     }
163
164     private void addSignInPromo() {
165         mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
166                 R.string.support_sign_in_required_title, R.string.support_sign_in_required_summary,
167                 null /* intent */));
168         mSupportData.add(new SupportData(TYPE_SIGN_IN_BUTTON, 0 /* icon */,
169                 R.string.support_sign_in_button_text, R.string.support_sign_in_required_help,
170                 null /* intent */));
171
172     }
173
174     private void addMoreHelpItems() {
175         mSupportData.add(new SupportData(TYPE_SUBTITLE, 0 /* icon */,
176                 R.string.support_more_help_title, 0 /* summary */, null /* intent */));
177         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_forum_24dp,
178                 R.string.support_forum_title, 0 /* summary */,
179                 mSupportFeatureProvider.getForumIntent()));
180         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_help_24dp,
181                 R.string.support_articles_title, 0 /* summary */, null /*intent */));
182         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_feedback_24dp,
183                 R.string.support_feedback_title, 0 /* summary */, null /*intent */));
184     }
185
186     private void bindSignInPromoTile(ViewHolder holder, SupportData data) {
187         holder.text1View.setText(data.text1);
188         holder.text2View.setText(data.text2);
189         holder.text1View.setOnClickListener(mSignInPromoClickListener);
190         holder.text2View.setOnClickListener(mSignInPromoClickListener);
191     }
192
193     private void bindSupportTile(ViewHolder holder, SupportData data) {
194         if (holder.iconView != null) {
195             holder.iconView.setImageResource(data.icon);
196         }
197         if (holder.text1View != null) {
198             holder.text1View.setText(data.text1);
199         }
200         if (holder.text2View != null) {
201             holder.text2View.setText(data.text2);
202         }
203         holder.itemView.setOnClickListener(mItemClickListener);
204     }
205
206     /**
207      * Click handler for sign-in promo.
208      */
209     private final class SignInPromoClickListener implements View.OnClickListener {
210         @Override
211         public void onClick(View v) {
212             switch (v.getId()) {
213                 case android.R.id.text1:
214                     mActivity.startActivityForResult(
215                             mSupportFeatureProvider.getAccountLoginIntent(), 0 /* requestCode */);
216                     break;
217                 case android.R.id.text2:
218                     mActivity.startActivityForResult(
219                             mSupportFeatureProvider.getSignInHelpIntent(mActivity),
220                             0 /* requestCode */);
221                     break;
222             }
223         }
224     }
225
226     /**
227      * {@link RecyclerView.ViewHolder} for support items.
228      */
229     static final class ViewHolder extends RecyclerView.ViewHolder {
230
231         final ImageView iconView;
232         final TextView text1View;
233         final TextView text2View;
234
235         ViewHolder(View itemView) {
236             super(itemView);
237             iconView = (ImageView) itemView.findViewById(android.R.id.icon);
238             text1View = (TextView) itemView.findViewById(android.R.id.text1);
239             text2View = (TextView) itemView.findViewById(android.R.id.text2);
240         }
241     }
242
243     /**
244      * Data for a single support item.
245      */
246     private static final class SupportData {
247
248         final Intent intent;
249         @LayoutRes final int type;
250         @DrawableRes final int icon;
251         @StringRes final int text1;
252         @StringRes final int text2;
253
254         SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int text1,
255                 @StringRes int text2, Intent intent) {
256             this.type = type;
257             this.icon = icon;
258             this.text1 = text1;
259             this.text2 = text2;
260             this.intent = intent;
261         }
262     }
263 }