blob: b4cc4b1586a2d93c1642b023c285eab199fea673 [file] [log] [blame]
John Spurlock486b78e2014-07-07 08:37:56 -04001/*
2 * Copyright (C) 2014 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
17package com.android.systemui.qs;
18
19import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020020import android.content.res.Configuration;
Jason Monkb27ec6d32014-12-01 17:50:16 -050021import android.graphics.drawable.Drawable;
John Spurlock486b78e2014-07-07 08:37:56 -040022import android.os.Handler;
23import android.os.Looper;
24import android.os.Message;
25import android.text.TextUtils;
26import android.util.AttributeSet;
27import android.util.Log;
28import android.view.LayoutInflater;
29import android.view.View;
30import android.view.ViewGroup;
Jason Monk6573ef22016-04-06 12:37:18 -040031import android.widget.BaseAdapter;
John Spurlock486b78e2014-07-07 08:37:56 -040032import android.widget.FrameLayout;
33import android.widget.ImageView;
John Spurlock486b78e2014-07-07 08:37:56 -040034import android.widget.TextView;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020035import com.android.systemui.FontSizeUtils;
John Spurlock486b78e2014-07-07 08:37:56 -040036import com.android.systemui.R;
37
38/**
39 * Quick settings common detail view with line items.
40 */
41public class QSDetailItems extends FrameLayout {
42 private static final String TAG = "QSDetailItems";
43 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
44
Muyuan Li72a63872016-05-14 11:35:41 -070045 private final int mQsDetailIconOverlaySize;
John Spurlock486b78e2014-07-07 08:37:56 -040046 private final Context mContext;
47 private final H mHandler = new H();
Jason Monk6573ef22016-04-06 12:37:18 -040048 private final Adapter mAdapter = new Adapter();
John Spurlock486b78e2014-07-07 08:37:56 -040049
50 private String mTag;
51 private Callback mCallback;
52 private boolean mItemsVisible = true;
Jason Monk6573ef22016-04-06 12:37:18 -040053 private AutoSizingList mItemList;
John Spurlock486b78e2014-07-07 08:37:56 -040054 private View mEmpty;
55 private TextView mEmptyText;
56 private ImageView mEmptyIcon;
Jason Monk6573ef22016-04-06 12:37:18 -040057
58 private Item[] mItems;
John Spurlock486b78e2014-07-07 08:37:56 -040059
60 public QSDetailItems(Context context, AttributeSet attrs) {
61 super(context, attrs);
62 mContext = context;
63 mTag = TAG;
Muyuan Li72a63872016-05-14 11:35:41 -070064 mQsDetailIconOverlaySize = (int) getResources().getDimension(
65 R.dimen.qs_detail_icon_overlay_size);
John Spurlock486b78e2014-07-07 08:37:56 -040066 }
67
68 public static QSDetailItems convertOrInflate(Context context, View convert, ViewGroup parent) {
69 if (convert instanceof QSDetailItems) {
70 return (QSDetailItems) convert;
71 }
72 return (QSDetailItems) LayoutInflater.from(context).inflate(R.layout.qs_detail_items,
73 parent, false);
74 }
75
76 @Override
77 protected void onFinishInflate() {
78 super.onFinishInflate();
Alan Viverette51efddb2017-04-05 10:00:01 -040079 mItemList = findViewById(android.R.id.list);
Jason Monk6573ef22016-04-06 12:37:18 -040080 mItemList.setVisibility(GONE);
81 mItemList.setAdapter(mAdapter);
John Spurlock486b78e2014-07-07 08:37:56 -040082 mEmpty = findViewById(android.R.id.empty);
83 mEmpty.setVisibility(GONE);
Jason Monkbe3235a2017-04-05 09:29:53 -040084 mEmptyText = mEmpty.findViewById(android.R.id.title);
85 mEmptyIcon = mEmpty.findViewById(android.R.id.icon);
John Spurlock486b78e2014-07-07 08:37:56 -040086 }
87
Jorim Jaggie17c4b42014-08-26 17:27:31 +020088 @Override
89 protected void onConfigurationChanged(Configuration newConfig) {
90 super.onConfigurationChanged(newConfig);
91 FontSizeUtils.updateFontSize(mEmptyText, R.dimen.qs_detail_empty_text_size);
Jason Monk6573ef22016-04-06 12:37:18 -040092 int count = mItemList.getChildCount();
Jorim Jaggie17c4b42014-08-26 17:27:31 +020093 for (int i = 0; i < count; i++) {
Jason Monk6573ef22016-04-06 12:37:18 -040094 View item = mItemList.getChildAt(i);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020095 FontSizeUtils.updateFontSize(item, android.R.id.title,
96 R.dimen.qs_detail_item_primary_text_size);
97 FontSizeUtils.updateFontSize(item, android.R.id.summary,
98 R.dimen.qs_detail_item_secondary_text_size);
99 }
100 }
101
John Spurlock486b78e2014-07-07 08:37:56 -0400102 public void setTagSuffix(String suffix) {
103 mTag = TAG + "." + suffix;
104 }
105
106 public void setEmptyState(int icon, int text) {
Jason Monkbe3235a2017-04-05 09:29:53 -0400107 mEmptyIcon.post(() -> {
108 mEmptyIcon.setImageResource(icon);
109 mEmptyText.setText(text);
110 });
John Spurlock486b78e2014-07-07 08:37:56 -0400111 }
112
113 @Override
114 protected void onAttachedToWindow() {
115 super.onAttachedToWindow();
116 if (DEBUG) Log.d(mTag, "onAttachedToWindow");
117 }
118
119 @Override
120 protected void onDetachedFromWindow() {
121 super.onDetachedFromWindow();
122 if (DEBUG) Log.d(mTag, "onDetachedFromWindow");
123 mCallback = null;
124 }
125
126 public void setCallback(Callback callback) {
127 mHandler.removeMessages(H.SET_CALLBACK);
128 mHandler.obtainMessage(H.SET_CALLBACK, callback).sendToTarget();
129 }
130
131 public void setItems(Item[] items) {
132 mHandler.removeMessages(H.SET_ITEMS);
133 mHandler.obtainMessage(H.SET_ITEMS, items).sendToTarget();
134 }
135
136 public void setItemsVisible(boolean visible) {
137 mHandler.removeMessages(H.SET_ITEMS_VISIBLE);
138 mHandler.obtainMessage(H.SET_ITEMS_VISIBLE, visible ? 1 : 0, 0).sendToTarget();
139 }
140
141 private void handleSetCallback(Callback callback) {
142 mCallback = callback;
143 }
144
145 private void handleSetItems(Item[] items) {
Jason Monk6573ef22016-04-06 12:37:18 -0400146 final int itemCount = items != null ? items.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400147 mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE);
Jason Monk6573ef22016-04-06 12:37:18 -0400148 mItemList.setVisibility(itemCount == 0 ? GONE : VISIBLE);
149 mItems = items;
150 mAdapter.notifyDataSetChanged();
John Spurlock486b78e2014-07-07 08:37:56 -0400151 }
152
153 private void handleSetItemsVisible(boolean visible) {
154 if (mItemsVisible == visible) return;
155 mItemsVisible = visible;
Jason Monk6573ef22016-04-06 12:37:18 -0400156 for (int i = 0; i < mItemList.getChildCount(); i++) {
157 mItemList.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
John Spurlock486b78e2014-07-07 08:37:56 -0400158 }
159 }
160
Jason Monk6573ef22016-04-06 12:37:18 -0400161 private class Adapter extends BaseAdapter {
162
163 @Override
164 public int getCount() {
165 return mItems != null ? mItems.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400166 }
Jason Monk6573ef22016-04-06 12:37:18 -0400167
168 @Override
169 public Object getItem(int position) {
170 return mItems[position];
Jason Monkb27ec6d32014-12-01 17:50:16 -0500171 }
Jason Monk6573ef22016-04-06 12:37:18 -0400172
173 @Override
174 public long getItemId(int position) {
175 return 0;
176 }
177
178 @Override
179 public View getView(int position, View view, ViewGroup parent) {
180 final Item item = mItems[position];
181 if (view == null) {
182 view = LayoutInflater.from(mContext).inflate(R.layout.qs_detail_item, parent,
183 false);
John Spurlock486b78e2014-07-07 08:37:56 -0400184 }
Jason Monk6573ef22016-04-06 12:37:18 -0400185 view.setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
186 final ImageView iv = (ImageView) view.findViewById(android.R.id.icon);
Jason Monk3fd0b142017-08-30 18:11:21 -0400187 if (item.iconDrawable != null) {
188 iv.setImageDrawable(item.iconDrawable);
189 } else {
190 iv.setImageResource(item.icon);
191 }
Jason Monk6573ef22016-04-06 12:37:18 -0400192 iv.getOverlay().clear();
193 if (item.overlay != null) {
Muyuan Li72a63872016-05-14 11:35:41 -0700194 item.overlay.setBounds(0, 0, mQsDetailIconOverlaySize, mQsDetailIconOverlaySize);
Jason Monk6573ef22016-04-06 12:37:18 -0400195 iv.getOverlay().add(item.overlay);
John Spurlock486b78e2014-07-07 08:37:56 -0400196 }
Jason Monk6573ef22016-04-06 12:37:18 -0400197 final TextView title = (TextView) view.findViewById(android.R.id.title);
198 title.setText(item.line1);
199 final TextView summary = (TextView) view.findViewById(android.R.id.summary);
200 final boolean twoLines = !TextUtils.isEmpty(item.line2);
201 title.setMaxLines(twoLines ? 1 : 2);
202 summary.setVisibility(twoLines ? VISIBLE : GONE);
203 summary.setText(twoLines ? item.line2 : null);
204 view.setOnClickListener(new OnClickListener() {
205 @Override
206 public void onClick(View v) {
207 if (mCallback != null) {
208 mCallback.onDetailItemClick(item);
209 }
210 }
211 });
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800212
213 final ImageView icon2 = (ImageView) view.findViewById(android.R.id.icon2);
214 if (item.canDisconnect) {
215 icon2.setImageResource(R.drawable.ic_qs_cancel);
216 icon2.setVisibility(VISIBLE);
217 icon2.setClickable(true);
218 icon2.setOnClickListener(new OnClickListener() {
219 @Override
220 public void onClick(View v) {
221 if (mCallback != null) {
222 mCallback.onDetailItemDisconnect(item);
223 }
Jason Monk6573ef22016-04-06 12:37:18 -0400224 }
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800225 });
226 } else if (item.icon2 != -1) {
227 icon2.setVisibility(VISIBLE);
228 icon2.setImageResource(item.icon2);
229 icon2.setClickable(false);
230 } else {
231 icon2.setVisibility(GONE);
232 }
233
Jason Monk6573ef22016-04-06 12:37:18 -0400234 return view;
235 }
236 };
John Spurlock486b78e2014-07-07 08:37:56 -0400237
238 private class H extends Handler {
239 private static final int SET_ITEMS = 1;
240 private static final int SET_CALLBACK = 2;
241 private static final int SET_ITEMS_VISIBLE = 3;
242
243 public H() {
244 super(Looper.getMainLooper());
245 }
246
247 @Override
248 public void handleMessage(Message msg) {
249 if (msg.what == SET_ITEMS) {
250 handleSetItems((Item[]) msg.obj);
251 } else if (msg.what == SET_CALLBACK) {
252 handleSetCallback((QSDetailItems.Callback) msg.obj);
253 } else if (msg.what == SET_ITEMS_VISIBLE) {
254 handleSetItemsVisible(msg.arg1 != 0);
255 }
256 }
257 }
258
259 public static class Item {
260 public int icon;
Jason Monk3fd0b142017-08-30 18:11:21 -0400261 public Drawable iconDrawable;
Jason Monkb27ec6d32014-12-01 17:50:16 -0500262 public Drawable overlay;
Jason Monk6980d122015-06-15 10:07:55 -0400263 public CharSequence line1;
264 public CharSequence line2;
John Spurlock486b78e2014-07-07 08:37:56 -0400265 public Object tag;
266 public boolean canDisconnect;
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800267 public int icon2 = -1;
John Spurlock486b78e2014-07-07 08:37:56 -0400268 }
269
270 public interface Callback {
271 void onDetailItemClick(Item item);
272 void onDetailItemDisconnect(Item item);
273 }
274}