OSDN Git Service

Merge "Don\'t crash on non-app data usage" into nyc-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 import java.util.Objects;
37
38 import static com.android.settings.overlay.SupportFeatureProvider.SupportType.CHAT;
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_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;
51
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;
57
58     private boolean mHasInternet;
59     private Account mAccount;
60
61     public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider,
62             View.OnClickListener itemClickListener) {
63         mActivity = activity;
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.
69         mHasInternet = true;
70         setAccount(mSupportFeatureProvider.getSupportEligibleAccount(mActivity));
71         refreshData();
72     }
73
74     @Override
75     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
76         return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
77                 viewType, parent, false));
78     }
79
80     @Override
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);
86                 break;
87             case TYPE_ESCALATION_OPTIONS:
88                 bindEscalationOptions(holder, data);
89                 break;
90             default:
91                 bindSupportTile(holder, data);
92                 break;
93         }
94     }
95
96     @Override
97     public int getItemViewType(int position) {
98         return mSupportData.get(position).type;
99     }
100
101     @Override
102     public int getItemCount() {
103         return mSupportData.size();
104     }
105
106     /**
107      * Called when a support item is clicked.
108      */
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);
114             }
115         }
116     }
117
118     public void setHasInternet(boolean hasInternet) {
119         if (mHasInternet != hasInternet) {
120             mHasInternet = hasInternet;
121             refreshData();
122         }
123     }
124
125     public void setAccount(Account account) {
126         if (!Objects.equals(mAccount, account)) {
127             mAccount = account;
128             refreshData();
129         }
130     }
131
132     /**
133      * Create data for the adapter. If there is already data in the adapter, they will be
134      * destroyed and recreated.
135      */
136     private void refreshData() {
137         mSupportData.clear();
138         if (mAccount == null) {
139             addSignInPromo();
140         } else {
141             addEscalationCards();
142         }
143         addMoreHelpItems();
144         notifyDataSetChanged();
145     }
146
147     private void addEscalationCards() {
148         if (mHasInternet) {
149             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
150                     R.string.support_escalation_title, R.string.support_escalation_summary,
151                     null /* intent */));
152         } else {
153             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
154                     R.string.support_offline_title, R.string.support_offline_summary,
155                     null /* intent */));
156         }
157         final int phoneSupportText = mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)
158                 ? R.string.support_escalation_by_phone : 0;
159         final int chatSupportText = mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)
160                 ? R.string.support_escalation_by_chat : 0;
161         mSupportData.add(new SupportData(TYPE_ESCALATION_OPTIONS, 0 /* icon */,
162                 phoneSupportText, chatSupportText, null /* intent */));
163     }
164
165     private void addSignInPromo() {
166         mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
167                 R.string.support_sign_in_required_title, R.string.support_sign_in_required_summary,
168                 null /* intent */));
169         mSupportData.add(new SupportData(TYPE_SIGN_IN_BUTTON, 0 /* icon */,
170                 R.string.support_sign_in_button_text, R.string.support_sign_in_required_help,
171                 null /* intent */));
172
173     }
174
175     private void addMoreHelpItems() {
176         mSupportData.add(new SupportData(TYPE_SUBTITLE, 0 /* icon */,
177                 R.string.support_more_help_title, 0 /* summary */, null /* intent */));
178         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_forum_24dp,
179                 R.string.support_forum_title, 0 /* summary */,
180                 mSupportFeatureProvider.getForumIntent()));
181         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_help_24dp,
182                 R.string.support_articles_title, 0 /* summary */, null /*intent */));
183         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_feedback_24dp,
184                 R.string.support_feedback_title, 0 /* summary */, null /*intent */));
185     }
186
187     private void bindEscalationOptions(ViewHolder holder, SupportData data) {
188         if (data.text1 == 0) {
189             holder.text1View.setVisibility(View.GONE);
190         } else {
191             holder.text1View.setText(data.text1);
192             holder.text1View.setOnClickListener(mEscalationClickListener);
193             holder.text1View.setEnabled(mHasInternet);
194             holder.text1View.setVisibility(View.VISIBLE);
195         }
196         if (data.text2 == 0) {
197             holder.text2View.setVisibility(View.GONE);
198         } else {
199             holder.text2View.setText(data.text2);
200             holder.text2View.setOnClickListener(mEscalationClickListener);
201             holder.text2View.setEnabled(mHasInternet);
202             holder.text2View.setVisibility(View.VISIBLE);
203         }
204     }
205
206     private void bindSignInPromoTile(ViewHolder holder, SupportData data) {
207         holder.text1View.setText(data.text1);
208         holder.text2View.setText(data.text2);
209         holder.text1View.setOnClickListener(mEscalationClickListener);
210         holder.text2View.setOnClickListener(mEscalationClickListener);
211     }
212
213     private void bindSupportTile(ViewHolder holder, SupportData data) {
214         if (holder.iconView != null) {
215             holder.iconView.setImageResource(data.icon);
216         }
217         if (holder.text1View != null) {
218             holder.text1View.setText(data.text1);
219         }
220         if (holder.text2View != null) {
221             holder.text2View.setText(data.text2);
222         }
223         holder.itemView.setOnClickListener(mItemClickListener);
224     }
225
226     /**
227      * Click handler for starting escalation options.
228      */
229     private final class EscalationClickListener implements View.OnClickListener {
230         @Override
231         public void onClick(View v) {
232             switch (v.getId()) {
233                 case android.R.id.text1: {
234                     final Intent intent = mAccount == null
235                             ? mSupportFeatureProvider.getAccountLoginIntent()
236                             : mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, PHONE);
237                     mActivity.startActivityForResult(intent, 0 /* requestCode */);
238                     break;
239                 }
240                 case android.R.id.text2: {
241                     final Intent intent = mAccount == null
242                             ? mSupportFeatureProvider.getSignInHelpIntent(mActivity)
243                             : mSupportFeatureProvider.getSupportIntent(mActivity, mAccount, CHAT);
244                     mActivity.startActivityForResult(intent, 0 /* requestCode */);
245                     break;
246                 }
247             }
248         }
249     }
250
251     /**
252      * {@link RecyclerView.ViewHolder} for support items.
253      */
254     static final class ViewHolder extends RecyclerView.ViewHolder {
255
256         final ImageView iconView;
257         final TextView text1View;
258         final TextView text2View;
259
260         ViewHolder(View itemView) {
261             super(itemView);
262             iconView = (ImageView) itemView.findViewById(android.R.id.icon);
263             text1View = (TextView) itemView.findViewById(android.R.id.text1);
264             text2View = (TextView) itemView.findViewById(android.R.id.text2);
265         }
266     }
267
268     /**
269      * Data for a single support item.
270      */
271     private static final class SupportData {
272
273         final Intent intent;
274         @LayoutRes
275         final int type;
276         @DrawableRes
277         final int icon;
278         @StringRes
279         final int text1;
280         @StringRes
281         final int text2;
282
283         SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int text1,
284                 @StringRes int text2, Intent intent) {
285             this.type = type;
286             this.icon = icon;
287             this.text1 = text1;
288             this.text2 = text2;
289             this.intent = intent;
290         }
291     }
292 }