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.systemui.statusbar.notification;
18
19import android.util.Pools;
20import android.view.NotificationHeaderView;
21import android.view.View;
22
23import com.android.systemui.statusbar.CrossFadeHelper;
24
25/**
26 * A transform state of a text view.
27*/
28public class HeaderTransformState extends TransformState {
29
30    private static Pools.SimplePool<HeaderTransformState> sInstancePool
31            = new Pools.SimplePool<>(40);
32    private View mExpandButton;
33    private View mWorkProfileIcon;
34    private TransformState mWorkProfileState;
35
36    @Override
37    public void initFrom(View view) {
38        super.initFrom(view);
39        if (view instanceof NotificationHeaderView) {
40            NotificationHeaderView header = (NotificationHeaderView) view;
41            mExpandButton = header.getExpandButton();
42            mWorkProfileState = TransformState.obtain();
43            mWorkProfileIcon = header.getWorkProfileIcon();
44            mWorkProfileState.initFrom(mWorkProfileIcon);
45        }
46    }
47
48    @Override
49    public boolean transformViewTo(TransformState otherState, float transformationAmount) {
50        // if the transforming notification has a header, we have ensured that it looks the same
51        // but the expand button, so lets fade just that one and transform the work profile icon.
52        if (!(mTransformedView instanceof NotificationHeaderView)) {
53            return false;
54        }
55        NotificationHeaderView header = (NotificationHeaderView) mTransformedView;
56        int childCount = header.getChildCount();
57        for (int i = 0; i < childCount; i++) {
58            View headerChild = header.getChildAt(i);
59            if (headerChild.getVisibility() == View.GONE) {
60                continue;
61            }
62            if (headerChild != mExpandButton) {
63                headerChild.setVisibility(View.INVISIBLE);
64            } else {
65                CrossFadeHelper.fadeOut(mExpandButton, transformationAmount);
66            }
67        }
68        return true;
69    }
70
71    @Override
72    public void transformViewFrom(TransformState otherState, float transformationAmount) {
73        // if the transforming notification has a header, we have ensured that it looks the same
74        // but the expand button, so lets fade just that one and transform the work profile icon.
75        if (!(mTransformedView instanceof NotificationHeaderView)) {
76            return;
77        }
78        NotificationHeaderView header = (NotificationHeaderView) mTransformedView;
79        header.setVisibility(View.VISIBLE);
80        header.setAlpha(1.0f);
81        int childCount = header.getChildCount();
82        for (int i = 0; i < childCount; i++) {
83            View headerChild = header.getChildAt(i);
84            if (headerChild.getVisibility() == View.GONE) {
85                continue;
86            }
87            if (headerChild == mExpandButton) {
88                CrossFadeHelper.fadeIn(mExpandButton, transformationAmount);
89            } else {
90                headerChild.setVisibility(View.VISIBLE);
91                if (headerChild == mWorkProfileIcon) {
92                    mWorkProfileState.transformViewFullyFrom(
93                            ((HeaderTransformState) otherState).mWorkProfileState,
94                            transformationAmount);
95                }
96            }
97        }
98        return;
99    }
100
101    public static HeaderTransformState obtain() {
102        HeaderTransformState instance = sInstancePool.acquire();
103        if (instance != null) {
104            return instance;
105        }
106        return new HeaderTransformState();
107    }
108
109    @Override
110    public void recycle() {
111        super.recycle();
112        sInstancePool.release(this);
113    }
114
115    @Override
116    protected void reset() {
117        super.reset();
118        mExpandButton = null;
119        mWorkProfileState = null;
120        if (mWorkProfileState != null) {
121            mWorkProfileState.recycle();
122            mWorkProfileState = null;
123        }
124    }
125
126    @Override
127    public void setVisible(boolean visible, boolean force) {
128        super.setVisible(visible, force);
129        if (!(mTransformedView instanceof NotificationHeaderView)) {
130            return;
131        }
132        NotificationHeaderView header = (NotificationHeaderView) mTransformedView;
133        int childCount = header.getChildCount();
134        for (int i = 0; i < childCount; i++) {
135            View headerChild = header.getChildAt(i);
136            if (!force && headerChild.getVisibility() == View.GONE) {
137                continue;
138            }
139            headerChild.animate().cancel();
140            if (headerChild.getVisibility() != View.GONE) {
141                headerChild.setVisibility(visible ? View.VISIBLE : View.INVISIBLE);
142            }
143            if (headerChild == mExpandButton) {
144                headerChild.setAlpha(visible ? 1.0f : 0.0f);
145            }
146            if (headerChild == mWorkProfileIcon) {
147                headerChild.setTranslationX(0);
148                headerChild.setTranslationY(0);
149            }
150        }
151    }
152
153    @Override
154    public void prepareFadeIn() {
155        super.prepareFadeIn();
156        if (!(mTransformedView instanceof NotificationHeaderView)) {
157            return;
158        }
159        NotificationHeaderView header = (NotificationHeaderView) mTransformedView;
160        int childCount = header.getChildCount();
161        for (int i = 0; i < childCount; i++) {
162            View headerChild = header.getChildAt(i);
163            if (headerChild.getVisibility() == View.GONE) {
164                continue;
165            }
166            headerChild.animate().cancel();
167            headerChild.setVisibility(View.VISIBLE);
168            headerChild.setAlpha(1.0f);
169            if (headerChild == mWorkProfileIcon) {
170                headerChild.setTranslationX(0);
171                headerChild.setTranslationY(0);
172            }
173        }
174    }
175}
176