1/*
2 * Copyright (C) 2015 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.tv.ui;
18
19import android.animation.Animator;
20import android.animation.ValueAnimator;
21import android.util.Log;
22import android.view.View;
23import android.view.ViewGroup.LayoutParams;
24
25import java.lang.reflect.InvocationTargetException;
26import java.lang.reflect.Method;
27
28/**
29 * A class that includes convenience methods for view classes.
30 */
31public class ViewUtils {
32    private static final String TAG = "ViewUtils";
33
34    private ViewUtils() {
35        // Prevent instantiation.
36    }
37
38    public static void setTransitionAlpha(View v, float alpha) {
39        Method method;
40        try {
41            method = View.class.getDeclaredMethod("setTransitionAlpha", Float.TYPE);
42            method.invoke(v, alpha);
43        } catch (NoSuchMethodException|IllegalAccessException|IllegalArgumentException
44                |InvocationTargetException e) {
45            Log.e(TAG, "Fail to call View.setTransitionAlpha", e);
46        }
47    }
48
49    /**
50     * Creates an animator in view's height
51     * @param target the {@link view} animator performs on.
52     */
53    public static Animator createHeightAnimator(
54            final View target, int initialHeight, int targetHeight) {
55        ValueAnimator animator = ValueAnimator.ofInt(initialHeight, targetHeight);
56        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
57            @Override
58            public void onAnimationUpdate(ValueAnimator animation) {
59                int value = (Integer) animation.getAnimatedValue();
60                if (value == 0) {
61                    if (target.getVisibility() != View.GONE) {
62                        target.setVisibility(View.GONE);
63                    }
64                } else {
65                    if (target.getVisibility() != View.VISIBLE) {
66                        target.setVisibility(View.VISIBLE);
67                    }
68                    setLayoutHeight(target, value);
69                }
70            }
71        });
72        return animator;
73    }
74
75    /**
76     * Gets view's layout height.
77     */
78    public static int getLayoutHeight(View view) {
79        LayoutParams layoutParams = view.getLayoutParams();
80        return layoutParams.height;
81    }
82
83    /**
84     * Sets view's layout height.
85     */
86    public static void setLayoutHeight(View view, int height) {
87        LayoutParams layoutParams = view.getLayoutParams();
88        if (height != layoutParams.height) {
89            layoutParams.height = height;
90            view.setLayoutParams(layoutParams);
91        }
92    }
93}