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        @Override
63        public void setClipToRoundedOutline(View view, boolean clip, int radius) {
64            // Not supported
65        }
66    }
67
68    /**
69     * Implementation used on api 21 (and above).
70     */
71    private static final class Api21Impl implements Impl {
72        @Override
73        public void setClipToRoundedOutline(View view, boolean clip, int radius) {
74            RoundedRectHelperApi21.setClipToRoundedOutline(view, clip, radius);
75        }
76    }
77
78    private RoundedRectHelper() {
79        if (supportsRoundedCorner()) {
80            mImpl = new Api21Impl();
81        } else {
82            mImpl = new StubImpl();
83        }
84    }
85}
86