1/*
2 * Copyright (C) 2014 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 android.support.v4.view;
18
19import android.content.res.ColorStateList;
20import android.graphics.PorterDuff;
21import android.view.View;
22
23import java.lang.reflect.Field;
24
25class ViewCompatBase {
26
27    private static final String TAG = "ViewCompatBase";
28
29    private static Field sMinWidthField;
30    private static boolean sMinWidthFieldFetched;
31    private static Field sMinHeightField;
32    private static boolean sMinHeightFieldFetched;
33
34    static ColorStateList getBackgroundTintList(View view) {
35        return (view instanceof TintableBackgroundView)
36                ? ((TintableBackgroundView) view).getSupportBackgroundTintList()
37                : null;
38    }
39
40    static void setBackgroundTintList(View view, ColorStateList tintList) {
41        if (view instanceof TintableBackgroundView) {
42            ((TintableBackgroundView) view).setSupportBackgroundTintList(tintList);
43        }
44    }
45
46    static PorterDuff.Mode getBackgroundTintMode(View view) {
47        return (view instanceof TintableBackgroundView)
48                ? ((TintableBackgroundView) view).getSupportBackgroundTintMode()
49                : null;
50    }
51
52    static void setBackgroundTintMode(View view, PorterDuff.Mode mode) {
53        if (view instanceof TintableBackgroundView) {
54            ((TintableBackgroundView) view).setSupportBackgroundTintMode(mode);
55        }
56    }
57
58    static boolean isLaidOut(View view) {
59        return view.getWidth() > 0 && view.getHeight() > 0;
60    }
61
62    static int getMinimumWidth(View view) {
63        if (!sMinWidthFieldFetched) {
64            try {
65                sMinWidthField = View.class.getDeclaredField("mMinWidth");
66                sMinWidthField.setAccessible(true);
67            } catch (NoSuchFieldException e) {
68                // Couldn't find the field. Abort!
69            }
70            sMinWidthFieldFetched = true;
71        }
72
73        if (sMinWidthField != null) {
74            try {
75                return (int) sMinWidthField.get(view);
76            } catch (Exception e) {
77                // Field get failed. Oh well...
78            }
79        }
80
81        // We failed, return 0
82        return 0;
83    }
84
85    static int getMinimumHeight(View view) {
86        if (!sMinHeightFieldFetched) {
87            try {
88                sMinHeightField = View.class.getDeclaredField("mMinHeight");
89                sMinHeightField.setAccessible(true);
90            } catch (NoSuchFieldException e) {
91                // Couldn't find the field. Abort!
92            }
93            sMinHeightFieldFetched = true;
94        }
95
96        if (sMinHeightField != null) {
97            try {
98                return (int) sMinHeightField.get(view);
99            } catch (Exception e) {
100                // Field get failed. Oh well...
101            }
102        }
103
104        // We failed, return 0
105        return 0;
106    }
107
108    static boolean isAttachedToWindow(View view) {
109        return view.getWindowToken() != null;
110    }
111}
112