blob: dab0efe10b5d8b6dfc69960ec4317e5ff7dc8a2f [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;
Gus Prevasab336792018-11-14 13:52:20 -050035
Jorim Jaggie17c4b42014-08-26 17:27:31 +020036import com.android.systemui.FontSizeUtils;
John Spurlock486b78e2014-07-07 08:37:56 -040037import com.android.systemui.R;
jackqdyulei4b7f43b2017-10-19 11:05:09 -070038import com.android.systemui.plugins.qs.QSTile;
John Spurlock486b78e2014-07-07 08:37:56 -040039
40/**
41 * Quick settings common detail view with line items.
42 */
43public class QSDetailItems extends FrameLayout {
44 private static final String TAG = "QSDetailItems";
45 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
46
Muyuan Li72a63872016-05-14 11:35:41 -070047 private final int mQsDetailIconOverlaySize;
John Spurlock486b78e2014-07-07 08:37:56 -040048 private final Context mContext;
49 private final H mHandler = new H();
Jason Monk6573ef22016-04-06 12:37:18 -040050 private final Adapter mAdapter = new Adapter();
John Spurlock486b78e2014-07-07 08:37:56 -040051
52 private String mTag;
53 private Callback mCallback;
54 private boolean mItemsVisible = true;
Jason Monk6573ef22016-04-06 12:37:18 -040055 private AutoSizingList mItemList;
John Spurlock486b78e2014-07-07 08:37:56 -040056 private View mEmpty;
57 private TextView mEmptyText;
58 private ImageView mEmptyIcon;
Jason Monk6573ef22016-04-06 12:37:18 -040059
60 private Item[] mItems;
John Spurlock486b78e2014-07-07 08:37:56 -040061
62 public QSDetailItems(Context context, AttributeSet attrs) {
63 super(context, attrs);
64 mContext = context;
65 mTag = TAG;
Muyuan Li72a63872016-05-14 11:35:41 -070066 mQsDetailIconOverlaySize = (int) getResources().getDimension(
67 R.dimen.qs_detail_icon_overlay_size);
John Spurlock486b78e2014-07-07 08:37:56 -040068 }
69
70 public static QSDetailItems convertOrInflate(Context context, View convert, ViewGroup parent) {
71 if (convert instanceof QSDetailItems) {
72 return (QSDetailItems) convert;
73 }
74 return (QSDetailItems) LayoutInflater.from(context).inflate(R.layout.qs_detail_items,
75 parent, false);
76 }
77
78 @Override
79 protected void onFinishInflate() {
80 super.onFinishInflate();
Alan Viverette51efddb2017-04-05 10:00:01 -040081 mItemList = findViewById(android.R.id.list);
Jason Monk6573ef22016-04-06 12:37:18 -040082 mItemList.setVisibility(GONE);
83 mItemList.setAdapter(mAdapter);
John Spurlock486b78e2014-07-07 08:37:56 -040084 mEmpty = findViewById(android.R.id.empty);
85 mEmpty.setVisibility(GONE);
Jason Monkbe3235a2017-04-05 09:29:53 -040086 mEmptyText = mEmpty.findViewById(android.R.id.title);
87 mEmptyIcon = mEmpty.findViewById(android.R.id.icon);
John Spurlock486b78e2014-07-07 08:37:56 -040088 }
89
Jorim Jaggie17c4b42014-08-26 17:27:31 +020090 @Override
91 protected void onConfigurationChanged(Configuration newConfig) {
92 super.onConfigurationChanged(newConfig);
93 FontSizeUtils.updateFontSize(mEmptyText, R.dimen.qs_detail_empty_text_size);
Jason Monk6573ef22016-04-06 12:37:18 -040094 int count = mItemList.getChildCount();
Jorim Jaggie17c4b42014-08-26 17:27:31 +020095 for (int i = 0; i < count; i++) {
Jason Monk6573ef22016-04-06 12:37:18 -040096 View item = mItemList.getChildAt(i);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020097 FontSizeUtils.updateFontSize(item, android.R.id.title,
98 R.dimen.qs_detail_item_primary_text_size);
99 FontSizeUtils.updateFontSize(item, android.R.id.summary,
100 R.dimen.qs_detail_item_secondary_text_size);
101 }
102 }
103
John Spurlock486b78e2014-07-07 08:37:56 -0400104 public void setTagSuffix(String suffix) {
105 mTag = TAG + "." + suffix;
106 }
107
108 public void setEmptyState(int icon, int text) {
Jason Monkbe3235a2017-04-05 09:29:53 -0400109 mEmptyIcon.post(() -> {
110 mEmptyIcon.setImageResource(icon);
111 mEmptyText.setText(text);
112 });
John Spurlock486b78e2014-07-07 08:37:56 -0400113 }
114
115 @Override
116 protected void onAttachedToWindow() {
117 super.onAttachedToWindow();
118 if (DEBUG) Log.d(mTag, "onAttachedToWindow");
119 }
120
121 @Override
122 protected void onDetachedFromWindow() {
123 super.onDetachedFromWindow();
124 if (DEBUG) Log.d(mTag, "onDetachedFromWindow");
125 mCallback = null;
126 }
127
128 public void setCallback(Callback callback) {
129 mHandler.removeMessages(H.SET_CALLBACK);
130 mHandler.obtainMessage(H.SET_CALLBACK, callback).sendToTarget();
131 }
132
133 public void setItems(Item[] items) {
134 mHandler.removeMessages(H.SET_ITEMS);
135 mHandler.obtainMessage(H.SET_ITEMS, items).sendToTarget();
136 }
137
138 public void setItemsVisible(boolean visible) {
139 mHandler.removeMessages(H.SET_ITEMS_VISIBLE);
140 mHandler.obtainMessage(H.SET_ITEMS_VISIBLE, visible ? 1 : 0, 0).sendToTarget();
141 }
142
143 private void handleSetCallback(Callback callback) {
144 mCallback = callback;
145 }
146
147 private void handleSetItems(Item[] items) {
Jason Monk6573ef22016-04-06 12:37:18 -0400148 final int itemCount = items != null ? items.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400149 mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE);
Jason Monk6573ef22016-04-06 12:37:18 -0400150 mItemList.setVisibility(itemCount == 0 ? GONE : VISIBLE);
151 mItems = items;
152 mAdapter.notifyDataSetChanged();
John Spurlock486b78e2014-07-07 08:37:56 -0400153 }
154
155 private void handleSetItemsVisible(boolean visible) {
156 if (mItemsVisible == visible) return;
157 mItemsVisible = visible;
Jason Monk6573ef22016-04-06 12:37:18 -0400158 for (int i = 0; i < mItemList.getChildCount(); i++) {
159 mItemList.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
John Spurlock486b78e2014-07-07 08:37:56 -0400160 }
161 }
162
Jason Monk6573ef22016-04-06 12:37:18 -0400163 private class Adapter extends BaseAdapter {
164
165 @Override
166 public int getCount() {
167 return mItems != null ? mItems.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400168 }
Jason Monk6573ef22016-04-06 12:37:18 -0400169
170 @Override
171 public Object getItem(int position) {
172 return mItems[position];
Jason Monkb27ec6d32014-12-01 17:50:16 -0500173 }
Jason Monk6573ef22016-04-06 12:37:18 -0400174
175 @Override
176 public long getItemId(int position) {
177 return 0;
178 }
179
180 @Override
181 public View getView(int position, View view, ViewGroup parent) {
182 final Item item = mItems[position];
183 if (view == null) {
184 view = LayoutInflater.from(mContext).inflate(R.layout.qs_detail_item, parent,
185 false);
John Spurlock486b78e2014-07-07 08:37:56 -0400186 }
Jason Monk6573ef22016-04-06 12:37:18 -0400187 view.setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
188 final ImageView iv = (ImageView) view.findViewById(android.R.id.icon);
jackqdyulei786a4902017-10-24 13:27:53 -0700189 if (item.icon != null) {
190 iv.setImageDrawable(item.icon.getDrawable(iv.getContext()));
Jason Monk3fd0b142017-08-30 18:11:21 -0400191 } else {
jackqdyulei786a4902017-10-24 13:27:53 -0700192 iv.setImageResource(item.iconResId);
Jason Monk3fd0b142017-08-30 18:11:21 -0400193 }
Jason Monk6573ef22016-04-06 12:37:18 -0400194 iv.getOverlay().clear();
195 if (item.overlay != null) {
Muyuan Li72a63872016-05-14 11:35:41 -0700196 item.overlay.setBounds(0, 0, mQsDetailIconOverlaySize, mQsDetailIconOverlaySize);
Jason Monk6573ef22016-04-06 12:37:18 -0400197 iv.getOverlay().add(item.overlay);
John Spurlock486b78e2014-07-07 08:37:56 -0400198 }
Jason Monk6573ef22016-04-06 12:37:18 -0400199 final TextView title = (TextView) view.findViewById(android.R.id.title);
200 title.setText(item.line1);
201 final TextView summary = (TextView) view.findViewById(android.R.id.summary);
202 final boolean twoLines = !TextUtils.isEmpty(item.line2);
203 title.setMaxLines(twoLines ? 1 : 2);
204 summary.setVisibility(twoLines ? VISIBLE : GONE);
205 summary.setText(twoLines ? item.line2 : null);
206 view.setOnClickListener(new OnClickListener() {
207 @Override
208 public void onClick(View v) {
209 if (mCallback != null) {
210 mCallback.onDetailItemClick(item);
211 }
212 }
213 });
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800214
215 final ImageView icon2 = (ImageView) view.findViewById(android.R.id.icon2);
216 if (item.canDisconnect) {
217 icon2.setImageResource(R.drawable.ic_qs_cancel);
218 icon2.setVisibility(VISIBLE);
219 icon2.setClickable(true);
220 icon2.setOnClickListener(new OnClickListener() {
221 @Override
222 public void onClick(View v) {
223 if (mCallback != null) {
224 mCallback.onDetailItemDisconnect(item);
225 }
Jason Monk6573ef22016-04-06 12:37:18 -0400226 }
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800227 });
228 } else if (item.icon2 != -1) {
229 icon2.setVisibility(VISIBLE);
230 icon2.setImageResource(item.icon2);
231 icon2.setClickable(false);
232 } else {
233 icon2.setVisibility(GONE);
234 }
235
Jason Monk6573ef22016-04-06 12:37:18 -0400236 return view;
237 }
238 };
John Spurlock486b78e2014-07-07 08:37:56 -0400239
240 private class H extends Handler {
241 private static final int SET_ITEMS = 1;
242 private static final int SET_CALLBACK = 2;
243 private static final int SET_ITEMS_VISIBLE = 3;
244
245 public H() {
246 super(Looper.getMainLooper());
247 }
248
249 @Override
250 public void handleMessage(Message msg) {
251 if (msg.what == SET_ITEMS) {
252 handleSetItems((Item[]) msg.obj);
253 } else if (msg.what == SET_CALLBACK) {
254 handleSetCallback((QSDetailItems.Callback) msg.obj);
255 } else if (msg.what == SET_ITEMS_VISIBLE) {
256 handleSetItemsVisible(msg.arg1 != 0);
257 }
258 }
259 }
260
261 public static class Item {
jackqdyulei786a4902017-10-24 13:27:53 -0700262 public int iconResId;
263 public QSTile.Icon icon;
Jason Monkb27ec6d32014-12-01 17:50:16 -0500264 public Drawable overlay;
Jason Monk6980d122015-06-15 10:07:55 -0400265 public CharSequence line1;
266 public CharSequence line2;
John Spurlock486b78e2014-07-07 08:37:56 -0400267 public Object tag;
268 public boolean canDisconnect;
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800269 public int icon2 = -1;
John Spurlock486b78e2014-07-07 08:37:56 -0400270 }
271
272 public interface Callback {
273 void onDetailItemClick(Item item);
274 void onDetailItemDisconnect(Item item);
275 }
276}