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