Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.launcher3; |
| 18 | |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 19 | import android.animation.ObjectAnimator; |
| 20 | import android.animation.PropertyValuesHolder; |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.graphics.Canvas; |
| 23 | import android.util.AttributeSet; |
| 24 | import android.util.Pair; |
| 25 | import android.view.View; |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 26 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 27 | import com.android.launcher3.util.Thunk; |
| 28 | |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 29 | public class FocusIndicatorView extends View implements View.OnFocusChangeListener { |
| 30 | |
| 31 | // It can be any number >0. The view is resized using scaleX and scaleY. |
| 32 | static final int DEFAULT_LAYOUT_SIZE = 100; |
| 33 | private static final float MIN_VISIBLE_ALPHA = 0.2f; |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 34 | private static final long ANIM_DURATION = 150; |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 35 | |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 36 | private final int[] mIndicatorPos = new int[2]; |
| 37 | private final int[] mTargetViewPos = new int[2]; |
| 38 | |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 39 | private ObjectAnimator mCurrentAnimation; |
| 40 | private ViewAnimState mTargetState; |
| 41 | |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 42 | private View mLastFocusedView; |
| 43 | private boolean mInitiated; |
| 44 | |
| 45 | private Pair<View, Boolean> mPendingCall; |
| 46 | |
| 47 | public FocusIndicatorView(Context context) { |
| 48 | this(context, null); |
| 49 | } |
| 50 | |
| 51 | public FocusIndicatorView(Context context, AttributeSet attrs) { |
| 52 | super(context, attrs); |
| 53 | setAlpha(0); |
| 54 | setBackgroundColor(getResources().getColor(R.color.focused_background)); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 59 | super.onSizeChanged(w, h, oldw, oldh); |
| 60 | |
| 61 | // Redraw if it is already showing. This avoids a bug where the height changes by a small |
| 62 | // amount on connecting/disconnecting a bluetooth keyboard. |
| 63 | if (mLastFocusedView != null) { |
| 64 | mPendingCall = Pair.create(mLastFocusedView, Boolean.TRUE); |
| 65 | invalidate(); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onFocusChange(View v, boolean hasFocus) { |
| 71 | mPendingCall = null; |
| 72 | if (!mInitiated && (getWidth() == 0)) { |
| 73 | // View not yet laid out. Wait until the view is ready to be drawn, so that be can |
| 74 | // get the location on screen. |
| 75 | mPendingCall = Pair.create(v, hasFocus); |
| 76 | invalidate(); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | if (!mInitiated) { |
Sunny Goyal | a39b82e | 2015-03-05 13:43:28 -0800 | [diff] [blame] | 81 | // The parent view should always the a parent of the target view. |
Sunny Goyal | ee99510 | 2015-03-05 16:27:37 -0800 | [diff] [blame] | 82 | computeLocationRelativeToParent(this, (View) getParent(), mIndicatorPos); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 83 | mInitiated = true; |
| 84 | } |
| 85 | |
| 86 | if (hasFocus) { |
| 87 | int indicatorWidth = getWidth(); |
| 88 | int indicatorHeight = getHeight(); |
| 89 | |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 90 | endCurrentAnimation(); |
| 91 | ViewAnimState nextState = new ViewAnimState(); |
| 92 | nextState.scaleX = v.getScaleX() * v.getWidth() / indicatorWidth; |
| 93 | nextState.scaleY = v.getScaleY() * v.getHeight() / indicatorHeight; |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 94 | |
Sunny Goyal | ee99510 | 2015-03-05 16:27:37 -0800 | [diff] [blame] | 95 | computeLocationRelativeToParent(v, (View) getParent(), mTargetViewPos); |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 96 | nextState.x = mTargetViewPos[0] - mIndicatorPos[0] - (1 - nextState.scaleX) * indicatorWidth / 2; |
| 97 | nextState.y = mTargetViewPos[1] - mIndicatorPos[1] - (1 - nextState.scaleY) * indicatorHeight / 2; |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 98 | |
| 99 | if (getAlpha() > MIN_VISIBLE_ALPHA) { |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 100 | mTargetState = nextState; |
| 101 | mCurrentAnimation = LauncherAnimUtils.ofPropertyValuesHolder(this, |
| 102 | PropertyValuesHolder.ofFloat(View.ALPHA, 1), |
| 103 | PropertyValuesHolder.ofFloat(View.TRANSLATION_X, mTargetState.x), |
| 104 | PropertyValuesHolder.ofFloat(View.TRANSLATION_Y, mTargetState.y), |
| 105 | PropertyValuesHolder.ofFloat(View.SCALE_X, mTargetState.scaleX), |
| 106 | PropertyValuesHolder.ofFloat(View.SCALE_Y, mTargetState.scaleY)); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 107 | } else { |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 108 | applyState(nextState); |
| 109 | mCurrentAnimation = LauncherAnimUtils.ofPropertyValuesHolder(this, |
| 110 | PropertyValuesHolder.ofFloat(View.ALPHA, 1)); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 111 | } |
| 112 | mLastFocusedView = v; |
| 113 | } else { |
| 114 | if (mLastFocusedView == v) { |
| 115 | mLastFocusedView = null; |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 116 | endCurrentAnimation(); |
| 117 | mCurrentAnimation = LauncherAnimUtils.ofPropertyValuesHolder(this, |
| 118 | PropertyValuesHolder.ofFloat(View.ALPHA, 0)); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 119 | } |
| 120 | } |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 121 | if (mCurrentAnimation != null) { |
| 122 | mCurrentAnimation.setDuration(ANIM_DURATION).start(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | private void endCurrentAnimation() { |
| 127 | if (mCurrentAnimation != null) { |
| 128 | mCurrentAnimation.cancel(); |
| 129 | mCurrentAnimation = null; |
| 130 | } |
| 131 | if (mTargetState != null) { |
| 132 | applyState(mTargetState); |
| 133 | mTargetState = null; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | private void applyState(ViewAnimState state) { |
| 138 | setTranslationX(state.x); |
| 139 | setTranslationY(state.y); |
| 140 | setScaleX(state.scaleX); |
| 141 | setScaleY(state.scaleY); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | @Override |
| 145 | protected void onDraw(Canvas canvas) { |
| 146 | if (mPendingCall != null) { |
| 147 | onFocusChange(mPendingCall.first, mPendingCall.second); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
Sunny Goyal | 059228a | 2015-04-07 15:30:26 -0700 | [diff] [blame] | 152 | * Computes the location of a view relative to {@param parent}, off-setting |
Sunny Goyal | a39b82e | 2015-03-05 13:43:28 -0800 | [diff] [blame] | 153 | * any shift due to page view scroll. |
| 154 | * @param pos an array of two integers in which to hold the coordinates |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 155 | */ |
Sunny Goyal | a39b82e | 2015-03-05 13:43:28 -0800 | [diff] [blame] | 156 | private static void computeLocationRelativeToParent(View v, View parent, int[] pos) { |
| 157 | pos[0] = pos[1] = 0; |
| 158 | computeLocationRelativeToParentHelper(v, parent, pos); |
| 159 | |
| 160 | // If a view is scaled, its position will also shift accordingly. For optimization, only |
| 161 | // consider this for the last node. |
| 162 | pos[0] += (1 - v.getScaleX()) * v.getWidth() / 2; |
| 163 | pos[1] += (1 - v.getScaleY()) * v.getHeight() / 2; |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Sunny Goyal | a39b82e | 2015-03-05 13:43:28 -0800 | [diff] [blame] | 166 | private static void computeLocationRelativeToParentHelper(View child, |
| 167 | View commonParent, int[] shift) { |
| 168 | View parent = (View) child.getParent(); |
Sunny Goyal | a39b82e | 2015-03-05 13:43:28 -0800 | [diff] [blame] | 169 | shift[0] += child.getLeft(); |
| 170 | shift[1] += child.getTop(); |
Sunny Goyal | 059228a | 2015-04-07 15:30:26 -0700 | [diff] [blame] | 171 | if (parent instanceof PagedView) { |
| 172 | PagedView page = (PagedView) parent; |
| 173 | shift[0] -= page.getScrollForPage(page.indexOfChild(child)); |
| 174 | } |
Sunny Goyal | a39b82e | 2015-03-05 13:43:28 -0800 | [diff] [blame] | 175 | |
| 176 | if (parent != commonParent) { |
| 177 | computeLocationRelativeToParentHelper(parent, commonParent, shift); |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 178 | } |
| 179 | } |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 180 | |
Adam Cohen | 091440a | 2015-03-18 14:16:05 -0700 | [diff] [blame] | 181 | @Thunk static final class ViewAnimState { |
Sunny Goyal | 5edd6bf | 2014-10-14 16:12:12 -0700 | [diff] [blame] | 182 | float x, y, scaleX, scaleY; |
| 183 | } |
Sunny Goyal | dcbcc86 | 2014-08-12 15:58:36 -0700 | [diff] [blame] | 184 | } |