OSDN Git Service

Merge "Fix summary lifecycle" 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.util.Log;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.ImageView;
30 import android.widget.TextView;
31
32 import com.android.settings.R;
33 import com.android.settings.overlay.SupportFeatureProvider;
34
35 import java.util.ArrayList;
36 import java.util.List;
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 String TAG = "SupportItemAdapter";
48
49     private static final int TYPE_TITLE = R.layout.support_item_title;
50     private static final int TYPE_SUBTITLE = R.layout.support_item_subtitle;
51     private static final int TYPE_ESCALATION_CARD = R.layout.support_escalation_card;
52     private static final int TYPE_SUPPORT_TILE = R.layout.support_tile;
53
54     private final Activity mActivity;
55     private final SupportFeatureProvider mSupportFeatureProvider;
56     private final View.OnClickListener mItemClickListener;
57     private final List<SupportData> mSupportData;
58
59     private boolean mHasInternet;
60
61     public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider,
62             View.OnClickListener itemClickListener) {
63         mActivity = activity;
64         mSupportFeatureProvider = supportFeatureProvider;
65         mItemClickListener = itemClickListener;
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         if (holder.iconView != null) {
83             holder.iconView.setImageResource(data.icon);
84         }
85         if (holder.titleView != null) {
86             holder.titleView.setText(data.title);
87         }
88         if (holder.summaryView != null) {
89             holder.summaryView.setText(data.summary);
90         }
91         holder.itemView.setOnClickListener(mItemClickListener);
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     /**
124      * Create data for the adapter. If there is already data in the adapter, they will be
125      * destroyed and recreated.
126      */
127     public void refreshData() {
128         mSupportData.clear();
129         final Account[] accounts = mSupportFeatureProvider.getSupportEligibleAccounts(mActivity);
130         if (accounts.length == 0) {
131             Log.d(TAG, "Account unavailable. Skipping");
132         } else {
133             addEscalationCards(accounts[0]);
134         }
135         addMoreHelpItems();
136         notifyDataSetChanged();
137     }
138
139     private void addEscalationCards(Account account) {
140         if (mHasInternet) {
141             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
142                     R.string.support_escalation_title, R.string.support_escalation_summary,
143                     null /* intent */));
144         } else {
145             mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
146                     R.string.support_offline_title, R.string.support_offline_summary,
147                     null /* intent */));
148         }
149         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) {
150             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_call_24dp,
151                     R.string.support_escalation_by_phone, 0 /* summary */,
152                     mSupportFeatureProvider.getSupportIntent(mActivity, account, PHONE)));
153         }
154         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, EMAIL)) {
155             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_mail_24dp,
156                     R.string.support_escalation_by_email, 0 /* summary */,
157                     mSupportFeatureProvider.getSupportIntent(mActivity, account, EMAIL)));
158         }
159         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) {
160             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_chat_24dp,
161                     R.string.support_escalation_by_chat, 0 /* summary */,
162                     mSupportFeatureProvider.getSupportIntent(mActivity, account, CHAT)));
163         }
164     }
165
166     private void addMoreHelpItems() {
167         mSupportData.add(new SupportData(TYPE_SUBTITLE, 0 /* icon */,
168                 R.string.support_more_help_title, 0 /* summary */, null /* intent */));
169         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_forum_24dp,
170                 R.string.support_forum_title, 0 /* summary */,
171                 mSupportFeatureProvider.getForumIntent()));
172         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_help_24dp,
173                 R.string.support_articles_title, 0 /* summary */, null /*intent */));
174         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_feedback_24dp,
175                 R.string.support_feedback_title, 0 /* summary */, null /*intent */));
176     }
177
178     /**
179      * {@link RecyclerView.ViewHolder} for support items.
180      */
181     static final class ViewHolder extends RecyclerView.ViewHolder {
182
183         final ImageView iconView;
184         final TextView titleView;
185         final TextView summaryView;
186
187         ViewHolder(View itemView) {
188             super(itemView);
189             iconView = (ImageView) itemView.findViewById(android.R.id.icon);
190             titleView = (TextView) itemView.findViewById(android.R.id.title);
191             summaryView = (TextView) itemView.findViewById(android.R.id.summary);
192         }
193     }
194
195     /**
196      * Data for a single support item.
197      */
198     private static final class SupportData {
199
200         final Intent intent;
201         @LayoutRes final int type;
202         @DrawableRes final int icon;
203         @StringRes final int title;
204         @StringRes final int summary;
205
206         SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int title,
207                 @StringRes int summary, Intent intent) {
208             this.type = type;
209             this.icon = icon;
210             this.title = title;
211             this.summary = summary;
212             this.intent = intent;
213         }
214     }
215 }