blob: 2dd4a0ab76eb3c4e23b4b43b48069b1d5a659ded [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
45 private final Context mContext;
46 private final H mHandler = new H();
Jason Monk6573ef22016-04-06 12:37:18 -040047 private final Adapter mAdapter = new Adapter();
John Spurlock486b78e2014-07-07 08:37:56 -040048
49 private String mTag;
50 private Callback mCallback;
51 private boolean mItemsVisible = true;
Jason Monk6573ef22016-04-06 12:37:18 -040052 private AutoSizingList mItemList;
John Spurlock486b78e2014-07-07 08:37:56 -040053 private View mEmpty;
54 private TextView mEmptyText;
55 private ImageView mEmptyIcon;
Jason Monk6573ef22016-04-06 12:37:18 -040056
57 private Item[] mItems;
John Spurlock486b78e2014-07-07 08:37:56 -040058
59 public QSDetailItems(Context context, AttributeSet attrs) {
60 super(context, attrs);
61 mContext = context;
62 mTag = TAG;
63 }
64
65 public static QSDetailItems convertOrInflate(Context context, View convert, ViewGroup parent) {
66 if (convert instanceof QSDetailItems) {
67 return (QSDetailItems) convert;
68 }
69 return (QSDetailItems) LayoutInflater.from(context).inflate(R.layout.qs_detail_items,
70 parent, false);
71 }
72
73 @Override
74 protected void onFinishInflate() {
75 super.onFinishInflate();
Jason Monk6573ef22016-04-06 12:37:18 -040076 mItemList = (AutoSizingList) findViewById(android.R.id.list);
77 mItemList.setVisibility(GONE);
78 mItemList.setAdapter(mAdapter);
John Spurlock486b78e2014-07-07 08:37:56 -040079 mEmpty = findViewById(android.R.id.empty);
80 mEmpty.setVisibility(GONE);
81 mEmptyText = (TextView) mEmpty.findViewById(android.R.id.title);
82 mEmptyIcon = (ImageView) mEmpty.findViewById(android.R.id.icon);
83 }
84
Jorim Jaggie17c4b42014-08-26 17:27:31 +020085 @Override
86 protected void onConfigurationChanged(Configuration newConfig) {
87 super.onConfigurationChanged(newConfig);
88 FontSizeUtils.updateFontSize(mEmptyText, R.dimen.qs_detail_empty_text_size);
Jason Monk6573ef22016-04-06 12:37:18 -040089 int count = mItemList.getChildCount();
Jorim Jaggie17c4b42014-08-26 17:27:31 +020090 for (int i = 0; i < count; i++) {
Jason Monk6573ef22016-04-06 12:37:18 -040091 View item = mItemList.getChildAt(i);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020092 FontSizeUtils.updateFontSize(item, android.R.id.title,
93 R.dimen.qs_detail_item_primary_text_size);
94 FontSizeUtils.updateFontSize(item, android.R.id.summary,
95 R.dimen.qs_detail_item_secondary_text_size);
96 }
97 }
98
John Spurlock486b78e2014-07-07 08:37:56 -040099 public void setTagSuffix(String suffix) {
100 mTag = TAG + "." + suffix;
101 }
102
103 public void setEmptyState(int icon, int text) {
104 mEmptyIcon.setImageResource(icon);
105 mEmptyText.setText(text);
106 }
107
108 @Override
109 protected void onAttachedToWindow() {
110 super.onAttachedToWindow();
111 if (DEBUG) Log.d(mTag, "onAttachedToWindow");
112 }
113
114 @Override
115 protected void onDetachedFromWindow() {
116 super.onDetachedFromWindow();
117 if (DEBUG) Log.d(mTag, "onDetachedFromWindow");
118 mCallback = null;
119 }
120
121 public void setCallback(Callback callback) {
122 mHandler.removeMessages(H.SET_CALLBACK);
123 mHandler.obtainMessage(H.SET_CALLBACK, callback).sendToTarget();
124 }
125
126 public void setItems(Item[] items) {
127 mHandler.removeMessages(H.SET_ITEMS);
128 mHandler.obtainMessage(H.SET_ITEMS, items).sendToTarget();
129 }
130
131 public void setItemsVisible(boolean visible) {
132 mHandler.removeMessages(H.SET_ITEMS_VISIBLE);
133 mHandler.obtainMessage(H.SET_ITEMS_VISIBLE, visible ? 1 : 0, 0).sendToTarget();
134 }
135
136 private void handleSetCallback(Callback callback) {
137 mCallback = callback;
138 }
139
140 private void handleSetItems(Item[] items) {
Jason Monk6573ef22016-04-06 12:37:18 -0400141 final int itemCount = items != null ? items.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400142 mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE);
Jason Monk6573ef22016-04-06 12:37:18 -0400143 mItemList.setVisibility(itemCount == 0 ? GONE : VISIBLE);
144 mItems = items;
145 mAdapter.notifyDataSetChanged();
John Spurlock486b78e2014-07-07 08:37:56 -0400146 }
147
148 private void handleSetItemsVisible(boolean visible) {
149 if (mItemsVisible == visible) return;
150 mItemsVisible = visible;
Jason Monk6573ef22016-04-06 12:37:18 -0400151 for (int i = 0; i < mItemList.getChildCount(); i++) {
152 mItemList.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
John Spurlock486b78e2014-07-07 08:37:56 -0400153 }
154 }
155
Jason Monk6573ef22016-04-06 12:37:18 -0400156 private class Adapter extends BaseAdapter {
157
158 @Override
159 public int getCount() {
160 return mItems != null ? mItems.length : 0;
John Spurlock486b78e2014-07-07 08:37:56 -0400161 }
Jason Monk6573ef22016-04-06 12:37:18 -0400162
163 @Override
164 public Object getItem(int position) {
165 return mItems[position];
Jason Monkb27ec6d32014-12-01 17:50:16 -0500166 }
Jason Monk6573ef22016-04-06 12:37:18 -0400167
168 @Override
169 public long getItemId(int position) {
170 return 0;
171 }
172
173 @Override
174 public View getView(int position, View view, ViewGroup parent) {
175 final Item item = mItems[position];
176 if (view == null) {
177 view = LayoutInflater.from(mContext).inflate(R.layout.qs_detail_item, parent,
178 false);
John Spurlock486b78e2014-07-07 08:37:56 -0400179 }
Jason Monk6573ef22016-04-06 12:37:18 -0400180 view.setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
181 final ImageView iv = (ImageView) view.findViewById(android.R.id.icon);
182 iv.setImageResource(item.icon);
183 iv.getOverlay().clear();
184 if (item.overlay != null) {
185 item.overlay.setBounds(0, 0, item.overlay.getIntrinsicWidth(),
186 item.overlay.getIntrinsicHeight());
187 iv.getOverlay().add(item.overlay);
John Spurlock486b78e2014-07-07 08:37:56 -0400188 }
Jason Monk6573ef22016-04-06 12:37:18 -0400189 final TextView title = (TextView) view.findViewById(android.R.id.title);
190 title.setText(item.line1);
191 final TextView summary = (TextView) view.findViewById(android.R.id.summary);
192 final boolean twoLines = !TextUtils.isEmpty(item.line2);
193 title.setMaxLines(twoLines ? 1 : 2);
194 summary.setVisibility(twoLines ? VISIBLE : GONE);
195 summary.setText(twoLines ? item.line2 : null);
196 view.setOnClickListener(new OnClickListener() {
197 @Override
198 public void onClick(View v) {
199 if (mCallback != null) {
200 mCallback.onDetailItemClick(item);
201 }
202 }
203 });
204 final ImageView disconnect = (ImageView) view.findViewById(android.R.id.icon2);
205 disconnect.setVisibility(item.canDisconnect ? VISIBLE : GONE);
206 disconnect.setOnClickListener(new OnClickListener() {
207 @Override
208 public void onClick(View v) {
209 if (mCallback != null) {
210 mCallback.onDetailItemDisconnect(item);
211 }
212 }
213 });
214 return view;
215 }
216 };
John Spurlock486b78e2014-07-07 08:37:56 -0400217
218 private class H extends Handler {
219 private static final int SET_ITEMS = 1;
220 private static final int SET_CALLBACK = 2;
221 private static final int SET_ITEMS_VISIBLE = 3;
222
223 public H() {
224 super(Looper.getMainLooper());
225 }
226
227 @Override
228 public void handleMessage(Message msg) {
229 if (msg.what == SET_ITEMS) {
230 handleSetItems((Item[]) msg.obj);
231 } else if (msg.what == SET_CALLBACK) {
232 handleSetCallback((QSDetailItems.Callback) msg.obj);
233 } else if (msg.what == SET_ITEMS_VISIBLE) {
234 handleSetItemsVisible(msg.arg1 != 0);
235 }
236 }
237 }
238
239 public static class Item {
240 public int icon;
Jason Monkb27ec6d32014-12-01 17:50:16 -0500241 public Drawable overlay;
Jason Monk6980d122015-06-15 10:07:55 -0400242 public CharSequence line1;
243 public CharSequence line2;
John Spurlock486b78e2014-07-07 08:37:56 -0400244 public Object tag;
245 public boolean canDisconnect;
246 }
247
248 public interface Callback {
249 void onDetailItemClick(Item item);
250 void onDetailItemDisconnect(Item item);
251 }
252}