Home | History | Annotate | Download | only in util
      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.android.setupwizardlib.util;
     18 
     19 import android.content.Context;
     20 import android.graphics.drawable.Drawable;
     21 import android.graphics.drawable.InsetDrawable;
     22 import android.os.Build;
     23 import android.view.View;
     24 
     25 /**
     26  * Provides convenience methods to handle drawable layout directions in different SDK versions.
     27  */
     28 public class DrawableLayoutDirectionHelper {
     29 
     30     /**
     31      * Creates an {@link android.graphics.drawable.InsetDrawable} according to the layout direction
     32      * of {@code view}.
     33      */
     34     public static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
     35             int insetStart, int insetTop, int insetEnd, int insetBottom, View view) {
     36         boolean isRtl = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1
     37                 && view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
     38         return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom,
     39                 isRtl);
     40     }
     41 
     42     /**
     43      * Creates an {@link android.graphics.drawable.InsetDrawable} according to the layout direction
     44      * of {@code context}.
     45      */
     46     public static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
     47             int insetStart, int insetTop, int insetEnd, int insetBottom, Context context) {
     48         boolean isRtl = false;
     49         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
     50             final int layoutDirection =
     51                     context.getResources().getConfiguration().getLayoutDirection();
     52             isRtl = layoutDirection == View.LAYOUT_DIRECTION_RTL;
     53         }
     54         return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom,
     55                 isRtl);
     56     }
     57 
     58     /**
     59      * Creates an {@link android.graphics.drawable.InsetDrawable} according to
     60      * {@code layoutDirection}.
     61      */
     62     public static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
     63             int insetStart, int insetTop, int insetEnd, int insetBottom, int layoutDirection) {
     64         //noinspection AndroidLintInlinedApi
     65         return createRelativeInsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom,
     66                 layoutDirection == View.LAYOUT_DIRECTION_RTL);
     67     }
     68 
     69     private static InsetDrawable createRelativeInsetDrawable(Drawable drawable,
     70             int insetStart, int insetTop, int insetEnd, int insetBottom, boolean isRtl) {
     71         if (isRtl) {
     72             return new InsetDrawable(drawable, insetEnd, insetTop, insetStart, insetBottom);
     73         } else {
     74             return new InsetDrawable(drawable, insetStart, insetTop, insetEnd, insetBottom);
     75         }
     76     }
     77 }
     78