John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -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; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 21 | import android.content.res.Resources; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 22 | import android.util.MathUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 23 | import android.view.Gravity; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 25 | import android.view.View; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 26 | import android.widget.ImageView; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 27 | import android.widget.TextView; |
Winson | c0d7058 | 2016-01-29 10:24:39 -0800 | [diff] [blame] | 28 | |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 29 | import com.android.systemui.FontSizeUtils; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 30 | import com.android.systemui.R; |
Jason Monk | 7435155 | 2016-02-20 09:44:46 -0500 | [diff] [blame] | 31 | import libcore.util.Objects; |
John Spurlock | 2d69581 | 2014-10-30 13:25:21 -0400 | [diff] [blame] | 32 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 33 | /** View that represents a standard quick settings tile. **/ |
Jason Monk | c133d26 | 2015-10-27 12:32:45 -0400 | [diff] [blame] | 34 | public class QSTileView extends QSTileBaseView { |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 35 | protected final Context mContext; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 36 | private QSIconView mIconView; |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 37 | private final int mTileSpacingPx; |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 38 | private int mTilePaddingTopPx; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 40 | private TextView mLabel; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 41 | private ImageView mPadLock; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 42 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 43 | public QSTileView(Context context, QSIconView icon) { |
| 44 | super(context, icon); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | |
| 46 | mContext = context; |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 47 | mIconView = icon; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 48 | final Resources res = context.getResources(); |
John Spurlock | 92d9b19 | 2014-06-29 12:54:24 -0400 | [diff] [blame] | 49 | mTileSpacingPx = res.getDimensionPixelSize(R.dimen.qs_tile_spacing); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 50 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 51 | setClipChildren(false); |
| 52 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | setClickable(true); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 54 | updateTopPadding(); |
Selim Cinek | 4949c75 | 2015-06-26 14:49:18 -0400 | [diff] [blame] | 55 | setId(View.generateViewId()); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 56 | createLabel(); |
| 57 | setOrientation(VERTICAL); |
| 58 | setGravity(Gravity.CENTER); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 59 | } |
| 60 | |
Jason Monk | 162011e | 2016-02-19 08:11:55 -0500 | [diff] [blame] | 61 | TextView getLabel() { |
| 62 | return mLabel; |
| 63 | } |
| 64 | |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 65 | private void updateTopPadding() { |
| 66 | Resources res = getResources(); |
| 67 | int padding = res.getDimensionPixelSize(R.dimen.qs_tile_padding_top); |
| 68 | int largePadding = res.getDimensionPixelSize(R.dimen.qs_tile_padding_top_large_text); |
| 69 | float largeFactor = (MathUtils.constrain(getResources().getConfiguration().fontScale, |
| 70 | 1.0f, FontSizeUtils.LARGE_TEXT_SCALE) - 1f) / (FontSizeUtils.LARGE_TEXT_SCALE - 1f); |
| 71 | mTilePaddingTopPx = Math.round((1 - largeFactor) * padding + largeFactor * largePadding); |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 72 | setPadding(mTileSpacingPx, mTilePaddingTopPx + mTileSpacingPx, mTileSpacingPx, |
| 73 | mTileSpacingPx); |
Jorim Jaggi | e17c4b4 | 2014-08-26 17:27:31 +0200 | [diff] [blame] | 74 | requestLayout(); |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | protected void onConfigurationChanged(Configuration newConfig) { |
| 79 | super.onConfigurationChanged(newConfig); |
| 80 | updateTopPadding(); |
| 81 | FontSizeUtils.updateFontSize(mLabel, R.dimen.qs_tile_text_size); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 84 | private void createLabel() { |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 85 | final Resources res = mContext.getResources(); |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 86 | View view = LayoutInflater.from(mContext).inflate(R.layout.qs_tile_label, null); |
| 87 | mLabel = (TextView) view.findViewById(R.id.tile_label); |
| 88 | mPadLock = (ImageView) view.findViewById(R.id.restricted_padlock); |
| 89 | addView(view); |
John Spurlock | d47a3f3 | 2014-05-18 19:14:14 -0400 | [diff] [blame] | 90 | } |
| 91 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 92 | protected void handleStateChanged(QSTile.State state) { |
Jason Monk | dc35dcb | 2015-12-04 16:36:15 -0500 | [diff] [blame] | 93 | super.handleStateChanged(state); |
Jason Monk | 7435155 | 2016-02-20 09:44:46 -0500 | [diff] [blame] | 94 | if (!Objects.equal(mLabel.getText(), state.label)) { |
| 95 | mLabel.setText(state.label); |
| 96 | } |
Sudheer Shanka | 1c7cda8 | 2015-12-31 14:46:02 +0000 | [diff] [blame] | 97 | mLabel.setEnabled(!state.disabledByPolicy); |
| 98 | mPadLock.setVisibility(state.disabledByPolicy ? View.VISIBLE : View.GONE); |
Selim Cinek | 4949c75 | 2015-06-26 14:49:18 -0400 | [diff] [blame] | 99 | } |
Selim Cinek | ece4a83 | 2014-08-22 15:35:53 +0000 | [diff] [blame] | 100 | } |