blob: a311d6e3c4c360c17507f472373d19c5d0ba3daf [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;
31import android.widget.FrameLayout;
32import android.widget.ImageView;
33import android.widget.LinearLayout;
34import android.widget.TextView;
35
Jorim Jaggie17c4b42014-08-26 17:27:31 +020036import com.android.systemui.FontSizeUtils;
John Spurlock486b78e2014-07-07 08:37:56 -040037import com.android.systemui.R;
38
39/**
40 * Quick settings common detail view with line items.
41 */
42public class QSDetailItems extends FrameLayout {
43 private static final String TAG = "QSDetailItems";
44 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
45
46 private final Context mContext;
47 private final H mHandler = new H();
48
49 private String mTag;
50 private Callback mCallback;
51 private boolean mItemsVisible = true;
52 private LinearLayout mItems;
53 private View mEmpty;
54 private TextView mEmptyText;
55 private ImageView mEmptyIcon;
56
57 public QSDetailItems(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 mContext = context;
60 mTag = TAG;
61 }
62
63 public static QSDetailItems convertOrInflate(Context context, View convert, ViewGroup parent) {
64 if (convert instanceof QSDetailItems) {
65 return (QSDetailItems) convert;
66 }
67 return (QSDetailItems) LayoutInflater.from(context).inflate(R.layout.qs_detail_items,
68 parent, false);
69 }
70
71 @Override
72 protected void onFinishInflate() {
73 super.onFinishInflate();
74 mItems = (LinearLayout) findViewById(android.R.id.list);
75 mItems.setVisibility(GONE);
76 mEmpty = findViewById(android.R.id.empty);
77 mEmpty.setVisibility(GONE);
78 mEmptyText = (TextView) mEmpty.findViewById(android.R.id.title);
79 mEmptyIcon = (ImageView) mEmpty.findViewById(android.R.id.icon);
80 }
81
Jorim Jaggie17c4b42014-08-26 17:27:31 +020082 @Override
83 protected void onConfigurationChanged(Configuration newConfig) {
84 super.onConfigurationChanged(newConfig);
85 FontSizeUtils.updateFontSize(mEmptyText, R.dimen.qs_detail_empty_text_size);
86 int count = mItems.getChildCount();
87 for (int i = 0; i < count; i++) {
88 View item = mItems.getChildAt(i);
89 FontSizeUtils.updateFontSize(item, android.R.id.title,
90 R.dimen.qs_detail_item_primary_text_size);
91 FontSizeUtils.updateFontSize(item, android.R.id.summary,
92 R.dimen.qs_detail_item_secondary_text_size);
93 }
94 }
95
John Spurlock486b78e2014-07-07 08:37:56 -040096 public void setTagSuffix(String suffix) {
97 mTag = TAG + "." + suffix;
98 }
99
100 public void setEmptyState(int icon, int text) {
101 mEmptyIcon.setImageResource(icon);
102 mEmptyText.setText(text);
103 }
104
105 @Override
106 protected void onAttachedToWindow() {
107 super.onAttachedToWindow();
108 if (DEBUG) Log.d(mTag, "onAttachedToWindow");
109 }
110
111 @Override
112 protected void onDetachedFromWindow() {
113 super.onDetachedFromWindow();
114 if (DEBUG) Log.d(mTag, "onDetachedFromWindow");
115 mCallback = null;
116 }
117
118 public void setCallback(Callback callback) {
119 mHandler.removeMessages(H.SET_CALLBACK);
120 mHandler.obtainMessage(H.SET_CALLBACK, callback).sendToTarget();
121 }
122
123 public void setItems(Item[] items) {
124 mHandler.removeMessages(H.SET_ITEMS);
125 mHandler.obtainMessage(H.SET_ITEMS, items).sendToTarget();
126 }
127
128 public void setItemsVisible(boolean visible) {
129 mHandler.removeMessages(H.SET_ITEMS_VISIBLE);
130 mHandler.obtainMessage(H.SET_ITEMS_VISIBLE, visible ? 1 : 0, 0).sendToTarget();
131 }
132
133 private void handleSetCallback(Callback callback) {
134 mCallback = callback;
135 }
136
137 private void handleSetItems(Item[] items) {
138 final int itemCount = items != null ? items.length : 0;
139 mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE);
140 mItems.setVisibility(itemCount == 0 ? GONE : VISIBLE);
141 for (int i = mItems.getChildCount() - 1; i >= itemCount; i--) {
142 mItems.removeViewAt(i);
143 }
144 for (int i = 0; i < itemCount; i++) {
145 bind(items[i], mItems.getChildAt(i));
146 }
147 }
148
149 private void handleSetItemsVisible(boolean visible) {
150 if (mItemsVisible == visible) return;
151 mItemsVisible = visible;
152 for (int i = 0; i < mItems.getChildCount(); i++) {
153 mItems.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
154 }
155 }
156
157 private void bind(final Item item, View view) {
158 if (view == null) {
159 view = LayoutInflater.from(mContext).inflate(R.layout.qs_detail_item, this, false);
160 mItems.addView(view);
161 }
162 view.setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
163 final ImageView iv = (ImageView) view.findViewById(android.R.id.icon);
164 iv.setImageResource(item.icon);
Jason Monkb27ec6d32014-12-01 17:50:16 -0500165 iv.getOverlay().clear();
166 if (item.overlay != null) {
167 item.overlay.setBounds(0, 0, item.overlay.getIntrinsicWidth(),
168 item.overlay.getIntrinsicHeight());
169 iv.getOverlay().add(item.overlay);
170 }
John Spurlock486b78e2014-07-07 08:37:56 -0400171 final TextView title = (TextView) view.findViewById(android.R.id.title);
172 title.setText(item.line1);
173 final TextView summary = (TextView) view.findViewById(android.R.id.summary);
174 final boolean twoLines = !TextUtils.isEmpty(item.line2);
175 summary.setVisibility(twoLines ? VISIBLE : GONE);
176 summary.setText(twoLines ? item.line2 : null);
177 view.setMinimumHeight(mContext.getResources() .getDimensionPixelSize(
178 twoLines ? R.dimen.qs_detail_item_height_twoline : R.dimen.qs_detail_item_height));
179 view.setOnClickListener(new OnClickListener() {
180 @Override
181 public void onClick(View v) {
182 if (mCallback != null) {
183 mCallback.onDetailItemClick(item);
184 }
185 }
186 });
187 final ImageView disconnect = (ImageView) view.findViewById(android.R.id.icon2);
188 disconnect.setVisibility(item.canDisconnect ? VISIBLE : GONE);
189 disconnect.setOnClickListener(new OnClickListener() {
190 @Override
191 public void onClick(View v) {
192 if (mCallback != null) {
193 mCallback.onDetailItemDisconnect(item);
194 }
195 }
196 });
197 }
198
199 private class H extends Handler {
200 private static final int SET_ITEMS = 1;
201 private static final int SET_CALLBACK = 2;
202 private static final int SET_ITEMS_VISIBLE = 3;
203
204 public H() {
205 super(Looper.getMainLooper());
206 }
207
208 @Override
209 public void handleMessage(Message msg) {
210 if (msg.what == SET_ITEMS) {
211 handleSetItems((Item[]) msg.obj);
212 } else if (msg.what == SET_CALLBACK) {
213 handleSetCallback((QSDetailItems.Callback) msg.obj);
214 } else if (msg.what == SET_ITEMS_VISIBLE) {
215 handleSetItemsVisible(msg.arg1 != 0);
216 }
217 }
218 }
219
220 public static class Item {
221 public int icon;
Jason Monkb27ec6d32014-12-01 17:50:16 -0500222 public Drawable overlay;
John Spurlock486b78e2014-07-07 08:37:56 -0400223 public String line1;
224 public String line2;
225 public Object tag;
226 public boolean canDisconnect;
227 }
228
229 public interface Callback {
230 void onDetailItemClick(Item item);
231 void onDetailItemDisconnect(Item item);
232 }
233}