blob: 10ba7f6704de4a34e4207c44cbaf8b398ff6fec2 [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 Dupinf8e274c2018-02-22 17:01:55 -080019import android.annotation.ColorInt;
Lucas Dupin957e50c2017-10-10 11:23:27 -070020import android.app.PendingIntent;
Lucas Dupin6bd86012017-12-05 17:58:57 -080021import android.arch.lifecycle.LiveData;
22import android.arch.lifecycle.Observer;
Lucas Dupin957e50c2017-10-10 11:23:27 -070023import android.content.Context;
Lucas Dupin957e50c2017-10-10 11:23:27 -070024import android.graphics.Color;
Lucas Dupin6bd86012017-12-05 17:58:57 -080025import android.graphics.drawable.Drawable;
Lucas Dupin957e50c2017-10-10 11:23:27 -070026import android.net.Uri;
Lucas Dupin6bd86012017-12-05 17:58:57 -080027import android.provider.Settings;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -080028import android.text.Layout;
29import android.text.TextUtils;
30import android.text.TextUtils.TruncateAt;
Lucas Dupin957e50c2017-10-10 11:23:27 -070031import android.util.AttributeSet;
Lucas Dupin6bd86012017-12-05 17:58:57 -080032import android.util.Log;
33import android.view.View;
34import android.widget.Button;
Lucas Dupin957e50c2017-10-10 11:23:27 -070035import android.widget.LinearLayout;
36import android.widget.TextView;
37
Lucas Dupinf8e274c2018-02-22 17:01:55 -080038import com.android.internal.annotations.VisibleForTesting;
Lucas Dupin957e50c2017-10-10 11:23:27 -070039import com.android.internal.graphics.ColorUtils;
Lucas Dupin6bd86012017-12-05 17:58:57 -080040import com.android.settingslib.Utils;
41import com.android.systemui.Dependency;
Lucas Dupin957e50c2017-10-10 11:23:27 -070042import com.android.systemui.R;
43import com.android.systemui.keyguard.KeyguardSliceProvider;
Lucas Dupin6bd86012017-12-05 17:58:57 -080044import com.android.systemui.tuner.TunerService;
Lucas Dupin957e50c2017-10-10 11:23:27 -070045
Lucas Dupin6bd86012017-12-05 17:58:57 -080046import java.util.HashMap;
47import java.util.List;
48import java.util.function.Consumer;
49
Alan Viverettee8935882018-03-15 21:19:36 +000050import androidx.slice.Slice;
51import androidx.slice.SliceItem;
52import androidx.slice.core.SliceQuery;
53import androidx.slice.widget.ListContent;
54import androidx.slice.widget.RowContent;
55import androidx.slice.widget.SliceLiveData;
Jason Monk2af19982017-11-07 19:38:27 -050056
Lucas Dupin957e50c2017-10-10 11:23:27 -070057/**
58 * View visible under the clock on the lock screen and AoD.
59 */
Lucas Dupin6bd86012017-12-05 17:58:57 -080060public class KeyguardSliceView extends LinearLayout implements View.OnClickListener,
61 Observer<Slice>, TunerService.Tunable {
Lucas Dupin957e50c2017-10-10 11:23:27 -070062
Lucas Dupin6bd86012017-12-05 17:58:57 -080063 private static final String TAG = "KeyguardSliceView";
64 private final HashMap<View, PendingIntent> mClickActions;
65 private Uri mKeyguardSliceUri;
Lucas Dupin957e50c2017-10-10 11:23:27 -070066 private TextView mTitle;
Lucas Dupin6bd86012017-12-05 17:58:57 -080067 private LinearLayout mRow;
Lucas Dupin957e50c2017-10-10 11:23:27 -070068 private int mTextColor;
69 private float mDarkAmount = 0;
70
Lucas Dupin6bd86012017-12-05 17:58:57 -080071 private LiveData<Slice> mLiveData;
72 private int mIconSize;
73 private Consumer<Boolean> mListener;
74 private boolean mHasHeader;
Lucas Dupin2c5fce62018-03-05 19:06:00 -080075 private boolean mHideContent;
Lucas Dupin957e50c2017-10-10 11:23:27 -070076
77 public KeyguardSliceView(Context context) {
78 this(context, null, 0);
79 }
80
81 public KeyguardSliceView(Context context, AttributeSet attrs) {
82 this(context, attrs, 0);
83 }
84
85 public KeyguardSliceView(Context context, AttributeSet attrs, int defStyle) {
86 super(context, attrs, defStyle);
Lucas Dupin6bd86012017-12-05 17:58:57 -080087
88 TunerService tunerService = Dependency.get(TunerService.class);
89 tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI);
90
91 mClickActions = new HashMap<>();
Lucas Dupin957e50c2017-10-10 11:23:27 -070092 }
93
94 @Override
95 protected void onFinishInflate() {
96 super.onFinishInflate();
97 mTitle = findViewById(R.id.title);
Lucas Dupin6bd86012017-12-05 17:58:57 -080098 mRow = findViewById(R.id.row);
99 mTextColor = Utils.getColorAttr(mContext, R.attr.wallpaperTextColor);
100 mIconSize = (int) mContext.getResources().getDimension(R.dimen.widget_icon_size);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700101 }
102
103 @Override
104 protected void onAttachedToWindow() {
105 super.onAttachedToWindow();
106
Lucas Dupin957e50c2017-10-10 11:23:27 -0700107 // Make sure we always have the most current slice
Lucas Dupin6bd86012017-12-05 17:58:57 -0800108 mLiveData.observeForever(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700109 }
110
111 @Override
112 protected void onDetachedFromWindow() {
113 super.onDetachedFromWindow();
114
Lucas Dupin6bd86012017-12-05 17:58:57 -0800115 mLiveData.removeObserver(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700116 }
117
118 private void showSlice(Slice slice) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700119
Alan Viverettee8935882018-03-15 21:19:36 +0000120 ListContent lc = new ListContent(getContext(), slice);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800121 mHasHeader = lc.hasHeader();
122 List<SliceItem> subItems = lc.getRowItems();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800123 if (!mHasHeader) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700124 mTitle.setVisibility(GONE);
125 } else {
126 mTitle.setVisibility(VISIBLE);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800127 // If there's a header it'll be the first subitem
Alan Viverettee8935882018-03-15 21:19:36 +0000128 RowContent header = new RowContent(getContext(), subItems.get(0),
129 true /* showStartItem */);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800130 SliceItem mainTitle = header.getTitleItem();
131 CharSequence title = mainTitle != null ? mainTitle.getText() : null;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800132 mTitle.setText(title);
133
134 // Check if we're already ellipsizing the text.
135 // We're going to figure out the best possible line break if not.
136 Layout layout = mTitle.getLayout();
137 if (layout != null){
138 final int lineCount = layout.getLineCount();
139 if (lineCount > 0) {
140 if (layout.getEllipsisCount(lineCount - 1) == 0) {
141 mTitle.setText(findBestLineBreak(title));
142 }
143 }
144 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700145 }
146
Lucas Dupin6bd86012017-12-05 17:58:57 -0800147 mClickActions.clear();
148 final int subItemsCount = subItems.size();
Lucas Dupina2a7a402018-01-12 17:45:51 -0800149 final int blendedColor = getTextColor();
Mady Mellor90e9fce2018-01-22 17:30:40 -0800150 final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it
151 for (int i = startIndex; i < subItemsCount; i++) {
Lucas Dupin6bd86012017-12-05 17:58:57 -0800152 SliceItem item = subItems.get(i);
Alan Viverettee8935882018-03-15 21:19:36 +0000153 RowContent rc = new RowContent(getContext(), item, true /* showStartItem */);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800154 final Uri itemTag = item.getSlice().getUri();
155 // Try to reuse the view if already exists in the layout
156 KeyguardSliceButton button = mRow.findViewWithTag(itemTag);
157 if (button == null) {
158 button = new KeyguardSliceButton(mContext);
Lucas Dupina2a7a402018-01-12 17:45:51 -0800159 button.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800160 button.setTag(itemTag);
161 } else {
162 mRow.removeView(button);
163 }
Lucas Dupinb16d8232018-01-26 12:44:52 -0800164 mRow.addView(button);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800165
Mady Mellor90e9fce2018-01-22 17:30:40 -0800166 PendingIntent pendingIntent = null;
Alan Viverette85935282018-02-27 22:10:18 +0000167 if (rc.getPrimaryAction() != null) {
168 pendingIntent = rc.getPrimaryAction().getAction();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800169 }
170 mClickActions.put(button, pendingIntent);
171
Lucas Dupin1f7374a2018-02-26 18:08:33 -0800172 final SliceItem titleItem = rc.getTitleItem();
173 button.setText(titleItem == null ? null : titleItem.getText());
Lucas Dupin6bd86012017-12-05 17:58:57 -0800174
175 Drawable iconDrawable = null;
176 SliceItem icon = SliceQuery.find(item.getSlice(),
177 android.app.slice.SliceItem.FORMAT_IMAGE);
178 if (icon != null) {
179 iconDrawable = icon.getIcon().loadDrawable(mContext);
180 final int width = (int) (iconDrawable.getIntrinsicWidth()
181 / (float) iconDrawable.getIntrinsicHeight() * mIconSize);
182 iconDrawable.setBounds(0, 0, Math.max(width, 1), mIconSize);
183 }
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800184 button.setCompoundDrawables(iconDrawable, null, null, null);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800185 button.setOnClickListener(this);
Lucas Dupin2c0d52e2018-03-14 12:39:00 -0700186 button.setClickable(pendingIntent != null);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700187 }
188
Lucas Dupin6bd86012017-12-05 17:58:57 -0800189 // Removing old views
190 for (int i = 0; i < mRow.getChildCount(); i++) {
191 View child = mRow.getChildAt(i);
192 if (!mClickActions.containsKey(child)) {
193 mRow.removeView(child);
194 i--;
195 }
196 }
197
Lucas Dupin2c5fce62018-03-05 19:06:00 -0800198 updateVisibility();
199 mListener.accept(mHasHeader);
200 }
201
202 private void updateVisibility() {
203 final boolean hasContent = mHasHeader || mRow.getChildCount() > 0;
204 final int visibility = hasContent && !mHideContent ? VISIBLE : GONE;
Lucas Dupin957e50c2017-10-10 11:23:27 -0700205 if (visibility != getVisibility()) {
206 setVisibility(visibility);
207 }
208 }
209
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800210 /**
211 * Breaks a string in 2 lines where both have similar character count
212 * but first line is always longer.
213 *
214 * @param charSequence Original text.
215 * @return Optimal string.
216 */
217 private CharSequence findBestLineBreak(CharSequence charSequence) {
218 if (TextUtils.isEmpty(charSequence)) {
219 return charSequence;
220 }
221
222 String source = charSequence.toString();
223 // Ignore if there is only 1 word,
224 // or if line breaks were manually set.
225 if (source.contains("\n") || !source.contains(" ")) {
226 return source;
227 }
228
229 final String[] words = source.split(" ");
230 final StringBuilder optimalString = new StringBuilder(source.length());
231 int current = 0;
232 while (optimalString.length() < source.length() - optimalString.length()) {
233 optimalString.append(words[current]);
234 if (current < words.length - 1) {
235 optimalString.append(" ");
236 }
237 current++;
238 }
239 optimalString.append("\n");
240 for (int i = current; i < words.length; i++) {
241 optimalString.append(words[i]);
242 if (current < words.length - 1) {
243 optimalString.append(" ");
244 }
245 }
246
247 return optimalString.toString();
248 }
249
Lucas Dupin957e50c2017-10-10 11:23:27 -0700250 public void setDark(float darkAmount) {
251 mDarkAmount = darkAmount;
252 updateTextColors();
253 }
254
Lucas Dupin957e50c2017-10-10 11:23:27 -0700255 private void updateTextColors() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800256 final int blendedColor = getTextColor();
Lucas Dupin957e50c2017-10-10 11:23:27 -0700257 mTitle.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800258 int childCount = mRow.getChildCount();
259 for (int i = 0; i < childCount; i++) {
260 View v = mRow.getChildAt(i);
261 if (v instanceof Button) {
262 ((Button) v).setTextColor(blendedColor);
263 }
264 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700265 }
266
Lucas Dupin6bd86012017-12-05 17:58:57 -0800267 @Override
268 public void onClick(View v) {
269 final PendingIntent action = mClickActions.get(v);
270 if (action != null) {
271 try {
272 action.send();
273 } catch (PendingIntent.CanceledException e) {
274 Log.i(TAG, "Pending intent cancelled, nothing to launch", e);
275 }
276 }
277 }
278
279 public void setListener(Consumer<Boolean> listener) {
280 mListener = listener;
281 }
282
283 public boolean hasHeader() {
284 return mHasHeader;
285 }
286
287 /**
288 * LiveData observer lifecycle.
289 * @param slice the new slice content.
290 */
291 @Override
292 public void onChanged(Slice slice) {
293 showSlice(slice);
294 }
295
296 @Override
297 public void onTuningChanged(String key, String newValue) {
298 setupUri(newValue);
299 }
300
301 public void setupUri(String uriString) {
302 if (uriString == null) {
303 uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI;
304 }
305
306 boolean wasObserving = false;
307 if (mLiveData != null && mLiveData.hasActiveObservers()) {
308 wasObserving = true;
309 mLiveData.removeObserver(this);
310 }
311
312 mKeyguardSliceUri = Uri.parse(uriString);
313 mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri);
314
315 if (wasObserving) {
316 mLiveData.observeForever(this);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800317 }
318 }
319
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800320 @VisibleForTesting
321 int getTextColor() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800322 return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount);
323 }
324
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800325 @VisibleForTesting
326 void setTextColor(@ColorInt int textColor) {
327 mTextColor = textColor;
328 updateTextColors();
329 }
330
Lucas Dupin2c5fce62018-03-05 19:06:00 -0800331 public void setHideContent(boolean hideContent) {
332 mHideContent = hideContent;
333 updateVisibility();
334 }
335
Lucas Dupin4603fe22018-03-08 16:51:16 -0800336 public static class Row extends LinearLayout {
337
338 public Row(Context context) {
339 super(context);
340 }
341
342 public Row(Context context, AttributeSet attrs) {
343 super(context, attrs);
344 }
345
346 public Row(Context context, AttributeSet attrs, int defStyleAttr) {
347 super(context, attrs, defStyleAttr);
348 }
349
350 public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
351 super(context, attrs, defStyleAttr, defStyleRes);
352 }
353
354 @Override
355 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
356 int width = MeasureSpec.getSize(widthMeasureSpec);
357 for (int i = 0; i < getChildCount(); i++) {
358 View child = getChildAt(i);
359 if (child instanceof KeyguardSliceButton) {
360 ((KeyguardSliceButton) child).setMaxWidth(width / 2);
361 }
362 }
363 super.onMeasure(widthMeasureSpec, heightMeasureSpec);
364 }
365 }
366
Lucas Dupin6bd86012017-12-05 17:58:57 -0800367 /**
368 * Representation of an item that appears under the clock on main keyguard message.
Lucas Dupin6bd86012017-12-05 17:58:57 -0800369 */
370 private class KeyguardSliceButton extends Button {
371
Lucas Dupin6bd86012017-12-05 17:58:57 -0800372 public KeyguardSliceButton(Context context) {
Lucas Dupinb16d8232018-01-26 12:44:52 -0800373 super(context, null /* attrs */, 0 /* styleAttr */,
Lucas Dupin6bd86012017-12-05 17:58:57 -0800374 com.android.keyguard.R.style.TextAppearance_Keyguard_Secondary);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800375 int horizontalPadding = (int) context.getResources()
376 .getDimension(R.dimen.widget_horizontal_padding);
Lucas Dupinb16d8232018-01-26 12:44:52 -0800377 setPadding(horizontalPadding / 2, 0, horizontalPadding / 2, 0);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800378 setCompoundDrawablePadding((int) context.getResources()
379 .getDimension(R.dimen.widget_icon_padding));
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800380 setMaxLines(1);
381 setEllipsize(TruncateAt.END);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800382 }
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800383
384 @Override
385 public void setTextColor(int color) {
386 super.setTextColor(color);
387 updateDrawableColors();
388 }
389
390 @Override
391 public void setCompoundDrawables(Drawable left, Drawable top, Drawable right,
392 Drawable bottom) {
393 super.setCompoundDrawables(left, top, right, bottom);
394 updateDrawableColors();
395 }
396
397 private void updateDrawableColors() {
398 final int color = getCurrentTextColor();
399 for (Drawable drawable : getCompoundDrawables()) {
400 if (drawable != null) {
401 drawable.setTint(color);
402 }
403 }
404 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700405 }
406}