blob: f61f585cfe7ce698e2dd8677b50fe93262af3634 [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
Aurimas Liutikasd0cec562018-08-15 11:03:53 -070019import static android.app.slice.Slice.HINT_LIST_ITEM;
wilsonshihe7903ea2018-09-26 16:17:59 +080020import static android.view.Display.DEFAULT_DISPLAY;
21import static android.view.Display.INVALID_DISPLAY;
Aurimas Liutikasd0cec562018-08-15 11:03:53 -070022
Lucas Dupinad079442019-04-03 13:14:11 -070023import static com.android.systemui.util.InjectionInflationController.VIEW_CONTEXT;
24
Lucas Dupinc68ec482018-03-30 14:24:53 -070025import android.animation.LayoutTransition;
26import android.animation.ObjectAnimator;
27import android.animation.PropertyValuesHolder;
Lucas Dupinf8e274c2018-02-22 17:01:55 -080028import android.annotation.ColorInt;
Fabian Kozynski648e5552018-12-18 16:37:08 -050029import android.annotation.StyleRes;
Lucas Dupin957e50c2017-10-10 11:23:27 -070030import android.app.PendingIntent;
Lucas Dupin957e50c2017-10-10 11:23:27 -070031import android.content.Context;
Lucas Dupin957e50c2017-10-10 11:23:27 -070032import android.graphics.Color;
Lucas Dupin6bd86012017-12-05 17:58:57 -080033import android.graphics.drawable.Drawable;
Lucas Dupin2b619602019-10-15 10:49:56 -070034import android.graphics.text.LineBreaker;
Lucas Dupin957e50c2017-10-10 11:23:27 -070035import android.net.Uri;
Lucas Dupina0d51192018-08-15 11:29:32 -070036import android.os.Trace;
Lucas Dupin6bd86012017-12-05 17:58:57 -080037import android.provider.Settings;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -080038import android.text.TextUtils;
39import android.text.TextUtils.TruncateAt;
Lucas Dupin957e50c2017-10-10 11:23:27 -070040import android.util.AttributeSet;
Lucas Dupin6bd86012017-12-05 17:58:57 -080041import android.util.Log;
Robert Snoeberger78a79d22019-05-07 16:47:07 -040042import android.util.TypedValue;
Lucas Dupin6bd86012017-12-05 17:58:57 -080043import android.view.View;
Lucas Dupinc68ec482018-03-30 14:24:53 -070044import android.view.animation.Animation;
Lucas Dupin6bd86012017-12-05 17:58:57 -080045import android.widget.Button;
Lucas Dupin957e50c2017-10-10 11:23:27 -070046import android.widget.LinearLayout;
Lucas Dupine570af62019-02-10 14:52:30 -080047import android.widget.TextView;
Lucas Dupin957e50c2017-10-10 11:23:27 -070048
Lucas Dupined002622018-10-25 18:12:15 -070049import androidx.lifecycle.LiveData;
50import androidx.lifecycle.Observer;
Alan Viverettee8935882018-03-15 21:19:36 +000051import androidx.slice.Slice;
52import androidx.slice.SliceItem;
Jason Monkf852f562018-05-23 16:08:11 -040053import androidx.slice.SliceViewManager;
Alan Viverettee8935882018-03-15 21:19:36 +000054import androidx.slice.core.SliceQuery;
55import androidx.slice.widget.ListContent;
56import androidx.slice.widget.RowContent;
Aurimas Liutikasd0cec562018-08-15 11:03:53 -070057import androidx.slice.widget.SliceContent;
Alan Viverettee8935882018-03-15 21:19:36 +000058import androidx.slice.widget.SliceLiveData;
Jason Monk2af19982017-11-07 19:38:27 -050059
Lucas Dupined002622018-10-25 18:12:15 -070060import com.android.internal.annotations.VisibleForTesting;
61import com.android.internal.graphics.ColorUtils;
62import com.android.settingslib.Utils;
63import com.android.systemui.Dependency;
64import com.android.systemui.Interpolators;
Lucas Dupine570af62019-02-10 14:52:30 -080065import com.android.systemui.R;
Lucas Dupined002622018-10-25 18:12:15 -070066import com.android.systemui.keyguard.KeyguardSliceProvider;
Lucas Dupinad079442019-04-03 13:14:11 -070067import com.android.systemui.plugins.ActivityStarter;
Lucas Dupined002622018-10-25 18:12:15 -070068import com.android.systemui.statusbar.policy.ConfigurationController;
69import com.android.systemui.tuner.TunerService;
70import com.android.systemui.util.wakelock.KeepAwakeAnimationListener;
71
Lucas Dupin43499522019-03-04 16:45:58 -080072import java.io.FileDescriptor;
73import java.io.PrintWriter;
Lucas Dupined002622018-10-25 18:12:15 -070074import java.util.ArrayList;
75import java.util.HashMap;
76import java.util.List;
77
Lucas Dupinad079442019-04-03 13:14:11 -070078import javax.inject.Inject;
79import javax.inject.Named;
80
Lucas Dupin957e50c2017-10-10 11:23:27 -070081/**
82 * View visible under the clock on the lock screen and AoD.
83 */
Lucas Dupin6bd86012017-12-05 17:58:57 -080084public class KeyguardSliceView extends LinearLayout implements View.OnClickListener,
Lucas Dupin4e023812018-04-02 21:19:23 -070085 Observer<Slice>, TunerService.Tunable, ConfigurationController.ConfigurationListener {
Lucas Dupin957e50c2017-10-10 11:23:27 -070086
Lucas Dupin6bd86012017-12-05 17:58:57 -080087 private static final String TAG = "KeyguardSliceView";
Lucas Dupin4e023812018-04-02 21:19:23 -070088 public static final int DEFAULT_ANIM_DURATION = 550;
89
Lucas Dupin6bd86012017-12-05 17:58:57 -080090 private final HashMap<View, PendingIntent> mClickActions;
Lucas Dupinad079442019-04-03 13:14:11 -070091 private final ActivityStarter mActivityStarter;
92 private final ConfigurationController mConfigurationController;
Robert Snoebergerdc8fee72019-07-19 16:35:35 -040093 private final LayoutTransition mLayoutTransition;
Lucas Dupin6bd86012017-12-05 17:58:57 -080094 private Uri mKeyguardSliceUri;
Lucas Dupine570af62019-02-10 14:52:30 -080095 @VisibleForTesting
96 TextView mTitle;
Lucas Dupin4e023812018-04-02 21:19:23 -070097 private Row mRow;
Lucas Dupin957e50c2017-10-10 11:23:27 -070098 private int mTextColor;
99 private float mDarkAmount = 0;
100
Lucas Dupin6bd86012017-12-05 17:58:57 -0800101 private LiveData<Slice> mLiveData;
wilsonshihe7903ea2018-09-26 16:17:59 +0800102 private int mDisplayId = INVALID_DISPLAY;
Lucas Dupin6bd86012017-12-05 17:58:57 -0800103 private int mIconSize;
Robert Snoeberger78a79d22019-05-07 16:47:07 -0400104 private int mIconSizeWithHeader;
Lucas Dupin4e023812018-04-02 21:19:23 -0700105 /**
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700106 * Runnable called whenever the view contents change.
Lucas Dupin4e023812018-04-02 21:19:23 -0700107 */
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700108 private Runnable mContentChangeListener;
Lucas Dupin4e023812018-04-02 21:19:23 -0700109 private Slice mSlice;
Lucas Dupin00be88f2019-01-03 17:50:52 -0800110 private boolean mHasHeader;
Lucas Dupine570af62019-02-10 14:52:30 -0800111 private final int mRowWithHeaderPadding;
112 private final int mRowPadding;
Robert Snoeberger78a79d22019-05-07 16:47:07 -0400113 private float mRowTextSize;
114 private float mRowWithHeaderTextSize;
Lucas Dupin957e50c2017-10-10 11:23:27 -0700115
Lucas Dupinad079442019-04-03 13:14:11 -0700116 @Inject
117 public KeyguardSliceView(@Named(VIEW_CONTEXT) Context context, AttributeSet attrs,
118 ActivityStarter activityStarter, ConfigurationController configurationController) {
119 super(context, attrs);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800120
121 TunerService tunerService = Dependency.get(TunerService.class);
122 tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI);
123
124 mClickActions = new HashMap<>();
Lucas Dupine570af62019-02-10 14:52:30 -0800125 mRowPadding = context.getResources().getDimensionPixelSize(R.dimen.subtitle_clock_padding);
126 mRowWithHeaderPadding = context.getResources()
127 .getDimensionPixelSize(R.dimen.header_subtitle_padding);
Lucas Dupinad079442019-04-03 13:14:11 -0700128 mActivityStarter = activityStarter;
129 mConfigurationController = configurationController;
Lucas Dupin4e023812018-04-02 21:19:23 -0700130
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400131 mLayoutTransition = new LayoutTransition();
132 mLayoutTransition.setStagger(LayoutTransition.CHANGE_APPEARING, DEFAULT_ANIM_DURATION / 2);
133 mLayoutTransition.setDuration(LayoutTransition.APPEARING, DEFAULT_ANIM_DURATION);
134 mLayoutTransition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 2);
135 mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_APPEARING);
136 mLayoutTransition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
137 mLayoutTransition.setInterpolator(LayoutTransition.APPEARING,
138 Interpolators.FAST_OUT_SLOW_IN);
139 mLayoutTransition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT);
140 mLayoutTransition.setAnimateParentHierarchy(false);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700141 }
142
Andy Wickhama72a8f82020-01-16 13:57:14 -0800143 // Temporary workaround to allow KeyguardStatusView to inflate a copy for Universal Smartspace.
144 // Eventually the existing copy will be reparented instead, and we won't need this.
145 public KeyguardSliceView(Context context, AttributeSet attributeSet) {
146 this(context, attributeSet, Dependency.get(ActivityStarter.class),
147 Dependency.get(ConfigurationController.class));
148 }
149
Lucas Dupin957e50c2017-10-10 11:23:27 -0700150 @Override
151 protected void onFinishInflate() {
152 super.onFinishInflate();
Lucas Dupine570af62019-02-10 14:52:30 -0800153 mTitle = findViewById(R.id.title);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800154 mRow = findViewById(R.id.row);
Jason Changb4e879d2018-04-11 11:17:58 +0800155 mTextColor = Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800156 mIconSize = (int) mContext.getResources().getDimension(R.dimen.widget_icon_size);
Robert Snoeberger78a79d22019-05-07 16:47:07 -0400157 mIconSizeWithHeader = (int) mContext.getResources().getDimension(R.dimen.header_icon_size);
158 mRowTextSize = mContext.getResources().getDimensionPixelSize(
159 R.dimen.widget_label_font_size);
160 mRowWithHeaderTextSize = mContext.getResources().getDimensionPixelSize(
161 R.dimen.header_row_font_size);
Lucas Dupinad079442019-04-03 13:14:11 -0700162 mTitle.setOnClickListener(this);
Lucas Dupin2b619602019-10-15 10:49:56 -0700163 mTitle.setBreakStrategy(LineBreaker.BREAK_STRATEGY_BALANCED);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700164 }
165
166 @Override
167 protected void onAttachedToWindow() {
168 super.onAttachedToWindow();
169
wilsonshihe7903ea2018-09-26 16:17:59 +0800170 mDisplayId = getDisplay().getDisplayId();
Lucas Dupin957e50c2017-10-10 11:23:27 -0700171 // Make sure we always have the most current slice
Lucas Dupin6bd86012017-12-05 17:58:57 -0800172 mLiveData.observeForever(this);
Lucas Dupinad079442019-04-03 13:14:11 -0700173 mConfigurationController.addCallback(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700174 }
175
176 @Override
177 protected void onDetachedFromWindow() {
178 super.onDetachedFromWindow();
179
wilsonshihe7903ea2018-09-26 16:17:59 +0800180 // TODO(b/117344873) Remove below work around after this issue be fixed.
181 if (mDisplayId == DEFAULT_DISPLAY) {
182 mLiveData.removeObserver(this);
183 }
Lucas Dupinad079442019-04-03 13:14:11 -0700184 mConfigurationController.removeCallback(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700185 }
186
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400187 @Override
188 public void onVisibilityAggregated(boolean isVisible) {
189 super.onVisibilityAggregated(isVisible);
190 setLayoutTransition(isVisible ? mLayoutTransition : null);
191 }
192
Lucas Dupin00be88f2019-01-03 17:50:52 -0800193 /**
194 * Returns whether the current visible slice has a title/header.
195 */
196 public boolean hasHeader() {
197 return mHasHeader;
198 }
199
Lucas Dupin4e023812018-04-02 21:19:23 -0700200 private void showSlice() {
Lucas Dupina0d51192018-08-15 11:29:32 -0700201 Trace.beginSection("KeyguardSliceView#showSlice");
Lucas Dupin00be88f2019-01-03 17:50:52 -0800202 if (mSlice == null) {
Lucas Dupine570af62019-02-10 14:52:30 -0800203 mTitle.setVisibility(GONE);
Lucas Dupin4e023812018-04-02 21:19:23 -0700204 mRow.setVisibility(GONE);
Lucas Dupin00be88f2019-01-03 17:50:52 -0800205 mHasHeader = false;
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700206 if (mContentChangeListener != null) {
207 mContentChangeListener.run();
208 }
Lucas Dupin43499522019-03-04 16:45:58 -0800209 Trace.endSection();
Lucas Dupin4e023812018-04-02 21:19:23 -0700210 return;
211 }
Lucas Dupinad079442019-04-03 13:14:11 -0700212 mClickActions.clear();
Lucas Dupin957e50c2017-10-10 11:23:27 -0700213
Aurimas Liutikasd0cec562018-08-15 11:03:53 -0700214 ListContent lc = new ListContent(getContext(), mSlice);
215 SliceContent headerContent = lc.getHeader();
Lucas Dupine570af62019-02-10 14:52:30 -0800216 mHasHeader = headerContent != null && !headerContent.getSliceItem().hasHint(HINT_LIST_ITEM);
Lucas Dupined002622018-10-25 18:12:15 -0700217 List<SliceContent> subItems = new ArrayList<>();
Mady Mellor78f04ef2018-05-23 15:04:47 -0700218 for (int i = 0; i < lc.getRowItems().size(); i++) {
Aurimas Liutikasd0cec562018-08-15 11:03:53 -0700219 SliceContent subItem = lc.getRowItems().get(i);
220 String itemUri = subItem.getSliceItem().getSlice().getUri().toString();
Mady Mellor78f04ef2018-05-23 15:04:47 -0700221 // Filter out the action row
222 if (!KeyguardSliceProvider.KEYGUARD_ACTION_URI.equals(itemUri)) {
223 subItems.add(subItem);
224 }
225 }
Lucas Dupine570af62019-02-10 14:52:30 -0800226 if (!mHasHeader) {
227 mTitle.setVisibility(GONE);
228 } else {
229 mTitle.setVisibility(VISIBLE);
230
231 RowContent header = lc.getHeader();
232 SliceItem mainTitle = header.getTitleItem();
233 CharSequence title = mainTitle != null ? mainTitle.getText() : null;
234 mTitle.setText(title);
Lucas Dupinad079442019-04-03 13:14:11 -0700235 if (header.getPrimaryAction() != null
236 && header.getPrimaryAction().getAction() != null) {
237 mClickActions.put(mTitle, header.getPrimaryAction().getAction());
238 }
Lucas Dupine570af62019-02-10 14:52:30 -0800239 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700240
Lucas Dupin6bd86012017-12-05 17:58:57 -0800241 final int subItemsCount = subItems.size();
Lucas Dupina2a7a402018-01-12 17:45:51 -0800242 final int blendedColor = getTextColor();
Lucas Dupin00be88f2019-01-03 17:50:52 -0800243 final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it
Lucas Dupin4e023812018-04-02 21:19:23 -0700244 mRow.setVisibility(subItemsCount > 0 ? VISIBLE : GONE);
Lucas Dupine570af62019-02-10 14:52:30 -0800245 LinearLayout.LayoutParams layoutParams = (LayoutParams) mRow.getLayoutParams();
246 layoutParams.topMargin = mHasHeader ? mRowWithHeaderPadding : mRowPadding;
247 mRow.setLayoutParams(layoutParams);
248
Mady Mellor90e9fce2018-01-22 17:30:40 -0800249 for (int i = startIndex; i < subItemsCount; i++) {
Aurimas Liutikasd0cec562018-08-15 11:03:53 -0700250 RowContent rc = (RowContent) subItems.get(i);
251 SliceItem item = rc.getSliceItem();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800252 final Uri itemTag = item.getSlice().getUri();
253 // Try to reuse the view if already exists in the layout
254 KeyguardSliceButton button = mRow.findViewWithTag(itemTag);
255 if (button == null) {
256 button = new KeyguardSliceButton(mContext);
Lucas Dupina2a7a402018-01-12 17:45:51 -0800257 button.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800258 button.setTag(itemTag);
Lucas Dupin00be88f2019-01-03 17:50:52 -0800259 final int viewIndex = i - (mHasHeader ? 1 : 0);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700260 mRow.addView(button, viewIndex);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800261 }
Lucas Dupin6bd86012017-12-05 17:58:57 -0800262
Mady Mellor90e9fce2018-01-22 17:30:40 -0800263 PendingIntent pendingIntent = null;
Alan Viverette85935282018-02-27 22:10:18 +0000264 if (rc.getPrimaryAction() != null) {
265 pendingIntent = rc.getPrimaryAction().getAction();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800266 }
267 mClickActions.put(button, pendingIntent);
268
Lucas Dupin1f7374a2018-02-26 18:08:33 -0800269 final SliceItem titleItem = rc.getTitleItem();
270 button.setText(titleItem == null ? null : titleItem.getText());
Lucas Dupin1d5e9102018-05-21 17:58:05 -0700271 button.setContentDescription(rc.getContentDescription());
Robert Snoeberger78a79d22019-05-07 16:47:07 -0400272 button.setTextSize(TypedValue.COMPLEX_UNIT_PX,
273 mHasHeader ? mRowWithHeaderTextSize : mRowTextSize);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800274
275 Drawable iconDrawable = null;
276 SliceItem icon = SliceQuery.find(item.getSlice(),
277 android.app.slice.SliceItem.FORMAT_IMAGE);
278 if (icon != null) {
Robert Snoeberger78a79d22019-05-07 16:47:07 -0400279 final int iconSize = mHasHeader ? mIconSizeWithHeader : mIconSize;
Lucas Dupin6bd86012017-12-05 17:58:57 -0800280 iconDrawable = icon.getIcon().loadDrawable(mContext);
Lucas Dupin2955bf12018-12-26 10:15:51 -0800281 if (iconDrawable != null) {
282 final int width = (int) (iconDrawable.getIntrinsicWidth()
Robert Snoeberger78a79d22019-05-07 16:47:07 -0400283 / (float) iconDrawable.getIntrinsicHeight() * iconSize);
284 iconDrawable.setBounds(0, 0, Math.max(width, 1), iconSize);
Lucas Dupin2955bf12018-12-26 10:15:51 -0800285 }
Lucas Dupin6bd86012017-12-05 17:58:57 -0800286 }
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800287 button.setCompoundDrawables(iconDrawable, null, null, null);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800288 button.setOnClickListener(this);
Lucas Dupin2c0d52e2018-03-14 12:39:00 -0700289 button.setClickable(pendingIntent != null);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700290 }
291
Lucas Dupin6bd86012017-12-05 17:58:57 -0800292 // Removing old views
293 for (int i = 0; i < mRow.getChildCount(); i++) {
294 View child = mRow.getChildAt(i);
295 if (!mClickActions.containsKey(child)) {
296 mRow.removeView(child);
297 i--;
298 }
299 }
300
Lucas Dupin4e023812018-04-02 21:19:23 -0700301 if (mContentChangeListener != null) {
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700302 mContentChangeListener.run();
Lucas Dupin4e023812018-04-02 21:19:23 -0700303 }
Lucas Dupina0d51192018-08-15 11:29:32 -0700304 Trace.endSection();
Lucas Dupin2c5fce62018-03-05 19:06:00 -0800305 }
306
Lucas Dupin4e023812018-04-02 21:19:23 -0700307 public void setDarkAmount(float darkAmount) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700308 mDarkAmount = darkAmount;
Lucas Dupin4e023812018-04-02 21:19:23 -0700309 mRow.setDarkAmount(darkAmount);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700310 updateTextColors();
311 }
312
Lucas Dupin957e50c2017-10-10 11:23:27 -0700313 private void updateTextColors() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800314 final int blendedColor = getTextColor();
Lucas Dupine570af62019-02-10 14:52:30 -0800315 mTitle.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800316 int childCount = mRow.getChildCount();
317 for (int i = 0; i < childCount; i++) {
318 View v = mRow.getChildAt(i);
319 if (v instanceof Button) {
320 ((Button) v).setTextColor(blendedColor);
321 }
322 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700323 }
324
Lucas Dupin6bd86012017-12-05 17:58:57 -0800325 @Override
326 public void onClick(View v) {
327 final PendingIntent action = mClickActions.get(v);
328 if (action != null) {
Lucas Dupinad079442019-04-03 13:14:11 -0700329 mActivityStarter.startPendingIntentDismissingKeyguard(action);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800330 }
331 }
332
Lucas Dupin4e023812018-04-02 21:19:23 -0700333 /**
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700334 * Runnable that gets invoked every time the title or the row visibility changes.
Lucas Dupin4e023812018-04-02 21:19:23 -0700335 * @param contentChangeListener The listener.
336 */
Lucas Dupin3978e6e2018-05-22 18:08:35 -0700337 public void setContentChangeListener(Runnable contentChangeListener) {
Lucas Dupin4e023812018-04-02 21:19:23 -0700338 mContentChangeListener = contentChangeListener;
Lucas Dupin6bd86012017-12-05 17:58:57 -0800339 }
340
Lucas Dupin6bd86012017-12-05 17:58:57 -0800341 /**
342 * LiveData observer lifecycle.
343 * @param slice the new slice content.
344 */
345 @Override
346 public void onChanged(Slice slice) {
Lucas Dupin4e023812018-04-02 21:19:23 -0700347 mSlice = slice;
348 showSlice();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800349 }
350
351 @Override
352 public void onTuningChanged(String key, String newValue) {
353 setupUri(newValue);
354 }
355
Lucas Dupine570af62019-02-10 14:52:30 -0800356 /**
357 * Sets the slice provider Uri.
358 */
359 public void setupUri(String uriString) {
Lucas Dupin6bd86012017-12-05 17:58:57 -0800360 if (uriString == null) {
361 uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI;
362 }
363
364 boolean wasObserving = false;
365 if (mLiveData != null && mLiveData.hasActiveObservers()) {
366 wasObserving = true;
367 mLiveData.removeObserver(this);
368 }
369
370 mKeyguardSliceUri = Uri.parse(uriString);
371 mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri);
372
373 if (wasObserving) {
374 mLiveData.observeForever(this);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800375 }
376 }
377
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800378 @VisibleForTesting
379 int getTextColor() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800380 return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount);
381 }
382
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800383 @VisibleForTesting
384 void setTextColor(@ColorInt int textColor) {
385 mTextColor = textColor;
386 updateTextColors();
387 }
388
Lucas Dupin4e023812018-04-02 21:19:23 -0700389 @Override
390 public void onDensityOrFontScaleChanged() {
391 mIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.widget_icon_size);
Robert Snoeberger78a79d22019-05-07 16:47:07 -0400392 mIconSizeWithHeader = (int) mContext.getResources().getDimension(R.dimen.header_icon_size);
393 mRowTextSize = mContext.getResources().getDimensionPixelSize(
394 R.dimen.widget_label_font_size);
395 mRowWithHeaderTextSize = mContext.getResources().getDimensionPixelSize(
396 R.dimen.header_row_font_size);
Lucas Dupin2c5fce62018-03-05 19:06:00 -0800397 }
398
Lucas Dupin9fedb892018-05-04 17:42:33 -0700399 public void refresh() {
Lucas Dupina0d51192018-08-15 11:29:32 -0700400 Slice slice;
401 Trace.beginSection("KeyguardSliceView#refresh");
402 // We can optimize performance and avoid binder calls when we know that we're bound
403 // to a Slice on the same process.
404 if (KeyguardSliceProvider.KEYGUARD_SLICE_URI.equals(mKeyguardSliceUri.toString())) {
405 KeyguardSliceProvider instance = KeyguardSliceProvider.getAttachedInstance();
406 if (instance != null) {
407 slice = instance.onBindSlice(mKeyguardSliceUri);
408 } else {
409 Log.w(TAG, "Keyguard slice not bound yet?");
410 slice = null;
411 }
412 } else {
413 slice = SliceViewManager.getInstance(getContext()).bindSlice(mKeyguardSliceUri);
414 }
Lucas Dupin9fedb892018-05-04 17:42:33 -0700415 onChanged(slice);
Lucas Dupina0d51192018-08-15 11:29:32 -0700416 Trace.endSection();
Lucas Dupin9fedb892018-05-04 17:42:33 -0700417 }
418
Lucas Dupin43499522019-03-04 16:45:58 -0800419 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
420 pw.println("KeyguardSliceView:");
421 pw.println(" mClickActions: " + mClickActions);
422 pw.println(" mTitle: " + (mTitle == null ? "null" : mTitle.getVisibility() == VISIBLE));
423 pw.println(" mRow: " + (mRow == null ? "null" : mRow.getVisibility() == VISIBLE));
424 pw.println(" mTextColor: " + Integer.toHexString(mTextColor));
425 pw.println(" mDarkAmount: " + mDarkAmount);
426 pw.println(" mSlice: " + mSlice);
427 pw.println(" mHasHeader: " + mHasHeader);
428 }
429
Lucas Dupin4603fe22018-03-08 16:51:16 -0800430 public static class Row extends LinearLayout {
431
Lucas Dupin4e023812018-04-02 21:19:23 -0700432 /**
433 * This view is visible in AOD, which means that the device will sleep if we
434 * don't hold a wake lock. We want to enter doze only after all views have reached
435 * their desired positions.
436 */
437 private final Animation.AnimationListener mKeepAwakeListener;
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400438 private LayoutTransition mLayoutTransition;
Lucas Dupin4e023812018-04-02 21:19:23 -0700439 private float mDarkAmount;
Lucas Dupinc68ec482018-03-30 14:24:53 -0700440
Lucas Dupin4603fe22018-03-08 16:51:16 -0800441 public Row(Context context) {
Lucas Dupinc68ec482018-03-30 14:24:53 -0700442 this(context, null);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800443 }
444
445 public Row(Context context, AttributeSet attrs) {
Lucas Dupinc68ec482018-03-30 14:24:53 -0700446 this(context, attrs, 0);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800447 }
448
449 public Row(Context context, AttributeSet attrs, int defStyleAttr) {
Lucas Dupinc68ec482018-03-30 14:24:53 -0700450 this(context, attrs, defStyleAttr, 0);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800451 }
452
453 public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
454 super(context, attrs, defStyleAttr, defStyleRes);
Lucas Dupin4e023812018-04-02 21:19:23 -0700455 mKeepAwakeListener = new KeepAwakeAnimationListener(mContext);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700456 }
457
458 @Override
459 protected void onFinishInflate() {
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400460 mLayoutTransition = new LayoutTransition();
461 mLayoutTransition.setDuration(DEFAULT_ANIM_DURATION);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700462
463 PropertyValuesHolder left = PropertyValuesHolder.ofInt("left", 0, 1);
464 PropertyValuesHolder right = PropertyValuesHolder.ofInt("right", 0, 1);
465 ObjectAnimator changeAnimator = ObjectAnimator.ofPropertyValuesHolder((Object) null,
466 left, right);
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400467 mLayoutTransition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeAnimator);
468 mLayoutTransition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeAnimator);
469 mLayoutTransition.setInterpolator(LayoutTransition.CHANGE_APPEARING,
Lucas Dupinc68ec482018-03-30 14:24:53 -0700470 Interpolators.ACCELERATE_DECELERATE);
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400471 mLayoutTransition.setInterpolator(LayoutTransition.CHANGE_DISAPPEARING,
Lucas Dupinc68ec482018-03-30 14:24:53 -0700472 Interpolators.ACCELERATE_DECELERATE);
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400473 mLayoutTransition.setStartDelay(LayoutTransition.CHANGE_APPEARING,
474 DEFAULT_ANIM_DURATION);
475 mLayoutTransition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING,
476 DEFAULT_ANIM_DURATION);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700477
478 ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f);
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400479 mLayoutTransition.setAnimator(LayoutTransition.APPEARING, appearAnimator);
480 mLayoutTransition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700481
482 ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f);
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400483 mLayoutTransition.setInterpolator(LayoutTransition.DISAPPEARING,
484 Interpolators.ALPHA_OUT);
485 mLayoutTransition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 4);
486 mLayoutTransition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator);
Lucas Dupinc68ec482018-03-30 14:24:53 -0700487
Robert Snoebergerdc8fee72019-07-19 16:35:35 -0400488 mLayoutTransition.setAnimateParentHierarchy(false);
489 }
490
491 @Override
492 public void onVisibilityAggregated(boolean isVisible) {
493 super.onVisibilityAggregated(isVisible);
494 setLayoutTransition(isVisible ? mLayoutTransition : null);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800495 }
496
497 @Override
498 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
499 int width = MeasureSpec.getSize(widthMeasureSpec);
Lucas Dupin8b77a3b2018-05-01 13:23:03 -0700500 int childCount = getChildCount();
501 for (int i = 0; i < childCount; i++) {
Lucas Dupin4603fe22018-03-08 16:51:16 -0800502 View child = getChildAt(i);
503 if (child instanceof KeyguardSliceButton) {
Lucas Dupin8b77a3b2018-05-01 13:23:03 -0700504 ((KeyguardSliceButton) child).setMaxWidth(width / childCount);
Lucas Dupin4603fe22018-03-08 16:51:16 -0800505 }
506 }
507 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
508 }
Lucas Dupin4e023812018-04-02 21:19:23 -0700509
510 public void setDarkAmount(float darkAmount) {
511 boolean isAwake = darkAmount != 0;
512 boolean wasAwake = mDarkAmount != 0;
513 if (isAwake == wasAwake) {
514 return;
515 }
516 mDarkAmount = darkAmount;
517 setLayoutAnimationListener(isAwake ? null : mKeepAwakeListener);
518 }
519
520 @Override
521 public boolean hasOverlappingRendering() {
522 return false;
523 }
Lucas Dupin4603fe22018-03-08 16:51:16 -0800524 }
525
Lucas Dupin6bd86012017-12-05 17:58:57 -0800526 /**
527 * Representation of an item that appears under the clock on main keyguard message.
Lucas Dupin6bd86012017-12-05 17:58:57 -0800528 */
Lucas Dupin4e023812018-04-02 21:19:23 -0700529 @VisibleForTesting
530 static class KeyguardSliceButton extends Button implements
531 ConfigurationController.ConfigurationListener {
Lucas Dupin6bd86012017-12-05 17:58:57 -0800532
Fabian Kozynski648e5552018-12-18 16:37:08 -0500533 @StyleRes
534 private static int sStyleId = R.style.TextAppearance_Keyguard_Secondary;
535
Lucas Dupin6bd86012017-12-05 17:58:57 -0800536 public KeyguardSliceButton(Context context) {
Fabian Kozynski648e5552018-12-18 16:37:08 -0500537 super(context, null /* attrs */, 0 /* styleAttr */, sStyleId);
Lucas Dupin4e023812018-04-02 21:19:23 -0700538 onDensityOrFontScaleChanged();
539 setEllipsize(TruncateAt.END);
540 }
541
542 @Override
543 protected void onAttachedToWindow() {
544 super.onAttachedToWindow();
545 Dependency.get(ConfigurationController.class).addCallback(this);
546 }
547
548 @Override
549 protected void onDetachedFromWindow() {
550 super.onDetachedFromWindow();
551 Dependency.get(ConfigurationController.class).removeCallback(this);
552 }
553
554 @Override
555 public void onDensityOrFontScaleChanged() {
Lucas Dupin8b77a3b2018-05-01 13:23:03 -0700556 updatePadding();
557 }
558
559 @Override
Fabian Kozynski648e5552018-12-18 16:37:08 -0500560 public void onOverlayChanged() {
561 setTextAppearance(sStyleId);
562 }
563
564 @Override
Lucas Dupin8b77a3b2018-05-01 13:23:03 -0700565 public void setText(CharSequence text, BufferType type) {
566 super.setText(text, type);
567 updatePadding();
568 }
569
570 private void updatePadding() {
571 boolean hasText = !TextUtils.isEmpty(getText());
572 int horizontalPadding = (int) getContext().getResources()
573 .getDimension(R.dimen.widget_horizontal_padding) / 2;
574 setPadding(horizontalPadding, 0, horizontalPadding * (hasText ? 1 : -1), 0);
Lucas Dupin4e023812018-04-02 21:19:23 -0700575 setCompoundDrawablePadding((int) mContext.getResources()
Lucas Dupin6bd86012017-12-05 17:58:57 -0800576 .getDimension(R.dimen.widget_icon_padding));
577 }
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800578
579 @Override
580 public void setTextColor(int color) {
581 super.setTextColor(color);
582 updateDrawableColors();
583 }
584
585 @Override
586 public void setCompoundDrawables(Drawable left, Drawable top, Drawable right,
587 Drawable bottom) {
588 super.setCompoundDrawables(left, top, right, bottom);
589 updateDrawableColors();
Lucas Dupin8b77a3b2018-05-01 13:23:03 -0700590 updatePadding();
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800591 }
592
593 private void updateDrawableColors() {
594 final int color = getCurrentTextColor();
595 for (Drawable drawable : getCompoundDrawables()) {
596 if (drawable != null) {
597 drawable.setTint(color);
598 }
599 }
600 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700601 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700602}