RoundedRectHelper.java revision 4f34a05cdf73b68c3b2eb8678f740ab15225126a
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.widget;
15
16import android.graphics.Color;
17import android.graphics.drawable.ColorDrawable;
18import android.os.Build;
19import android.view.View;
20
21/**
22 * Helper for setting rounded rectangle backgrounds on a view.
23 */
24final class RoundedRectHelper {
25
26    private final static RoundedRectHelper sInstance = new RoundedRectHelper();
27    private Impl mImpl;
28
29    /**
30     * Returns an instance of the helper.
31     */
32    public static RoundedRectHelper getInstance() {
33        return sInstance;
34    }
35
36    /**
37     * Sets a rounded rectangle background on the given view, and clips the view to the
38     * background.  If clipping isn't supported on the android runtime, a simple rectangle
39     * background is set instead.
40     *
41     * @param view The view to be modified
42     * @param color The color of the background
43     */
44    public void setRoundedRectBackground(View view, int color) {
45        mImpl.setRoundedRectBackground(view, color);
46    }
47
48    /**
49     * Clears the background of the view to transparent.
50     *
51     * @param view The view to be modified
52     */
53    public void clearBackground(View view) {
54        mImpl.clearBackground(view);
55    }
56
57    static interface Impl {
58        public void setRoundedRectBackground(View view, int color);
59        public void clearBackground(View view);
60    }
61
62    /**
63     * Implementation used prior to L.
64     */
65    private static final class StubImpl implements Impl {
66        @Override
67        public void setRoundedRectBackground(View view, int color) {
68            // We could set a rounded rect background, but we don't
69            // because we can't do setClipToOutline.
70            // So just set a regular rectangle.
71            view.setBackgroundColor(color);
72        }
73
74        @Override
75        public void clearBackground(View view) {
76            view.setBackground(null);
77        }
78    }
79
80    /**
81     * Implementation used on api 21 (and above).
82     */
83    private static final class Api21Impl implements Impl {
84        @Override
85        public void setRoundedRectBackground(View view, int color) {
86            RoundedRectHelperApi21.setRoundedRectBackground(view, color);
87        }
88
89        @Override
90        public void clearBackground(View view) {
91            RoundedRectHelperApi21.clearBackground(view);
92        }
93    }
94
95    private RoundedRectHelper() {
96        // TODO: we should use version number once "L" is published
97        if ("L".equals(Build.VERSION.RELEASE)) {
98            mImpl = new Api21Impl();
99        } else {
100            mImpl = new StubImpl();
101        }
102    }
103}
104