blob: 7572512eac3147067fd883152226df6bf38c3338 [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
50import androidx.app.slice.Slice;
51import androidx.app.slice.SliceItem;
52import androidx.app.slice.core.SliceQuery;
Mady Mellor90e9fce2018-01-22 17:30:40 -080053import androidx.app.slice.widget.ListContent;
54import androidx.app.slice.widget.RowContent;
Lucas Dupin6bd86012017-12-05 17:58:57 -080055import androidx.app.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 Dupin957e50c2017-10-10 11:23:27 -070075
76 public KeyguardSliceView(Context context) {
77 this(context, null, 0);
78 }
79
80 public KeyguardSliceView(Context context, AttributeSet attrs) {
81 this(context, attrs, 0);
82 }
83
84 public KeyguardSliceView(Context context, AttributeSet attrs, int defStyle) {
85 super(context, attrs, defStyle);
Lucas Dupin6bd86012017-12-05 17:58:57 -080086
87 TunerService tunerService = Dependency.get(TunerService.class);
88 tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI);
89
90 mClickActions = new HashMap<>();
Lucas Dupin957e50c2017-10-10 11:23:27 -070091 }
92
93 @Override
94 protected void onFinishInflate() {
95 super.onFinishInflate();
96 mTitle = findViewById(R.id.title);
Lucas Dupin6bd86012017-12-05 17:58:57 -080097 mRow = findViewById(R.id.row);
98 mTextColor = Utils.getColorAttr(mContext, R.attr.wallpaperTextColor);
99 mIconSize = (int) mContext.getResources().getDimension(R.dimen.widget_icon_size);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700100 }
101
102 @Override
103 protected void onAttachedToWindow() {
104 super.onAttachedToWindow();
105
Lucas Dupin957e50c2017-10-10 11:23:27 -0700106 // Make sure we always have the most current slice
Lucas Dupin6bd86012017-12-05 17:58:57 -0800107 mLiveData.observeForever(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700108 }
109
110 @Override
111 protected void onDetachedFromWindow() {
112 super.onDetachedFromWindow();
113
Lucas Dupin6bd86012017-12-05 17:58:57 -0800114 mLiveData.removeObserver(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700115 }
116
117 private void showSlice(Slice slice) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700118
Mady Mellor90e9fce2018-01-22 17:30:40 -0800119 ListContent lc = new ListContent(slice);
120 mHasHeader = lc.hasHeader();
121 List<SliceItem> subItems = lc.getRowItems();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800122 if (!mHasHeader) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700123 mTitle.setVisibility(GONE);
124 } else {
125 mTitle.setVisibility(VISIBLE);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800126 // If there's a header it'll be the first subitem
127 RowContent header = new RowContent(subItems.get(0), true /* showStartItem */);
128 SliceItem mainTitle = header.getTitleItem();
129 CharSequence title = mainTitle != null ? mainTitle.getText() : null;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800130 mTitle.setText(title);
131
132 // Check if we're already ellipsizing the text.
133 // We're going to figure out the best possible line break if not.
134 Layout layout = mTitle.getLayout();
135 if (layout != null){
136 final int lineCount = layout.getLineCount();
137 if (lineCount > 0) {
138 if (layout.getEllipsisCount(lineCount - 1) == 0) {
139 mTitle.setText(findBestLineBreak(title));
140 }
141 }
142 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700143 }
144
Lucas Dupin6bd86012017-12-05 17:58:57 -0800145 mClickActions.clear();
146 final int subItemsCount = subItems.size();
Lucas Dupina2a7a402018-01-12 17:45:51 -0800147 final int blendedColor = getTextColor();
Mady Mellor90e9fce2018-01-22 17:30:40 -0800148 final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it
149 for (int i = startIndex; i < subItemsCount; i++) {
Lucas Dupin6bd86012017-12-05 17:58:57 -0800150 SliceItem item = subItems.get(i);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800151 RowContent rc = new RowContent(item, true /* showStartItem */);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800152 final Uri itemTag = item.getSlice().getUri();
153 // Try to reuse the view if already exists in the layout
154 KeyguardSliceButton button = mRow.findViewWithTag(itemTag);
155 if (button == null) {
156 button = new KeyguardSliceButton(mContext);
Lucas Dupina2a7a402018-01-12 17:45:51 -0800157 button.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800158 button.setTag(itemTag);
159 } else {
160 mRow.removeView(button);
161 }
Lucas Dupinb16d8232018-01-26 12:44:52 -0800162 mRow.addView(button);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800163
Mady Mellor90e9fce2018-01-22 17:30:40 -0800164 PendingIntent pendingIntent = null;
Alan Viverette85935282018-02-27 22:10:18 +0000165 if (rc.getPrimaryAction() != null) {
166 pendingIntent = rc.getPrimaryAction().getAction();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800167 }
168 mClickActions.put(button, pendingIntent);
169
Lucas Dupin1f7374a2018-02-26 18:08:33 -0800170 final SliceItem titleItem = rc.getTitleItem();
171 button.setText(titleItem == null ? null : titleItem.getText());
Lucas Dupin6bd86012017-12-05 17:58:57 -0800172
173 Drawable iconDrawable = null;
174 SliceItem icon = SliceQuery.find(item.getSlice(),
175 android.app.slice.SliceItem.FORMAT_IMAGE);
176 if (icon != null) {
177 iconDrawable = icon.getIcon().loadDrawable(mContext);
178 final int width = (int) (iconDrawable.getIntrinsicWidth()
179 / (float) iconDrawable.getIntrinsicHeight() * mIconSize);
180 iconDrawable.setBounds(0, 0, Math.max(width, 1), mIconSize);
181 }
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800182 button.setCompoundDrawables(iconDrawable, null, null, null);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800183 button.setOnClickListener(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700184 }
185
Lucas Dupin6bd86012017-12-05 17:58:57 -0800186 // Removing old views
187 for (int i = 0; i < mRow.getChildCount(); i++) {
188 View child = mRow.getChildAt(i);
189 if (!mClickActions.containsKey(child)) {
190 mRow.removeView(child);
191 i--;
192 }
193 }
194
195 final int visibility = mHasHeader || subItemsCount > 0 ? VISIBLE : GONE;
Lucas Dupin957e50c2017-10-10 11:23:27 -0700196 if (visibility != getVisibility()) {
197 setVisibility(visibility);
198 }
Lucas Dupin6bd86012017-12-05 17:58:57 -0800199
200 mListener.accept(mHasHeader);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700201 }
202
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800203 /**
204 * Breaks a string in 2 lines where both have similar character count
205 * but first line is always longer.
206 *
207 * @param charSequence Original text.
208 * @return Optimal string.
209 */
210 private CharSequence findBestLineBreak(CharSequence charSequence) {
211 if (TextUtils.isEmpty(charSequence)) {
212 return charSequence;
213 }
214
215 String source = charSequence.toString();
216 // Ignore if there is only 1 word,
217 // or if line breaks were manually set.
218 if (source.contains("\n") || !source.contains(" ")) {
219 return source;
220 }
221
222 final String[] words = source.split(" ");
223 final StringBuilder optimalString = new StringBuilder(source.length());
224 int current = 0;
225 while (optimalString.length() < source.length() - optimalString.length()) {
226 optimalString.append(words[current]);
227 if (current < words.length - 1) {
228 optimalString.append(" ");
229 }
230 current++;
231 }
232 optimalString.append("\n");
233 for (int i = current; i < words.length; i++) {
234 optimalString.append(words[i]);
235 if (current < words.length - 1) {
236 optimalString.append(" ");
237 }
238 }
239
240 return optimalString.toString();
241 }
242
Lucas Dupin957e50c2017-10-10 11:23:27 -0700243 public void setDark(float darkAmount) {
244 mDarkAmount = darkAmount;
245 updateTextColors();
246 }
247
Lucas Dupin957e50c2017-10-10 11:23:27 -0700248 private void updateTextColors() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800249 final int blendedColor = getTextColor();
Lucas Dupin957e50c2017-10-10 11:23:27 -0700250 mTitle.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800251 int childCount = mRow.getChildCount();
252 for (int i = 0; i < childCount; i++) {
253 View v = mRow.getChildAt(i);
254 if (v instanceof Button) {
255 ((Button) v).setTextColor(blendedColor);
256 }
257 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700258 }
259
Lucas Dupin6bd86012017-12-05 17:58:57 -0800260 @Override
261 public void onClick(View v) {
262 final PendingIntent action = mClickActions.get(v);
263 if (action != null) {
264 try {
265 action.send();
266 } catch (PendingIntent.CanceledException e) {
267 Log.i(TAG, "Pending intent cancelled, nothing to launch", e);
268 }
269 }
270 }
271
272 public void setListener(Consumer<Boolean> listener) {
273 mListener = listener;
274 }
275
276 public boolean hasHeader() {
277 return mHasHeader;
278 }
279
280 /**
281 * LiveData observer lifecycle.
282 * @param slice the new slice content.
283 */
284 @Override
285 public void onChanged(Slice slice) {
286 showSlice(slice);
287 }
288
289 @Override
290 public void onTuningChanged(String key, String newValue) {
291 setupUri(newValue);
292 }
293
294 public void setupUri(String uriString) {
295 if (uriString == null) {
296 uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI;
297 }
298
299 boolean wasObserving = false;
300 if (mLiveData != null && mLiveData.hasActiveObservers()) {
301 wasObserving = true;
302 mLiveData.removeObserver(this);
303 }
304
305 mKeyguardSliceUri = Uri.parse(uriString);
306 mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri);
307
308 if (wasObserving) {
309 mLiveData.observeForever(this);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800310 }
311 }
312
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800313 @VisibleForTesting
314 int getTextColor() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800315 return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount);
316 }
317
Lucas Dupinf8e274c2018-02-22 17:01:55 -0800318 @VisibleForTesting
319 void setTextColor(@ColorInt int textColor) {
320 mTextColor = textColor;
321 updateTextColors();
322 }
323
Lucas Dupin6bd86012017-12-05 17:58:57 -0800324 /**
325 * Representation of an item that appears under the clock on main keyguard message.
Lucas Dupin6bd86012017-12-05 17:58:57 -0800326 */
327 private class KeyguardSliceButton extends Button {
328
Lucas Dupin6bd86012017-12-05 17:58:57 -0800329 public KeyguardSliceButton(Context context) {
Lucas Dupinb16d8232018-01-26 12:44:52 -0800330 super(context, null /* attrs */, 0 /* styleAttr */,
Lucas Dupin6bd86012017-12-05 17:58:57 -0800331 com.android.keyguard.R.style.TextAppearance_Keyguard_Secondary);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800332 int horizontalPadding = (int) context.getResources()
333 .getDimension(R.dimen.widget_horizontal_padding);
Lucas Dupinb16d8232018-01-26 12:44:52 -0800334 setPadding(horizontalPadding / 2, 0, horizontalPadding / 2, 0);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800335 setCompoundDrawablePadding((int) context.getResources()
336 .getDimension(R.dimen.widget_icon_padding));
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800337 setMaxWidth(KeyguardSliceView.this.getWidth() / 2);
338 setMaxLines(1);
339 setEllipsize(TruncateAt.END);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800340 }
Lucas Dupinacb0eed2018-03-03 14:05:04 -0800341
342 @Override
343 public void setTextColor(int color) {
344 super.setTextColor(color);
345 updateDrawableColors();
346 }
347
348 @Override
349 public void setCompoundDrawables(Drawable left, Drawable top, Drawable right,
350 Drawable bottom) {
351 super.setCompoundDrawables(left, top, right, bottom);
352 updateDrawableColors();
353 }
354
355 private void updateDrawableColors() {
356 final int color = getCurrentTextColor();
357 for (Drawable drawable : getCompoundDrawables()) {
358 if (drawable != null) {
359 drawable.setTint(color);
360 }
361 }
362 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700363 }
364}