OSDN Git Service

Merge "Don\'t crash on Settings 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
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         refreshData();
70     }
71
72     @Override
73     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
74         return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
75                 viewType, parent, false));
76     }
77
78     @Override
79     public void onBindViewHolder(ViewHolder holder, int position) {
80         final SupportData data = mSupportData.get(position);
81         switch (holder.getItemViewType()) {
82             case TYPE_SIGN_IN_BUTTON:
83                 bindSignInPromoTile(holder, data);
84                 break;
85             default:
86                 bindSupportTile(holder, data);
87                 break;
88         }
89     }
90
91     @Override
92     public int getItemViewType(int position) {
93         return mSupportData.get(position).type;
94     }
95
96     @Override
97     public int getItemCount() {
98         return mSupportData.size();
99     }
100
101     /**
102      * Called when a support item is clicked.
103      */
104     public void onItemClicked(int position) {
105         if (position >= 0 && position < mSupportData.size()) {
106             final SupportData data = mSupportData.get(position);
107             if (data.intent != null) {
108                 mActivity.startActivityForResult(data.intent, 0);
109             }
110         }
111     }
112
113     public void setHasInternet(boolean hasInternet) {
114         if (mHasInternet != hasInternet) {
115             mHasInternet = hasInternet;
116             refreshData();
117         }
118     }
119
120     /**
121      * Create data for the adapter. If there is already data in the adapter, they will be
122      * destroyed and recreated.
123      */
124     public void refreshData() {
125         mSupportData.clear();
126         final Account[] accounts = mSupportFeatureProvider.getSupportEligibleAccounts(mActivity);
127         if (accounts.length == 0) {
128             addSignInPromo();
129         } else {
130             addEscalationCards(accounts[0]);
131         }
132         addMoreHelpItems();
133         notifyDataSetChanged();
134     }
135
136     private void addEscalationCards(Account account) {
137         if (mHasInternet) {
138             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
139                     R.string.support_escalation_title, R.string.support_escalation_summary,
140                     null /* intent */));
141         } else {
142             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
143                     R.string.support_offline_title, R.string.support_offline_summary,
144                     null /* intent */));
145         }
146         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) {
147             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_call_24dp,
148                     R.string.support_escalation_by_phone, 0 /* summary */,
149                     mSupportFeatureProvider.getSupportIntent(mActivity, account, PHONE)));
150         }
151         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, EMAIL)) {
152             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_mail_24dp,
153                     R.string.support_escalation_by_email, 0 /* summary */,
154                     mSupportFeatureProvider.getSupportIntent(mActivity, account, EMAIL)));
155         }
156         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) {
157             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_chat_24dp,
158                     R.string.support_escalation_by_chat, 0 /* summary */,
159                     mSupportFeatureProvider.getSupportIntent(mActivity, account, CHAT)));
160         }
161     }
162
163     private void addSignInPromo() {
164         mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
165                 R.string.support_sign_in_required_title, R.string.support_sign_in_required_summary,
166                 null /* intent */));
167         mSupportData.add(new SupportData(TYPE_SIGN_IN_BUTTON, 0 /* icon */,
168                 R.string.support_sign_in_button_text, R.string.support_sign_in_required_help,
169                 null /* intent */));
170
171     }
172
173     private void addMoreHelpItems() {
174         mSupportData.add(new SupportData(TYPE_SUBTITLE, 0 /* icon */,
175                 R.string.support_more_help_title, 0 /* summary */, null /* intent */));
176         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_forum_24dp,
177                 R.string.support_forum_title, 0 /* summary */,
178                 mSupportFeatureProvider.getForumIntent()));
179         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_help_24dp,
180                 R.string.support_articles_title, 0 /* summary */, null /*intent */));
181         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_feedback_24dp,
182                 R.string.support_feedback_title, 0 /* summary */, null /*intent */));
183     }
184
185     private void bindSignInPromoTile(ViewHolder holder, SupportData data) {
186         holder.text1View.setText(data.text1);
187         holder.text2View.setText(data.text2);
188         holder.text1View.setOnClickListener(mSignInPromoClickListener);
189         holder.text2View.setOnClickListener(mSignInPromoClickListener);
190     }
191
192     private void bindSupportTile(ViewHolder holder, SupportData data) {
193         if (holder.iconView != null) {
194             holder.iconView.setImageResource(data.icon);
195         }
196         if (holder.text1View != null) {
197             holder.text1View.setText(data.text1);
198         }
199         if (holder.text2View != null) {
200             holder.text2View.setText(data.text2);
201         }
202         holder.itemView.setOnClickListener(mItemClickListener);
203     }
204
205     /**
206      * Click handler for sign-in promo.
207      */
208     private final class SignInPromoClickListener implements View.OnClickListener {
209         @Override
210         public void onClick(View v) {
211             switch (v.getId()) {
212                 case android.R.id.text1:
213                     mActivity.startActivityForResult(
214                             mSupportFeatureProvider.getAccountLoginIntent(), 0 /* requestCode */);
215                     break;
216                 case android.R.id.text2:
217                     mActivity.startActivityForResult(
218                             mSupportFeatureProvider.getSignInHelpIntent(mActivity),
219                             0 /* requestCode */);
220                     break;
221             }
222         }
223     }
224
225     /**
226      * {@link RecyclerView.ViewHolder} for support items.
227      */
228     static final class ViewHolder extends RecyclerView.ViewHolder {
229
230         final ImageView iconView;
231         final TextView text1View;
232         final TextView text2View;
233
234         ViewHolder(View itemView) {
235             super(itemView);
236             iconView = (ImageView) itemView.findViewById(android.R.id.icon);
237             text1View = (TextView) itemView.findViewById(android.R.id.text1);
238             text2View = (TextView) itemView.findViewById(android.R.id.text2);
239         }
240     }
241
242     /**
243      * Data for a single support item.
244      */
245     private static final class SupportData {
246
247         final Intent intent;
248         @LayoutRes final int type;
249         @DrawableRes final int icon;
250         @StringRes final int text1;
251         @StringRes final int text2;
252
253         SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int text1,
254                 @StringRes int text2, Intent intent) {
255             this.type = type;
256             this.icon = icon;
257             this.text1 = text1;
258             this.text2 = text2;
259             this.intent = intent;
260         }
261     }
262 }