blob: 98a1c23aa6453c0f95e7f3a96ce52f836f9d731d [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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
17package com.android.systemui.qs;
18
19import android.content.Context;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020020import android.content.res.Configuration;
John Spurlockaf8d6c42014-05-07 17:49:08 -040021import android.content.res.Resources;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020022import android.util.MathUtils;
John Spurlockaf8d6c42014-05-07 17:49:08 -040023import android.view.Gravity;
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000024import android.view.LayoutInflater;
John Spurlockaf8d6c42014-05-07 17:49:08 -040025import android.view.View;
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000026import android.widget.ImageView;
John Spurlockaf8d6c42014-05-07 17:49:08 -040027import android.widget.TextView;
Winsonc0d70582016-01-29 10:24:39 -080028
Jorim Jaggie17c4b42014-08-26 17:27:31 +020029import com.android.systemui.FontSizeUtils;
John Spurlockaf8d6c42014-05-07 17:49:08 -040030import com.android.systemui.R;
Jason Monk74351552016-02-20 09:44:46 -050031import libcore.util.Objects;
John Spurlock2d695812014-10-30 13:25:21 -040032
John Spurlockaf8d6c42014-05-07 17:49:08 -040033/** View that represents a standard quick settings tile. **/
Jason Monkc133d262015-10-27 12:32:45 -040034public class QSTileView extends QSTileBaseView {
John Spurlockaf8d6c42014-05-07 17:49:08 -040035 protected final Context mContext;
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000036 private QSIconView mIconView;
John Spurlock92d9b192014-06-29 12:54:24 -040037 private final int mTileSpacingPx;
Jorim Jaggie17c4b42014-08-26 17:27:31 +020038 private int mTilePaddingTopPx;
John Spurlockaf8d6c42014-05-07 17:49:08 -040039
John Spurlockd47a3f32014-05-18 19:14:14 -040040 private TextView mLabel;
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000041 private ImageView mPadLock;
John Spurlockaf8d6c42014-05-07 17:49:08 -040042
Jason Monkdc35dcb2015-12-04 16:36:15 -050043 public QSTileView(Context context, QSIconView icon) {
44 super(context, icon);
John Spurlockaf8d6c42014-05-07 17:49:08 -040045
46 mContext = context;
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000047 mIconView = icon;
John Spurlockaf8d6c42014-05-07 17:49:08 -040048 final Resources res = context.getResources();
John Spurlock92d9b192014-06-29 12:54:24 -040049 mTileSpacingPx = res.getDimensionPixelSize(R.dimen.qs_tile_spacing);
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000050
John Spurlockaf8d6c42014-05-07 17:49:08 -040051 setClipChildren(false);
52
John Spurlockaf8d6c42014-05-07 17:49:08 -040053 setClickable(true);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020054 updateTopPadding();
Selim Cinek4949c752015-06-26 14:49:18 -040055 setId(View.generateViewId());
Jason Monkdc35dcb2015-12-04 16:36:15 -050056 createLabel();
57 setOrientation(VERTICAL);
58 setGravity(Gravity.CENTER);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020059 }
60
Jason Monk162011e2016-02-19 08:11:55 -050061 TextView getLabel() {
62 return mLabel;
63 }
64
Jorim Jaggie17c4b42014-08-26 17:27:31 +020065 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 Monkdc35dcb2015-12-04 16:36:15 -050072 setPadding(mTileSpacingPx, mTilePaddingTopPx + mTileSpacingPx, mTileSpacingPx,
73 mTileSpacingPx);
Jorim Jaggie17c4b42014-08-26 17:27:31 +020074 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 Spurlockaf8d6c42014-05-07 17:49:08 -040082 }
83
Jason Monkdc35dcb2015-12-04 16:36:15 -050084 private void createLabel() {
John Spurlockd47a3f32014-05-18 19:14:14 -040085 final Resources res = mContext.getResources();
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000086 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 Spurlockd47a3f32014-05-18 19:14:14 -040090 }
91
John Spurlockaf8d6c42014-05-07 17:49:08 -040092 protected void handleStateChanged(QSTile.State state) {
Jason Monkdc35dcb2015-12-04 16:36:15 -050093 super.handleStateChanged(state);
Jason Monk74351552016-02-20 09:44:46 -050094 if (!Objects.equal(mLabel.getText(), state.label)) {
95 mLabel.setText(state.label);
96 }
Sudheer Shanka1c7cda82015-12-31 14:46:02 +000097 mLabel.setEnabled(!state.disabledByPolicy);
98 mPadLock.setVisibility(state.disabledByPolicy ? View.VISIBLE : View.GONE);
Selim Cinek4949c752015-06-26 14:49:18 -040099 }
Selim Cinekece4a832014-08-22 15:35:53 +0000100}