1 /* 2 * Copyright (C) 2016 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 19 import android.animation.AnimatorSet; 20 import android.animation.ObjectAnimator; 21 import android.animation.ValueAnimator; 22 import android.animation.ValueAnimator.AnimatorUpdateListener; 23 import android.content.Context; 24 import android.graphics.Canvas; 25 import android.graphics.Color; 26 import android.graphics.Paint; 27 import android.util.AttributeSet; 28 import android.view.View; 29 30 import com.android.launcher3.Workspace.State; 31 32 /** 33 * A simple view used to show the region blocked by QSB during drag and drop. 34 */ 35 public class QsbBlockerView extends View implements Workspace.OnStateChangeListener { 36 37 private static final int VISIBLE_ALPHA = 100; 38 39 private final Paint mBgPaint; 40 QsbBlockerView(Context context, AttributeSet attrs)41 public QsbBlockerView(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 44 mBgPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 45 mBgPaint.setColor(Color.WHITE); 46 mBgPaint.setAlpha(0); 47 } 48 49 @Override onAttachedToWindow()50 protected void onAttachedToWindow() { 51 super.onAttachedToWindow(); 52 53 Workspace w = Launcher.getLauncher(getContext()).getWorkspace(); 54 w.setOnStateChangeListener(this); 55 prepareStateChange(w.getState(), null); 56 } 57 58 @Override prepareStateChange(State toState, AnimatorSet targetAnim)59 public void prepareStateChange(State toState, AnimatorSet targetAnim) { 60 int finalAlpha = getAlphaForState(toState); 61 if (targetAnim == null) { 62 mBgPaint.setAlpha(finalAlpha); 63 invalidate(); 64 } else { 65 ObjectAnimator anim = ObjectAnimator.ofArgb(mBgPaint, "alpha", finalAlpha); 66 anim.addUpdateListener(new AnimatorUpdateListener() { 67 @Override 68 public void onAnimationUpdate(ValueAnimator valueAnimator) { 69 invalidate(); 70 } 71 }); 72 targetAnim.play(anim); 73 } 74 } 75 getAlphaForState(State state)76 private static int getAlphaForState(State state) { 77 switch (state) { 78 case SPRING_LOADED: 79 case OVERVIEW: 80 case OVERVIEW_HIDDEN: 81 return VISIBLE_ALPHA; 82 } 83 return 0; 84 } 85 86 @Override onDraw(Canvas canvas)87 protected void onDraw(Canvas canvas) { 88 canvas.drawPaint(mBgPaint); 89 } 90 } 91