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 | |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 19 | import static android.app.slice.Slice.HINT_LIST_ITEM; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 20 | import static android.view.Display.DEFAULT_DISPLAY; |
| 21 | import static android.view.Display.INVALID_DISPLAY; |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 22 | |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 23 | import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT; |
| 24 | |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 25 | import android.animation.LayoutTransition; |
| 26 | import android.animation.ObjectAnimator; |
| 27 | import android.animation.PropertyValuesHolder; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 28 | import android.annotation.ColorInt; |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 29 | import android.annotation.StyleRes; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 30 | import android.app.PendingIntent; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 31 | import android.content.Context; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 32 | import android.graphics.Color; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 33 | import android.graphics.drawable.Drawable; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 34 | import android.net.Uri; |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 35 | import android.os.Trace; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 36 | import android.provider.Settings; |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 37 | import android.text.TextUtils; |
| 38 | import android.text.TextUtils.TruncateAt; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 39 | import android.util.AttributeSet; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 40 | import android.util.Log; |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 41 | import android.util.TypedValue; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 42 | import android.view.View; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 43 | import android.view.animation.Animation; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 44 | import android.widget.Button; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 45 | import android.widget.LinearLayout; |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 46 | import android.widget.TextView; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 47 | |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 48 | import androidx.lifecycle.LiveData; |
| 49 | import androidx.lifecycle.Observer; |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 50 | import androidx.slice.Slice; |
| 51 | import androidx.slice.SliceItem; |
Jason Monk | f852f56 | 2018-05-23 16:08:11 -0400 | [diff] [blame] | 52 | import androidx.slice.SliceViewManager; |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 53 | import androidx.slice.core.SliceQuery; |
| 54 | import androidx.slice.widget.ListContent; |
| 55 | import androidx.slice.widget.RowContent; |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 56 | import androidx.slice.widget.SliceContent; |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 57 | import androidx.slice.widget.SliceLiveData; |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 58 | |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 59 | import com.android.internal.annotations.VisibleForTesting; |
| 60 | import com.android.internal.graphics.ColorUtils; |
| 61 | import com.android.settingslib.Utils; |
| 62 | import com.android.systemui.Dependency; |
| 63 | import com.android.systemui.Interpolators; |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 64 | import com.android.systemui.R; |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 65 | import com.android.systemui.keyguard.KeyguardSliceProvider; |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 66 | import com.android.systemui.plugins.ActivityStarter; |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 67 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 68 | import com.android.systemui.tuner.TunerService; |
| 69 | import com.android.systemui.util.wakelock.KeepAwakeAnimationListener; |
| 70 | |
Lucas Dupin | 4349952 | 2019-03-04 16:45:58 -0800 | [diff] [blame] | 71 | import java.io.FileDescriptor; |
| 72 | import java.io.PrintWriter; |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 73 | import java.util.ArrayList; |
| 74 | import java.util.HashMap; |
| 75 | import java.util.List; |
| 76 | |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 77 | import javax.inject.Inject; |
| 78 | import javax.inject.Named; |
| 79 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 80 | /** |
| 81 | * View visible under the clock on the lock screen and AoD. |
| 82 | */ |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 83 | public class KeyguardSliceView extends LinearLayout implements View.OnClickListener, |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 84 | Observer<Slice>, TunerService.Tunable, ConfigurationController.ConfigurationListener { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 85 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 86 | private static final String TAG = "KeyguardSliceView"; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 87 | public static final int DEFAULT_ANIM_DURATION = 550; |
| 88 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 89 | private final HashMap<View, PendingIntent> mClickActions; |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 90 | private final ActivityStarter mActivityStarter; |
| 91 | private final ConfigurationController mConfigurationController; |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 92 | private final LayoutTransition mLayoutTransition; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 93 | private Uri mKeyguardSliceUri; |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 94 | @VisibleForTesting |
| 95 | TextView mTitle; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 96 | private Row mRow; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 97 | private int mTextColor; |
| 98 | private float mDarkAmount = 0; |
| 99 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 100 | private LiveData<Slice> mLiveData; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 101 | private int mDisplayId = INVALID_DISPLAY; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 102 | private int mIconSize; |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 103 | private int mIconSizeWithHeader; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 104 | /** |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 105 | * Runnable called whenever the view contents change. |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 106 | */ |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 107 | private Runnable mContentChangeListener; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 108 | private Slice mSlice; |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 109 | private boolean mHasHeader; |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 110 | private final int mRowWithHeaderPadding; |
| 111 | private final int mRowPadding; |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 112 | private float mRowTextSize; |
| 113 | private float mRowWithHeaderTextSize; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 114 | |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 115 | @Inject |
| 116 | public KeyguardSliceView(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs, |
| 117 | ActivityStarter activityStarter, ConfigurationController configurationController) { |
| 118 | super(context, attrs); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 119 | |
| 120 | TunerService tunerService = Dependency.get(TunerService.class); |
| 121 | tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI); |
| 122 | |
| 123 | mClickActions = new HashMap<>(); |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 124 | mRowPadding = context.getResources().getDimensionPixelSize(R.dimen.subtitle_clock_padding); |
| 125 | mRowWithHeaderPadding = context.getResources() |
| 126 | .getDimensionPixelSize(R.dimen.header_subtitle_padding); |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 127 | mActivityStarter = activityStarter; |
| 128 | mConfigurationController = configurationController; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 129 | |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 130 | mLayoutTransition = new LayoutTransition(); |
| 131 | mLayoutTransition.setStagger(LayoutTransition.CHANGE_APPEARING, DEFAULT_ANIM_DURATION / 2); |
| 132 | mLayoutTransition.setDuration(LayoutTransition.APPEARING, DEFAULT_ANIM_DURATION); |
| 133 | mLayoutTransition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 2); |
| 134 | mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); |
| 135 | mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); |
| 136 | mLayoutTransition.setInterpolator(LayoutTransition.APPEARING, |
| 137 | Interpolators.FAST_OUT_SLOW_IN); |
| 138 | mLayoutTransition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
| 139 | mLayoutTransition.setAnimateParentHierarchy(false); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void onFinishInflate() { |
| 144 | super.onFinishInflate(); |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 145 | mTitle = findViewById(R.id.title); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 146 | mRow = findViewById(R.id.row); |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 147 | mTextColor = Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 148 | mIconSize = (int) mContext.getResources().getDimension(R.dimen.widget_icon_size); |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 149 | mIconSizeWithHeader = (int) mContext.getResources().getDimension(R.dimen.header_icon_size); |
| 150 | mRowTextSize = mContext.getResources().getDimensionPixelSize( |
| 151 | R.dimen.widget_label_font_size); |
| 152 | mRowWithHeaderTextSize = mContext.getResources().getDimensionPixelSize( |
| 153 | R.dimen.header_row_font_size); |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 154 | mTitle.setOnClickListener(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
| 158 | protected void onAttachedToWindow() { |
| 159 | super.onAttachedToWindow(); |
| 160 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 161 | mDisplayId = getDisplay().getDisplayId(); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 162 | // Make sure we always have the most current slice |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 163 | mLiveData.observeForever(this); |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 164 | mConfigurationController.addCallback(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | @Override |
| 168 | protected void onDetachedFromWindow() { |
| 169 | super.onDetachedFromWindow(); |
| 170 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 171 | // TODO(b/117344873) Remove below work around after this issue be fixed. |
| 172 | if (mDisplayId == DEFAULT_DISPLAY) { |
| 173 | mLiveData.removeObserver(this); |
| 174 | } |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 175 | mConfigurationController.removeCallback(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 178 | @Override |
| 179 | public void onVisibilityAggregated(boolean isVisible) { |
| 180 | super.onVisibilityAggregated(isVisible); |
| 181 | setLayoutTransition(isVisible ? mLayoutTransition : null); |
| 182 | } |
| 183 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 184 | /** |
| 185 | * Returns whether the current visible slice has a title/header. |
| 186 | */ |
| 187 | public boolean hasHeader() { |
| 188 | return mHasHeader; |
| 189 | } |
| 190 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 191 | private void showSlice() { |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 192 | Trace.beginSection("KeyguardSliceView#showSlice"); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 193 | if (mSlice == null) { |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 194 | mTitle.setVisibility(GONE); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 195 | mRow.setVisibility(GONE); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 196 | mHasHeader = false; |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 197 | if (mContentChangeListener != null) { |
| 198 | mContentChangeListener.run(); |
| 199 | } |
Lucas Dupin | 4349952 | 2019-03-04 16:45:58 -0800 | [diff] [blame] | 200 | Trace.endSection(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 201 | return; |
| 202 | } |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 203 | mClickActions.clear(); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 204 | |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 205 | ListContent lc = new ListContent(getContext(), mSlice); |
| 206 | SliceContent headerContent = lc.getHeader(); |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 207 | mHasHeader = headerContent != null && !headerContent.getSliceItem().hasHint(HINT_LIST_ITEM); |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 208 | List<SliceContent> subItems = new ArrayList<>(); |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 209 | for (int i = 0; i < lc.getRowItems().size(); i++) { |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 210 | SliceContent subItem = lc.getRowItems().get(i); |
| 211 | String itemUri = subItem.getSliceItem().getSlice().getUri().toString(); |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 212 | // Filter out the action row |
| 213 | if (!KeyguardSliceProvider.KEYGUARD_ACTION_URI.equals(itemUri)) { |
| 214 | subItems.add(subItem); |
| 215 | } |
| 216 | } |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 217 | if (!mHasHeader) { |
| 218 | mTitle.setVisibility(GONE); |
| 219 | } else { |
| 220 | mTitle.setVisibility(VISIBLE); |
| 221 | |
| 222 | RowContent header = lc.getHeader(); |
| 223 | SliceItem mainTitle = header.getTitleItem(); |
| 224 | CharSequence title = mainTitle != null ? mainTitle.getText() : null; |
| 225 | mTitle.setText(title); |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 226 | if (header.getPrimaryAction() != null |
| 227 | && header.getPrimaryAction().getAction() != null) { |
| 228 | mClickActions.put(mTitle, header.getPrimaryAction().getAction()); |
| 229 | } |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 230 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 231 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 232 | final int subItemsCount = subItems.size(); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 233 | final int blendedColor = getTextColor(); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 234 | final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 235 | mRow.setVisibility(subItemsCount > 0 ? VISIBLE : GONE); |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 236 | LinearLayout.LayoutParams layoutParams = (LayoutParams) mRow.getLayoutParams(); |
| 237 | layoutParams.topMargin = mHasHeader ? mRowWithHeaderPadding : mRowPadding; |
| 238 | mRow.setLayoutParams(layoutParams); |
| 239 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 240 | for (int i = startIndex; i < subItemsCount; i++) { |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 241 | RowContent rc = (RowContent) subItems.get(i); |
| 242 | SliceItem item = rc.getSliceItem(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 243 | final Uri itemTag = item.getSlice().getUri(); |
| 244 | // Try to reuse the view if already exists in the layout |
| 245 | KeyguardSliceButton button = mRow.findViewWithTag(itemTag); |
| 246 | if (button == null) { |
| 247 | button = new KeyguardSliceButton(mContext); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 248 | button.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 249 | button.setTag(itemTag); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 250 | final int viewIndex = i - (mHasHeader ? 1 : 0); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 251 | mRow.addView(button, viewIndex); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 252 | } |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 253 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 254 | PendingIntent pendingIntent = null; |
Alan Viverette | 8593528 | 2018-02-27 22:10:18 +0000 | [diff] [blame] | 255 | if (rc.getPrimaryAction() != null) { |
| 256 | pendingIntent = rc.getPrimaryAction().getAction(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 257 | } |
| 258 | mClickActions.put(button, pendingIntent); |
| 259 | |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 260 | final SliceItem titleItem = rc.getTitleItem(); |
| 261 | button.setText(titleItem == null ? null : titleItem.getText()); |
Lucas Dupin | 1d5e910 | 2018-05-21 17:58:05 -0700 | [diff] [blame] | 262 | button.setContentDescription(rc.getContentDescription()); |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 263 | button.setTextSize(TypedValue.COMPLEX_UNIT_PX, |
| 264 | mHasHeader ? mRowWithHeaderTextSize : mRowTextSize); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 265 | |
| 266 | Drawable iconDrawable = null; |
| 267 | SliceItem icon = SliceQuery.find(item.getSlice(), |
| 268 | android.app.slice.SliceItem.FORMAT_IMAGE); |
| 269 | if (icon != null) { |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 270 | final int iconSize = mHasHeader ? mIconSizeWithHeader : mIconSize; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 271 | iconDrawable = icon.getIcon().loadDrawable(mContext); |
Lucas Dupin | 2955bf1 | 2018-12-26 10:15:51 -0800 | [diff] [blame] | 272 | if (iconDrawable != null) { |
| 273 | final int width = (int) (iconDrawable.getIntrinsicWidth() |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 274 | / (float) iconDrawable.getIntrinsicHeight() * iconSize); |
| 275 | iconDrawable.setBounds(0, 0, Math.max(width, 1), iconSize); |
Lucas Dupin | 2955bf1 | 2018-12-26 10:15:51 -0800 | [diff] [blame] | 276 | } |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 277 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 278 | button.setCompoundDrawables(iconDrawable, null, null, null); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 279 | button.setOnClickListener(this); |
Lucas Dupin | 2c0d52e | 2018-03-14 12:39:00 -0700 | [diff] [blame] | 280 | button.setClickable(pendingIntent != null); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 283 | // Removing old views |
| 284 | for (int i = 0; i < mRow.getChildCount(); i++) { |
| 285 | View child = mRow.getChildAt(i); |
| 286 | if (!mClickActions.containsKey(child)) { |
| 287 | mRow.removeView(child); |
| 288 | i--; |
| 289 | } |
| 290 | } |
| 291 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 292 | if (mContentChangeListener != null) { |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 293 | mContentChangeListener.run(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 294 | } |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 295 | Trace.endSection(); |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 298 | public void setDarkAmount(float darkAmount) { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 299 | mDarkAmount = darkAmount; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 300 | mRow.setDarkAmount(darkAmount); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 301 | updateTextColors(); |
| 302 | } |
| 303 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 304 | private void updateTextColors() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 305 | final int blendedColor = getTextColor(); |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 306 | mTitle.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 307 | int childCount = mRow.getChildCount(); |
| 308 | for (int i = 0; i < childCount; i++) { |
| 309 | View v = mRow.getChildAt(i); |
| 310 | if (v instanceof Button) { |
| 311 | ((Button) v).setTextColor(blendedColor); |
| 312 | } |
| 313 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 316 | @Override |
| 317 | public void onClick(View v) { |
| 318 | final PendingIntent action = mClickActions.get(v); |
| 319 | if (action != null) { |
Lucas Dupin | ad07944 | 2019-04-03 13:14:11 -0700 | [diff] [blame] | 320 | mActivityStarter.startPendingIntentDismissingKeyguard(action); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 324 | /** |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 325 | * Runnable that gets invoked every time the title or the row visibility changes. |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 326 | * @param contentChangeListener The listener. |
| 327 | */ |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 328 | public void setContentChangeListener(Runnable contentChangeListener) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 329 | mContentChangeListener = contentChangeListener; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 332 | /** |
| 333 | * LiveData observer lifecycle. |
| 334 | * @param slice the new slice content. |
| 335 | */ |
| 336 | @Override |
| 337 | public void onChanged(Slice slice) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 338 | mSlice = slice; |
| 339 | showSlice(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | @Override |
| 343 | public void onTuningChanged(String key, String newValue) { |
| 344 | setupUri(newValue); |
| 345 | } |
| 346 | |
Lucas Dupin | e570af6 | 2019-02-10 14:52:30 -0800 | [diff] [blame] | 347 | /** |
| 348 | * Sets the slice provider Uri. |
| 349 | */ |
| 350 | public void setupUri(String uriString) { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 351 | if (uriString == null) { |
| 352 | uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI; |
| 353 | } |
| 354 | |
| 355 | boolean wasObserving = false; |
| 356 | if (mLiveData != null && mLiveData.hasActiveObservers()) { |
| 357 | wasObserving = true; |
| 358 | mLiveData.removeObserver(this); |
| 359 | } |
| 360 | |
| 361 | mKeyguardSliceUri = Uri.parse(uriString); |
| 362 | mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri); |
| 363 | |
| 364 | if (wasObserving) { |
| 365 | mLiveData.observeForever(this); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 369 | @VisibleForTesting |
| 370 | int getTextColor() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 371 | return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount); |
| 372 | } |
| 373 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 374 | @VisibleForTesting |
| 375 | void setTextColor(@ColorInt int textColor) { |
| 376 | mTextColor = textColor; |
| 377 | updateTextColors(); |
| 378 | } |
| 379 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 380 | @Override |
| 381 | public void onDensityOrFontScaleChanged() { |
| 382 | mIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.widget_icon_size); |
Robert Snoeberger | 78a79d2 | 2019-05-07 16:47:07 -0400 | [diff] [blame] | 383 | mIconSizeWithHeader = (int) mContext.getResources().getDimension(R.dimen.header_icon_size); |
| 384 | mRowTextSize = mContext.getResources().getDimensionPixelSize( |
| 385 | R.dimen.widget_label_font_size); |
| 386 | mRowWithHeaderTextSize = mContext.getResources().getDimensionPixelSize( |
| 387 | R.dimen.header_row_font_size); |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 388 | } |
| 389 | |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 390 | public void refresh() { |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 391 | Slice slice; |
| 392 | Trace.beginSection("KeyguardSliceView#refresh"); |
| 393 | // We can optimize performance and avoid binder calls when we know that we're bound |
| 394 | // to a Slice on the same process. |
| 395 | if (KeyguardSliceProvider.KEYGUARD_SLICE_URI.equals(mKeyguardSliceUri.toString())) { |
| 396 | KeyguardSliceProvider instance = KeyguardSliceProvider.getAttachedInstance(); |
| 397 | if (instance != null) { |
| 398 | slice = instance.onBindSlice(mKeyguardSliceUri); |
| 399 | } else { |
| 400 | Log.w(TAG, "Keyguard slice not bound yet?"); |
| 401 | slice = null; |
| 402 | } |
| 403 | } else { |
| 404 | slice = SliceViewManager.getInstance(getContext()).bindSlice(mKeyguardSliceUri); |
| 405 | } |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 406 | onChanged(slice); |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 407 | Trace.endSection(); |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Lucas Dupin | 4349952 | 2019-03-04 16:45:58 -0800 | [diff] [blame] | 410 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 411 | pw.println("KeyguardSliceView:"); |
| 412 | pw.println(" mClickActions: " + mClickActions); |
| 413 | pw.println(" mTitle: " + (mTitle == null ? "null" : mTitle.getVisibility() == VISIBLE)); |
| 414 | pw.println(" mRow: " + (mRow == null ? "null" : mRow.getVisibility() == VISIBLE)); |
| 415 | pw.println(" mTextColor: " + Integer.toHexString(mTextColor)); |
| 416 | pw.println(" mDarkAmount: " + mDarkAmount); |
| 417 | pw.println(" mSlice: " + mSlice); |
| 418 | pw.println(" mHasHeader: " + mHasHeader); |
| 419 | } |
| 420 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 421 | public static class Row extends LinearLayout { |
| 422 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 423 | /** |
| 424 | * This view is visible in AOD, which means that the device will sleep if we |
| 425 | * don't hold a wake lock. We want to enter doze only after all views have reached |
| 426 | * their desired positions. |
| 427 | */ |
| 428 | private final Animation.AnimationListener mKeepAwakeListener; |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 429 | private LayoutTransition mLayoutTransition; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 430 | private float mDarkAmount; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 431 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 432 | public Row(Context context) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 433 | this(context, null); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | public Row(Context context, AttributeSet attrs) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 437 | this(context, attrs, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | public Row(Context context, AttributeSet attrs, int defStyleAttr) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 441 | this(context, attrs, defStyleAttr, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 445 | super(context, attrs, defStyleAttr, defStyleRes); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 446 | mKeepAwakeListener = new KeepAwakeAnimationListener(mContext); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | @Override |
| 450 | protected void onFinishInflate() { |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 451 | mLayoutTransition = new LayoutTransition(); |
| 452 | mLayoutTransition.setDuration(DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 453 | |
| 454 | PropertyValuesHolder left = PropertyValuesHolder.ofInt("left", 0, 1); |
| 455 | PropertyValuesHolder right = PropertyValuesHolder.ofInt("right", 0, 1); |
| 456 | ObjectAnimator changeAnimator = ObjectAnimator.ofPropertyValuesHolder((Object) null, |
| 457 | left, right); |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 458 | mLayoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeAnimator); |
| 459 | mLayoutTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeAnimator); |
| 460 | mLayoutTransition.setInterpolator(LayoutTransition.CHANGE_APPEARING, |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 461 | Interpolators.ACCELERATE_DECELERATE); |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 462 | mLayoutTransition.setInterpolator(LayoutTransition.CHANGE_DISAPPEARING, |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 463 | Interpolators.ACCELERATE_DECELERATE); |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 464 | mLayoutTransition.setStartDelay(LayoutTransition.CHANGE_APPEARING, |
| 465 | DEFAULT_ANIM_DURATION); |
| 466 | mLayoutTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, |
| 467 | DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 468 | |
| 469 | ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f); |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 470 | mLayoutTransition.setAnimator(LayoutTransition.APPEARING, appearAnimator); |
| 471 | mLayoutTransition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 472 | |
| 473 | ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f); |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 474 | mLayoutTransition.setInterpolator(LayoutTransition.DISAPPEARING, |
| 475 | Interpolators.ALPHA_OUT); |
| 476 | mLayoutTransition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 4); |
| 477 | mLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 478 | |
Robert Snoeberger | dc8fee7 | 2019-07-19 16:35:35 -0400 | [diff] [blame^] | 479 | mLayoutTransition.setAnimateParentHierarchy(false); |
| 480 | } |
| 481 | |
| 482 | @Override |
| 483 | public void onVisibilityAggregated(boolean isVisible) { |
| 484 | super.onVisibilityAggregated(isVisible); |
| 485 | setLayoutTransition(isVisible ? mLayoutTransition : null); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | @Override |
| 489 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 490 | int width = MeasureSpec.getSize(widthMeasureSpec); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 491 | int childCount = getChildCount(); |
| 492 | for (int i = 0; i < childCount; i++) { |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 493 | View child = getChildAt(i); |
| 494 | if (child instanceof KeyguardSliceButton) { |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 495 | ((KeyguardSliceButton) child).setMaxWidth(width / childCount); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 499 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 500 | |
| 501 | public void setDarkAmount(float darkAmount) { |
| 502 | boolean isAwake = darkAmount != 0; |
| 503 | boolean wasAwake = mDarkAmount != 0; |
| 504 | if (isAwake == wasAwake) { |
| 505 | return; |
| 506 | } |
| 507 | mDarkAmount = darkAmount; |
| 508 | setLayoutAnimationListener(isAwake ? null : mKeepAwakeListener); |
| 509 | } |
| 510 | |
| 511 | @Override |
| 512 | public boolean hasOverlappingRendering() { |
| 513 | return false; |
| 514 | } |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 517 | /** |
| 518 | * 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] | 519 | */ |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 520 | @VisibleForTesting |
| 521 | static class KeyguardSliceButton extends Button implements |
| 522 | ConfigurationController.ConfigurationListener { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 523 | |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 524 | @StyleRes |
| 525 | private static int sStyleId = R.style.TextAppearance_Keyguard_Secondary; |
| 526 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 527 | public KeyguardSliceButton(Context context) { |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 528 | super(context, null /* attrs */, 0 /* styleAttr */, sStyleId); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 529 | onDensityOrFontScaleChanged(); |
| 530 | setEllipsize(TruncateAt.END); |
| 531 | } |
| 532 | |
| 533 | @Override |
| 534 | protected void onAttachedToWindow() { |
| 535 | super.onAttachedToWindow(); |
| 536 | Dependency.get(ConfigurationController.class).addCallback(this); |
| 537 | } |
| 538 | |
| 539 | @Override |
| 540 | protected void onDetachedFromWindow() { |
| 541 | super.onDetachedFromWindow(); |
| 542 | Dependency.get(ConfigurationController.class).removeCallback(this); |
| 543 | } |
| 544 | |
| 545 | @Override |
| 546 | public void onDensityOrFontScaleChanged() { |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 547 | updatePadding(); |
| 548 | } |
| 549 | |
| 550 | @Override |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 551 | public void onOverlayChanged() { |
| 552 | setTextAppearance(sStyleId); |
| 553 | } |
| 554 | |
| 555 | @Override |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 556 | public void setText(CharSequence text, BufferType type) { |
| 557 | super.setText(text, type); |
| 558 | updatePadding(); |
| 559 | } |
| 560 | |
| 561 | private void updatePadding() { |
| 562 | boolean hasText = !TextUtils.isEmpty(getText()); |
| 563 | int horizontalPadding = (int) getContext().getResources() |
| 564 | .getDimension(R.dimen.widget_horizontal_padding) / 2; |
| 565 | setPadding(horizontalPadding, 0, horizontalPadding * (hasText ? 1 : -1), 0); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 566 | setCompoundDrawablePadding((int) mContext.getResources() |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 567 | .getDimension(R.dimen.widget_icon_padding)); |
| 568 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 569 | |
| 570 | @Override |
| 571 | public void setTextColor(int color) { |
| 572 | super.setTextColor(color); |
| 573 | updateDrawableColors(); |
| 574 | } |
| 575 | |
| 576 | @Override |
| 577 | public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, |
| 578 | Drawable bottom) { |
| 579 | super.setCompoundDrawables(left, top, right, bottom); |
| 580 | updateDrawableColors(); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 581 | updatePadding(); |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | private void updateDrawableColors() { |
| 585 | final int color = getCurrentTextColor(); |
| 586 | for (Drawable drawable : getCompoundDrawables()) { |
| 587 | if (drawable != null) { |
| 588 | drawable.setTint(color); |
| 589 | } |
| 590 | } |
| 591 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 592 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 593 | } |