• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
20 import android.content.res.Configuration;
21 import android.content.res.Resources;
22 import android.util.MathUtils;
23 import android.view.Gravity;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import com.android.systemui.FontSizeUtils;
30 import com.android.systemui.R;
31 import libcore.util.Objects;
32 
33 /** View that represents a standard quick settings tile. **/
34 public class QSTileView extends QSTileBaseView {
35     protected final Context mContext;
36     private final int mTileSpacingPx;
37     private int mTilePaddingTopPx;
38 
39     protected TextView mLabel;
40     private ImageView mPadLock;
41 
QSTileView(Context context, QSIconView icon)42     public QSTileView(Context context, QSIconView icon) {
43         this(context, icon, false);
44     }
45 
QSTileView(Context context, QSIconView icon, boolean collapsedView)46     public QSTileView(Context context, QSIconView icon, boolean collapsedView) {
47         super(context, icon, collapsedView);
48 
49         mContext = context;
50         final Resources res = context.getResources();
51         mTileSpacingPx = res.getDimensionPixelSize(R.dimen.qs_tile_spacing);
52 
53         setClipChildren(false);
54 
55         setClickable(true);
56         updateTopPadding();
57         setId(View.generateViewId());
58         createLabel();
59         setOrientation(VERTICAL);
60         setGravity(Gravity.CENTER);
61     }
62 
getLabel()63     TextView getLabel() {
64         return mLabel;
65     }
66 
updateTopPadding()67     private void updateTopPadding() {
68         Resources res = getResources();
69         int padding = res.getDimensionPixelSize(R.dimen.qs_tile_padding_top);
70         int largePadding = res.getDimensionPixelSize(R.dimen.qs_tile_padding_top_large_text);
71         float largeFactor = (MathUtils.constrain(getResources().getConfiguration().fontScale,
72                 1.0f, FontSizeUtils.LARGE_TEXT_SCALE) - 1f) / (FontSizeUtils.LARGE_TEXT_SCALE - 1f);
73         mTilePaddingTopPx = Math.round((1 - largeFactor) * padding + largeFactor * largePadding);
74         setPadding(mTileSpacingPx, mTilePaddingTopPx + mTileSpacingPx, mTileSpacingPx,
75                 mTileSpacingPx);
76         requestLayout();
77     }
78 
79     @Override
onConfigurationChanged(Configuration newConfig)80     protected void onConfigurationChanged(Configuration newConfig) {
81         super.onConfigurationChanged(newConfig);
82         updateTopPadding();
83         FontSizeUtils.updateFontSize(mLabel, R.dimen.qs_tile_text_size);
84     }
85 
createLabel()86     protected void createLabel() {
87         View view = LayoutInflater.from(mContext).inflate(R.layout.qs_tile_label, null);
88         mLabel = (TextView) view.findViewById(R.id.tile_label);
89         mPadLock = (ImageView) view.findViewById(R.id.restricted_padlock);
90         addView(view);
91     }
92 
93     @Override
handleStateChanged(QSTile.State state)94     protected void handleStateChanged(QSTile.State state) {
95         super.handleStateChanged(state);
96         if (!Objects.equal(mLabel.getText(), state.label)) {
97             mLabel.setText(state.label);
98         }
99         mLabel.setEnabled(!state.disabledByPolicy);
100         mPadLock.setVisibility(state.disabledByPolicy ? View.VISIBLE : View.GONE);
101     }
102 }
103