blob: deb975b783a13c042526352d2d3c6fdc4779c2db [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
19import android.app.PendingIntent;
Lucas Dupin6bd86012017-12-05 17:58:57 -080020import android.arch.lifecycle.LiveData;
21import android.arch.lifecycle.Observer;
Lucas Dupin957e50c2017-10-10 11:23:27 -070022import android.content.Context;
Lucas Dupin957e50c2017-10-10 11:23:27 -070023import android.graphics.Color;
Lucas Dupin6bd86012017-12-05 17:58:57 -080024import android.graphics.drawable.Drawable;
Lucas Dupin957e50c2017-10-10 11:23:27 -070025import android.net.Uri;
Lucas Dupin6bd86012017-12-05 17:58:57 -080026import android.provider.Settings;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -080027import android.text.Layout;
28import android.text.TextUtils;
29import android.text.TextUtils.TruncateAt;
Lucas Dupin957e50c2017-10-10 11:23:27 -070030import android.util.AttributeSet;
Lucas Dupin6bd86012017-12-05 17:58:57 -080031import android.util.Log;
32import android.view.View;
33import android.widget.Button;
Lucas Dupin957e50c2017-10-10 11:23:27 -070034import android.widget.LinearLayout;
35import android.widget.TextView;
36
37import com.android.internal.graphics.ColorUtils;
Lucas Dupin6bd86012017-12-05 17:58:57 -080038import com.android.settingslib.Utils;
39import com.android.systemui.Dependency;
Lucas Dupin957e50c2017-10-10 11:23:27 -070040import com.android.systemui.R;
41import com.android.systemui.keyguard.KeyguardSliceProvider;
Lucas Dupin6bd86012017-12-05 17:58:57 -080042import com.android.systemui.tuner.TunerService;
Lucas Dupin957e50c2017-10-10 11:23:27 -070043
Lucas Dupin6bd86012017-12-05 17:58:57 -080044import java.util.HashMap;
45import java.util.List;
46import java.util.function.Consumer;
47
48import androidx.app.slice.Slice;
49import androidx.app.slice.SliceItem;
50import androidx.app.slice.core.SliceQuery;
Mady Mellor90e9fce2018-01-22 17:30:40 -080051import androidx.app.slice.widget.ListContent;
52import androidx.app.slice.widget.RowContent;
Lucas Dupin6bd86012017-12-05 17:58:57 -080053import androidx.app.slice.widget.SliceLiveData;
Jason Monk2af19982017-11-07 19:38:27 -050054
Lucas Dupin957e50c2017-10-10 11:23:27 -070055/**
56 * View visible under the clock on the lock screen and AoD.
57 */
Lucas Dupin6bd86012017-12-05 17:58:57 -080058public class KeyguardSliceView extends LinearLayout implements View.OnClickListener,
59 Observer<Slice>, TunerService.Tunable {
Lucas Dupin957e50c2017-10-10 11:23:27 -070060
Lucas Dupin6bd86012017-12-05 17:58:57 -080061 private static final String TAG = "KeyguardSliceView";
62 private final HashMap<View, PendingIntent> mClickActions;
63 private Uri mKeyguardSliceUri;
Lucas Dupin957e50c2017-10-10 11:23:27 -070064 private TextView mTitle;
Lucas Dupin6bd86012017-12-05 17:58:57 -080065 private LinearLayout mRow;
Lucas Dupin957e50c2017-10-10 11:23:27 -070066 private int mTextColor;
67 private float mDarkAmount = 0;
68
Lucas Dupin6bd86012017-12-05 17:58:57 -080069 private LiveData<Slice> mLiveData;
70 private int mIconSize;
71 private Consumer<Boolean> mListener;
72 private boolean mHasHeader;
Lucas Dupin957e50c2017-10-10 11:23:27 -070073
74 public KeyguardSliceView(Context context) {
75 this(context, null, 0);
76 }
77
78 public KeyguardSliceView(Context context, AttributeSet attrs) {
79 this(context, attrs, 0);
80 }
81
82 public KeyguardSliceView(Context context, AttributeSet attrs, int defStyle) {
83 super(context, attrs, defStyle);
Lucas Dupin6bd86012017-12-05 17:58:57 -080084
85 TunerService tunerService = Dependency.get(TunerService.class);
86 tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI);
87
88 mClickActions = new HashMap<>();
Lucas Dupin957e50c2017-10-10 11:23:27 -070089 }
90
91 @Override
92 protected void onFinishInflate() {
93 super.onFinishInflate();
94 mTitle = findViewById(R.id.title);
Lucas Dupin6bd86012017-12-05 17:58:57 -080095 mRow = findViewById(R.id.row);
96 mTextColor = Utils.getColorAttr(mContext, R.attr.wallpaperTextColor);
97 mIconSize = (int) mContext.getResources().getDimension(R.dimen.widget_icon_size);
Lucas Dupin957e50c2017-10-10 11:23:27 -070098 }
99
100 @Override
101 protected void onAttachedToWindow() {
102 super.onAttachedToWindow();
103
Lucas Dupin957e50c2017-10-10 11:23:27 -0700104 // Make sure we always have the most current slice
Lucas Dupin6bd86012017-12-05 17:58:57 -0800105 mLiveData.observeForever(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700106 }
107
108 @Override
109 protected void onDetachedFromWindow() {
110 super.onDetachedFromWindow();
111
Lucas Dupin6bd86012017-12-05 17:58:57 -0800112 mLiveData.removeObserver(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700113 }
114
115 private void showSlice(Slice slice) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700116
Mady Mellor90e9fce2018-01-22 17:30:40 -0800117 ListContent lc = new ListContent(slice);
118 mHasHeader = lc.hasHeader();
119 List<SliceItem> subItems = lc.getRowItems();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800120 if (!mHasHeader) {
Lucas Dupin957e50c2017-10-10 11:23:27 -0700121 mTitle.setVisibility(GONE);
122 } else {
123 mTitle.setVisibility(VISIBLE);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800124 // If there's a header it'll be the first subitem
125 RowContent header = new RowContent(subItems.get(0), true /* showStartItem */);
126 SliceItem mainTitle = header.getTitleItem();
127 CharSequence title = mainTitle != null ? mainTitle.getText() : null;
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800128 mTitle.setText(title);
129
130 // Check if we're already ellipsizing the text.
131 // We're going to figure out the best possible line break if not.
132 Layout layout = mTitle.getLayout();
133 if (layout != null){
134 final int lineCount = layout.getLineCount();
135 if (lineCount > 0) {
136 if (layout.getEllipsisCount(lineCount - 1) == 0) {
137 mTitle.setText(findBestLineBreak(title));
138 }
139 }
140 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700141 }
142
Lucas Dupin6bd86012017-12-05 17:58:57 -0800143 mClickActions.clear();
144 final int subItemsCount = subItems.size();
Lucas Dupina2a7a402018-01-12 17:45:51 -0800145 final int blendedColor = getTextColor();
Mady Mellor90e9fce2018-01-22 17:30:40 -0800146 final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it
147 for (int i = startIndex; i < subItemsCount; i++) {
Lucas Dupin6bd86012017-12-05 17:58:57 -0800148 SliceItem item = subItems.get(i);
Mady Mellor90e9fce2018-01-22 17:30:40 -0800149 RowContent rc = new RowContent(item, true /* showStartItem */);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800150 final Uri itemTag = item.getSlice().getUri();
151 // Try to reuse the view if already exists in the layout
152 KeyguardSliceButton button = mRow.findViewWithTag(itemTag);
153 if (button == null) {
154 button = new KeyguardSliceButton(mContext);
Lucas Dupina2a7a402018-01-12 17:45:51 -0800155 button.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800156 button.setTag(itemTag);
157 } else {
158 mRow.removeView(button);
159 }
Lucas Dupinb16d8232018-01-26 12:44:52 -0800160 mRow.addView(button);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800161
Mady Mellor90e9fce2018-01-22 17:30:40 -0800162 PendingIntent pendingIntent = null;
Alan Viverette85935282018-02-27 22:10:18 +0000163 if (rc.getPrimaryAction() != null) {
164 pendingIntent = rc.getPrimaryAction().getAction();
Lucas Dupin6bd86012017-12-05 17:58:57 -0800165 }
166 mClickActions.put(button, pendingIntent);
167
Mady Mellor90e9fce2018-01-22 17:30:40 -0800168 button.setText(rc.getTitleItem().getText());
Lucas Dupin6bd86012017-12-05 17:58:57 -0800169
170 Drawable iconDrawable = null;
171 SliceItem icon = SliceQuery.find(item.getSlice(),
172 android.app.slice.SliceItem.FORMAT_IMAGE);
173 if (icon != null) {
174 iconDrawable = icon.getIcon().loadDrawable(mContext);
175 final int width = (int) (iconDrawable.getIntrinsicWidth()
176 / (float) iconDrawable.getIntrinsicHeight() * mIconSize);
177 iconDrawable.setBounds(0, 0, Math.max(width, 1), mIconSize);
178 }
179 button.setCompoundDrawablesRelative(iconDrawable, null, null, null);
180 button.setOnClickListener(this);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700181 }
182
Lucas Dupin6bd86012017-12-05 17:58:57 -0800183 // Removing old views
184 for (int i = 0; i < mRow.getChildCount(); i++) {
185 View child = mRow.getChildAt(i);
186 if (!mClickActions.containsKey(child)) {
187 mRow.removeView(child);
188 i--;
189 }
190 }
191
192 final int visibility = mHasHeader || subItemsCount > 0 ? VISIBLE : GONE;
Lucas Dupin957e50c2017-10-10 11:23:27 -0700193 if (visibility != getVisibility()) {
194 setVisibility(visibility);
195 }
Lucas Dupin6bd86012017-12-05 17:58:57 -0800196
197 mListener.accept(mHasHeader);
Lucas Dupin957e50c2017-10-10 11:23:27 -0700198 }
199
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800200 /**
201 * Breaks a string in 2 lines where both have similar character count
202 * but first line is always longer.
203 *
204 * @param charSequence Original text.
205 * @return Optimal string.
206 */
207 private CharSequence findBestLineBreak(CharSequence charSequence) {
208 if (TextUtils.isEmpty(charSequence)) {
209 return charSequence;
210 }
211
212 String source = charSequence.toString();
213 // Ignore if there is only 1 word,
214 // or if line breaks were manually set.
215 if (source.contains("\n") || !source.contains(" ")) {
216 return source;
217 }
218
219 final String[] words = source.split(" ");
220 final StringBuilder optimalString = new StringBuilder(source.length());
221 int current = 0;
222 while (optimalString.length() < source.length() - optimalString.length()) {
223 optimalString.append(words[current]);
224 if (current < words.length - 1) {
225 optimalString.append(" ");
226 }
227 current++;
228 }
229 optimalString.append("\n");
230 for (int i = current; i < words.length; i++) {
231 optimalString.append(words[i]);
232 if (current < words.length - 1) {
233 optimalString.append(" ");
234 }
235 }
236
237 return optimalString.toString();
238 }
239
Lucas Dupin957e50c2017-10-10 11:23:27 -0700240 public void setDark(float darkAmount) {
241 mDarkAmount = darkAmount;
242 updateTextColors();
243 }
244
Lucas Dupin957e50c2017-10-10 11:23:27 -0700245 private void updateTextColors() {
Lucas Dupina2a7a402018-01-12 17:45:51 -0800246 final int blendedColor = getTextColor();
Lucas Dupin957e50c2017-10-10 11:23:27 -0700247 mTitle.setTextColor(blendedColor);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800248 int childCount = mRow.getChildCount();
249 for (int i = 0; i < childCount; i++) {
250 View v = mRow.getChildAt(i);
251 if (v instanceof Button) {
252 ((Button) v).setTextColor(blendedColor);
253 }
254 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700255 }
256
Lucas Dupin6bd86012017-12-05 17:58:57 -0800257 @Override
258 public void onClick(View v) {
259 final PendingIntent action = mClickActions.get(v);
260 if (action != null) {
261 try {
262 action.send();
263 } catch (PendingIntent.CanceledException e) {
264 Log.i(TAG, "Pending intent cancelled, nothing to launch", e);
265 }
266 }
267 }
268
269 public void setListener(Consumer<Boolean> listener) {
270 mListener = listener;
271 }
272
273 public boolean hasHeader() {
274 return mHasHeader;
275 }
276
277 /**
278 * LiveData observer lifecycle.
279 * @param slice the new slice content.
280 */
281 @Override
282 public void onChanged(Slice slice) {
283 showSlice(slice);
284 }
285
286 @Override
287 public void onTuningChanged(String key, String newValue) {
288 setupUri(newValue);
289 }
290
291 public void setupUri(String uriString) {
292 if (uriString == null) {
293 uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI;
294 }
295
296 boolean wasObserving = false;
297 if (mLiveData != null && mLiveData.hasActiveObservers()) {
298 wasObserving = true;
299 mLiveData.removeObserver(this);
300 }
301
302 mKeyguardSliceUri = Uri.parse(uriString);
303 mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri);
304
305 if (wasObserving) {
306 mLiveData.observeForever(this);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800307 }
308 }
309
Lucas Dupina2a7a402018-01-12 17:45:51 -0800310 public int getTextColor() {
311 return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount);
312 }
313
Lucas Dupin6bd86012017-12-05 17:58:57 -0800314 /**
315 * Representation of an item that appears under the clock on main keyguard message.
Lucas Dupin6bd86012017-12-05 17:58:57 -0800316 */
317 private class KeyguardSliceButton extends Button {
318
Lucas Dupin6bd86012017-12-05 17:58:57 -0800319 public KeyguardSliceButton(Context context) {
Lucas Dupinb16d8232018-01-26 12:44:52 -0800320 super(context, null /* attrs */, 0 /* styleAttr */,
Lucas Dupin6bd86012017-12-05 17:58:57 -0800321 com.android.keyguard.R.style.TextAppearance_Keyguard_Secondary);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800322 int horizontalPadding = (int) context.getResources()
323 .getDimension(R.dimen.widget_horizontal_padding);
Lucas Dupinb16d8232018-01-26 12:44:52 -0800324 setPadding(horizontalPadding / 2, 0, horizontalPadding / 2, 0);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800325 setCompoundDrawablePadding((int) context.getResources()
326 .getDimension(R.dimen.widget_icon_padding));
Lucas Dupin2a3c3e32018-01-05 17:02:43 -0800327 setMaxWidth(KeyguardSliceView.this.getWidth() / 2);
328 setMaxLines(1);
329 setEllipsize(TruncateAt.END);
Lucas Dupin6bd86012017-12-05 17:58:57 -0800330 }
Lucas Dupin957e50c2017-10-10 11:23:27 -0700331 }
332}