Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 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 |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 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. |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 15 | */ |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 16 | package com.android.systemui.qs; |
| 17 | |
| 18 | import android.content.Context; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 19 | import android.content.res.TypedArray; |
| 20 | import android.graphics.drawable.Drawable; |
| 21 | import android.graphics.drawable.RippleDrawable; |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 22 | import android.os.Handler; |
| 23 | import android.os.Looper; |
| 24 | import android.os.Message; |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 25 | import android.view.View; |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 26 | import android.widget.LinearLayout; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 27 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 28 | import com.android.systemui.R; |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 29 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 30 | public class QSTileBaseView extends LinearLayout { |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 31 | |
| 32 | private final H mHandler = new H(); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 33 | private QSIconView mIcon; |
| 34 | private RippleDrawable mRipple; |
| 35 | private Drawable mTileBackground; |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 36 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 37 | public QSTileBaseView(Context context, QSIconView icon) { |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 38 | super(context); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 39 | 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 Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 52 | setClipChildren(false); |
| 53 | setClipToPadding(false); |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 54 | } |
| 55 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 56 | 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 Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 64 | 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 Monk | 76c67aa | 2016-02-19 14:49:42 -0500 | [diff] [blame] | 82 | setOnLongClickListener(longClick); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 83 | } |
| 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 Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 106 | |
| 107 | public void onStateChanged(QSTile.State state) { |
| 108 | mHandler.obtainMessage(H.STATE_CHANGED, state).sendToTarget(); |
| 109 | } |
| 110 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 111 | protected void handleStateChanged(QSTile.State state) { |
| 112 | mIcon.setIcon(state); |
| 113 | setContentDescription(state.contentDescription); |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 116 | View getIcon() { |
| 117 | return mIcon; |
| 118 | } |
| 119 | |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 120 | 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 | } |