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
17package com.android.launcher3.anim;
18
19import android.animation.Animator;
20import android.animation.AnimatorListenerAdapter;
21import android.util.ArrayMap;
22import android.view.View;
23import java.util.Iterator;
24import java.util.Map;
25
26/**
27 * Helper class to automatically build view hardware layers for the duration of an animation.
28 */
29public class AnimationLayerSet extends AnimatorListenerAdapter {
30
31    private final ArrayMap<View, Integer> mViewsToLayerTypeMap;
32
33    public AnimationLayerSet() {
34        mViewsToLayerTypeMap = new ArrayMap<>();
35    }
36
37    public AnimationLayerSet(View v) {
38        mViewsToLayerTypeMap = new ArrayMap<>(1);
39        addView(v);
40    }
41
42    public void addView(View v) {
43        mViewsToLayerTypeMap.put(v, v.getLayerType());
44    }
45
46    @Override
47    public void onAnimationStart(Animator animation) {
48        // Enable all necessary layers
49        Iterator<Map.Entry<View, Integer>> itr = mViewsToLayerTypeMap.entrySet().iterator();
50        while (itr.hasNext()) {
51            Map.Entry<View, Integer> entry = itr.next();
52            View v = entry.getKey();
53            entry.setValue(v.getLayerType());
54            v.setLayerType(View.LAYER_TYPE_HARDWARE, null);
55            if (v.isAttachedToWindow() && v.getVisibility() == View.VISIBLE) {
56                v.buildLayer();
57            }
58        }
59    }
60
61    @Override
62    public void onAnimationEnd(Animator animation) {
63        Iterator<Map.Entry<View, Integer>> itr = mViewsToLayerTypeMap.entrySet().iterator();
64        while (itr.hasNext()) {
65            Map.Entry<View, Integer> entry = itr.next();
66            entry.getKey().setLayerType(entry.getValue(), null);
67        }
68    }
69}
70