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; |
| 93 | private boolean mPulsing; |
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 | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 153 | private void showSlice() { |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 154 | Trace.beginSection("KeyguardSliceView#showSlice"); |
Lucas Dupin | f39ce10 | 2018-05-02 14:23:44 -0700 | [diff] [blame] | 155 | if (mPulsing || mSlice == null) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 156 | mRow.setVisibility(GONE); |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 157 | if (mContentChangeListener != null) { |
| 158 | mContentChangeListener.run(); |
| 159 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 160 | return; |
| 161 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 162 | |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 163 | ListContent lc = new ListContent(getContext(), mSlice); |
| 164 | SliceContent headerContent = lc.getHeader(); |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 165 | boolean hasHeader = headerContent != null |
| 166 | && !headerContent.getSliceItem().hasHint(HINT_LIST_ITEM); |
| 167 | List<SliceContent> subItems = new ArrayList<>(); |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 168 | for (int i = 0; i < lc.getRowItems().size(); i++) { |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 169 | SliceContent subItem = lc.getRowItems().get(i); |
| 170 | String itemUri = subItem.getSliceItem().getSlice().getUri().toString(); |
Mady Mellor | 78f04ef | 2018-05-23 15:04:47 -0700 | [diff] [blame] | 171 | // Filter out the action row |
| 172 | if (!KeyguardSliceProvider.KEYGUARD_ACTION_URI.equals(itemUri)) { |
| 173 | subItems.add(subItem); |
| 174 | } |
| 175 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 176 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 177 | mClickActions.clear(); |
| 178 | final int subItemsCount = subItems.size(); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 179 | final int blendedColor = getTextColor(); |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 180 | final int startIndex = hasHeader ? 1 : 0; // First item is header; skip it |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 181 | mRow.setVisibility(subItemsCount > 0 ? VISIBLE : GONE); |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 182 | for (int i = startIndex; i < subItemsCount; i++) { |
Aurimas Liutikas | d0cec56 | 2018-08-15 11:03:53 -0700 | [diff] [blame] | 183 | RowContent rc = (RowContent) subItems.get(i); |
| 184 | SliceItem item = rc.getSliceItem(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 185 | final Uri itemTag = item.getSlice().getUri(); |
| 186 | // Try to reuse the view if already exists in the layout |
| 187 | KeyguardSliceButton button = mRow.findViewWithTag(itemTag); |
| 188 | if (button == null) { |
| 189 | button = new KeyguardSliceButton(mContext); |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 190 | button.setTextColor(blendedColor); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 191 | button.setTag(itemTag); |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 192 | final int viewIndex = i - (hasHeader ? 1 : 0); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 193 | mRow.addView(button, viewIndex); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 194 | } |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 195 | |
Mady Mellor | 90e9fce | 2018-01-22 17:30:40 -0800 | [diff] [blame] | 196 | PendingIntent pendingIntent = null; |
Alan Viverette | 8593528 | 2018-02-27 22:10:18 +0000 | [diff] [blame] | 197 | if (rc.getPrimaryAction() != null) { |
| 198 | pendingIntent = rc.getPrimaryAction().getAction(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 199 | } |
| 200 | mClickActions.put(button, pendingIntent); |
| 201 | |
Lucas Dupin | 1f7374a | 2018-02-26 18:08:33 -0800 | [diff] [blame] | 202 | final SliceItem titleItem = rc.getTitleItem(); |
| 203 | button.setText(titleItem == null ? null : titleItem.getText()); |
Lucas Dupin | 1d5e910 | 2018-05-21 17:58:05 -0700 | [diff] [blame] | 204 | button.setContentDescription(rc.getContentDescription()); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 205 | |
| 206 | Drawable iconDrawable = null; |
| 207 | SliceItem icon = SliceQuery.find(item.getSlice(), |
| 208 | android.app.slice.SliceItem.FORMAT_IMAGE); |
| 209 | if (icon != null) { |
| 210 | iconDrawable = icon.getIcon().loadDrawable(mContext); |
Lucas Dupin | 2955bf1 | 2018-12-26 10:15:51 -0800 | [diff] [blame^] | 211 | if (iconDrawable != null) { |
| 212 | final int width = (int) (iconDrawable.getIntrinsicWidth() |
| 213 | / (float) iconDrawable.getIntrinsicHeight() * mIconSize); |
| 214 | iconDrawable.setBounds(0, 0, Math.max(width, 1), mIconSize); |
| 215 | } |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 216 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 217 | button.setCompoundDrawables(iconDrawable, null, null, null); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 218 | button.setOnClickListener(this); |
Lucas Dupin | 2c0d52e | 2018-03-14 12:39:00 -0700 | [diff] [blame] | 219 | button.setClickable(pendingIntent != null); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 222 | // Removing old views |
| 223 | for (int i = 0; i < mRow.getChildCount(); i++) { |
| 224 | View child = mRow.getChildAt(i); |
| 225 | if (!mClickActions.containsKey(child)) { |
| 226 | mRow.removeView(child); |
| 227 | i--; |
| 228 | } |
| 229 | } |
| 230 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 231 | if (mContentChangeListener != null) { |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 232 | mContentChangeListener.run(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 233 | } |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 234 | Trace.endSection(); |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 237 | public void setPulsing(boolean pulsing, boolean animate) { |
| 238 | mPulsing = pulsing; |
| 239 | LayoutTransition transition = getLayoutTransition(); |
| 240 | if (!animate) { |
| 241 | setLayoutTransition(null); |
| 242 | } |
| 243 | showSlice(); |
| 244 | if (!animate) { |
| 245 | setLayoutTransition(transition); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 246 | } |
| 247 | } |
| 248 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 249 | public void setDarkAmount(float darkAmount) { |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 250 | mDarkAmount = darkAmount; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 251 | mRow.setDarkAmount(darkAmount); |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 252 | updateTextColors(); |
| 253 | } |
| 254 | |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 255 | private void updateTextColors() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 256 | final int blendedColor = getTextColor(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 257 | int childCount = mRow.getChildCount(); |
| 258 | for (int i = 0; i < childCount; i++) { |
| 259 | View v = mRow.getChildAt(i); |
| 260 | if (v instanceof Button) { |
| 261 | ((Button) v).setTextColor(blendedColor); |
| 262 | } |
| 263 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 266 | @Override |
| 267 | public void onClick(View v) { |
| 268 | final PendingIntent action = mClickActions.get(v); |
| 269 | if (action != null) { |
| 270 | try { |
| 271 | action.send(); |
| 272 | } catch (PendingIntent.CanceledException e) { |
| 273 | Log.i(TAG, "Pending intent cancelled, nothing to launch", e); |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 278 | /** |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 279 | * 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] | 280 | * @param contentChangeListener The listener. |
| 281 | */ |
Lucas Dupin | 3978e6e | 2018-05-22 18:08:35 -0700 | [diff] [blame] | 282 | public void setContentChangeListener(Runnable contentChangeListener) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 283 | mContentChangeListener = contentChangeListener; |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 286 | /** |
| 287 | * LiveData observer lifecycle. |
| 288 | * @param slice the new slice content. |
| 289 | */ |
| 290 | @Override |
| 291 | public void onChanged(Slice slice) { |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 292 | mSlice = slice; |
| 293 | showSlice(); |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | @Override |
| 297 | public void onTuningChanged(String key, String newValue) { |
| 298 | setupUri(newValue); |
| 299 | } |
| 300 | |
Lucas Dupin | ed00262 | 2018-10-25 18:12:15 -0700 | [diff] [blame] | 301 | private void setupUri(String uriString) { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 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 Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 317 | } |
| 318 | } |
| 319 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 320 | @VisibleForTesting |
| 321 | int getTextColor() { |
Lucas Dupin | a2a7a40 | 2018-01-12 17:45:51 -0800 | [diff] [blame] | 322 | return ColorUtils.blendARGB(mTextColor, Color.WHITE, mDarkAmount); |
| 323 | } |
| 324 | |
Lucas Dupin | f8e274c | 2018-02-22 17:01:55 -0800 | [diff] [blame] | 325 | @VisibleForTesting |
| 326 | void setTextColor(@ColorInt int textColor) { |
| 327 | mTextColor = textColor; |
| 328 | updateTextColors(); |
| 329 | } |
| 330 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 331 | @Override |
| 332 | public void onDensityOrFontScaleChanged() { |
| 333 | mIconSize = mContext.getResources().getDimensionPixelSize(R.dimen.widget_icon_size); |
Lucas Dupin | 2c5fce6 | 2018-03-05 19:06:00 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 336 | public void refresh() { |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 337 | Slice slice; |
| 338 | Trace.beginSection("KeyguardSliceView#refresh"); |
| 339 | // We can optimize performance and avoid binder calls when we know that we're bound |
| 340 | // to a Slice on the same process. |
| 341 | if (KeyguardSliceProvider.KEYGUARD_SLICE_URI.equals(mKeyguardSliceUri.toString())) { |
| 342 | KeyguardSliceProvider instance = KeyguardSliceProvider.getAttachedInstance(); |
| 343 | if (instance != null) { |
| 344 | slice = instance.onBindSlice(mKeyguardSliceUri); |
| 345 | } else { |
| 346 | Log.w(TAG, "Keyguard slice not bound yet?"); |
| 347 | slice = null; |
| 348 | } |
| 349 | } else { |
| 350 | slice = SliceViewManager.getInstance(getContext()).bindSlice(mKeyguardSliceUri); |
| 351 | } |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 352 | onChanged(slice); |
Lucas Dupin | a0d5119 | 2018-08-15 11:29:32 -0700 | [diff] [blame] | 353 | Trace.endSection(); |
Lucas Dupin | 9fedb89 | 2018-05-04 17:42:33 -0700 | [diff] [blame] | 354 | } |
| 355 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 356 | public static class Row extends LinearLayout { |
| 357 | |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 358 | /** |
| 359 | * This view is visible in AOD, which means that the device will sleep if we |
| 360 | * don't hold a wake lock. We want to enter doze only after all views have reached |
| 361 | * their desired positions. |
| 362 | */ |
| 363 | private final Animation.AnimationListener mKeepAwakeListener; |
| 364 | private float mDarkAmount; |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 365 | |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 366 | public Row(Context context) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 367 | this(context, null); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | public Row(Context context, AttributeSet attrs) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 371 | this(context, attrs, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | public Row(Context context, AttributeSet attrs, int defStyleAttr) { |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 375 | this(context, attrs, defStyleAttr, 0); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | public Row(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 379 | super(context, attrs, defStyleAttr, defStyleRes); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 380 | mKeepAwakeListener = new KeepAwakeAnimationListener(mContext); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
| 384 | protected void onFinishInflate() { |
| 385 | LayoutTransition transition = new LayoutTransition(); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 386 | transition.setDuration(DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 387 | |
| 388 | PropertyValuesHolder left = PropertyValuesHolder.ofInt("left", 0, 1); |
| 389 | PropertyValuesHolder right = PropertyValuesHolder.ofInt("right", 0, 1); |
| 390 | ObjectAnimator changeAnimator = ObjectAnimator.ofPropertyValuesHolder((Object) null, |
| 391 | left, right); |
| 392 | transition.setAnimator(LayoutTransition.CHANGE_APPEARING, changeAnimator); |
| 393 | transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, changeAnimator); |
| 394 | transition.setInterpolator(LayoutTransition.CHANGE_APPEARING, |
| 395 | Interpolators.ACCELERATE_DECELERATE); |
| 396 | transition.setInterpolator(LayoutTransition.CHANGE_DISAPPEARING, |
| 397 | Interpolators.ACCELERATE_DECELERATE); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 398 | transition.setStartDelay(LayoutTransition.CHANGE_APPEARING, DEFAULT_ANIM_DURATION); |
| 399 | transition.setStartDelay(LayoutTransition.CHANGE_DISAPPEARING, DEFAULT_ANIM_DURATION); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 400 | |
| 401 | ObjectAnimator appearAnimator = ObjectAnimator.ofFloat(null, "alpha", 0f, 1f); |
| 402 | transition.setAnimator(LayoutTransition.APPEARING, appearAnimator); |
| 403 | transition.setInterpolator(LayoutTransition.APPEARING, Interpolators.ALPHA_IN); |
| 404 | |
| 405 | ObjectAnimator disappearAnimator = ObjectAnimator.ofFloat(null, "alpha", 1f, 0f); |
| 406 | transition.setInterpolator(LayoutTransition.DISAPPEARING, Interpolators.ALPHA_OUT); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 407 | transition.setDuration(LayoutTransition.DISAPPEARING, DEFAULT_ANIM_DURATION / 4); |
Lucas Dupin | c68ec48 | 2018-03-30 14:24:53 -0700 | [diff] [blame] | 408 | transition.setAnimator(LayoutTransition.DISAPPEARING, disappearAnimator); |
| 409 | |
| 410 | transition.setAnimateParentHierarchy(false); |
| 411 | setLayoutTransition(transition); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | @Override |
| 415 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
| 416 | int width = MeasureSpec.getSize(widthMeasureSpec); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 417 | int childCount = getChildCount(); |
| 418 | for (int i = 0; i < childCount; i++) { |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 419 | View child = getChildAt(i); |
| 420 | if (child instanceof KeyguardSliceButton) { |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 421 | ((KeyguardSliceButton) child).setMaxWidth(width / childCount); |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
| 425 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 426 | |
| 427 | public void setDarkAmount(float darkAmount) { |
| 428 | boolean isAwake = darkAmount != 0; |
| 429 | boolean wasAwake = mDarkAmount != 0; |
| 430 | if (isAwake == wasAwake) { |
| 431 | return; |
| 432 | } |
| 433 | mDarkAmount = darkAmount; |
| 434 | setLayoutAnimationListener(isAwake ? null : mKeepAwakeListener); |
| 435 | } |
| 436 | |
| 437 | @Override |
| 438 | public boolean hasOverlappingRendering() { |
| 439 | return false; |
| 440 | } |
Lucas Dupin | 4603fe2 | 2018-03-08 16:51:16 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 443 | /** |
| 444 | * 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] | 445 | */ |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 446 | @VisibleForTesting |
| 447 | static class KeyguardSliceButton extends Button implements |
| 448 | ConfigurationController.ConfigurationListener { |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 449 | |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 450 | @StyleRes |
| 451 | private static int sStyleId = R.style.TextAppearance_Keyguard_Secondary; |
| 452 | |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 453 | public KeyguardSliceButton(Context context) { |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 454 | super(context, null /* attrs */, 0 /* styleAttr */, sStyleId); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 455 | onDensityOrFontScaleChanged(); |
| 456 | setEllipsize(TruncateAt.END); |
| 457 | } |
| 458 | |
| 459 | @Override |
| 460 | protected void onAttachedToWindow() { |
| 461 | super.onAttachedToWindow(); |
| 462 | Dependency.get(ConfigurationController.class).addCallback(this); |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | protected void onDetachedFromWindow() { |
| 467 | super.onDetachedFromWindow(); |
| 468 | Dependency.get(ConfigurationController.class).removeCallback(this); |
| 469 | } |
| 470 | |
| 471 | @Override |
| 472 | public void onDensityOrFontScaleChanged() { |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 473 | updatePadding(); |
| 474 | } |
| 475 | |
| 476 | @Override |
Fabian Kozynski | 648e555 | 2018-12-18 16:37:08 -0500 | [diff] [blame] | 477 | public void onOverlayChanged() { |
| 478 | setTextAppearance(sStyleId); |
| 479 | } |
| 480 | |
| 481 | @Override |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 482 | public void setText(CharSequence text, BufferType type) { |
| 483 | super.setText(text, type); |
| 484 | updatePadding(); |
| 485 | } |
| 486 | |
| 487 | private void updatePadding() { |
| 488 | boolean hasText = !TextUtils.isEmpty(getText()); |
| 489 | int horizontalPadding = (int) getContext().getResources() |
| 490 | .getDimension(R.dimen.widget_horizontal_padding) / 2; |
| 491 | setPadding(horizontalPadding, 0, horizontalPadding * (hasText ? 1 : -1), 0); |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 492 | setCompoundDrawablePadding((int) mContext.getResources() |
Lucas Dupin | 6bd8601 | 2017-12-05 17:58:57 -0800 | [diff] [blame] | 493 | .getDimension(R.dimen.widget_icon_padding)); |
| 494 | } |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 495 | |
| 496 | @Override |
| 497 | public void setTextColor(int color) { |
| 498 | super.setTextColor(color); |
| 499 | updateDrawableColors(); |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public void setCompoundDrawables(Drawable left, Drawable top, Drawable right, |
| 504 | Drawable bottom) { |
| 505 | super.setCompoundDrawables(left, top, right, bottom); |
| 506 | updateDrawableColors(); |
Lucas Dupin | 8b77a3b | 2018-05-01 13:23:03 -0700 | [diff] [blame] | 507 | updatePadding(); |
Lucas Dupin | acb0eed | 2018-03-03 14:05:04 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | private void updateDrawableColors() { |
| 511 | final int color = getCurrentTextColor(); |
| 512 | for (Drawable drawable : getCompoundDrawables()) { |
| 513 | if (drawable != null) { |
| 514 | drawable.setTint(color); |
| 515 | } |
| 516 | } |
| 517 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 518 | } |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 519 | |
| 520 | private class SliceViewTransitionListener implements LayoutTransition.TransitionListener { |
| 521 | @Override |
| 522 | public void startTransition(LayoutTransition transition, ViewGroup container, View view, |
| 523 | int transitionType) { |
| 524 | switch (transitionType) { |
| 525 | case LayoutTransition.APPEARING: |
| 526 | int translation = getResources().getDimensionPixelSize( |
| 527 | R.dimen.pulsing_notification_appear_translation); |
| 528 | view.setTranslationY(translation); |
| 529 | view.animate() |
| 530 | .translationY(0) |
| 531 | .setDuration(DEFAULT_ANIM_DURATION) |
| 532 | .setInterpolator(Interpolators.ALPHA_IN) |
| 533 | .start(); |
| 534 | break; |
Lucas Dupin | 4e02381 | 2018-04-02 21:19:23 -0700 | [diff] [blame] | 535 | } |
| 536 | } |
| 537 | |
| 538 | @Override |
| 539 | public void endTransition(LayoutTransition transition, ViewGroup container, View view, |
| 540 | int transitionType) { |
| 541 | |
| 542 | } |
| 543 | } |
Lucas Dupin | 957e50c | 2017-10-10 11:23:27 -0700 | [diff] [blame] | 544 | } |