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