John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.qs; |
| 18 | |
| 19 | import android.content.Context; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 20 | import android.content.res.Configuration; |
Jason Monk | b27ec6d3 | 2014-12-01 17:50:16 -0500 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Looper; |
| 24 | import android.os.Message; |
| 25 | import android.text.TextUtils; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.util.Log; |
| 28 | import android.view.LayoutInflater; |
| 29 | import android.view.View; |
| 30 | import android.view.ViewGroup; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 31 | import android.widget.BaseAdapter; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 32 | import android.widget.FrameLayout; |
| 33 | import android.widget.ImageView; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 34 | import android.widget.TextView; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 35 | import com.android.systemui.FontSizeUtils; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 36 | import com.android.systemui.R; |
| 37 | |
| 38 | /** |
| 39 | * Quick settings common detail view with line items. |
| 40 | */ |
| 41 | public class QSDetailItems extends FrameLayout { |
| 42 | private static final String TAG = "QSDetailItems"; |
| 43 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 44 | |
Muyuan Li | 72a6387 | 2016-05-14 11:35:41 -0700 | [diff] [blame] | 45 | private final int mQsDetailIconOverlaySize; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 46 | private final Context mContext; |
| 47 | private final H mHandler = new H(); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 48 | private final Adapter mAdapter = new Adapter(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 49 | |
| 50 | private String mTag; |
| 51 | private Callback mCallback; |
| 52 | private boolean mItemsVisible = true; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 53 | private AutoSizingList mItemList; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 54 | private View mEmpty; |
| 55 | private TextView mEmptyText; |
| 56 | private ImageView mEmptyIcon; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 57 | |
| 58 | private Item[] mItems; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 59 | |
| 60 | public QSDetailItems(Context context, AttributeSet attrs) { |
| 61 | super(context, attrs); |
| 62 | mContext = context; |
| 63 | mTag = TAG; |
Muyuan Li | 72a6387 | 2016-05-14 11:35:41 -0700 | [diff] [blame] | 64 | mQsDetailIconOverlaySize = (int) getResources().getDimension( |
| 65 | R.dimen.qs_detail_icon_overlay_size); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 66 | } |
| 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 Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 79 | mItemList = (AutoSizingList) findViewById(android.R.id.list); |
| 80 | mItemList.setVisibility(GONE); |
| 81 | mItemList.setAdapter(mAdapter); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 82 | mEmpty = findViewById(android.R.id.empty); |
| 83 | mEmpty.setVisibility(GONE); |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame^] | 84 | mEmptyText = mEmpty.findViewById(android.R.id.title); |
| 85 | mEmptyIcon = mEmpty.findViewById(android.R.id.icon); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 88 | @Override |
| 89 | protected void onConfigurationChanged(Configuration newConfig) { |
| 90 | super.onConfigurationChanged(newConfig); |
| 91 | FontSizeUtils.updateFontSize(mEmptyText, R.dimen.qs_detail_empty_text_size); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 92 | int count = mItemList.getChildCount(); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 93 | for (int i = 0; i < count; i++) { |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 94 | View item = mItemList.getChildAt(i); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 95 | 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 Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 102 | public void setTagSuffix(String suffix) { |
| 103 | mTag = TAG + "." + suffix; |
| 104 | } |
| 105 | |
| 106 | public void setEmptyState(int icon, int text) { |
Jason Monk | be3235a | 2017-04-05 09:29:53 -0400 | [diff] [blame^] | 107 | mEmptyIcon.post(() -> { |
| 108 | mEmptyIcon.setImageResource(icon); |
| 109 | mEmptyText.setText(text); |
| 110 | }); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 111 | } |
| 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 Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 146 | final int itemCount = items != null ? items.length : 0; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 147 | mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 148 | mItemList.setVisibility(itemCount == 0 ? GONE : VISIBLE); |
| 149 | mItems = items; |
| 150 | mAdapter.notifyDataSetChanged(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private void handleSetItemsVisible(boolean visible) { |
| 154 | if (mItemsVisible == visible) return; |
| 155 | mItemsVisible = visible; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 156 | for (int i = 0; i < mItemList.getChildCount(); i++) { |
| 157 | mItemList.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 161 | private class Adapter extends BaseAdapter { |
| 162 | |
| 163 | @Override |
| 164 | public int getCount() { |
| 165 | return mItems != null ? mItems.length : 0; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 166 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 167 | |
| 168 | @Override |
| 169 | public Object getItem(int position) { |
| 170 | return mItems[position]; |
Jason Monk | b27ec6d3 | 2014-12-01 17:50:16 -0500 | [diff] [blame] | 171 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 172 | |
| 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 Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 184 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 185 | view.setVisibility(mItemsVisible ? VISIBLE : INVISIBLE); |
| 186 | final ImageView iv = (ImageView) view.findViewById(android.R.id.icon); |
| 187 | iv.setImageResource(item.icon); |
| 188 | iv.getOverlay().clear(); |
| 189 | if (item.overlay != null) { |
Muyuan Li | 72a6387 | 2016-05-14 11:35:41 -0700 | [diff] [blame] | 190 | item.overlay.setBounds(0, 0, mQsDetailIconOverlaySize, mQsDetailIconOverlaySize); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 191 | iv.getOverlay().add(item.overlay); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 192 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 193 | final TextView title = (TextView) view.findViewById(android.R.id.title); |
| 194 | title.setText(item.line1); |
| 195 | final TextView summary = (TextView) view.findViewById(android.R.id.summary); |
| 196 | final boolean twoLines = !TextUtils.isEmpty(item.line2); |
| 197 | title.setMaxLines(twoLines ? 1 : 2); |
| 198 | summary.setVisibility(twoLines ? VISIBLE : GONE); |
| 199 | summary.setText(twoLines ? item.line2 : null); |
| 200 | view.setOnClickListener(new OnClickListener() { |
| 201 | @Override |
| 202 | public void onClick(View v) { |
| 203 | if (mCallback != null) { |
| 204 | mCallback.onDetailItemClick(item); |
| 205 | } |
| 206 | } |
| 207 | }); |
Sundeep Ghuman | ff0f108 | 2017-02-10 11:49:36 -0800 | [diff] [blame] | 208 | |
| 209 | final ImageView icon2 = (ImageView) view.findViewById(android.R.id.icon2); |
| 210 | if (item.canDisconnect) { |
| 211 | icon2.setImageResource(R.drawable.ic_qs_cancel); |
| 212 | icon2.setVisibility(VISIBLE); |
| 213 | icon2.setClickable(true); |
| 214 | icon2.setOnClickListener(new OnClickListener() { |
| 215 | @Override |
| 216 | public void onClick(View v) { |
| 217 | if (mCallback != null) { |
| 218 | mCallback.onDetailItemDisconnect(item); |
| 219 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 220 | } |
Sundeep Ghuman | ff0f108 | 2017-02-10 11:49:36 -0800 | [diff] [blame] | 221 | }); |
| 222 | } else if (item.icon2 != -1) { |
| 223 | icon2.setVisibility(VISIBLE); |
| 224 | icon2.setImageResource(item.icon2); |
| 225 | icon2.setClickable(false); |
| 226 | } else { |
| 227 | icon2.setVisibility(GONE); |
| 228 | } |
| 229 | |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 230 | return view; |
| 231 | } |
| 232 | }; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 233 | |
| 234 | private class H extends Handler { |
| 235 | private static final int SET_ITEMS = 1; |
| 236 | private static final int SET_CALLBACK = 2; |
| 237 | private static final int SET_ITEMS_VISIBLE = 3; |
| 238 | |
| 239 | public H() { |
| 240 | super(Looper.getMainLooper()); |
| 241 | } |
| 242 | |
| 243 | @Override |
| 244 | public void handleMessage(Message msg) { |
| 245 | if (msg.what == SET_ITEMS) { |
| 246 | handleSetItems((Item[]) msg.obj); |
| 247 | } else if (msg.what == SET_CALLBACK) { |
| 248 | handleSetCallback((QSDetailItems.Callback) msg.obj); |
| 249 | } else if (msg.what == SET_ITEMS_VISIBLE) { |
| 250 | handleSetItemsVisible(msg.arg1 != 0); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | public static class Item { |
| 256 | public int icon; |
Jason Monk | b27ec6d3 | 2014-12-01 17:50:16 -0500 | [diff] [blame] | 257 | public Drawable overlay; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 258 | public CharSequence line1; |
| 259 | public CharSequence line2; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 260 | public Object tag; |
| 261 | public boolean canDisconnect; |
Sundeep Ghuman | ff0f108 | 2017-02-10 11:49:36 -0800 | [diff] [blame] | 262 | public int icon2 = -1; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | public interface Callback { |
| 266 | void onDetailItemClick(Item item); |
| 267 | void onDetailItemDisconnect(Item item); |
| 268 | } |
| 269 | } |