Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.keyguard; |
| 18 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 19 | import android.annotation.ColorInt; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 20 | import android.app.PendingIntent; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 21 | import android.arch.lifecycle.LiveData; |
| 22 | import android.arch.lifecycle.Observer; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 24 | import android.graphics.Color; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 25 | import android.graphics.drawable.Drawable; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 26 | import android.net.Uri; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 27 | import android.provider.Settings; |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 28 | import android.text.Layout; |
| 29 | import android.text.TextUtils; |
| 30 | import android.text.TextUtils.TruncateAt; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 31 | import android.util.AttributeSet; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 32 | import android.util.Log; |
| 33 | import android.view.View; |
| 34 | import android.widget.Button; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 35 | import android.widget.LinearLayout; |
| 36 | import android.widget.TextView; |
| 37 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 38 | import com.android.internal.annotations.VisibleForTesting; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 39 | import com.android.internal.graphics.ColorUtils; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 40 | import com.android.settingslib.Utils; |
| 41 | import com.android.systemui.Dependency; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 42 | import com.android.systemui.R; |
| 43 | import com.android.systemui.keyguard.KeyguardSliceProvider; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 44 | import com.android.systemui.tuner.TunerService; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 45 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 46 | import java.util.HashMap; |
| 47 | import java.util.List; |
| 48 | import java.util.function.Consumer; |
| 49 | |
| 50 | import androidx.app.slice.Slice; |
| 51 | import androidx.app.slice.SliceItem; |
| 52 | import androidx.app.slice.core.SliceQuery; |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 53 | import androidx.app.slice.widget.ListContent; |
| 54 | import androidx.app.slice.widget.RowContent; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 55 | import androidx.app.slice.widget.SliceLiveData; |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 56 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 57 | /** |
| 58 | * View visible under the clock on the lock screen and AoD. |
| 59 | */ |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 60 | public class KeyguardSliceView extends LinearLayout implements View.OnClickListener, |
| 61 | Observer<Slice>, TunerService.Tunable { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 62 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 63 | private static final String TAG = "KeyguardSliceView"; |
| 64 | private final HashMap<View, PendingIntent> mClickActions; |
| 65 | private Uri mKeyguardSliceUri; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 66 | private TextView mTitle; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 67 | private LinearLayout mRow; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 68 | private int mTextColor; |
| 69 | private float mDarkAmount = 0; |
| 70 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 71 | private LiveData<Slice> mLiveData; |
| 72 | private int mIconSize; |
| 73 | private Consumer<Boolean> mListener; |
| 74 | private boolean mHasHeader; |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 75 | private boolean mHideContent; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 76 | |
| 77 | public KeyguardSliceView(Context context) { |
| 78 | this(context, null, 0); |
| 79 | } |
| 80 | |
| 81 | public KeyguardSliceView(Context context, AttributeSet attrs) { |
| 82 | this(context, attrs, 0); |
| 83 | } |
| 84 | |
| 85 | public KeyguardSliceView(Context context, AttributeSet attrs, int defStyle) { |
| 86 | super(context, attrs, defStyle); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 87 | |
| 88 | TunerService tunerService = Dependency.get(TunerService.class); |
| 89 | tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI); |
| 90 | |
| 91 | mClickActions = new HashMap<>(); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void onFinishInflate() { |
| 96 | super.onFinishInflate(); |
| 97 | mTitle = findViewById(R.id.title); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 98 | mRow = findViewById(R.id.row); |
| 99 | mTextColor = Utils.getColorAttr(mContext, R.attr.wallpaperTextColor); |
| 100 | mIconSize = (int) mContext.getResources().getDimension(R.dimen.widget_icon_size); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | protected void onAttachedToWindow() { |
| 105 | super.onAttachedToWindow(); |
| 106 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 107 | // Make sure we always have the most current slice |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 108 | mLiveData.observeForever(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected void onDetachedFromWindow() { |
| 113 | super.onDetachedFromWindow(); |
| 114 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 115 | mLiveData.removeObserver(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | private void showSlice(Slice slice) { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 119 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 120 | ListContent lc = new ListContent(slice); |
| 121 | mHasHeader = lc.hasHeader(); |
| 122 | List<SliceItem> subItems = lc.getRowItems(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 123 | if (!mHasHeader) { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 124 | mTitle.setVisibility(GONE); |
| 125 | } else { |
| 126 | mTitle.setVisibility(VISIBLE); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 127 | // If there's a header it'll be the first subitem |
| 128 | RowContent header = new RowContent(subItems.get(0), true /* showStartItem */); |
| 129 | SliceItem mainTitle = header.getTitleItem(); |
| 130 | CharSequence title = mainTitle != null ? mainTitle.getText() : null; |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 131 | mTitle.setText(title); |
| 132 | |
| 133 | // Check if we're already ellipsizing the text. |
| 134 | // We're going to figure out the best possible line break if not. |
| 135 | Layout layout = mTitle.getLayout(); |
| 136 | if (layout != null){ |
| 137 | final int lineCount = layout.getLineCount(); |
| 138 | if (lineCount > 0) { |
| 139 | if (layout.getEllipsisCount(lineCount - 1) == 0) { |
| 140 | mTitle.setText(findBestLineBreak(title)); |
| 141 | } |
| 142 | } |
| 143 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 146 | mClickActions.clear(); |
| 147 | final int subItemsCount = subItems.size(); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 148 | final int blendedColor = getTextColor(); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 149 | final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it |
| 150 | for (int i = startIndex; i < subItemsCount; i++) { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 151 | SliceItem item = subItems.get(i); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 152 | RowContent rc = new RowContent(item, true /* showStartItem */); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 153 | final Uri itemTag = item.getSlice().getUri(); |
| 154 | // Try to reuse the view if already exists in the layout |
| 155 | KeyguardSliceButton button = mRow.findViewWithTag(itemTag); |
| 156 | if (button == null) { |
| 157 | button = new KeyguardSliceButton(mContext); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 158 | button.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 159 | button.setTag(itemTag); |
| 160 | } else { |
| 161 | mRow.removeView(button); |
| 162 | } |
Lucas Dupin | b16d823 | 2018-01-26 12:44:52 -0800 | [diff] [blame] | 163 | mRow.addView(button); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 164 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 165 | PendingIntent pendingIntent = null; |
Alan Viverette | 8593528 | 2018-02-27 22:10:18 +0000 | [diff] [blame] | 166 | if (rc.getPrimaryAction() != null) { |
| 167 | pendingIntent = rc.getPrimaryAction().getAction(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 168 | } |
| 169 | mClickActions.put(button, pendingIntent); |
| 170 | |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 171 | final SliceItem titleItem = rc.getTitleItem(); |
| 172 | button.setText(titleItem == null ? null : titleItem.getText()); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 173 | |
| 174 | Drawable iconDrawable = null; |
| 175 | SliceItem icon = SliceQuery.find(item.getSlice(), |
| 176 | android.app.slice.SliceItem.FORMAT_IMAGE); |
| 177 | if (icon != null) { |
| 178 | iconDrawable = icon.getIcon().loadDrawable(mContext); |
| 179 | final int width = (int) (iconDrawable.getIntrinsicWidth() |
| 180 | / (float) iconDrawable.getIntrinsicHeight() * mIconSize); |
| 181 | iconDrawable.setBounds(0, 0, Math.max(width, 1), mIconSize); |
| 182 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 183 | button.setCompoundDrawables(iconDrawable, null, null, null); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 184 | button.setOnClickListener(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 187 | // Removing old views |
| 188 | for (int i = 0; i < mRow.getChildCount(); i++) { |
| 189 | View child = mRow.getChildAt(i); |
| 190 | if (!mClickActions.containsKey(child)) { |
| 191 | mRow.removeView(child); |
| 192 | i--; |
| 193 | } |
| 194 | } |
| 195 | |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 196 | updateVisibility(); |
| 197 | mListener.accept(mHasHeader); |
| 198 | } |
| 199 | |
| 200 | private void updateVisibility() { |
| 201 | final boolean hasContent = mHasHeader || mRow.getChildCount() > 0; |
| 202 | final int visibility = hasContent && !mHideContent ? VISIBLE : GONE; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 203 | if (visibility != getVisibility()) { |
| 204 | setVisibility(visibility); |
| 205 | } |
| 206 | } |
| 207 | |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 208 | /** |
| 209 | * Breaks a string in 2 lines where both have similar character count |
| 210 | * but first line is always longer. |
| 211 | * |
| 212 | * @param charSequence Original text. |
| 213 | * @return Optimal string. |
| 214 | */ |
| 215 | private CharSequence findBestLineBreak(CharSequence charSequence) { |
| 216 | if (TextUtils.isEmpty(charSequence)) { |
| 217 | return charSequence; |
| 218 | } |
| 219 | |
| 220 | String source = charSequence.toString(); |
| 221 | // Ignore if there is only 1 word, |
| 222 | // or if line breaks were manually set. |
| 223 | if (source.contains("\n") || !source.contains(" ")) { |
| 224 | return source; |
| 225 | } |
| 226 | |
| 227 | final String[] words = source.split(" "); |
| 228 | final StringBuilder optimalString = new StringBuilder(source.length()); |
| 229 | int current = 0; |
| 230 | while (optimalString.length() < source.length() - optimalString.length()) { |
| 231 | optimalString.append(words[current]); |
| 232 | if (current < words.length - 1) { |
| 233 | optimalString.append(" "); |
| 234 | } |
| 235 | current++; |
| 236 | } |
| 237 | optimalString.append("\n"); |
| 238 | for (int i = current; i < words.length; i++) { |
| 239 | optimalString.append(words[i]); |
| 240 | if (current < words.length - 1) { |
| 241 | optimalString.append(" "); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return optimalString.toString(); |
| 246 | } |
| 247 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 248 | public void setDark(float darkAmount) { |
| 249 | mDarkAmount = darkAmount; |
| 250 | updateTextColors(); |
| 251 | } |
| 252 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 253 | private void updateTextColors() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 254 | final int blendedColor = getTextColor(); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 255 | mTitle.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 256 | int childCount = mRow.getChildCount(); |
| 257 | for (int i = 0; i < childCount; i++) { |
| 258 | View v = mRow.getChildAt(i); |
| 259 | if (v instanceof Button) { |
| 260 | ((Button) v).setTextColor(blendedColor); |
| 261 | } |
| 262 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 265 | @Override |
| 266 | public void onClick(View v) { |
| 267 | final PendingIntent action = mClickActions.get(v); |
| 268 | if (action != null) { |
| 269 | try { |
| 270 | action.send(); |
| 271 | } catch (PendingIntent.CanceledException e) { |
| 272 | Log.i(TAG, "Pending intent cancelled, nothing to launch", e); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | public void setListener(Consumer<Boolean> listener) { |
| 278 | mListener = listener; |
| 279 | } |
| 280 | |
| 281 | public boolean hasHeader() { |
| 282 | return mHasHeader; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * LiveData observer lifecycle. |
| 287 | * @param slice the new slice content. |
| 288 | */ |
| 289 | @Override |
| 290 | public void onChanged(Slice slice) { |
| 291 | showSlice(slice); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void onTuningChanged(String key, String newValue) { |
| 296 | setupUri(newValue); |
| 297 | } |
| 298 | |
| 299 | public void setupUri(String uriString) { |
| 300 | if (uriString == null) { |
| 301 | uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI; |
| 302 | } |
| 303 | |
| 304 | boolean wasObserving = false; |
| 305 | if (mLiveData != null && mLiveData.hasActiveObservers()) { |
| 306 | wasObserving = true; |
| 307 | mLiveData.removeObserver(this); |
| 308 | } |
| 309 | |
| 310 | mKeyguardSliceUri = Uri.parse(uriString); |
| 311 | mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri); |
| 312 | |
| 313 | if (wasObserving) { |
| 314 | mLiveData.observeForever(this); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 318 | @VisibleForTesting |
| 319 | int getTextColor() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 320 | return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount); |
| 321 | } |
| 322 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 323 | @VisibleForTesting |
| 324 | void setTextColor(@ColorInt int textColor) { |
| 325 | mTextColor = textColor; |
| 326 | updateTextColors(); |
| 327 | } |
| 328 | |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 329 | public void setHideContent(boolean hideContent) { |
| 330 | mHideContent = hideContent; |
| 331 | updateVisibility(); |
| 332 | } |
| 333 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 334 | public static class Row extends LinearLayout { |
| 335 | |
| 336 | public Row(Context context) { |
| 337 | super(context); |
| 338 | } |
| 339 | |
| 340 | public Row(Context context, AttributeSet attrs) { |
| 341 | super(context, attrs); |
| 342 | } |
| 343 | |
| 344 | public Row(Context context, AttributeSet attrs, int defStyleAttr) { |
| 345 | super(context, attrs, defStyleAttr); |
| 346 | } |
| 347 | |
| 348 | public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 349 | super(context, attrs, defStyleAttr, defStyleRes); |
| 350 | } |
| 351 | |
| 352 | @Override |
| 353 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 354 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 355 | for (int i = 0; i < getChildCount(); i++) { |
| 356 | View child = getChildAt(i); |
| 357 | if (child instanceof KeyguardSliceButton) { |
| 358 | ((KeyguardSliceButton) child).setMaxWidth(width / 2); |
| 359 | } |
| 360 | } |
| 361 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 362 | } |
| 363 | } |
| 364 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 365 | /** |
| 366 | * Representation of an item that appears under the clock on main keyguard message. |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 367 | */ |
| 368 | private class KeyguardSliceButton extends Button { |
| 369 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 370 | public KeyguardSliceButton(Context context) { |
Lucas Dupin | b16d823 | 2018-01-26 12:44:52 -0800 | [diff] [blame] | 371 | super(context, null /* attrs */, 0 /* styleAttr */, |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 372 | com.android.keyguard.R.style.TextAppearance_Keyguard_Secondary); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 373 | int horizontalPadding = (int) context.getResources() |
| 374 | .getDimension(R.dimen.widget_horizontal_padding); |
Lucas Dupin | b16d823 | 2018-01-26 12:44:52 -0800 | [diff] [blame] | 375 | setPadding(horizontalPadding / 2, 0, horizontalPadding / 2, 0); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 376 | setCompoundDrawablePadding((int) context.getResources() |
| 377 | .getDimension(R.dimen.widget_icon_padding)); |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 378 | setMaxLines(1); |
| 379 | setEllipsize(TruncateAt.END); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 380 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 381 | |
| 382 | @Override |
| 383 | public void setTextColor(int color) { |
| 384 | super.setTextColor(color); |
| 385 | updateDrawableColors(); |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, |
| 390 | Drawable bottom) { |
| 391 | super.setCompoundDrawables(left, top, right, bottom); |
| 392 | updateDrawableColors(); |
| 393 | } |
| 394 | |
| 395 | private void updateDrawableColors() { |
| 396 | final int color = getCurrentTextColor(); |
| 397 | for (Drawable drawable : getCompoundDrawables()) { |
| 398 | if (drawable != null) { |
| 399 | drawable.setTint(color); |
| 400 | } |
| 401 | } |
| 402 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 403 | } |
| 404 | } |