OSDN Git Service

Merge "Show \'on the lockscreen\' setting in more contexts." 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
51     private final Activity mActivity;
52     private final SupportFeatureProvider mSupportFeatureProvider;
53     private final View.OnClickListener mItemClickListener;
54     private final List<SupportData> mSupportData;
55
56     public SupportItemAdapter(Activity activity, SupportFeatureProvider supportFeatureProvider,
57             View.OnClickListener itemClickListener) {
58         mActivity = activity;
59         mSupportFeatureProvider = supportFeatureProvider;
60         mItemClickListener = itemClickListener;
61         mSupportData = new ArrayList<>();
62         setHasStableIds(true);
63         refreshData();
64     }
65
66     @Override
67     public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
68         return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(
69                 viewType, parent, false));
70     }
71
72     @Override
73     public void onBindViewHolder(ViewHolder holder, int position) {
74         final SupportData data = mSupportData.get(position);
75         if (holder.iconView != null) {
76             holder.iconView.setImageResource(data.icon);
77         }
78         if (holder.titleView != null) {
79             holder.titleView.setText(data.title);
80         }
81         if (holder.summaryView != null) {
82             holder.summaryView.setText(data.summary);
83         }
84         holder.itemView.setOnClickListener(mItemClickListener);
85     }
86
87     @Override
88     public int getItemViewType(int position) {
89         return mSupportData.get(position).type;
90     }
91
92     @Override
93     public int getItemCount() {
94         return mSupportData.size();
95     }
96
97     /**
98      * Called when a support item is clicked.
99      */
100     public void onItemClicked(int position) {
101         if (position >= 0 && position < mSupportData.size()) {
102             final SupportData data = mSupportData.get(position);
103             if (data.intent != null) {
104                 mActivity.startActivityForResult(data.intent, 0);
105             }
106         }
107     }
108
109     /**
110      * Create data for the adapter. If there is already data in the adapter, they will be
111      * destroyed and recreated.
112      */
113     public void refreshData() {
114         mSupportData.clear();
115         final Account[] accounts = mSupportFeatureProvider.getSupportEligibleAccounts(mActivity);
116         if (accounts.length > 0) {
117             addEscalationCards(accounts[0]);
118         }
119         addMoreHelpItems();
120         notifyDataSetChanged();
121     }
122
123     private void addEscalationCards(Account account) {
124         mSupportData.add(new SupportData(TYPE_TITLE, 0 /* icon */,
125                 R.string.support_escalation_title, R.string.support_escalation_summary,
126                 null /* intent */));
127         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, PHONE)) {
128             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_call_24dp,
129                     R.string.support_escalation_by_phone, 0 /* summary */,
130                     mSupportFeatureProvider.getSupportIntent(mActivity, account, PHONE)));
131         }
132         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, EMAIL)) {
133             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_mail_24dp,
134                     R.string.support_escalation_by_email, 0 /* summary */,
135                     mSupportFeatureProvider.getSupportIntent(mActivity, account, EMAIL)));
136         }
137         if (mSupportFeatureProvider.isSupportTypeEnabled(mActivity, CHAT)) {
138             mSupportData.add(new SupportData(TYPE_ESCALATION_CARD, R.drawable.ic_chat_24dp,
139                     R.string.support_escalation_by_chat, 0 /* summary */,
140                     mSupportFeatureProvider.getSupportIntent(mActivity, account, CHAT)));
141         }
142     }
143
144     private void addMoreHelpItems() {
145         mSupportData.add(new SupportData(TYPE_SUBTITLE, 0 /* icon */,
146                 R.string.support_more_help_title, 0 /* summary */, null /* intent */));
147         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_forum_24dp,
148                 R.string.support_forum_title, 0 /* summary */,
149                 mSupportFeatureProvider.getForumIntent()));
150         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_help_24dp,
151                 R.string.support_articles_title, 0 /* summary */, null /*intent */));
152         mSupportData.add(new SupportData(TYPE_SUPPORT_TILE, R.drawable.ic_feedback_24dp,
153                 R.string.support_feedback_title, 0 /* summary */, null /*intent */));
154     }
155
156     /**
157      * {@link RecyclerView.ViewHolder} for support items.
158      */
159     static final class ViewHolder extends RecyclerView.ViewHolder {
160
161         final ImageView iconView;
162         final TextView titleView;
163         final TextView summaryView;
164
165         ViewHolder(View itemView) {
166             super(itemView);
167             iconView = (ImageView) itemView.findViewById(android.R.id.icon);
168             titleView = (TextView) itemView.findViewById(android.R.id.title);
169             summaryView = (TextView) itemView.findViewById(android.R.id.summary);
170         }
171     }
172
173     /**
174      * Data for a single support item.
175      */
176     private static final class SupportData {
177
178         final Intent intent;
179         @LayoutRes final int type;
180         @DrawableRes final int icon;
181         @StringRes final int title;
182         @StringRes final int summary;
183
184         SupportData(@LayoutRes int type, @DrawableRes int icon, @StringRes int title,
185                 @StringRes int summary, Intent intent) {
186             this.type = type;
187             this.icon = icon;
188             this.title = title;
189             this.summary = summary;
190             this.intent = intent;
191         }
192     }
193 }