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