Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.keyguard; |
| 18 | |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 19 | import static android.app.slice.Slice.HINT_LIST_ITEM; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 20 | import static android.view.Display.DEFAULT_DISPLAY; |
| 21 | import static android.view.Display.INVALID_DISPLAY; |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 22 | |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 23 | import android.animation.LayoutTransition; |
| 24 | import android.animation.ObjectAnimator; |
| 25 | import android.animation.PropertyValuesHolder; |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 26 | import android.annotation.ColorInt; |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 27 | import android.annotation.StyleRes; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 28 | import android.app.PendingIntent; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 29 | import android.content.Context; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 30 | import android.graphics.Color; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 31 | import android.graphics.drawable.Drawable; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 32 | import android.net.Uri; |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 33 | import android.os.Trace; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 34 | import android.provider.Settings; |
Lucas Dupin | 2a3c3e3 | 2018-01-05 17:02:43 -0800 | [diff] [blame] | 35 | import android.text.TextUtils; |
| 36 | import android.text.TextUtils.TruncateAt; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 37 | import android.util.AttributeSet; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 38 | import android.util.Log; |
| 39 | import android.view.View; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 40 | import android.view.ViewGroup; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 41 | import android.view.animation.Animation; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 42 | import android.widget.Button; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 43 | import android.widget.LinearLayout; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 44 | |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 45 | import androidx.lifecycle.LiveData; |
| 46 | import androidx.lifecycle.Observer; |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 47 | import androidx.slice.Slice; |
| 48 | import androidx.slice.SliceItem; |
Jason Monk | f852f56 | 2018-05-23 16:08:11 -0400 | [diff] [blame] | 49 | import androidx.slice.SliceViewManager; |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 50 | import androidx.slice.core.SliceQuery; |
| 51 | import androidx.slice.widget.ListContent; |
| 52 | import androidx.slice.widget.RowContent; |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 53 | import androidx.slice.widget.SliceContent; |
Alan Viverette | e893588 | 2018-03-15 21:19:36 +0000 | [diff] [blame] | 54 | import androidx.slice.widget.SliceLiveData; |
Jason Monk | 2af1998 | 2017-11-07 19:38:27 -0500 | [diff] [blame] | 55 | |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 56 | import com.android.internal.annotations.VisibleForTesting; |
| 57 | import com.android.internal.graphics.ColorUtils; |
| 58 | import com.android.settingslib.Utils; |
| 59 | import com.android.systemui.Dependency; |
| 60 | import com.android.systemui.Interpolators; |
| 61 | import com.android.systemui.keyguard.KeyguardSliceProvider; |
| 62 | import com.android.systemui.statusbar.policy.ConfigurationController; |
| 63 | import com.android.systemui.tuner.TunerService; |
| 64 | import com.android.systemui.util.wakelock.KeepAwakeAnimationListener; |
| 65 | |
| 66 | import java.util.ArrayList; |
| 67 | import java.util.HashMap; |
| 68 | import java.util.List; |
| 69 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 70 | /** |
| 71 | * View visible under the clock on the lock screen and AoD. |
| 72 | */ |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 73 | public class KeyguardSliceView extends LinearLayout implements View.OnClickListener, |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 74 | Observer<Slice>, TunerService.Tunable, ConfigurationController.ConfigurationListener { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 75 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 76 | private static final String TAG = "KeyguardSliceView"; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 77 | public static final int DEFAULT_ANIM_DURATION = 550; |
| 78 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 79 | private final HashMap<View, PendingIntent> mClickActions; |
| 80 | private Uri mKeyguardSliceUri; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 81 | private Row mRow; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 82 | private int mTextColor; |
| 83 | private float mDarkAmount = 0; |
| 84 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 85 | private LiveData<Slice> mLiveData; |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 86 | private int mDisplayId = INVALID_DISPLAY; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 87 | private int mIconSize; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 88 | /** |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 89 | * Runnable called whenever the view contents change. |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 90 | */ |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 91 | private Runnable mContentChangeListener; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 92 | private Slice mSlice; |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame^] | 93 | private boolean mHasHeader; |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 94 | |
| 95 | public KeyguardSliceView(Context context) { |
| 96 | this(context, null, 0); |
| 97 | } |
| 98 | |
| 99 | public KeyguardSliceView(Context context, AttributeSet attrs) { |
| 100 | this(context, attrs, 0); |
| 101 | } |
| 102 | |
| 103 | public KeyguardSliceView(Context context, AttributeSet attrs, int defStyle) { |
| 104 | super(context, attrs, defStyle); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 105 | |
| 106 | TunerService tunerService = Dependency.get(TunerService.class); |
| 107 | tunerService.addTunable(this, Settings.Secure.KEYGUARD_SLICE_URI); |
| 108 | |
| 109 | mClickActions = new HashMap<>(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 110 | |
| 111 | LayoutTransition transition = new LayoutTransition(); |
| 112 | transition.setStagger(LayoutTransition.CHANGE_APPEARING, DEFAULT_ANIM_DURATION / 2); |
| 113 | transition.setDuration(LayoutTransition.APPEARING, DEFAULT_ANIM_DURATION); |
| 114 | transition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 2); |
| 115 | transition.disableTransitionType(LayoutTransition.CHANGE_APPEARING); |
| 116 | transition.disableTransitionType(LayoutTransition.CHANGE_DISAPPEARING); |
| 117 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.FAST_OUT_SLOW_IN); |
| 118 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
| 119 | transition.setAnimateParentHierarchy(false); |
| 120 | transition.addTransitionListener(new SliceViewTransitionListener()); |
| 121 | setLayoutTransition(transition); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | @Override |
| 125 | protected void onFinishInflate() { |
| 126 | super.onFinishInflate(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 127 | mRow = findViewById(R.id.row); |
Jason Chang | b4e879d | 2018-04-11 11:17:58 +0800 | [diff] [blame] | 128 | mTextColor = Utils.getColorAttrDefaultColor(mContext, R.attr.wallpaperTextColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 129 | mIconSize = (int) mContext.getResources().getDimension(R.dimen.widget_icon_size); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
| 133 | protected void onAttachedToWindow() { |
| 134 | super.onAttachedToWindow(); |
| 135 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 136 | mDisplayId = getDisplay().getDisplayId(); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 137 | // Make sure we always have the most current slice |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 138 | mLiveData.observeForever(this); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 139 | Dependency.get(ConfigurationController.class).addCallback(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void onDetachedFromWindow() { |
| 144 | super.onDetachedFromWindow(); |
| 145 | |
wilsonshih | e7903ea | 2018-09-26 16:17:59 +0800 | [diff] [blame] | 146 | // TODO(b/117344873) Remove below work around after this issue be fixed. |
| 147 | if (mDisplayId == DEFAULT_DISPLAY) { |
| 148 | mLiveData.removeObserver(this); |
| 149 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 150 | Dependency.get(ConfigurationController.class).removeCallback(this); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame^] | 153 | /** |
| 154 | * Returns whether the current visible slice has a title/header. |
| 155 | */ |
| 156 | public boolean hasHeader() { |
| 157 | return mHasHeader; |
| 158 | } |
| 159 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 160 | private void showSlice() { |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 161 | Trace.beginSection("KeyguardSliceView#showSlice"); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame^] | 162 | if (mSlice == null) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 163 | mRow.setVisibility(GONE); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame^] | 164 | mHasHeader = false; |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 165 | if (mContentChangeListener != null) { |
| 166 | mContentChangeListener.run(); |
| 167 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 168 | return; |
| 169 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 170 | |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 171 | ListContent lc = new ListContent(getContext(), mSlice); |
| 172 | SliceContent headerContent = lc.getHeader(); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame^] | 173 | mHasHeader = headerContent != null |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 174 | && !headerContent.getSliceItem().hasHint(HINT_LIST_ITEM); |
| 175 | List<SliceContent> subItems = new ArrayList<>(); |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 176 | for (int i = 0; i < lc.getRowItems().size(); i++) { |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 177 | SliceContent subItem = lc.getRowItems().get(i); |
| 178 | String itemUri = subItem.getSliceItem().getSlice().getUri().toString(); |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 179 | // Filter out the action row |
| 180 | if (!KeyguardSliceProvider.KEYGUARD_ACTION_URI.equals(itemUri)) { |
| 181 | subItems.add(subItem); |
| 182 | } |
| 183 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 184 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 185 | mClickActions.clear(); |
| 186 | final int subItemsCount = subItems.size(); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 187 | final int blendedColor = getTextColor(); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame^] | 188 | final int startIndex = mHasHeader ? 1 : 0; // First item is header; skip it |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 189 | mRow.setVisibility(subItemsCount > 0 ? VISIBLE : GONE); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 190 | for (int i = startIndex; i < subItemsCount; i++) { |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 191 | RowContent rc = (RowContent) subItems.get(i); |
| 192 | SliceItem item = rc.getSliceItem(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 193 | final Uri itemTag = item.getSlice().getUri(); |
| 194 | // Try to reuse the view if already exists in the layout |
| 195 | KeyguardSliceButton button = mRow.findViewWithTag(itemTag); |
| 196 | if (button == null) { |
| 197 | button = new KeyguardSliceButton(mContext); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 198 | button.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 199 | button.setTag(itemTag); |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame^] | 200 | final int viewIndex = i - (mHasHeader ? 1 : 0); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 201 | mRow.addView(button, viewIndex); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 202 | } |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 203 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 204 | PendingIntent pendingIntent = null; |
Alan Viverette | 8593528 | 2018-02-27 22:10:18 +0000 | [diff] [blame] | 205 | if (rc.getPrimaryAction() != null) { |
| 206 | pendingIntent = rc.getPrimaryAction().getAction(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 207 | } |
| 208 | mClickActions.put(button, pendingIntent); |
| 209 | |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 210 | final SliceItem titleItem = rc.getTitleItem(); |
| 211 | button.setText(titleItem == null ? null : titleItem.getText()); |
Lucas Dupin | 1d5e910 | 2018-05-21 17:58:05 -0700 | [diff] [blame] | 212 | button.setContentDescription(rc.getContentDescription()); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 213 | |
| 214 | Drawable iconDrawable = null; |
| 215 | SliceItem icon = SliceQuery.find(item.getSlice(), |
| 216 | android.app.slice.SliceItem.FORMAT_IMAGE); |
| 217 | if (icon != null) { |
| 218 | iconDrawable = icon.getIcon().loadDrawable(mContext); |
Lucas Dupin | 2955bf1 | 2018-12-26 10:15:51 -0800 | [diff] [blame] | 219 | if (iconDrawable != null) { |
| 220 | final int width = (int) (iconDrawable.getIntrinsicWidth() |
| 221 | / (float) iconDrawable.getIntrinsicHeight() * mIconSize); |
| 222 | iconDrawable.setBounds(0, 0, Math.max(width, 1), mIconSize); |
| 223 | } |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 224 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 225 | button.setCompoundDrawables(iconDrawable, null, null, null); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 226 | button.setOnClickListener(this); |
Lucas Dupin | 2c0d52e | 2018-03-14 12:39:00 -0700 | [diff] [blame] | 227 | button.setClickable(pendingIntent != null); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 228 | } |
| 229 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 230 | // Removing old views |
| 231 | for (int i = 0; i < mRow.getChildCount(); i++) { |
| 232 | View child = mRow.getChildAt(i); |
| 233 | if (!mClickActions.containsKey(child)) { |
| 234 | mRow.removeView(child); |
| 235 | i--; |
| 236 | } |
| 237 | } |
| 238 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 239 | if (mContentChangeListener != null) { |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 240 | mContentChangeListener.run(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 241 | } |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 242 | Trace.endSection(); |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 245 | public void setDarkAmount(float darkAmount) { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 246 | mDarkAmount = darkAmount; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 247 | mRow.setDarkAmount(darkAmount); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 248 | updateTextColors(); |
| 249 | } |
| 250 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 251 | private void updateTextColors() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 252 | final int blendedColor = getTextColor(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 253 | int childCount = mRow.getChildCount(); |
| 254 | for (int i = 0; i < childCount; i++) { |
| 255 | View v = mRow.getChildAt(i); |
| 256 | if (v instanceof Button) { |
| 257 | ((Button) v).setTextColor(blendedColor); |
| 258 | } |
| 259 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 262 | @Override |
| 263 | public void onClick(View v) { |
| 264 | final PendingIntent action = mClickActions.get(v); |
| 265 | if (action != null) { |
| 266 | try { |
| 267 | action.send(); |
| 268 | } catch (PendingIntent.CanceledException e) { |
| 269 | Log.i(TAG, "Pending intent cancelled, nothing to launch", e); |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 274 | /** |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 275 | * Runnable that gets invoked every time the title or the row visibility changes. |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 276 | * @param contentChangeListener The listener. |
| 277 | */ |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 278 | public void setContentChangeListener(Runnable contentChangeListener) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 279 | mContentChangeListener = contentChangeListener; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 280 | } |
| 281 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 282 | /** |
| 283 | * LiveData observer lifecycle. |
| 284 | * @param slice the new slice content. |
| 285 | */ |
| 286 | @Override |
| 287 | public void onChanged(Slice slice) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 288 | mSlice = slice; |
| 289 | showSlice(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | @Override |
| 293 | public void onTuningChanged(String key, String newValue) { |
| 294 | setupUri(newValue); |
| 295 | } |
| 296 | |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 297 | private void setupUri(String uriString) { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 298 | if (uriString == null) { |
| 299 | uriString = KeyguardSliceProvider.KEYGUARD_SLICE_URI; |
| 300 | } |
| 301 | |
| 302 | boolean wasObserving = false; |
| 303 | if (mLiveData != null && mLiveData.hasActiveObservers()) { |
| 304 | wasObserving = true; |
| 305 | mLiveData.removeObserver(this); |
| 306 | } |
| 307 | |
| 308 | mKeyguardSliceUri = Uri.parse(uriString); |
| 309 | mLiveData = SliceLiveData.fromUri(mContext, mKeyguardSliceUri); |
| 310 | |
| 311 | if (wasObserving) { |
| 312 | mLiveData.observeForever(this); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 316 | @VisibleForTesting |
| 317 | int getTextColor() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 318 | return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount); |
| 319 | } |
| 320 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 321 | @VisibleForTesting |
| 322 | void setTextColor(@ColorInt int textColor) { |
| 323 | mTextColor = textColor; |
| 324 | updateTextColors(); |
| 325 | } |
| 326 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 327 | @Override |
| 328 | public void onDensityOrFontScaleChanged() { |
| 329 | mIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.widget_icon_size); |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 330 | } |
| 331 | |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 332 | public void refresh() { |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 333 | Slice slice; |
| 334 | Trace.beginSection("KeyguardSliceView#refresh"); |
| 335 | // We can optimize performance and avoid binder calls when we know that we're bound |
| 336 | // to a Slice on the same process. |
| 337 | if (KeyguardSliceProvider.KEYGUARD_SLICE_URI.equals(mKeyguardSliceUri.toString())) { |
| 338 | KeyguardSliceProvider instance = KeyguardSliceProvider.getAttachedInstance(); |
| 339 | if (instance != null) { |
| 340 | slice = instance.onBindSlice(mKeyguardSliceUri); |
| 341 | } else { |
| 342 | Log.w(TAG, "Keyguard slice not bound yet?"); |
| 343 | slice = null; |
| 344 | } |
| 345 | } else { |
| 346 | slice = SliceViewManager.getInstance(getContext()).bindSlice(mKeyguardSliceUri); |
| 347 | } |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 348 | onChanged(slice); |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 349 | Trace.endSection(); |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 352 | public static class Row extends LinearLayout { |
| 353 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 354 | /** |
| 355 | * This view is visible in AOD, which means that the device will sleep if we |
| 356 | * don't hold a wake lock. We want to enter doze only after all views have reached |
| 357 | * their desired positions. |
| 358 | */ |
| 359 | private final Animation.AnimationListener mKeepAwakeListener; |
| 360 | private float mDarkAmount; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 361 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 362 | public Row(Context context) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 363 | this(context, null); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | public Row(Context context, AttributeSet attrs) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 367 | this(context, attrs, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | public Row(Context context, AttributeSet attrs, int defStyleAttr) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 371 | this(context, attrs, defStyleAttr, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 375 | super(context, attrs, defStyleAttr, defStyleRes); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 376 | mKeepAwakeListener = new KeepAwakeAnimationListener(mContext); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | @Override |
| 380 | protected void onFinishInflate() { |
| 381 | LayoutTransition transition = new LayoutTransition(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 382 | transition.setDuration(DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 383 | |
| 384 | PropertyValuesHolder left = PropertyValuesHolder.ofInt("left", 0, 1); |
| 385 | PropertyValuesHolder right = PropertyValuesHolder.ofInt("right", 0, 1); |
| 386 | ObjectAnimator changeAnimator = ObjectAnimator.ofPropertyValuesHolder((Object) null, |
| 387 | left, right); |
| 388 | transition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeAnimator); |
| 389 | transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeAnimator); |
| 390 | transition.setInterpolator(LayoutTransition.CHANGE_APPEARING, |
| 391 | Interpolators.ACCELERATE_DECELERATE); |
| 392 | transition.setInterpolator(LayoutTransition.CHANGE_DISAPPEARING, |
| 393 | Interpolators.ACCELERATE_DECELERATE); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 394 | transition.setStartDelay(LayoutTransition.CHANGE_APPEARING, DEFAULT_ANIM_DURATION); |
| 395 | transition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 396 | |
| 397 | ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f); |
| 398 | transition.setAnimator(LayoutTransition.APPEARING, appearAnimator); |
| 399 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN); |
| 400 | |
| 401 | ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f); |
| 402 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 403 | transition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 4); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 404 | transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator); |
| 405 | |
| 406 | transition.setAnimateParentHierarchy(false); |
| 407 | setLayoutTransition(transition); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | @Override |
| 411 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 412 | int width = MeasureSpec.getSize(widthMeasureSpec); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 413 | int childCount = getChildCount(); |
| 414 | for (int i = 0; i < childCount; i++) { |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 415 | View child = getChildAt(i); |
| 416 | if (child instanceof KeyguardSliceButton) { |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 417 | ((KeyguardSliceButton) child).setMaxWidth(width / childCount); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 421 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 422 | |
| 423 | public void setDarkAmount(float darkAmount) { |
| 424 | boolean isAwake = darkAmount != 0; |
| 425 | boolean wasAwake = mDarkAmount != 0; |
| 426 | if (isAwake == wasAwake) { |
| 427 | return; |
| 428 | } |
| 429 | mDarkAmount = darkAmount; |
| 430 | setLayoutAnimationListener(isAwake ? null : mKeepAwakeListener); |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public boolean hasOverlappingRendering() { |
| 435 | return false; |
| 436 | } |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 437 | } |
| 438 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 439 | /** |
| 440 | * Representation of an item that appears under the clock on main keyguard message. |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 441 | */ |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 442 | @VisibleForTesting |
| 443 | static class KeyguardSliceButton extends Button implements |
| 444 | ConfigurationController.ConfigurationListener { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 445 | |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 446 | @StyleRes |
| 447 | private static int sStyleId = R.style.TextAppearance_Keyguard_Secondary; |
| 448 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 449 | public KeyguardSliceButton(Context context) { |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 450 | super(context, null /* attrs */, 0 /* styleAttr */, sStyleId); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 451 | onDensityOrFontScaleChanged(); |
| 452 | setEllipsize(TruncateAt.END); |
| 453 | } |
| 454 | |
| 455 | @Override |
| 456 | protected void onAttachedToWindow() { |
| 457 | super.onAttachedToWindow(); |
| 458 | Dependency.get(ConfigurationController.class).addCallback(this); |
| 459 | } |
| 460 | |
| 461 | @Override |
| 462 | protected void onDetachedFromWindow() { |
| 463 | super.onDetachedFromWindow(); |
| 464 | Dependency.get(ConfigurationController.class).removeCallback(this); |
| 465 | } |
| 466 | |
| 467 | @Override |
| 468 | public void onDensityOrFontScaleChanged() { |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 469 | updatePadding(); |
| 470 | } |
| 471 | |
| 472 | @Override |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 473 | public void onOverlayChanged() { |
| 474 | setTextAppearance(sStyleId); |
| 475 | } |
| 476 | |
| 477 | @Override |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 478 | public void setText(CharSequence text, BufferType type) { |
| 479 | super.setText(text, type); |
| 480 | updatePadding(); |
| 481 | } |
| 482 | |
| 483 | private void updatePadding() { |
| 484 | boolean hasText = !TextUtils.isEmpty(getText()); |
| 485 | int horizontalPadding = (int) getContext().getResources() |
| 486 | .getDimension(R.dimen.widget_horizontal_padding) / 2; |
| 487 | setPadding(horizontalPadding, 0, horizontalPadding * (hasText ? 1 : -1), 0); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 488 | setCompoundDrawablePadding((int) mContext.getResources() |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 489 | .getDimension(R.dimen.widget_icon_padding)); |
| 490 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 491 | |
| 492 | @Override |
| 493 | public void setTextColor(int color) { |
| 494 | super.setTextColor(color); |
| 495 | updateDrawableColors(); |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, |
| 500 | Drawable bottom) { |
| 501 | super.setCompoundDrawables(left, top, right, bottom); |
| 502 | updateDrawableColors(); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 503 | updatePadding(); |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | private void updateDrawableColors() { |
| 507 | final int color = getCurrentTextColor(); |
| 508 | for (Drawable drawable : getCompoundDrawables()) { |
| 509 | if (drawable != null) { |
| 510 | drawable.setTint(color); |
| 511 | } |
| 512 | } |
| 513 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 514 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 515 | |
| 516 | private class SliceViewTransitionListener implements LayoutTransition.TransitionListener { |
| 517 | @Override |
| 518 | public void startTransition(LayoutTransition transition, ViewGroup container, View view, |
| 519 | int transitionType) { |
| 520 | switch (transitionType) { |
| 521 | case LayoutTransition.APPEARING: |
| 522 | int translation = getResources().getDimensionPixelSize( |
| 523 | R.dimen.pulsing_notification_appear_translation); |
| 524 | view.setTranslationY(translation); |
| 525 | view.animate() |
| 526 | .translationY(0) |
| 527 | .setDuration(DEFAULT_ANIM_DURATION) |
| 528 | .setInterpolator(Interpolators.ALPHA_IN) |
| 529 | .start(); |
| 530 | break; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 531 | } |
| 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void endTransition(LayoutTransition transition, ViewGroup container, View view, |
| 536 | int transitionType) { |
| 537 | |
| 538 | } |
| 539 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 540 | } |