RoundedRectHelper.java revision 99ec8b0cb375f7e5577ea3ec9f09e6ff7a95de0d
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.support.v17.leanback.R;
17import android.os.Build;
18import android.view.View;
19
20/**
21 * Helper for setting rounded rectangle backgrounds on a view.
22 */
23final class RoundedRectHelper {
24
25    private final static RoundedRectHelper sInstance = new RoundedRectHelper();
26    private final Impl mImpl;
27
28    /**
29     * Returns an instance of the helper.
30     */
31    public static RoundedRectHelper getInstance() {
32        return sInstance;
33    }
34
35    public static boolean supportsRoundedCorner() {
36        return Build.VERSION.SDK_INT >= 21;
37    }
38
39    /**
40     * Sets or removes a rounded rectangle outline on the given view.
41     */
42    public void setClipToRoundedOutline(View view, boolean clip, int radius) {
43        mImpl.setClipToRoundedOutline(view, clip, radius);
44    }
45
46    /**
47     * Sets or removes a rounded rectangle outline on the given view.
48     */
49    public void setClipToRoundedOutline(View view, boolean clip) {
50        mImpl.setClipToRoundedOutline(view, clip, view.getResources().getDimensionPixelSize(
51                R.dimen.lb_rounded_rect_corner_radius));
52    }
53
54    static interface Impl {
55        public void setClipToRoundedOutline(View view, boolean clip, int radius);
56    }
57
58    /**
59     * Implementation used prior to L.
60     */
61    private static final class StubImpl implements Impl {
62        StubImpl() {
63        }
64
65        @Override
66        public void setClipToRoundedOutline(View view, boolean clip, int radius) {
67            // Not supported
68        }
69    }
70
71    /**
72     * Implementation used on api 21 (and above).
73     */
74    private static final class Api21Impl implements Impl {
75        Api21Impl() {
76        }
77
78        @Override
79        public void setClipToRoundedOutline(View view, boolean clip, int radius) {
80            RoundedRectHelperApi21.setClipToRoundedOutline(view, clip, radius);
81        }
82    }
83
84    private RoundedRectHelper() {
85        if (supportsRoundedCorner()) {
86            mImpl = new Api21Impl();
87        } else {
88            mImpl = new StubImpl();
89        }
90    }
91}
92