blob: 65238b118a6f6a9f7b09f47aeba6933ad0236a8b [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();
Jason Monk6573ef22016-04-06 12:37:18 -040079 mItemList = (AutoSizingList) findViewById(android.R.id.list);
80 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);
84 mEmptyText = (TextView) mEmpty.findViewById(android.R.id.title);
85 mEmptyIcon = (ImageView) mEmpty.findViewById(android.R.id.icon);
86 }
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) {
107 mEmptyIcon.setImageResource(icon);
108 mEmptyText.setText(text);
109 }
110
111 @Override
112 protected void onAttachedToWindow() {
113 super.onAttachedToWindow();
114 if (DEBUG) Log.d(mTag, "onAttachedToWindow");
115 }
116
117 @Override
118 protected void onDetachedFromWindow() {
119 super.onDetachedFromWindow();
120 if (DEBUG) Log.d(mTag, "onDetachedFromWindow");
121 mCallback = null;
122 }
123
124 public void setCallback(Callback callback) {
125 mHandler.removeMessages(H.SET_CALLBACK);
126 mHandler.obtainMessage(H.SET_CALLBACK, callback).sendToTarget();
127 }
128
129 public void setItems(Item[] items) {
130 mHandler.removeMessages(H.SET_ITEMS);
131 mHandler.obtainMessage(H.SET_ITEMS, items).sendToTarget();
132 }
133
134 public void setItemsVisible(boolean visible) {
135 mHandler.removeMessages(H.SET_ITEMS_VISIBLE);
136 mHandler.obtainMessage(H.SET_ITEMS_VISIBLE, visible ? 1 : 0, 0).sendToTarget();
137 }
138
139 private void handleSetCallback(Callback callback) {
140 mCallback = callback;
141 }
142
143 private void handleSetItems(Item[] items) {
Jason Monk6573ef22016-04-06 12:37:18 -0400144 final int itemCount = items != null ? items.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400145 mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE);
Jason Monk6573ef22016-04-06 12:37:18 -0400146 mItemList.setVisibility(itemCount == 0 ? GONE : VISIBLE);
147 mItems = items;
148 mAdapter.notifyDataSetChanged();
John Spurlock486b78e2014-07-07 08:37:56 -0400149 }
150
151 private void handleSetItemsVisible(boolean visible) {
152 if (mItemsVisible == visible) return;
153 mItemsVisible = visible;
Jason Monk6573ef22016-04-06 12:37:18 -0400154 for (int i = 0; i < mItemList.getChildCount(); i++) {
155 mItemList.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
John Spurlock486b78e2014-07-07 08:37:56 -0400156 }
157 }
158
Jason Monk6573ef22016-04-06 12:37:18 -0400159 private class Adapter extends BaseAdapter {
160
161 @Override
162 public int getCount() {
163 return mItems != null ? mItems.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400164 }
Jason Monk6573ef22016-04-06 12:37:18 -0400165
166 @Override
167 public Object getItem(int position) {
168 return mItems[position];
Jason Monkb27ec6d32014-12-01 17:50:16 -0500169 }
Jason Monk6573ef22016-04-06 12:37:18 -0400170
171 @Override
172 public long getItemId(int position) {
173 return 0;
174 }
175
176 @Override
177 public View getView(int position, View view, ViewGroup parent) {
178 final Item item = mItems[position];
179 if (view == null) {
180 view = LayoutInflater.from(mContext).inflate(R.layout.qs_detail_item, parent,
181 false);
John Spurlock486b78e2014-07-07 08:37:56 -0400182 }
Jason Monk6573ef22016-04-06 12:37:18 -0400183 view.setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
184 final ImageView iv = (ImageView) view.findViewById(android.R.id.icon);
185 iv.setImageResource(item.icon);
186 iv.getOverlay().clear();
187 if (item.overlay != null) {
Muyuan Li72a63872016-05-14 11:35:41 -0700188 item.overlay.setBounds(0, 0, mQsDetailIconOverlaySize, mQsDetailIconOverlaySize);
Jason Monk6573ef22016-04-06 12:37:18 -0400189 iv.getOverlay().add(item.overlay);
John Spurlock486b78e2014-07-07 08:37:56 -0400190 }
Jason Monk6573ef22016-04-06 12:37:18 -0400191 final TextView title = (TextView) view.findViewById(android.R.id.title);
192 title.setText(item.line1);
193 final TextView summary = (TextView) view.findViewById(android.R.id.summary);
194 final boolean twoLines = !TextUtils.isEmpty(item.line2);
195 title.setMaxLines(twoLines ? 1 : 2);
196 summary.setVisibility(twoLines ? VISIBLE : GONE);
197 summary.setText(twoLines ? item.line2 : null);
198 view.setOnClickListener(new OnClickListener() {
199 @Override
200 public void onClick(View v) {
201 if (mCallback != null) {
202 mCallback.onDetailItemClick(item);
203 }
204 }
205 });
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800206
207 final ImageView icon2 = (ImageView) view.findViewById(android.R.id.icon2);
208 if (item.canDisconnect) {
209 icon2.setImageResource(R.drawable.ic_qs_cancel);
210 icon2.setVisibility(VISIBLE);
211 icon2.setClickable(true);
212 icon2.setOnClickListener(new OnClickListener() {
213 @Override
214 public void onClick(View v) {
215 if (mCallback != null) {
216 mCallback.onDetailItemDisconnect(item);
217 }
Jason Monk6573ef22016-04-06 12:37:18 -0400218 }
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800219 });
220 } else if (item.icon2 != -1) {
221 icon2.setVisibility(VISIBLE);
222 icon2.setImageResource(item.icon2);
223 icon2.setClickable(false);
224 } else {
225 icon2.setVisibility(GONE);
226 }
227
Jason Monk6573ef22016-04-06 12:37:18 -0400228 return view;
229 }
230 };
John Spurlock486b78e2014-07-07 08:37:56 -0400231
232 private class H extends Handler {
233 private static final int SET_ITEMS = 1;
234 private static final int SET_CALLBACK = 2;
235 private static final int SET_ITEMS_VISIBLE = 3;
236
237 public H() {
238 super(Looper.getMainLooper());
239 }
240
241 @Override
242 public void handleMessage(Message msg) {
243 if (msg.what == SET_ITEMS) {
244 handleSetItems((Item[]) msg.obj);
245 } else if (msg.what == SET_CALLBACK) {
246 handleSetCallback((QSDetailItems.Callback) msg.obj);
247 } else if (msg.what == SET_ITEMS_VISIBLE) {
248 handleSetItemsVisible(msg.arg1 != 0);
249 }
250 }
251 }
252
253 public static class Item {
254 public int icon;
Jason Monkb27ec6d32014-12-01 17:50:16 -0500255 public Drawable overlay;
Jason Monk6980d122015-06-15 10:07:55 -0400256 public CharSequence line1;
257 public CharSequence line2;
John Spurlock486b78e2014-07-07 08:37:56 -0400258 public Object tag;
259 public boolean canDisconnect;
Sundeep Ghumanff0f1082017-02-10 11:49:36 -0800260 public int icon2 = -1;
John Spurlock486b78e2014-07-07 08:37:56 -0400261 }
262
263 public interface Callback {
264 void onDetailItemClick(Item item);
265 void onDetailItemDisconnect(Item item);
266 }
267}