1package android.support.v17.leanback.widget;
2
3import android.graphics.drawable.Drawable;
4import android.os.Build;
5import android.view.View;
6
7final class ForegroundHelper {
8
9    final static ForegroundHelper sInstance = new ForegroundHelper();
10    ForegroundHelperVersionImpl mImpl;
11
12    /**
13     * Interface implemented by classes that support Shadow.
14     */
15    static interface ForegroundHelperVersionImpl {
16
17        public void setForeground(View view, Drawable drawable);
18
19        public Drawable getForeground(View view);
20    }
21
22    /**
23     * Implementation used on api 23 (and above).
24     */
25    private static final class ForegroundHelperApi23Impl implements ForegroundHelperVersionImpl {
26        ForegroundHelperApi23Impl() {
27        }
28
29        @Override
30        public void setForeground(View view, Drawable drawable) {
31            ForegroundHelperApi23.setForeground(view, drawable);
32        }
33
34        @Override
35        public Drawable getForeground(View view) {
36            return ForegroundHelperApi23.getForeground(view);
37        }
38    }
39
40    /**
41     * Stub implementation
42     */
43    private static final class ForegroundHelperStubImpl implements ForegroundHelperVersionImpl {
44        ForegroundHelperStubImpl() {
45        }
46
47        @Override
48        public void setForeground(View view, Drawable drawable) {
49        }
50
51        @Override
52        public Drawable getForeground(View view) {
53            return null;
54        }
55    }
56
57    private ForegroundHelper() {
58        if (supportsForeground()) {
59            mImpl = new ForegroundHelperApi23Impl();
60        } else {
61            mImpl = new ForegroundHelperStubImpl();
62        }
63    }
64
65    public static ForegroundHelper getInstance() {
66        return sInstance;
67    }
68
69    /**
70     * Returns true if view.setForeground() is supported.
71     */
72    public static boolean supportsForeground() {
73        return Build.VERSION.SDK_INT >= 23;
74    }
75
76    public Drawable getForeground(View view) {
77        return mImpl.getForeground(view);
78    }
79
80    public void setForeground(View view, Drawable drawable) {
81        mImpl.setForeground(view, drawable);
82    }
83}
84