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