blob: f35aacf8f72d5738e35723302e841a75c7f3ea46 [file] [log] [blame]
Jason Monkc133d262015-10-27 12:32:45 -04001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
Jason Monkdc35dcb2015-12-04 16:36:15 -05004 * 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
Jason Monkc133d262015-10-27 12:32:45 -04007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Jason Monkdc35dcb2015-12-04 16:36:15 -050010 * 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.
Jason Monkc133d262015-10-27 12:32:45 -040015 */
Jason Monkc133d262015-10-27 12:32:45 -040016package com.android.systemui.qs;
17
18import android.content.Context;
Jason Monkdc35dcb2015-12-04 16:36:15 -050019import android.content.res.TypedArray;
20import android.graphics.drawable.Drawable;
21import android.graphics.drawable.RippleDrawable;
Jason Monkc133d262015-10-27 12:32:45 -040022import android.os.Handler;
23import android.os.Looper;
24import android.os.Message;
Jason Monkc133d262015-10-27 12:32:45 -040025import android.view.View;
Jason Monkdc35dcb2015-12-04 16:36:15 -050026import android.widget.LinearLayout;
Winsonc0d70582016-01-29 10:24:39 -080027
Jason Monkdc35dcb2015-12-04 16:36:15 -050028import com.android.systemui.R;
Jason Monkc133d262015-10-27 12:32:45 -040029
Jason Monkdc35dcb2015-12-04 16:36:15 -050030public class QSTileBaseView extends LinearLayout {
Jason Monkc133d262015-10-27 12:32:45 -040031
32 private final H mHandler = new H();
Jason Monkdc35dcb2015-12-04 16:36:15 -050033 private QSIconView mIcon;
34 private RippleDrawable mRipple;
35 private Drawable mTileBackground;
Jason Monkc133d262015-10-27 12:32:45 -040036
Jason Monkdc35dcb2015-12-04 16:36:15 -050037 public QSTileBaseView(Context context, QSIconView icon) {
Jason Monkc133d262015-10-27 12:32:45 -040038 super(context);
Jason Monkdc35dcb2015-12-04 16:36:15 -050039 mIcon = icon;
40 addView(mIcon);
41
42 mTileBackground = newTileBackground();
43 if (mTileBackground instanceof RippleDrawable) {
44 setRipple((RippleDrawable) mTileBackground);
45 }
46 setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES);
47 setBackground(mTileBackground);
48
49 // Default to Quick Tile padding, and QSTileView will specify its own padding.
50 int padding = context.getResources().getDimensionPixelSize(R.dimen.qs_quick_tile_padding);
51 setPadding(padding, padding, padding, padding);
Jason Monk162011e2016-02-19 08:11:55 -050052 setClipChildren(false);
53 setClipToPadding(false);
Jason Monkc133d262015-10-27 12:32:45 -040054 }
55
Jason Monkdc35dcb2015-12-04 16:36:15 -050056 private Drawable newTileBackground() {
57 final int[] attrs = new int[] { android.R.attr.selectableItemBackgroundBorderless };
58 final TypedArray ta = mContext.obtainStyledAttributes(attrs);
59 final Drawable d = ta.getDrawable(0);
60 ta.recycle();
61 return d;
Jason Monkc133d262015-10-27 12:32:45 -040062 }
63
Jason Monkdc35dcb2015-12-04 16:36:15 -050064 private void setRipple(RippleDrawable tileBackground) {
65 mRipple = tileBackground;
66 if (getWidth() != 0) {
67 updateRippleSize(getWidth(), getHeight());
68 }
69 }
70
71 private void updateRippleSize(int width, int height) {
72 // center the touch feedback on the center of the icon, and dial it down a bit
73 final int cx = width / 2;
74 final int cy = height / 2;
75 final int rad = (int)(mIcon.getHeight() * .85f);
76 mRipple.setHotspotBounds(cx - rad, cy - rad, cx + rad, cy + rad);
77 }
78
79 public void init(OnClickListener click, OnLongClickListener longClick) {
80 setClickable(true);
81 setOnClickListener(click);
Jason Monk76c67aa2016-02-19 14:49:42 -050082 setOnLongClickListener(longClick);
Jason Monkdc35dcb2015-12-04 16:36:15 -050083 }
84
85 @Override
86 protected void onLayout(boolean changed, int l, int t, int r, int b) {
87 super.onLayout(changed, l, t, r, b);
88 final int w = getMeasuredWidth();
89 final int h = getMeasuredHeight();
90
91 if (mRipple != null) {
92 updateRippleSize(w, h);
93 }
94 }
95
96 /**
97 * Update the accessibility order for this view.
98 *
99 * @param previousView the view which should be before this one
100 * @return the last view in this view which is accessible
101 */
102 public View updateAccessibilityOrder(View previousView) {
103 setAccessibilityTraversalAfter(previousView.getId());
104 return this;
105 }
Jason Monkc133d262015-10-27 12:32:45 -0400106
107 public void onStateChanged(QSTile.State state) {
108 mHandler.obtainMessage(H.STATE_CHANGED, state).sendToTarget();
109 }
110
Jason Monkdc35dcb2015-12-04 16:36:15 -0500111 protected void handleStateChanged(QSTile.State state) {
112 mIcon.setIcon(state);
113 setContentDescription(state.contentDescription);
Jason Monkc133d262015-10-27 12:32:45 -0400114 }
115
Jason Monk162011e2016-02-19 08:11:55 -0500116 View getIcon() {
117 return mIcon;
118 }
119
Jason Monkc133d262015-10-27 12:32:45 -0400120 private class H extends Handler {
121 private static final int STATE_CHANGED = 1;
122 public H() {
123 super(Looper.getMainLooper());
124 }
125
126 @Override
127 public void handleMessage(Message msg) {
128 if (msg.what == STATE_CHANGED) {
129 handleStateChanged((QSTile.State) msg.obj);
130 }
131 }
132 }
133}