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); |
| 84 | mEmptyText = (TextView) mEmpty.findViewById(android.R.id.title); |
| 85 | mEmptyIcon = (ImageView) mEmpty.findViewById(android.R.id.icon); |
| 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) { |
| 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 Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 144 | final int itemCount = items != null ? items.length : 0; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 145 | mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 146 | mItemList.setVisibility(itemCount == 0 ? GONE : VISIBLE); |
| 147 | mItems = items; |
| 148 | mAdapter.notifyDataSetChanged(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | private void handleSetItemsVisible(boolean visible) { |
| 152 | if (mItemsVisible == visible) return; |
| 153 | mItemsVisible = visible; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 154 | for (int i = 0; i < mItemList.getChildCount(); i++) { |
| 155 | mItemList.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 156 | } |
| 157 | } |
| 158 | |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 159 | private class Adapter extends BaseAdapter { |
| 160 | |
| 161 | @Override |
| 162 | public int getCount() { |
| 163 | return mItems != null ? mItems.length : 0; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 164 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 165 | |
| 166 | @Override |
| 167 | public Object getItem(int position) { |
| 168 | return mItems[position]; |
Jason Monk | b27ec6d3 | 2014-12-01 17:50:16 -0500 | [diff] [blame] | 169 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 170 | |
| 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 Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 182 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 183 | 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 Li | 72a6387 | 2016-05-14 11:35:41 -0700 | [diff] [blame] | 188 | item.overlay.setBounds(0, 0, mQsDetailIconOverlaySize, mQsDetailIconOverlaySize); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 189 | iv.getOverlay().add(item.overlay); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 190 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 191 | 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 Ghuman | ff0f108 | 2017-02-10 11:49:36 -0800 | [diff] [blame^] | 206 | |
| 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 Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 218 | } |
Sundeep Ghuman | ff0f108 | 2017-02-10 11:49:36 -0800 | [diff] [blame^] | 219 | }); |
| 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 Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame] | 228 | return view; |
| 229 | } |
| 230 | }; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 231 | |
| 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 Monk | b27ec6d3 | 2014-12-01 17:50:16 -0500 | [diff] [blame] | 255 | public Drawable overlay; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 256 | public CharSequence line1; |
| 257 | public CharSequence line2; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 258 | public Object tag; |
| 259 | public boolean canDisconnect; |
Sundeep Ghuman | ff0f108 | 2017-02-10 11:49:36 -0800 | [diff] [blame^] | 260 | public int icon2 = -1; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | public interface Callback { |
| 264 | void onDetailItemClick(Item item); |
| 265 | void onDetailItemDisconnect(Item item); |
| 266 | } |
| 267 | } |