1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.systemui.qs.tileimpl; 16 17 import android.content.Context; 18 import android.content.res.Configuration; 19 import android.service.quicksettings.Tile; 20 import android.text.SpannableStringBuilder; 21 import android.text.style.ForegroundColorSpan; 22 import android.view.Gravity; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 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 com.android.systemui.plugins.qs.QSIconView; 32 import com.android.systemui.plugins.qs.QSTile; 33 34 import libcore.util.Objects; 35 36 /** View that represents a standard quick settings tile. **/ 37 public class QSTileView extends QSTileBaseView { 38 39 private View mDivider; 40 protected TextView mLabel; 41 private ImageView mPadLock; 42 private int mState; 43 private ViewGroup mLabelContainer; 44 private View mExpandIndicator; 45 private View mExpandSpace; 46 47 public QSTileView(Context context, QSIconView icon) { 48 this(context, icon, false); 49 } 50 51 public QSTileView(Context context, QSIconView icon, boolean collapsedView) { 52 super(context, icon, collapsedView); 53 54 setClipChildren(false); 55 setClipToPadding(false); 56 57 setClickable(true); 58 setId(View.generateViewId()); 59 createLabel(); 60 setOrientation(VERTICAL); 61 setGravity(Gravity.CENTER); 62 } 63 64 TextView getLabel() { 65 return mLabel; 66 } 67 68 @Override 69 protected void onConfigurationChanged(Configuration newConfig) { 70 super.onConfigurationChanged(newConfig); 71 FontSizeUtils.updateFontSize(mLabel, R.dimen.qs_tile_text_size); 72 } 73 74 @Override 75 public int getDetailY() { 76 return getTop() + mLabelContainer.getTop() + mLabelContainer.getHeight() / 2; 77 } 78 79 protected void createLabel() { 80 mLabelContainer = (ViewGroup) LayoutInflater.from(getContext()) 81 .inflate(R.layout.qs_tile_label, this, false); 82 mLabelContainer.setClipChildren(false); 83 mLabelContainer.setClipToPadding(false); 84 mLabel = mLabelContainer.findViewById(R.id.tile_label); 85 mPadLock = mLabelContainer.findViewById(R.id.restricted_padlock); 86 mDivider = mLabelContainer.findViewById(R.id.underline); 87 mExpandIndicator = mLabelContainer.findViewById(R.id.expand_indicator); 88 mExpandSpace = mLabelContainer.findViewById(R.id.expand_space); 89 90 addView(mLabelContainer); 91 } 92 93 @Override 94 protected void handleStateChanged(QSTile.State state) { 95 super.handleStateChanged(state); 96 if (!Objects.equal(mLabel.getText(), state.label) || mState != state.state) { 97 if (state.state == Tile.STATE_UNAVAILABLE) { 98 int color = QSTileImpl.getColorForState(getContext(), state.state); 99 state.label = new SpannableStringBuilder().append(state.label, 100 new ForegroundColorSpan(color), 101 SpannableStringBuilder.SPAN_INCLUSIVE_INCLUSIVE); 102 } 103 mState = state.state; 104 mLabel.setText(state.label); 105 } 106 mExpandIndicator.setVisibility(state.dualTarget ? View.VISIBLE : View.GONE); 107 mExpandSpace.setVisibility(state.dualTarget ? View.VISIBLE : View.GONE); 108 mLabelContainer.setContentDescription(state.dualTarget ? state.dualLabelContentDescription 109 : null); 110 if (state.dualTarget != mLabelContainer.isClickable()) { 111 mLabelContainer.setClickable(state.dualTarget); 112 mLabelContainer.setLongClickable(state.dualTarget); 113 mLabelContainer.setBackground(state.dualTarget ? newTileBackground() : null); 114 } 115 mLabel.setEnabled(!state.disabledByPolicy); 116 mPadLock.setVisibility(state.disabledByPolicy ? View.VISIBLE : View.GONE); 117 } 118 119 @Override 120 public void init(OnClickListener click, OnClickListener secondaryClick, 121 OnLongClickListener longClick) { 122 super.init(click, secondaryClick, longClick); 123 mLabelContainer.setOnClickListener(secondaryClick); 124 mLabelContainer.setOnLongClickListener(longClick); 125 mLabelContainer.setClickable(false); 126 mLabelContainer.setLongClickable(false); 127 } 128 } 129