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