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 17 package com.google.android.setupdesign.util; 18 19 import android.annotation.SuppressLint; 20 import android.content.Context; 21 import android.graphics.drawable.Drawable; 22 import android.graphics.drawable.InsetDrawable; 23 import android.os.Build; 24 import android.view.View; 25 26 /** Provides convenience methods to handle drawable layout directions in different SDK versions. */ 27 public class DrawableLayoutDirectionHelper { 28 29 /** 30 * Creates an {@link android.graphics.drawable.InsetDrawable} according to the layout direction of 31 * {@code view}. 32 */ 33 @SuppressLint("InlinedApi") // Use of View.LAYOUT_DIRECTION_RTL is guarded by version check createRelativeInsetDrawable( Drawable drawable, int insetStart, int insetTop, int insetEnd, int insetBottom, View view)34 public static InsetDrawable createRelativeInsetDrawable( 35 Drawable drawable, int insetStart, int insetTop, int insetEnd, int insetBottom, View view) { 36 boolean isRtl = 37 Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 38 && view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL; 39 return createRelativeInsetDrawable( 40 drawable, insetStart, insetTop, insetEnd, insetBottom, isRtl); 41 } 42 43 /** 44 * Creates an {@link android.graphics.drawable.InsetDrawable} according to the layout direction of 45 * {@code context}. 46 */ 47 @SuppressLint("InlinedApi") // Use of View.LAYOUT_DIRECTION_RTL is guarded by version check createRelativeInsetDrawable( Drawable drawable, int insetStart, int insetTop, int insetEnd, int insetBottom, Context context)48 public static InsetDrawable createRelativeInsetDrawable( 49 Drawable drawable, 50 int insetStart, 51 int insetTop, 52 int insetEnd, 53 int insetBottom, 54 Context context) { 55 boolean isRtl = false; 56 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 57 final int layoutDirection = context.getResources().getConfiguration().getLayoutDirection(); 58 isRtl = layoutDirection == View.LAYOUT_DIRECTION_RTL; 59 } 60 return createRelativeInsetDrawable( 61 drawable, insetStart, insetTop, insetEnd, insetBottom, isRtl); 62 } 63 64 /** 65 * Creates an {@link android.graphics.drawable.InsetDrawable} according to {@code 66 * layoutDirection}. 67 */ 68 @SuppressLint("InlinedApi") // Given layoutDirection will not be View.LAYOUT_DIRECTION_RTL if 69 // SDK version doesn't support it. createRelativeInsetDrawable( Drawable drawable, int insetStart, int insetTop, int insetEnd, int insetBottom, int layoutDirection)70 public static InsetDrawable createRelativeInsetDrawable( 71 Drawable drawable, 72 int insetStart, 73 int insetTop, 74 int insetEnd, 75 int insetBottom, 76 int layoutDirection) { 77 return createRelativeInsetDrawable( 78 drawable, 79 insetStart, 80 insetTop, 81 insetEnd, 82 insetBottom, 83 layoutDirection == View.LAYOUT_DIRECTION_RTL); 84 } 85 createRelativeInsetDrawable( Drawable drawable, int insetStart, int insetTop, int insetEnd, int insetBottom, boolean isRtl)86 private static InsetDrawable createRelativeInsetDrawable( 87 Drawable drawable, 88 int insetStart, 89 int insetTop, 90 int insetEnd, 91 int insetBottom, 92 boolean isRtl) { 93 if (isRtl) { 94 return new InsetDrawable(drawable, insetEnd, insetTop, insetStart, insetBottom); 95 } else { 96 return new InsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom); 97 } 98 } 99 } 100