QSDetailItems.java revision 486b78e42652466f6241eb87d5bed60040db7a25
1486b78e42652466f6241eb87d5bed60040db7a25John Spurlock/*
2486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * Copyright (C) 2014 The Android Open Source Project
3486b78e42652466f6241eb87d5bed60040db7a25John Spurlock *
4486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
5486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * you may not use this file except in compliance with the License.
6486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * You may obtain a copy of the License at
7486b78e42652466f6241eb87d5bed60040db7a25John Spurlock *
8486b78e42652466f6241eb87d5bed60040db7a25John Spurlock *      http://www.apache.org/licenses/LICENSE-2.0
9486b78e42652466f6241eb87d5bed60040db7a25John Spurlock *
10486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * Unless required by applicable law or agreed to in writing, software
11486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
12486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * See the License for the specific language governing permissions and
14486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * limitations under the License.
15486b78e42652466f6241eb87d5bed60040db7a25John Spurlock */
16486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
17486b78e42652466f6241eb87d5bed60040db7a25John Spurlockpackage com.android.systemui.qs;
18486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
19486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.content.Context;
20486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.os.Handler;
21486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.os.Looper;
22486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.os.Message;
23486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.text.TextUtils;
24486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.util.AttributeSet;
25486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.util.Log;
26486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.view.LayoutInflater;
27486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.view.View;
28486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.view.ViewGroup;
29486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.widget.FrameLayout;
30486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.widget.ImageView;
31486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.widget.LinearLayout;
32486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport android.widget.TextView;
33486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
34486b78e42652466f6241eb87d5bed60040db7a25John Spurlockimport com.android.systemui.R;
35486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
36486b78e42652466f6241eb87d5bed60040db7a25John Spurlock/**
37486b78e42652466f6241eb87d5bed60040db7a25John Spurlock * Quick settings common detail view with line items.
38486b78e42652466f6241eb87d5bed60040db7a25John Spurlock */
39486b78e42652466f6241eb87d5bed60040db7a25John Spurlockpublic class QSDetailItems extends FrameLayout {
40486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private static final String TAG = "QSDetailItems";
41486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
42486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
43486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private final Context mContext;
44486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private final H mHandler = new H();
45486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
46486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private String mTag;
47486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private Callback mCallback;
48486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private boolean mItemsVisible = true;
49486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private LinearLayout mItems;
50486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private View mEmpty;
51486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private TextView mEmptyText;
52486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private ImageView mEmptyIcon;
53486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
54486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public QSDetailItems(Context context, AttributeSet attrs) {
55486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        super(context, attrs);
56486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mContext = context;
57486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mTag = TAG;
58486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
59486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
60486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public static QSDetailItems convertOrInflate(Context context, View convert, ViewGroup parent) {
61486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        if (convert instanceof QSDetailItems) {
62486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            return (QSDetailItems) convert;
63486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        }
64486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        return (QSDetailItems) LayoutInflater.from(context).inflate(R.layout.qs_detail_items,
65486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                parent, false);
66486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
67486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
68486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    @Override
69486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    protected void onFinishInflate() {
70486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        super.onFinishInflate();
71486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mItems = (LinearLayout) findViewById(android.R.id.list);
72486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mItems.setVisibility(GONE);
73486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mEmpty = findViewById(android.R.id.empty);
74486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mEmpty.setVisibility(GONE);
75486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mEmptyText = (TextView) mEmpty.findViewById(android.R.id.title);
76486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mEmptyIcon = (ImageView) mEmpty.findViewById(android.R.id.icon);
77486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
78486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
79486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public void setTagSuffix(String suffix) {
80486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mTag = TAG + "." + suffix;
81486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
82486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
83486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public void setEmptyState(int icon, int text) {
84486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mEmptyIcon.setImageResource(icon);
85486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mEmptyText.setText(text);
86486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
87486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
88486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    @Override
89486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    protected void onAttachedToWindow() {
90486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        super.onAttachedToWindow();
91486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        if (DEBUG) Log.d(mTag, "onAttachedToWindow");
92486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
93486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
94486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    @Override
95486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    protected void onDetachedFromWindow() {
96486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        super.onDetachedFromWindow();
97486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        if (DEBUG) Log.d(mTag, "onDetachedFromWindow");
98486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mCallback = null;
99486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
100486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
101486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public void setCallback(Callback callback) {
102486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mHandler.removeMessages(H.SET_CALLBACK);
103486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mHandler.obtainMessage(H.SET_CALLBACK, callback).sendToTarget();
104486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
105486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
106486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public void setItems(Item[] items) {
107486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mHandler.removeMessages(H.SET_ITEMS);
108486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mHandler.obtainMessage(H.SET_ITEMS, items).sendToTarget();
109486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
110486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
111486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public void setItemsVisible(boolean visible) {
112486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mHandler.removeMessages(H.SET_ITEMS_VISIBLE);
113486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mHandler.obtainMessage(H.SET_ITEMS_VISIBLE, visible ? 1 : 0, 0).sendToTarget();
114486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
115486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
116486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private void handleSetCallback(Callback callback) {
117486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mCallback = callback;
118486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
119486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
120486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private void handleSetItems(Item[] items) {
121486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        final int itemCount = items != null ? items.length : 0;
122486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mEmpty.setVisibility(itemCount == 0 ? VISIBLE : GONE);
123486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mItems.setVisibility(itemCount == 0 ? GONE : VISIBLE);
124486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        for (int i = mItems.getChildCount() - 1; i >= itemCount; i--) {
125486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            mItems.removeViewAt(i);
126486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        }
127486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        for (int i = 0; i < itemCount; i++) {
128486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            bind(items[i], mItems.getChildAt(i));
129486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        }
130486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
131486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
132486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private void handleSetItemsVisible(boolean visible) {
133486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        if (mItemsVisible == visible) return;
134486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        mItemsVisible = visible;
135486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        for (int i = 0; i < mItems.getChildCount(); i++) {
136486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            mItems.getChildAt(i).setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
137486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        }
138486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
139486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
140486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private void bind(final Item item, View view) {
141486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        if (view == null) {
142486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            view = LayoutInflater.from(mContext).inflate(R.layout.qs_detail_item, this, false);
143486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            mItems.addView(view);
144486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        }
145486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        view.setVisibility(mItemsVisible ? VISIBLE : INVISIBLE);
146486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        final ImageView iv = (ImageView) view.findViewById(android.R.id.icon);
147486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        iv.setImageResource(item.icon);
148486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        final TextView title = (TextView) view.findViewById(android.R.id.title);
149486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        title.setText(item.line1);
150486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        final TextView summary = (TextView) view.findViewById(android.R.id.summary);
151486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        final boolean twoLines = !TextUtils.isEmpty(item.line2);
152486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        summary.setVisibility(twoLines ? VISIBLE : GONE);
153486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        summary.setText(twoLines ? item.line2 : null);
154486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        view.setMinimumHeight(mContext.getResources() .getDimensionPixelSize(
155486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                twoLines ? R.dimen.qs_detail_item_height_twoline : R.dimen.qs_detail_item_height));
156486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        view.setOnClickListener(new OnClickListener() {
157486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            @Override
158486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            public void onClick(View v) {
159486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                if (mCallback != null) {
160486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                    mCallback.onDetailItemClick(item);
161486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                }
162486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            }
163486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        });
164486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        final ImageView disconnect = (ImageView) view.findViewById(android.R.id.icon2);
165486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        disconnect.setVisibility(item.canDisconnect ? VISIBLE : GONE);
166486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        disconnect.setOnClickListener(new OnClickListener() {
167486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            @Override
168486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            public void onClick(View v) {
169486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                if (mCallback != null) {
170486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                    mCallback.onDetailItemDisconnect(item);
171486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                }
172486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            }
173486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        });
174486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
175486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
176486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    private class H extends Handler {
177486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        private static final int SET_ITEMS = 1;
178486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        private static final int SET_CALLBACK = 2;
179486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        private static final int SET_ITEMS_VISIBLE = 3;
180486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
181486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        public H() {
182486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            super(Looper.getMainLooper());
183486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        }
184486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
185486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        @Override
186486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        public void handleMessage(Message msg) {
187486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            if (msg.what == SET_ITEMS) {
188486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                handleSetItems((Item[]) msg.obj);
189486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            } else if (msg.what == SET_CALLBACK) {
190486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                handleSetCallback((QSDetailItems.Callback) msg.obj);
191486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            } else if (msg.what == SET_ITEMS_VISIBLE) {
192486b78e42652466f6241eb87d5bed60040db7a25John Spurlock                handleSetItemsVisible(msg.arg1 != 0);
193486b78e42652466f6241eb87d5bed60040db7a25John Spurlock            }
194486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        }
195486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
196486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
197486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public static class Item {
198486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        public int icon;
199486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        public String line1;
200486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        public String line2;
201486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        public Object tag;
202486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        public boolean canDisconnect;
203486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
204486b78e42652466f6241eb87d5bed60040db7a25John Spurlock
205486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    public interface Callback {
206486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        void onDetailItemClick(Item item);
207486b78e42652466f6241eb87d5bed60040db7a25John Spurlock        void onDetailItemDisconnect(Item item);
208486b78e42652466f6241eb87d5bed60040db7a25John Spurlock    }
209486b78e42652466f6241eb87d5bed60040db7a25John Spurlock}
210