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 | |
| 45 | private final Context mContext; |
| 46 | private final H mHandler = new H(); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 47 | private final Adapter mAdapter = new Adapter(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 48 | |
| 49 | private String mTag; |
| 50 | private Callback mCallback; |
| 51 | private boolean mItemsVisible = true; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 52 | private AutoSizingList mItemList; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 53 | private View mEmpty; |
| 54 | private TextView mEmptyText; |
| 55 | private ImageView mEmptyIcon; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 56 | |
| 57 | private Item[] mItems; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 58 | |
| 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 Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 76 | mItemList = (AutoSizingList) findViewById(android.R.id.list); |
| 77 | mItemList.setVisibility(GONE); |
| 78 | mItemList.setAdapter(mAdapter); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 79 | 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 Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 85 | @Override |
| 86 | protected void onConfigurationChanged(Configuration newConfig) { |
| 87 | super.onConfigurationChanged(newConfig); |
| 88 | FontSizeUtils.updateFontSize(mEmptyText, R.dimen.qs_detail_empty_text_size); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 89 | int count = mItemList.getChildCount(); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 90 | for (int i = 0; i < count; i++) { |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 91 | View item = mItemList.getChildAt(i); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 92 | 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 Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 99 | 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 Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 141 | final int itemCount = items != null ? items.length : 0; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 142 | mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE); |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 143 | mItemList.setVisibility(itemCount == 0 ? GONE : VISIBLE); |
| 144 | mItems = items; |
| 145 | mAdapter.notifyDataSetChanged(); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | private void handleSetItemsVisible(boolean visible) { |
| 149 | if (mItemsVisible == visible) return; |
| 150 | mItemsVisible = visible; |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 151 | for (int i = 0; i < mItemList.getChildCount(); i++) { |
| 152 | mItemList.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE); |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 156 | private class Adapter extends BaseAdapter { |
| 157 | |
| 158 | @Override |
| 159 | public int getCount() { |
| 160 | return mItems != null ? mItems.length : 0; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 161 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 162 | |
| 163 | @Override |
| 164 | public Object getItem(int position) { |
| 165 | return mItems[position]; |
Jason Monk | b27ec6d3 | 2014-12-01 17:50:16 -0500 | [diff] [blame] | 166 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 167 | |
| 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 Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 179 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 180 | 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 Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 188 | } |
Jason Monk | 6573ef2 | 2016-04-06 12:37:18 -0400 | [diff] [blame^] | 189 | 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 Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 217 | |
| 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 Monk | b27ec6d3 | 2014-12-01 17:50:16 -0500 | [diff] [blame] | 241 | public Drawable overlay; |
Jason Monk | 6980d12 | 2015-06-15 10:07:55 -0400 | [diff] [blame] | 242 | public CharSequence line1; |
| 243 | public CharSequence line2; |
John Spurlock | 486b78e | 2014-07-07 08:37:56 -0400 | [diff] [blame] | 244 | 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 | } |