DrawableLayoutDirectionHelper.java revision 63cdc5ccb7f1f61d0e752e38d7e57c690658a0ac
1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.setupwizardlib.util;
18
19import android.annotation.SuppressLint;
20import android.content.Context;
21import android.graphics.drawable.Drawable;
22import android.graphics.drawable.InsetDrawable;
23import android.os.Build;
24import android.view.View;
25
26/**
27 * Provides convenience methods to handle drawable layout directions in different SDK versions.
28 */
29public class DrawableLayoutDirectionHelper {
30
31    /**
32     * Creates an {@link android.graphics.drawable.InsetDrawable} according to the layout direction
33     * of {@code view}.
34     */
35    @SuppressLint("InlinedApi")  // Use of View.LAYOUT_DIRECTION_RTL is guarded by version check
36    public static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
37            int insetStart, int insetTop, int insetEnd, int insetBottom, View view) {
38        boolean isRtl = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
39                && view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
40        return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom,
41                isRtl);
42    }
43
44    /**
45     * Creates an {@link android.graphics.drawable.InsetDrawable} according to the layout direction
46     * of {@code context}.
47     */
48    @SuppressLint("InlinedApi")  // Use of View.LAYOUT_DIRECTION_RTL is guarded by version check
49    public static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
50            int insetStart, int insetTop, int insetEnd, int insetBottom, Context context) {
51        boolean isRtl = false;
52        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
53            final int layoutDirection =
54                    context.getResources().getConfiguration().getLayoutDirection();
55            isRtl = layoutDirection == View.LAYOUT_DIRECTION_RTL;
56        }
57        return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom,
58                isRtl);
59    }
60
61    /**
62     * Creates an {@link android.graphics.drawable.InsetDrawable} according to
63     * {@code layoutDirection}.
64     */
65    @SuppressLint("InlinedApi")  // Given layoutDirection will not be View.LAYOUT_DIRECTION_RTL if
66                                 // SDK version doesn't support it.
67    public static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
68            int insetStart, int insetTop, int insetEnd, int insetBottom, int layoutDirection) {
69        return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom,
70                layoutDirection == View.LAYOUT_DIRECTION_RTL);
71    }
72
73    private static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
74            int insetStart, int insetTop, int insetEnd, int insetBottom, boolean isRtl) {
75        if (isRtl) {
76            return new InsetDrawable(drawable, insetEnd, insetTop, insetStart, insetBottom);
77        } else {
78            return new InsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom);
79        }
80    }
81}
82