OSDN Git Service

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