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