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