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 | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
| 20 | import android.animation.ObjectAnimator; |
| 21 | import android.animation.PropertyValuesHolder; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 22 | import android.annotation.ColorInt; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 23 | import android.app.PendingIntent; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 24 | import android.arch.lifecycle.LiveData; |
| 25 | import android.arch.lifecycle.Observer; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 27 | import android.graphics.Color; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 28 | import android.graphics.drawable.Drawable; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 29 | import android.net.Uri; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 30 | import android.provider.Settings; |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 31 | import android.text.Layout; |
| 32 | import android.text.TextUtils; |
| 33 | import android.text.TextUtils.TruncateAt; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 34 | import android.util.AttributeSet; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.view.View; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 37 | import android.view.ViewGroup; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 38 | import android.view.animation.Animation; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 39 | import android.widget.Button; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 40 | import android.widget.LinearLayout; |
| 41 | import android.widget.TextView; |
| 42 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 43 | import com.android.internal.annotations.VisibleForTesting; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 44 | import com.android.internal.graphics.ColorUtils; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 45 | import com.android.settingslib.Utils; |
| 46 | import com.android.systemui.Dependency; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 47 | import com.android.systemui.Interpolators; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 48 | import com.android.systemui.R; |
| 49 | import com.android.systemui.keyguard.KeyguardSliceProvider; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 50 | import com.android.systemui.statusbar.policy.ConfigurationController; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 51 | import com.android.systemui.tuner.TunerService; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 52 | import com.android.systemui.util.wakelock.KeepAwakeAnimationListener; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 53 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 54 | import java.util.HashMap; |
| 55 | import java.util.List; |
| 56 | import java.util.function.Consumer; |
| 57 | |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 58 | import androidx.slice.Slice; |
| 59 | import androidx.slice.SliceItem; |
| 60 | import androidx.slice.core.SliceQuery; |
| 61 | import androidx.slice.widget.ListContent; |
| 62 | import androidx.slice.widget.RowContent; |
| 63 | import androidx.slice.widget.SliceLiveData; |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 64 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 65 | /** |
| 66 | * View visible under the clock on the lock screen and AoD. |
| 67 | */ |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 68 | public class KeyguardSliceView extends LinearLayout implements View.OnClickListener, |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 69 | Observer<Slice>, TunerService.Tunable, ConfigurationController.ConfigurationListener { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 70 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 71 | private static final String TAG = "KeyguardSliceView"; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 72 | public static final int DEFAULT_ANIM_DURATION = 550; |
| 73 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 74 | private final HashMap<View, PendingIntent> mClickActions; |
| 75 | private Uri mKeyguardSliceUri; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 76 | @VisibleForTesting |
| 77 | TextView mTitle; |
| 78 | private Row mRow; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 79 | private int mTextColor; |
| 80 | private float mDarkAmount = 0; |
| 81 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 82 | private LiveData<Slice> mLiveData; |
| 83 | private int mIconSize; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 84 | /** |
| 85 | * Listener called whenever the view contents change. |
| 86 | * Boolean will be true when the change happens animated. |
| 87 | */ |
| 88 | private Consumer<Boolean> mContentChangeListener; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 89 | private boolean mHasHeader; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 90 | private Slice mSlice; |
| 91 | private boolean mPulsing; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 92 | |
| 93 | public KeyguardSliceView(Context context) { |
| 94 | this(context, null, 0); |
| 95 | } |
| 96 | |
| 97 | public KeyguardSliceView(Context context, AttributeSet attrs) { |
| 98 | this(context, attrs, 0); |
| 99 | } |
| 100 | |
| 101 | public KeyguardSliceView(Context context, AttributeSet attrs, int defStyle) { |
| 102 | super(context, attrs, defStyle); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 103 | |
| 104 | TunerService tunerService = Dependency.get(TunerService.class); |
| 105 | tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI); |
| 106 | |
| 107 | mClickActions = new HashMap<>(); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 108 | |
| 109 | LayoutTransition transition = new LayoutTransition(); |
| 110 | transition.setStagger(LayoutTransition.CHANGE_APPEARING, DEFAULT_ANIM_DURATION / 2); |
| 111 | transition.setDuration(LayoutTransition.APPEARING, DEFAULT_ANIM_DURATION); |
| 112 | transition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 2); |
| 113 | transition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); |
| 114 | transition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); |
| 115 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.FAST_OUT_SLOW_IN); |
| 116 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
| 117 | transition.setAnimateParentHierarchy(false); |
| 118 | transition.addTransitionListener(new SliceViewTransitionListener()); |
| 119 | setLayoutTransition(transition); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | @Override |
| 123 | protected void onFinishInflate() { |
| 124 | super.onFinishInflate(); |
| 125 | mTitle = findViewById(R.id.title); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 126 | mRow = findViewById(R.id.row); |
| 127 | mTextColor = Utils.getColorAttr(mContext, R.attr.wallpaperTextColor); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | protected void onAttachedToWindow() { |
| 132 | super.onAttachedToWindow(); |
| 133 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 134 | // Make sure we always have the most current slice |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 135 | mLiveData.observeForever(this); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 136 | Dependency.get(ConfigurationController.class).addCallback(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | @Override |
| 140 | protected void onDetachedFromWindow() { |
| 141 | super.onDetachedFromWindow(); |
| 142 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 143 | mLiveData.removeObserver(this); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 144 | Dependency.get(ConfigurationController.class).removeCallback(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 147 | private void showSlice() { |
| 148 | if (mPulsing) { |
| 149 | mTitle.setVisibility(GONE); |
| 150 | mRow.setVisibility(GONE); |
| 151 | mContentChangeListener.accept(getLayoutTransition() != null); |
| 152 | return; |
| 153 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 154 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 155 | if (mSlice == null) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | ListContent lc = new ListContent(getContext(), mSlice); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 160 | mHasHeader = lc.hasHeader(); |
| 161 | List<SliceItem> subItems = lc.getRowItems(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 162 | if (!mHasHeader) { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 163 | mTitle.setVisibility(GONE); |
| 164 | } else { |
| 165 | mTitle.setVisibility(VISIBLE); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 166 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 167 | // If there's a header it'll be the first subitem |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 168 | RowContent header = new RowContent(getContext(), subItems.get(0), |
| 169 | true /* showStartItem */); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 170 | SliceItem mainTitle = header.getTitleItem(); |
| 171 | CharSequence title = mainTitle != null ? mainTitle.getText() : null; |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 172 | mTitle.setText(title); |
| 173 | |
| 174 | // Check if we're already ellipsizing the text. |
| 175 | // We're going to figure out the best possible line break if not. |
| 176 | Layout layout = mTitle.getLayout(); |
| 177 | if (layout != null){ |
| 178 | final int lineCount = layout.getLineCount(); |
| 179 | if (lineCount > 0) { |
| 180 | if (layout.getEllipsisCount(lineCount - 1) == 0) { |
| 181 | mTitle.setText(findBestLineBreak(title)); |
| 182 | } |
| 183 | } |
| 184 | } |
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 | mClickActions.clear(); |
| 188 | final int subItemsCount = subItems.size(); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 189 | final int blendedColor = getTextColor(); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 190 | final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 191 | mRow.setVisibility(subItemsCount > 0 ? VISIBLE : GONE); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 192 | for (int i = startIndex; i < subItemsCount; i++) { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 193 | SliceItem item = subItems.get(i); |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 194 | RowContent rc = new RowContent(getContext(), item, true /* showStartItem */); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 195 | final Uri itemTag = item.getSlice().getUri(); |
| 196 | // Try to reuse the view if already exists in the layout |
| 197 | KeyguardSliceButton button = mRow.findViewWithTag(itemTag); |
| 198 | if (button == null) { |
| 199 | button = new KeyguardSliceButton(mContext); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 200 | button.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 201 | button.setTag(itemTag); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 202 | final int viewIndex = i - (mHasHeader ? 1 : 0); |
| 203 | mRow.addView(button, viewIndex); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 204 | } |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 205 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 206 | PendingIntent pendingIntent = null; |
Alan Viverette | 8593528 | 2018-02-27 22:10:18 +0000 | [diff] [blame] | 207 | if (rc.getPrimaryAction() != null) { |
| 208 | pendingIntent = rc.getPrimaryAction().getAction(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 209 | } |
| 210 | mClickActions.put(button, pendingIntent); |
| 211 | |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 212 | final SliceItem titleItem = rc.getTitleItem(); |
| 213 | button.setText(titleItem == null ? null : titleItem.getText()); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 214 | |
| 215 | Drawable iconDrawable = null; |
| 216 | SliceItem icon = SliceQuery.find(item.getSlice(), |
| 217 | android.app.slice.SliceItem.FORMAT_IMAGE); |
| 218 | if (icon != null) { |
| 219 | iconDrawable = icon.getIcon().loadDrawable(mContext); |
| 220 | final int width = (int) (iconDrawable.getIntrinsicWidth() |
| 221 | / (float) iconDrawable.getIntrinsicHeight() * mIconSize); |
| 222 | iconDrawable.setBounds(0, 0, Math.max(width, 1), mIconSize); |
| 223 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 224 | button.setCompoundDrawables(iconDrawable, null, null, null); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 225 | button.setOnClickListener(this); |
Lucas Dupin | 2c0d52e | 2018-03-14 12:39:00 -0700 | [diff] [blame] | 226 | button.setClickable(pendingIntent != null); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 229 | // Removing old views |
| 230 | for (int i = 0; i < mRow.getChildCount(); i++) { |
| 231 | View child = mRow.getChildAt(i); |
| 232 | if (!mClickActions.containsKey(child)) { |
| 233 | mRow.removeView(child); |
| 234 | i--; |
| 235 | } |
| 236 | } |
| 237 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 238 | if (mContentChangeListener != null) { |
| 239 | mContentChangeListener.accept(getLayoutTransition() != null); |
| 240 | } |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 243 | public void setPulsing(boolean pulsing, boolean animate) { |
| 244 | mPulsing = pulsing; |
| 245 | LayoutTransition transition = getLayoutTransition(); |
| 246 | if (!animate) { |
| 247 | setLayoutTransition(null); |
| 248 | } |
| 249 | showSlice(); |
| 250 | if (!animate) { |
| 251 | setLayoutTransition(transition); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Breaks a string in 2 lines where both have similar character count |
| 257 | * but first line is always longer. |
| 258 | * |
| 259 | * @param charSequence Original text. |
| 260 | * @return Optimal string. |
| 261 | */ |
| 262 | private CharSequence findBestLineBreak(CharSequence charSequence) { |
| 263 | if (TextUtils.isEmpty(charSequence)) { |
| 264 | return charSequence; |
| 265 | } |
| 266 | |
| 267 | String source = charSequence.toString(); |
| 268 | // Ignore if there is only 1 word, |
| 269 | // or if line breaks were manually set. |
| 270 | if (source.contains("\n") || !source.contains(" ")) { |
| 271 | return source; |
| 272 | } |
| 273 | |
| 274 | final String[] words = source.split(" "); |
| 275 | final StringBuilder optimalString = new StringBuilder(source.length()); |
| 276 | int current = 0; |
| 277 | while (optimalString.length() < source.length() - optimalString.length()) { |
| 278 | optimalString.append(words[current]); |
| 279 | if (current < words.length - 1) { |
| 280 | optimalString.append(" "); |
| 281 | } |
| 282 | current++; |
| 283 | } |
| 284 | optimalString.append("\n"); |
| 285 | for (int i = current; i < words.length; i++) { |
| 286 | optimalString.append(words[i]); |
| 287 | if (current < words.length - 1) { |
| 288 | optimalString.append(" "); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return optimalString.toString(); |
| 293 | } |
| 294 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 295 | public void setDarkAmount(float darkAmount) { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 296 | mDarkAmount = darkAmount; |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 297 | mRow.setDarkAmount(darkAmount); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 298 | updateTextColors(); |
| 299 | } |
| 300 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 301 | private void updateTextColors() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 302 | final int blendedColor = getTextColor(); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 303 | mTitle.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 304 | int childCount = mRow.getChildCount(); |
| 305 | for (int i = 0; i < childCount; i++) { |
| 306 | View v = mRow.getChildAt(i); |
| 307 | if (v instanceof Button) { |
| 308 | ((Button) v).setTextColor(blendedColor); |
| 309 | } |
| 310 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 313 | @Override |
| 314 | public void onClick(View v) { |
| 315 | final PendingIntent action = mClickActions.get(v); |
| 316 | if (action != null) { |
| 317 | try { |
| 318 | action.send(); |
| 319 | } catch (PendingIntent.CanceledException e) { |
| 320 | Log.i(TAG, "Pending intent cancelled, nothing to launch", e); |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 325 | /** |
| 326 | * Listener that gets invoked every time the title or the row visibility changes. |
| 327 | * Parameter will be {@code true} whenever the change happens animated. |
| 328 | * @param contentChangeListener The listener. |
| 329 | */ |
| 330 | public void setContentChangeListener(Consumer<Boolean> contentChangeListener) { |
| 331 | mContentChangeListener = contentChangeListener; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | public boolean hasHeader() { |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 335 | return mTitle.getVisibility() == VISIBLE; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /** |
| 339 | * LiveData observer lifecycle. |
| 340 | * @param slice the new slice content. |
| 341 | */ |
| 342 | @Override |
| 343 | public void onChanged(Slice slice) { |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 344 | mSlice = slice; |
| 345 | showSlice(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | @Override |
| 349 | public void onTuningChanged(String key, String newValue) { |
| 350 | setupUri(newValue); |
| 351 | } |
| 352 | |
| 353 | public void setupUri(String uriString) { |
| 354 | if (uriString == null) { |
| 355 | uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI; |
| 356 | } |
| 357 | |
| 358 | boolean wasObserving = false; |
| 359 | if (mLiveData != null && mLiveData.hasActiveObservers()) { |
| 360 | wasObserving = true; |
| 361 | mLiveData.removeObserver(this); |
| 362 | } |
| 363 | |
| 364 | mKeyguardSliceUri = Uri.parse(uriString); |
| 365 | mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri); |
| 366 | |
| 367 | if (wasObserving) { |
| 368 | mLiveData.observeForever(this); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 372 | @VisibleForTesting |
| 373 | int getTextColor() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 374 | return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount); |
| 375 | } |
| 376 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 377 | @VisibleForTesting |
| 378 | void setTextColor(@ColorInt int textColor) { |
| 379 | mTextColor = textColor; |
| 380 | updateTextColors(); |
| 381 | } |
| 382 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 383 | @Override |
| 384 | public void onDensityOrFontScaleChanged() { |
| 385 | mIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.widget_icon_size); |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 388 | public static class Row extends LinearLayout { |
| 389 | |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 390 | /** |
| 391 | * This view is visible in AOD, which means that the device will sleep if we |
| 392 | * don't hold a wake lock. We want to enter doze only after all views have reached |
| 393 | * their desired positions. |
| 394 | */ |
| 395 | private final Animation.AnimationListener mKeepAwakeListener; |
| 396 | private float mDarkAmount; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 397 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 398 | public Row(Context context) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 399 | this(context, null); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | public Row(Context context, AttributeSet attrs) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 403 | this(context, attrs, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | public Row(Context context, AttributeSet attrs, int defStyleAttr) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 407 | this(context, attrs, defStyleAttr, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 411 | super(context, attrs, defStyleAttr, defStyleRes); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 412 | mKeepAwakeListener = new KeepAwakeAnimationListener(mContext); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | @Override |
| 416 | protected void onFinishInflate() { |
| 417 | LayoutTransition transition = new LayoutTransition(); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 418 | transition.setDuration(DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 419 | |
| 420 | PropertyValuesHolder left = PropertyValuesHolder.ofInt("left", 0, 1); |
| 421 | PropertyValuesHolder right = PropertyValuesHolder.ofInt("right", 0, 1); |
| 422 | ObjectAnimator changeAnimator = ObjectAnimator.ofPropertyValuesHolder((Object) null, |
| 423 | left, right); |
| 424 | transition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeAnimator); |
| 425 | transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeAnimator); |
| 426 | transition.setInterpolator(LayoutTransition.CHANGE_APPEARING, |
| 427 | Interpolators.ACCELERATE_DECELERATE); |
| 428 | transition.setInterpolator(LayoutTransition.CHANGE_DISAPPEARING, |
| 429 | Interpolators.ACCELERATE_DECELERATE); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 430 | transition.setStartDelay(LayoutTransition.CHANGE_APPEARING, DEFAULT_ANIM_DURATION); |
| 431 | transition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 432 | |
| 433 | ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f); |
| 434 | transition.setAnimator(LayoutTransition.APPEARING, appearAnimator); |
| 435 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN); |
| 436 | |
| 437 | ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f); |
| 438 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 439 | transition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 4); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 440 | transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator); |
| 441 | |
| 442 | transition.setAnimateParentHierarchy(false); |
| 443 | setLayoutTransition(transition); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | @Override |
| 447 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 448 | int width = MeasureSpec.getSize(widthMeasureSpec); |
| 449 | for (int i = 0; i < getChildCount(); i++) { |
| 450 | View child = getChildAt(i); |
| 451 | if (child instanceof KeyguardSliceButton) { |
| 452 | ((KeyguardSliceButton) child).setMaxWidth(width / 2); |
| 453 | } |
| 454 | } |
| 455 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 456 | } |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 457 | |
| 458 | public void setDarkAmount(float darkAmount) { |
| 459 | boolean isAwake = darkAmount != 0; |
| 460 | boolean wasAwake = mDarkAmount != 0; |
| 461 | if (isAwake == wasAwake) { |
| 462 | return; |
| 463 | } |
| 464 | mDarkAmount = darkAmount; |
| 465 | setLayoutAnimationListener(isAwake ? null : mKeepAwakeListener); |
| 466 | } |
| 467 | |
| 468 | @Override |
| 469 | public boolean hasOverlappingRendering() { |
| 470 | return false; |
| 471 | } |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 472 | } |
| 473 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 474 | /** |
| 475 | * 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] | 476 | */ |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 477 | @VisibleForTesting |
| 478 | static class KeyguardSliceButton extends Button implements |
| 479 | ConfigurationController.ConfigurationListener { |
| 480 | private final Context mContext; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 481 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 482 | public KeyguardSliceButton(Context context) { |
Lucas Dupin | b16d823 | 2018-01-26 12:44:52 -0800 | [diff] [blame] | 483 | super(context, null /* attrs */, 0 /* styleAttr */, |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 484 | com.android.keyguard.R.style.TextAppearance_Keyguard_Secondary); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 485 | mContext = context; |
| 486 | onDensityOrFontScaleChanged(); |
| 487 | setEllipsize(TruncateAt.END); |
| 488 | } |
| 489 | |
| 490 | @Override |
| 491 | protected void onAttachedToWindow() { |
| 492 | super.onAttachedToWindow(); |
| 493 | Dependency.get(ConfigurationController.class).addCallback(this); |
| 494 | } |
| 495 | |
| 496 | @Override |
| 497 | protected void onDetachedFromWindow() { |
| 498 | super.onDetachedFromWindow(); |
| 499 | Dependency.get(ConfigurationController.class).removeCallback(this); |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public void onDensityOrFontScaleChanged() { |
| 504 | int horizontalPadding = (int) mContext.getResources() |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 505 | .getDimension(R.dimen.widget_horizontal_padding); |
Lucas Dupin | b16d823 | 2018-01-26 12:44:52 -0800 | [diff] [blame] | 506 | setPadding(horizontalPadding / 2, 0, horizontalPadding / 2, 0); |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 507 | setCompoundDrawablePadding((int) mContext.getResources() |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 508 | .getDimension(R.dimen.widget_icon_padding)); |
| 509 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 510 | |
| 511 | @Override |
| 512 | public void setTextColor(int color) { |
| 513 | super.setTextColor(color); |
| 514 | updateDrawableColors(); |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, |
| 519 | Drawable bottom) { |
| 520 | super.setCompoundDrawables(left, top, right, bottom); |
| 521 | updateDrawableColors(); |
| 522 | } |
| 523 | |
| 524 | private void updateDrawableColors() { |
| 525 | final int color = getCurrentTextColor(); |
| 526 | for (Drawable drawable : getCompoundDrawables()) { |
| 527 | if (drawable != null) { |
| 528 | drawable.setTint(color); |
| 529 | } |
| 530 | } |
| 531 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 532 | } |
Lucas Dupin | 3d7ccaf | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 533 | |
| 534 | private class SliceViewTransitionListener implements LayoutTransition.TransitionListener { |
| 535 | @Override |
| 536 | public void startTransition(LayoutTransition transition, ViewGroup container, View view, |
| 537 | int transitionType) { |
| 538 | switch (transitionType) { |
| 539 | case LayoutTransition.APPEARING: |
| 540 | int translation = getResources().getDimensionPixelSize( |
| 541 | R.dimen.pulsing_notification_appear_translation); |
| 542 | view.setTranslationY(translation); |
| 543 | view.animate() |
| 544 | .translationY(0) |
| 545 | .setDuration(DEFAULT_ANIM_DURATION) |
| 546 | .setInterpolator(Interpolators.ALPHA_IN) |
| 547 | .start(); |
| 548 | break; |
| 549 | case LayoutTransition.DISAPPEARING: |
| 550 | if (view == mTitle) { |
| 551 | // Translate the view to the inverse of its height, so the layout event |
| 552 | // won't misposition it. |
| 553 | LayoutParams params = (LayoutParams) mTitle.getLayoutParams(); |
| 554 | int margin = params.topMargin + params.bottomMargin; |
| 555 | mTitle.setTranslationY(-mTitle.getHeight() - margin); |
| 556 | } |
| 557 | break; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | @Override |
| 562 | public void endTransition(LayoutTransition transition, ViewGroup container, View view, |
| 563 | int transitionType) { |
| 564 | |
| 565 | } |
| 566 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 567 | } |