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