gt:             boolean isRtl = isLayoutRtl(textView);
context: // Copyright 2013 The Chromium Authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  package org.chromium.base;
  
  import android.annotation.TargetApi;
  import android.app.Activity;
  import android.app.ActivityManager;
  import android.app.PendingIntent;
  import android.content.ContentResolver;
  import android.content.Context;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.content.res.ColorStateList;
  import android.content.res.Configuration;
  import android.content.res.Resources;
  import android.content.res.Resources.NotFoundException;
  import android.graphics.Bitmap;
  import android.graphics.Color;
  import android.graphics.ColorFilter;
  import android.graphics.Rect;
  import android.graphics.drawable.Drawable;
  import android.graphics.drawable.VectorDrawable;
  import android.net.Uri;
  import android.os.Build;
  import android.os.PowerManager;
  import android.os.Process;
  import android.os.StatFs;
  import android.os.StrictMode;
  import android.os.UserManager;
  import android.provider.Settings;
  import android.text.Html;
  import android.text.Spanned;
  import android.view.View;
  import android.view.ViewGroup.MarginLayoutParams;
  import android.view.Window;
  import android.view.WindowManager;
  import android.view.inputmethod.InputMethodSubtype;
  import android.view.textclassifier.TextClassifier;
  import android.widget.TextView;
  
  import java.io.File;
  import java.io.UnsupportedEncodingException;
  
  /**
   * Utility class to use new APIs that were added after ICS (API level 14).
   */
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public class ApiCompatibilityUtils {
      private ApiCompatibilityUtils() {
      }
  
      /**
       * Compares two long values numerically. The value returned is identical to what would be
       * returned by {@link Long#compare(long, long)} which is available since API level 19.
       */
      public static int compareLong(long lhs, long rhs) {
          return lhs < rhs ? -1 : (lhs == rhs ? 0 : 1);
      }
  
      /**
       * Compares two boolean values. The value returned is identical to what would be returned by
       * {@link Boolean#compare(boolean, boolean)} which is available since API level 19.
       */
      public static int compareBoolean(boolean lhs, boolean rhs) {
          return lhs == rhs ? 0 : lhs ? 1 : -1;
      }
  
      /**
       * {@link String#getBytes()} but specifying UTF-8 as the encoding and capturing the resulting
       * UnsupportedEncodingException.
       */
      public static byte[] getBytesUtf8(String str) {
          try {
              return str.getBytes("UTF-8");
          } catch (UnsupportedEncodingException e) {
              throw new IllegalStateException("UTF-8 encoding not available.", e);
          }
      }
  
      /**
       * Returns true if view's layout direction is right-to-left.
       *
       * @param view the View whose layout is being considered
       */
      public static boolean isLayoutRtl(View view) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
          } else {
              // All layouts are LTR before JB MR1.
              return false;
          }
      }
  
      /**
       * @see Configuration#getLayoutDirection()
       */
      public static int getLayoutDirection(Configuration configuration) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              return configuration.getLayoutDirection();
          } else {
              // All layouts are LTR before JB MR1.
              return View.LAYOUT_DIRECTION_LTR;
          }
      }
  
      /**
       * @return True if the running version of the Android supports printing.
       */
      public static boolean isPrintingSupported() {
          return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
      }
  
      /**
       * @return True if the running version of the Android supports elevation. Elevation of a view
       * determines the visual appearance of its shadow.
       */
      public static boolean isElevationSupported() {
          return Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP;
      }
  
      /**
       * @see android.view.View#setLayoutDirection(int)
       */
      public static void setLayoutDirection(View view, int layoutDirection) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              view.setLayoutDirection(layoutDirection);
          } else {
              // Do nothing. RTL layouts aren't supported before JB MR1.
          }
      }
  
      /**
       * @see android.view.View#setTextAlignment(int)
       */
      public static void setTextAlignment(View view, int textAlignment) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              view.setTextAlignment(textAlignment);
          } else {
              // Do nothing. RTL text isn't supported before JB MR1.
          }
      }
  
      /**
       * @see android.view.View#setTextDirection(int)
       */
      public static void setTextDirection(View view, int textDirection) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              view.setTextDirection(textDirection);
          } else {
              // Do nothing. RTL text isn't supported before JB MR1.
          }
      }
  
      /**
       * See {@link android.view.View#setLabelFor(int)}.
       */
      public static void setLabelFor(View labelView, int id) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              labelView.setLabelFor(id);
          } else {
              // Do nothing. #setLabelFor() isn't supported before JB MR1.
          }
      }
  
      /**
       * @see android.view.ViewGroup.MarginLayoutParams#setMarginEnd(int)
       */
      public static void setMarginEnd(MarginLayoutParams layoutParams, int end) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              layoutParams.setMarginEnd(end);
          } else {
              layoutParams.rightMargin = end;
          }
      }
  
      /**
       * @see android.view.ViewGroup.MarginLayoutParams#getMarginEnd()
       */
      public static int getMarginEnd(MarginLayoutParams layoutParams) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              return layoutParams.getMarginEnd();
          } else {
              return layoutParams.rightMargin;
          }
      }
  
      /**
       * @see android.view.ViewGroup.MarginLayoutParams#setMarginStart(int)
       */
      public static void setMarginStart(MarginLayoutParams layoutParams, int start) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              layoutParams.setMarginStart(start);
          } else {
              layoutParams.leftMargin = start;
          }
      }
  
      /**
       * @see android.view.ViewGroup.MarginLayoutParams#getMarginStart()
       */
      public static int getMarginStart(MarginLayoutParams layoutParams) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              return layoutParams.getMarginStart();
          } else {
              return layoutParams.leftMargin;
          }
      }
  
      /**
       * @see android.view.View#setPaddingRelative(int, int, int, int)
       */
      public static void setPaddingRelative(View view, int start, int top, int end, int bottom) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              view.setPaddingRelative(start, top, end, bottom);
          } else {
              // Before JB MR1, all layouts are left-to-right, so start == left, etc.
              view.setPadding(start, top, end, bottom);
          }
      }
  
      /**
       * @see android.view.View#getPaddingStart()
       */
      public static int getPaddingStart(View view) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              return view.getPaddingStart();
          } else {
              // Before JB MR1, all layouts are left-to-right, so start == left.
              return view.getPaddingLeft();
          }
      }
  
      /**
       * @see android.view.View#getPaddingEnd()
       */
      public static int getPaddingEnd(View view) {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
              return view.getPaddingEnd();
          } else {
              // Before JB MR1, all layouts are left-to-right, so end == right.
              return view.getPaddingRight();
          }
      }
  
      /**
       * @see android.widget.TextView#setCompoundDrawablesRelative(Drawable, Drawable, Drawable,
       *      Drawable)
       */
      public static void setCompoundDrawablesRelative(TextView textView, Drawable start, Drawable top,
              Drawable end, Drawable bottom) {
          if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
              // On JB MR1, due to a platform bug, setCompoundDrawablesRelative() is a no-op if the
              // view has ever been measured. As a workaround, use setCompoundDrawables() directly.
              // See: http://crbug.com/368196 and http://crbug.com/361709
              boolean isRtl = isLayoutRtl(textView);
              textView.setCompoundDrawables(isRtl ? end : start, top, isRtl ? start : end, bottom);
          } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
              textView.setCompoundDrawablesRelative(start, top, end, bottom);
          } else {
              textView.setCompoundDrawables(start, top, end, bottom);
          }
      }
  
      /**
       * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable,
       *      Drawable, Drawable, Drawable)
       */
      public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
              Drawable start, Drawable top, Drawable end, Drawable bottom) {
          if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
              // Work around the platform bug described in setCompoundDrawablesRelative() above.
              boolean isRtl = isLayoutRtl(textView);
              textView.setCompoundDrawablesWithIntrinsicBounds(isRtl ? end : start, top,
                      isRtl ? start : end, bottom);
          } else if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN_MR1) {
              textView.setCompoundDrawablesRelativeWithIntrinsicBounds(start, top, end, bottom);
          } else {
              textView.setCompoundDrawablesWithIntrinsicBounds(start, top, end, bottom);
          }
      }
  
      /**
       * @see android.widget.TextView#setCompoundDrawablesRelativeWithIntrinsicBounds(int, int, int,
       *      int)
       */
      public static void setCompoundDrawablesRelativeWithIntrinsicBounds(TextView textView,
              int start, int top, int end, int bottom) {
          if (Build.VERSION.SDK_INT == Build.VERSION_CODES.JELLY_BEAN_MR1) {
              // Work around the platform bug described in setCompoundDrawablesRelative() above.
  