blob: 9a26b35c09398a001303275b2da15417cefd3a74 [file] [log] [blame]
Lucas Dupin957e50c2017-10-10 11:23:27 -07001/*
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
17package com.android.keyguard;
18
Lucas Dupinc68ec482018-03-30 14:24:53 -070019import android.animation.LayoutTransition;
20import android.animation.ObjectAnimator;
21import android.animation.PropertyValuesHolder;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080022import android.annotation.ColorInt;
Lucas Dupin957e50c2017-10-10 11:23:27 -070023import android.app.PendingIntent;
Lucas Dupin6bd86012017-12-05 17:58:57 -080024import android.arch.lifecycle.LiveData;
25import android.arch.lifecycle.Observer;
Lucas Dupin957e50c2017-10-10 11:23:27 -070026import android.content.Context;
Lucas Dupin957e50c2017-10-10 11:23:27 -070027import android.graphics.Color;
Lucas Dupin6bd86012017-12-05 17:58:57 -080028import android.graphics.drawable.Drawable;
Lucas Dupin957e50c2017-10-10 11:23:27 -070029import android.net.Uri;
Lucas Dupin6bd86012017-12-05 17:58:57 -080030import android.provider.Settings;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -080031import android.text.Layout;
32import android.text.TextUtils;
33import android.text.TextUtils.TruncateAt;
Lucas Dupin957e50c2017-10-10 11:23:27 -070034import android.util.AttributeSet;
Lucas Dupin6bd86012017-12-05 17:58:57 -080035import android.util.Log;
36import android.view.View;
Lucas Dupinc68ec482018-03-30 14:24:53 -070037import android.view.animation.Animation;
Lucas Dupin6bd86012017-12-05 17:58:57 -080038import android.widget.Button;
Lucas Dupin957e50c2017-10-10 11:23:27 -070039import android.widget.LinearLayout;
40import android.widget.TextView;
41
Lucas Dupinf8e274c2018-02-22 17:01:55 -080042import com.android.internal.annotations.VisibleForTesting;
Lucas Dupin957e50c2017-10-10 11:23:27 -070043import com.android.internal.graphics.ColorUtils;
Lucas Dupin6bd86012017-12-05 17:58:57 -080044import com.android.settingslib.Utils;
45import com.android.systemui.Dependency;
Lucas Dupinc68ec482018-03-30 14:24:53 -070046import com.android.systemui.Interpolators;
Lucas Dupin957e50c2017-10-10 11:23:27 -070047import com.android.systemui.R;
48import com.android.systemui.keyguard.KeyguardSliceProvider;
Lucas Dupin6bd86012017-12-05 17:58:57 -080049import com.android.systemui.tuner.TunerService;
Lucas Dupinc68ec482018-03-30 14:24:53 -070050import com.android.systemui.util.wakelock.WakeLock;
Lucas Dupin957e50c2017-10-10 11:23:27 -070051
Lucas Dupin6bd86012017-12-05 17:58:57 -080052import java.util.HashMap;
53import java.util.List;
54import java.util.function.Consumer;
55
Alan Viverettee8935882018-03-15 21:19:36 +000056import androidx.slice.Slice;
57import androidx.slice.SliceItem;
58import androidx.slice.core.SliceQuery;
59import androidx.slice.widget.ListContent;
60import androidx.slice.widget.RowContent;
61import androidx.slice.widget.SliceLiveData;
Jason Monk2af19982017-11-07 19:38:27 -050062
Lucas Dupin957e50c2017-10-10 11:23:27 -070063/**
64 * View visible under the clock on the lock screen and AoD.
65 */
Lucas Dupin6bd86012017-12-05 17:58:57 -080066public class KeyguardSliceView extends LinearLayout implements View.OnClickListener,
67 Observer<Slice>, TunerService.Tunable {
Lucas Dupin957e50c2017-10-10 11:23:27 -070068
Lucas Dupin6bd86012017-12-05 17:58:57 -080069 private static final String TAG = "KeyguardSliceView";
70 private final HashMap<View, PendingIntent> mClickActions;
71 private Uri mKeyguardSliceUri;
Lucas Dupin957e50c2017-10-10 11:23:27 -070072 private TextView mTitle;
Lucas Dupin6bd86012017-12-05 17:58:57 -080073 private LinearLayout mRow;
Lucas Dupin957e50c2017-10-10 11:23:27 -070074 private int mTextColor;
75 private float mDarkAmount = 0;
76
Lucas Dupin6bd86012017-12-05 17:58:57 -080077 private LiveData<Slice> mLiveData;
78 private int mIconSize;
79 private Consumer<Boolean> mListener;
80 private boolean mHasHeader;
Lucas Dupin2c5fce62018-03-05 19:06:00 -080081 private boolean mHideContent;
Lucas Dupin957e50c2017-10-10 11:23:27 -070082
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 Dupin6bd86012017-12-05 17:58:57 -080093
94 TunerService tunerService = Dependency.get(TunerService.class);
95 tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI);
96
97 mClickActions = new HashMap<>();
Lucas Dupin957e50c2017-10-10 11:23:27 -070098 }
99
100 @Override
101 protected void onFinishInflate() {
102 super.onFinishInflate();
103 mTitle = findViewById(R.id.title);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800104 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 Dupin957e50c2017-10-10 11:23:27 -0700107 }
108
109 @Override
110 protected void onAttachedToWindow() {
111 super.onAttachedToWindow();
112
Lucas Dupin957e50c2017-10-10 11:23:27 -0700113 // Make sure we always have the most current slice
Lucas Dupin6bd86012017-12-05 17:58:57 -0800114 mLiveData.observeForever(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700115 }
116
117 @Override
118 protected void onDetachedFromWindow() {
119 super.onDetachedFromWindow();
120
Lucas Dupin6bd86012017-12-05 17:58:57 -0800121 mLiveData.removeObserver(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700122 }
123
124 private void showSlice(Slice slice) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700125
Alan Viverettee8935882018-03-15 21:19:36 +0000126 ListContent lc = new ListContent(getContext(), slice);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800127 mHasHeader = lc.hasHeader();
128 List<SliceItem> subItems = lc.getRowItems();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800129 if (!mHasHeader) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700130 mTitle.setVisibility(GONE);
131 } else {
132 mTitle.setVisibility(VISIBLE);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800133 // If there's a header it'll be the first subitem
Alan Viverettee8935882018-03-15 21:19:36 +0000134 RowContent header = new RowContent(getContext(), subItems.get(0),
135 true /* showStartItem */);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800136 SliceItem mainTitle = header.getTitleItem();
137 CharSequence title = mainTitle != null ? mainTitle.getText() : null;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800138 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 Dupin957e50c2017-10-10 11:23:27 -0700151 }
152
Lucas Dupin6bd86012017-12-05 17:58:57 -0800153 mClickActions.clear();
154 final int subItemsCount = subItems.size();
Lucas Dupina2a7a402018-01-12 17:45:51 -0800155 final int blendedColor = getTextColor();
Mady Mellor90e9fce2018-01-22 17:30:40 -0800156 final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it
157 for (int i = startIndex; i < subItemsCount; i++) {
Lucas Dupin6bd86012017-12-05 17:58:57 -0800158 SliceItem item = subItems.get(i);
Alan Viverettee8935882018-03-15 21:19:36 +0000159 RowContent rc = new RowContent(getContext(), item, true /* showStartItem */);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800160 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 Dupina2a7a402018-01-12 17:45:51 -0800165 button.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800166 button.setTag(itemTag);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700167 final int viewIndex = i - (mHasHeader ? 1 : 0);
168 mRow.addView(button, viewIndex);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800169 }
Lucas Dupin6bd86012017-12-05 17:58:57 -0800170
Mady Mellor90e9fce2018-01-22 17:30:40 -0800171 PendingIntent pendingIntent = null;
Alan Viverette85935282018-02-27 22:10:18 +0000172 if (rc.getPrimaryAction() != null) {
173 pendingIntent = rc.getPrimaryAction().getAction();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800174 }
175 mClickActions.put(button, pendingIntent);
176
Lucas Dupin1f7374a2018-02-26 18:08:33 -0800177 final SliceItem titleItem = rc.getTitleItem();
178 button.setText(titleItem == null ? null : titleItem.getText());
Lucas Dupin6bd86012017-12-05 17:58:57 -0800179
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 Dupinacb0eed2018-03-03 14:05:04 -0800189 button.setCompoundDrawables(iconDrawable, null, null, null);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800190 button.setOnClickListener(this);
Lucas Dupin2c0d52e2018-03-14 12:39:00 -0700191 button.setClickable(pendingIntent != null);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700192 }
193
Lucas Dupin6bd86012017-12-05 17:58:57 -0800194 // 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 Dupin2c5fce62018-03-05 19:06:00 -0800203 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 Dupin957e50c2017-10-10 11:23:27 -0700210 if (visibility != getVisibility()) {
211 setVisibility(visibility);
212 }
213 }
214
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800215 /**
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 Dupin957e50c2017-10-10 11:23:27 -0700255 public void setDark(float darkAmount) {
256 mDarkAmount = darkAmount;
257 updateTextColors();
258 }
259
Lucas Dupin957e50c2017-10-10 11:23:27 -0700260 private void updateTextColors() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800261 final int blendedColor = getTextColor();
Lucas Dupin957e50c2017-10-10 11:23:27 -0700262 mTitle.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800263 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 Dupin957e50c2017-10-10 11:23:27 -0700270 }
271
Lucas Dupin6bd86012017-12-05 17:58:57 -0800272 @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 Dupin6bd86012017-12-05 17:58:57 -0800322 }
323 }
324
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800325 @VisibleForTesting
326 int getTextColor() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800327 return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount);
328 }
329
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800330 @VisibleForTesting
331 void setTextColor(@ColorInt int textColor) {
332 mTextColor = textColor;
333 updateTextColors();
334 }
335
Lucas Dupin2c5fce62018-03-05 19:06:00 -0800336 public void setHideContent(boolean hideContent) {
337 mHideContent = hideContent;
338 updateVisibility();
339 }
340
Lucas Dupin4603fe22018-03-08 16:51:16 -0800341 public static class Row extends LinearLayout {
342
Lucas Dupinc68ec482018-03-30 14:24:53 -0700343 private static final long ROW_ANIM_DURATION = 350;
344 private final WakeLock mAnimationWakeLock;
345
Lucas Dupin4603fe22018-03-08 16:51:16 -0800346 public Row(Context context) {
Lucas Dupinc68ec482018-03-30 14:24:53 -0700347 this(context, null);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800348 }
349
350 public Row(Context context, AttributeSet attrs) {
Lucas Dupinc68ec482018-03-30 14:24:53 -0700351 this(context, attrs, 0);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800352 }
353
354 public Row(Context context, AttributeSet attrs, int defStyleAttr) {
Lucas Dupinc68ec482018-03-30 14:24:53 -0700355 this(context, attrs, defStyleAttr, 0);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800356 }
357
358 public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
359 super(context, attrs, defStyleAttr, defStyleRes);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700360 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 Dupin4603fe22018-03-08 16:51:16 -0800414 }
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 Dupin6bd86012017-12-05 17:58:57 -0800429 /**
430 * Representation of an item that appears under the clock on main keyguard message.
Lucas Dupin6bd86012017-12-05 17:58:57 -0800431 */
432 private class KeyguardSliceButton extends Button {
433
Lucas Dupin6bd86012017-12-05 17:58:57 -0800434 public KeyguardSliceButton(Context context) {
Lucas Dupinb16d8232018-01-26 12:44:52 -0800435 super(context, null /* attrs */, 0 /* styleAttr */,
Lucas Dupin6bd86012017-12-05 17:58:57 -0800436 com.android.keyguard.R.style.TextAppearance_Keyguard_Secondary);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800437 int horizontalPadding = (int) context.getResources()
438 .getDimension(R.dimen.widget_horizontal_padding);
Lucas Dupinb16d8232018-01-26 12:44:52 -0800439 setPadding(horizontalPadding / 2, 0, horizontalPadding / 2, 0);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800440 setCompoundDrawablePadding((int) context.getResources()
441 .getDimension(R.dimen.widget_icon_padding));
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800442 setMaxLines(1);
443 setEllipsize(TruncateAt.END);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800444 }
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800445
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 Dupin957e50c2017-10-10 11:23:27 -0700467 }
468}