Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.systemui.assist.ui; |
| 18 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 19 | import android.animation.ArgbEvaluator; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 20 | import android.annotation.ColorInt; |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.graphics.Canvas; |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 24 | import android.graphics.Color; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 25 | import android.graphics.Paint; |
| 26 | import android.graphics.Path; |
| 27 | import android.util.AttributeSet; |
| 28 | import android.util.Log; |
| 29 | import android.util.MathUtils; |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 30 | import android.view.ContextThemeWrapper; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 31 | import android.view.View; |
| 32 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 33 | import com.android.settingslib.Utils; |
| 34 | import com.android.systemui.Dependency; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 35 | import com.android.systemui.R; |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 36 | import com.android.systemui.statusbar.NavigationBarController; |
| 37 | import com.android.systemui.statusbar.phone.NavigationBarFragment; |
| 38 | import com.android.systemui.statusbar.phone.NavigationBarTransitions; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
| 41 | |
| 42 | /** |
| 43 | * Shows lights at the bottom of the phone, marking the invocation progress. |
| 44 | */ |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 45 | public class InvocationLightsView extends View |
| 46 | implements NavigationBarTransitions.DarkIntensityListener { |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 47 | |
| 48 | private static final String TAG = "InvocationLightsView"; |
| 49 | |
| 50 | private static final int LIGHT_HEIGHT_DP = 3; |
| 51 | // minimum light length as a fraction of the corner length |
| 52 | private static final float MINIMUM_CORNER_RATIO = .6f; |
| 53 | |
| 54 | protected final ArrayList<EdgeLight> mAssistInvocationLights = new ArrayList<>(); |
| 55 | protected final PerimeterPathGuide mGuide; |
| 56 | |
| 57 | private final Paint mPaint = new Paint(); |
| 58 | // Path used to render lights. One instance is used to draw all lights and is cached to avoid |
| 59 | // allocation on each frame. |
| 60 | private final Path mPath = new Path(); |
| 61 | private final int mViewHeight; |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 62 | private final int mStrokeWidth; |
| 63 | @ColorInt |
| 64 | private final int mLightColor; |
| 65 | @ColorInt |
| 66 | private final int mDarkColor; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 67 | |
| 68 | // Allocate variable for screen location lookup to avoid memory alloc onDraw() |
| 69 | private int[] mScreenLocation = new int[2]; |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 70 | private boolean mRegistered = false; |
| 71 | private boolean mUseNavBarColor = true; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 72 | |
| 73 | public InvocationLightsView(Context context) { |
| 74 | this(context, null); |
| 75 | } |
| 76 | |
| 77 | public InvocationLightsView(Context context, AttributeSet attrs) { |
| 78 | this(context, attrs, 0); |
| 79 | } |
| 80 | |
| 81 | public InvocationLightsView(Context context, AttributeSet attrs, int defStyleAttr) { |
| 82 | this(context, attrs, defStyleAttr, 0); |
| 83 | } |
| 84 | |
| 85 | public InvocationLightsView(Context context, AttributeSet attrs, int defStyleAttr, |
| 86 | int defStyleRes) { |
| 87 | super(context, attrs, defStyleAttr, defStyleRes); |
| 88 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 89 | mStrokeWidth = DisplayUtils.convertDpToPx(LIGHT_HEIGHT_DP, context); |
| 90 | mPaint.setStrokeWidth(mStrokeWidth); |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 91 | mPaint.setStyle(Paint.Style.STROKE); |
| 92 | mPaint.setStrokeJoin(Paint.Join.MITER); |
| 93 | mPaint.setAntiAlias(true); |
| 94 | |
James O'Leary | b81ccb4 | 2019-06-21 15:30:43 +0000 | [diff] [blame] | 95 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 96 | int displayWidth = DisplayUtils.getWidth(context); |
| 97 | int displayHeight = DisplayUtils.getHeight(context); |
James O'Leary | b81ccb4 | 2019-06-21 15:30:43 +0000 | [diff] [blame] | 98 | mGuide = new PerimeterPathGuide(context, createCornerPathRenderer(context), |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 99 | mStrokeWidth / 2, displayWidth, displayHeight); |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 100 | |
James O'Leary | b81ccb4 | 2019-06-21 15:30:43 +0000 | [diff] [blame] | 101 | int cornerRadiusBottom = DisplayUtils.getCornerRadiusBottom(context); |
| 102 | int cornerRadiusTop = DisplayUtils.getCornerRadiusTop(context); |
Miranda Kephart | c874531 | 2019-08-13 12:46:53 -0400 | [diff] [blame] | 103 | // ensure that height is non-zero even for square corners |
| 104 | mViewHeight = Math.max(Math.max(cornerRadiusBottom, cornerRadiusTop), |
| 105 | DisplayUtils.convertDpToPx(LIGHT_HEIGHT_DP, context)); |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 106 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 107 | final int dualToneDarkTheme = Utils.getThemeAttr(mContext, R.attr.darkIconTheme); |
| 108 | final int dualToneLightTheme = Utils.getThemeAttr(mContext, R.attr.lightIconTheme); |
| 109 | Context lightContext = new ContextThemeWrapper(mContext, dualToneLightTheme); |
| 110 | Context darkContext = new ContextThemeWrapper(mContext, dualToneDarkTheme); |
| 111 | mLightColor = Utils.getColorAttrDefaultColor(lightContext, R.attr.singleToneColor); |
| 112 | mDarkColor = Utils.getColorAttrDefaultColor(darkContext, R.attr.singleToneColor); |
| 113 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 114 | for (int i = 0; i < 4; i++) { |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 115 | mAssistInvocationLights.add(new EdgeLight(Color.TRANSPARENT, 0, 0)); |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Updates positions of the invocation lights based on the progress (a float between 0 and 1). |
| 121 | * The lights begin at the device corners and expand inward until they meet at the center. |
| 122 | */ |
| 123 | public void onInvocationProgress(float progress) { |
| 124 | if (progress == 0) { |
| 125 | setVisibility(View.GONE); |
| 126 | } else { |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 127 | attemptRegisterNavBarListener(); |
| 128 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 129 | float cornerLengthNormalized = |
| 130 | mGuide.getRegionWidth(PerimeterPathGuide.Region.BOTTOM_LEFT); |
| 131 | float arcLengthNormalized = cornerLengthNormalized * MINIMUM_CORNER_RATIO; |
| 132 | float arcOffsetNormalized = (cornerLengthNormalized - arcLengthNormalized) / 2f; |
| 133 | |
James O'Leary | bb3b318 | 2019-06-11 13:48:54 +0000 | [diff] [blame] | 134 | float minLightLength = 0; |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 135 | float maxLightLength = mGuide.getRegionWidth(PerimeterPathGuide.Region.BOTTOM) / 4f; |
| 136 | |
| 137 | float lightLength = MathUtils.lerp(minLightLength, maxLightLength, progress); |
| 138 | |
| 139 | float leftStart = (-cornerLengthNormalized + arcOffsetNormalized) * (1 - progress); |
| 140 | float rightStart = mGuide.getRegionWidth(PerimeterPathGuide.Region.BOTTOM) |
| 141 | + (cornerLengthNormalized - arcOffsetNormalized) * (1 - progress); |
| 142 | |
| 143 | setLight(0, leftStart, lightLength); |
| 144 | setLight(1, leftStart + lightLength, lightLength); |
| 145 | setLight(2, rightStart - (lightLength * 2), lightLength); |
| 146 | setLight(3, rightStart - lightLength, lightLength); |
| 147 | setVisibility(View.VISIBLE); |
| 148 | } |
| 149 | invalidate(); |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Hides and resets the invocation lights. |
| 154 | */ |
| 155 | public void hide() { |
| 156 | setVisibility(GONE); |
| 157 | for (EdgeLight light : mAssistInvocationLights) { |
| 158 | light.setLength(0); |
| 159 | } |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 160 | attemptUnregisterNavBarListener(); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Sets all invocation lights to a single color. If color is null, uses the navigation bar |
| 165 | * color (updated when the nav bar color changes). |
| 166 | */ |
| 167 | public void setColors(@Nullable @ColorInt Integer color) { |
| 168 | if (color == null) { |
| 169 | mUseNavBarColor = true; |
| 170 | mPaint.setStrokeCap(Paint.Cap.BUTT); |
| 171 | attemptRegisterNavBarListener(); |
| 172 | } else { |
| 173 | setColors(color, color, color, color); |
| 174 | } |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Sets the invocation light colors, from left to right. |
| 179 | */ |
| 180 | public void setColors(@ColorInt int color1, @ColorInt int color2, |
| 181 | @ColorInt int color3, @ColorInt int color4) { |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 182 | mUseNavBarColor = false; |
| 183 | attemptUnregisterNavBarListener(); |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 184 | mAssistInvocationLights.get(0).setColor(color1); |
| 185 | mAssistInvocationLights.get(1).setColor(color2); |
| 186 | mAssistInvocationLights.get(2).setColor(color3); |
| 187 | mAssistInvocationLights.get(3).setColor(color4); |
| 188 | } |
| 189 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 190 | /** |
| 191 | * Reacts to changes in the navigation bar color |
| 192 | * |
| 193 | * @param darkIntensity 0 is the lightest color, 1 is the darkest. |
| 194 | */ |
| 195 | @Override // NavigationBarTransitions.DarkIntensityListener |
| 196 | public void onDarkIntensity(float darkIntensity) { |
| 197 | updateDarkness(darkIntensity); |
| 198 | } |
| 199 | |
| 200 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 201 | @Override |
| 202 | protected void onFinishInflate() { |
| 203 | getLayoutParams().height = mViewHeight; |
| 204 | requestLayout(); |
| 205 | } |
| 206 | |
| 207 | @Override |
| 208 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { |
| 209 | super.onLayout(changed, left, top, right, bottom); |
| 210 | |
| 211 | int rotation = getContext().getDisplay().getRotation(); |
| 212 | mGuide.setRotation(rotation); |
| 213 | } |
| 214 | |
| 215 | @Override |
| 216 | protected void onDraw(Canvas canvas) { |
| 217 | // If the view doesn't take up the whole screen, offset the canvas by its translation |
| 218 | // distance such that PerimeterPathGuide's paths are drawn properly based upon the actual |
| 219 | // screen edges. |
| 220 | getLocationOnScreen(mScreenLocation); |
| 221 | canvas.translate(-mScreenLocation[0], -mScreenLocation[1]); |
| 222 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 223 | if (mUseNavBarColor) { |
| 224 | for (EdgeLight light : mAssistInvocationLights) { |
| 225 | renderLight(light, canvas); |
| 226 | } |
| 227 | } else { |
| 228 | mPaint.setStrokeCap(Paint.Cap.ROUND); |
| 229 | renderLight(mAssistInvocationLights.get(0), canvas); |
| 230 | renderLight(mAssistInvocationLights.get(3), canvas); |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 231 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 232 | mPaint.setStrokeCap(Paint.Cap.BUTT); |
| 233 | renderLight(mAssistInvocationLights.get(1), canvas); |
| 234 | renderLight(mAssistInvocationLights.get(2), canvas); |
| 235 | } |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | protected void setLight(int index, float offset, float length) { |
| 239 | if (index < 0 || index >= 4) { |
| 240 | Log.w(TAG, "invalid invocation light index: " + index); |
| 241 | } |
| 242 | mAssistInvocationLights.get(index).setOffset(offset); |
| 243 | mAssistInvocationLights.get(index).setLength(length); |
| 244 | } |
| 245 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 246 | /** |
James O'Leary | b81ccb4 | 2019-06-21 15:30:43 +0000 | [diff] [blame] | 247 | * Returns CornerPathRenderer to be used for rendering invocation lights. |
| 248 | * |
| 249 | * To render corners that aren't circular, override this method in a subclass. |
| 250 | */ |
| 251 | protected CornerPathRenderer createCornerPathRenderer(Context context) { |
Miranda Kephart | 5393915 | 2019-06-28 15:32:29 -0400 | [diff] [blame] | 252 | return new CircularCornerPathRenderer(context); |
James O'Leary | b81ccb4 | 2019-06-21 15:30:43 +0000 | [diff] [blame] | 253 | } |
Miranda Kephart | 5393915 | 2019-06-28 15:32:29 -0400 | [diff] [blame] | 254 | |
James O'Leary | b81ccb4 | 2019-06-21 15:30:43 +0000 | [diff] [blame] | 255 | /** |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 256 | * Receives an intensity from 0 (lightest) to 1 (darkest) and sets the handle color |
| 257 | * appropriately. Intention is to match the home handle color. |
| 258 | */ |
| 259 | protected void updateDarkness(float darkIntensity) { |
| 260 | if (mUseNavBarColor) { |
| 261 | @ColorInt int invocationColor = (int) ArgbEvaluator.getInstance().evaluate( |
| 262 | darkIntensity, mLightColor, mDarkColor); |
| 263 | for (EdgeLight light : mAssistInvocationLights) { |
| 264 | light.setColor(invocationColor); |
| 265 | } |
| 266 | invalidate(); |
| 267 | } |
| 268 | } |
| 269 | |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 270 | private void renderLight(EdgeLight light, Canvas canvas) { |
| 271 | mGuide.strokeSegment(mPath, light.getOffset(), light.getOffset() + light.getLength()); |
| 272 | mPaint.setColor(light.getColor()); |
| 273 | canvas.drawPath(mPath, mPaint); |
| 274 | } |
| 275 | |
Miranda Kephart | 2c464b8 | 2019-06-05 16:09:15 -0400 | [diff] [blame] | 276 | private void attemptRegisterNavBarListener() { |
| 277 | if (!mRegistered) { |
| 278 | NavigationBarController controller = Dependency.get(NavigationBarController.class); |
| 279 | if (controller == null) { |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | NavigationBarFragment navBar = controller.getDefaultNavigationBarFragment(); |
| 284 | if (navBar == null) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | updateDarkness(navBar.getBarTransitions().addDarkIntensityListener(this)); |
| 289 | mRegistered = true; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | private void attemptUnregisterNavBarListener() { |
| 294 | if (mRegistered) { |
| 295 | NavigationBarController controller = Dependency.get(NavigationBarController.class); |
| 296 | if (controller == null) { |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | NavigationBarFragment navBar = controller.getDefaultNavigationBarFragment(); |
| 301 | if (navBar == null) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | navBar.getBarTransitions().removeDarkIntensityListener(this); |
| 306 | mRegistered = false; |
| 307 | } |
| 308 | } |
Miranda Kephart | 433c811 | 2019-05-22 12:25:51 -0400 | [diff] [blame] | 309 | } |