Java Code Examples for android.text.TextDirectionHeuristic

The following examples show how to use android.text.TextDirectionHeuristic. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: TextLayoutBuilder   Source File: StaticLayoutProxy.java    License: Apache License 2.0 6 votes vote down vote up
public static TextDirectionHeuristic fromTextDirectionHeuristicCompat(
    TextDirectionHeuristicCompat textDirection) {
  if (textDirection == TextDirectionHeuristicsCompat.LTR) {
    return TextDirectionHeuristics.LTR;
  } else if (textDirection == TextDirectionHeuristicsCompat.RTL) {
    return TextDirectionHeuristics.RTL;
  } else if (textDirection == TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR) {
    return TextDirectionHeuristics.FIRSTSTRONG_LTR;
  } else if (textDirection == TextDirectionHeuristicsCompat.FIRSTSTRONG_RTL) {
    return TextDirectionHeuristics.FIRSTSTRONG_RTL;
  } else if (textDirection == TextDirectionHeuristicsCompat.ANYRTL_LTR) {
    return TextDirectionHeuristics.ANYRTL_LTR;
  } else if (textDirection == TextDirectionHeuristicsCompat.LOCALE) {
    return TextDirectionHeuristics.LOCALE;
  } else {
    return TextDirectionHeuristics.FIRSTSTRONG_LTR;
  }
}
 
Example 2
public static void init() {
    if (initialized) {
        return;
    }

    try {
        final Class<?> textDirClass;
        if (Build.VERSION.SDK_INT >= 18) {
            textDirClass = TextDirectionHeuristic.class;
            sTextDirection = TextDirectionHeuristics.FIRSTSTRONG_LTR;
        } else {
            ClassLoader loader = StaticLayoutEx.class.getClassLoader();
            textDirClass = loader.loadClass(TEXT_DIR_CLASS);
            Class<?> textDirsClass = loader.loadClass(TEXT_DIRS_CLASS);
            sTextDirection = textDirsClass.getField(TEXT_DIR_FIRSTSTRONG_LTR).get(textDirsClass);
        }

        final Class<?>[] signature = new Class[]{
                CharSequence.class,
                int.class,
                int.class,
                TextPaint.class,
                int.class,
                Layout.Alignment.class,
                textDirClass,
                float.class,
                float.class,
                boolean.class,
                TextUtils.TruncateAt.class,
                int.class,
                int.class
        };

        sConstructor = StaticLayout.class.getDeclaredConstructor(signature);
        sConstructor.setAccessible(true);
        sConstructorArgs = new Object[signature.length];
        initialized = true;
    } catch (Throwable e) {
        FileLog.e(e);
    }
}
 
Example 3
public static void init() {
    if (initialized) {
        return;
    }

    try {
        final Class<?> textDirClass;
        if (Build.VERSION.SDK_INT >= 18) {
            textDirClass = TextDirectionHeuristic.class;
            sTextDirection = TextDirectionHeuristics.FIRSTSTRONG_LTR;
        } else {
            ClassLoader loader = StaticLayoutEx.class.getClassLoader();
            textDirClass = loader.loadClass(TEXT_DIR_CLASS);
            Class<?> textDirsClass = loader.loadClass(TEXT_DIRS_CLASS);
            sTextDirection = textDirsClass.getField(TEXT_DIR_FIRSTSTRONG_LTR).get(textDirsClass);
        }

        final Class<?>[] signature = new Class[]{
                CharSequence.class,
                int.class,
                int.class,
                TextPaint.class,
                int.class,
                Layout.Alignment.class,
                textDirClass,
                float.class,
                float.class,
                boolean.class,
                TextUtils.TruncateAt.class,
                int.class,
                int.class
        };

        sConstructor = StaticLayout.class.getDeclaredConstructor(signature);
        sConstructor.setAccessible(true);
        sConstructorArgs = new Object[signature.length];
        initialized = true;
    } catch (Throwable e) {
        FileLog.e(e);
    }
}
 
Example 4
/** A method that allows to create a StaticLayout with maxLines on all supported API levels. */
public StaticLayout build() throws StaticLayoutBuilderCompatException {
  if (source == null) {
    source = "";
  }


  int availableWidth = Math.max(0, width);
  CharSequence textToDraw = source;
  if (maxLines == 1) {
    textToDraw = TextUtils.ellipsize(source, paint, availableWidth, ellipsize);
  }

  end = Math.min(textToDraw.length(), end);
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (isRtl) {
      alignment = Alignment.ALIGN_OPPOSITE;
    }
    // Marshmallow introduced StaticLayout.Builder which allows us not to use
    // the hidden constructor.
    StaticLayout.Builder builder =
        StaticLayout.Builder.obtain(
            textToDraw, start, end, paint, availableWidth);
    builder.setAlignment(alignment);
    builder.setIncludePad(includePad);
    TextDirectionHeuristic textDirectionHeuristic = isRtl
        ? TextDirectionHeuristics.RTL
        : TextDirectionHeuristics.LTR;
    builder.setTextDirection(textDirectionHeuristic);
    if (ellipsize != null) {
      builder.setEllipsize(ellipsize);
    }
    builder.setMaxLines(maxLines);
    return builder.build();
  }

  createConstructorWithReflection();
  // Use the hidden constructor on older API levels.
  try {
    return checkNotNull(constructor)
        .newInstance(
            textToDraw,
            start,
            end,
            paint,
            availableWidth,
            alignment,
            checkNotNull(textDirection),
            1.0f,
            0.0f,
            includePad,
            null,
            availableWidth,
            maxLines);
  } catch (Exception cause) {
    throw new StaticLayoutBuilderCompatException(cause);
  }
}
 
Example 5
/**
 * set constructor to this hidden {@link StaticLayout constructor.}
 *
 * <pre>{@code
 * StaticLayout(
 *   CharSequence source,
 *   int bufstart,
 *   int bufend,
 *   TextPaint paint,
 *   int outerwidth,
 *   Alignment align,
 *   TextDirectionHeuristic textDir,
 *   float spacingmult,
 *   float spacingadd,
 *   boolean includepad,
 *   TextUtils.TruncateAt ellipsize,
 *   int ellipsizedWidth,
 *   int maxLines)
 * }</pre>
 */
private void createConstructorWithReflection() throws StaticLayoutBuilderCompatException {
  if (initialized) {
    return;
  }

  try {
    final Class<?> textDirClass;
    boolean useRtl = isRtl && Build.VERSION.SDK_INT >= VERSION_CODES.M;
    if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR2) {
      textDirClass = TextDirectionHeuristic.class;
      textDirection = useRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR;
    } else {
      ClassLoader loader = StaticLayoutBuilderCompat.class.getClassLoader();
      String textDirClassName = isRtl ? TEXT_DIR_CLASS_RTL : TEXT_DIR_CLASS_LTR;
      textDirClass = loader.loadClass(TEXT_DIR_CLASS);
      Class<?> textDirsClass = loader.loadClass(TEXT_DIRS_CLASS);
      textDirection = textDirsClass.getField(textDirClassName).get(textDirsClass);
    }

    final Class<?>[] signature =
        new Class<?>[] {
          CharSequence.class,
          int.class,
          int.class,
          TextPaint.class,
          int.class,
          Alignment.class,
          textDirClass,
          float.class,
          float.class,
          boolean.class,
          TextUtils.TruncateAt.class,
          int.class,
          int.class
        };

    constructor = StaticLayout.class.getDeclaredConstructor(signature);
    constructor.setAccessible(true);
    initialized = true;
  } catch (Exception cause) {
    throw new StaticLayoutBuilderCompatException(cause);
  }
}
 
Example 6
Source Project: edx-app-android   Source File: CustomTypefaceSpan.java    License: Apache License 2.0 4 votes vote down vote up
@TargetApi(JELLY_BEAN_MR1)
@CheckResult
private boolean needMirroring() {
    if (!mirrorable) return false;

    // Passwords fields should be LTR
    if (view.getTransformationMethod() instanceof PasswordTransformationMethod) {
        return false;
    }

    // Always need to resolve layout direction first
    final boolean defaultIsRtl = view.getLayoutDirection() == LAYOUT_DIRECTION_RTL;

    if (SDK_INT < JELLY_BEAN_MR2) {
        return defaultIsRtl;
    }

    // Select the text direction heuristic according to the
    // package-private getTextDirectionHeuristic() method in TextView
    TextDirectionHeuristic textDirectionHeuristic;
    switch (view.getTextDirection()) {
        default:
        case TEXT_DIRECTION_FIRST_STRONG:
            textDirectionHeuristic = defaultIsRtl ?
                    TextDirectionHeuristics.FIRSTSTRONG_RTL :
                    TextDirectionHeuristics.FIRSTSTRONG_LTR;
            break;
        case TEXT_DIRECTION_ANY_RTL:
            textDirectionHeuristic = TextDirectionHeuristics.ANYRTL_LTR;
            break;
        case TEXT_DIRECTION_LTR:
            textDirectionHeuristic = TextDirectionHeuristics.LTR;
            break;
        case TEXT_DIRECTION_RTL:
            textDirectionHeuristic = TextDirectionHeuristics.RTL;
            break;
        case TEXT_DIRECTION_LOCALE:
            textDirectionHeuristic = TextDirectionHeuristics.LOCALE;
            break;
        case TEXT_DIRECTION_FIRST_STRONG_LTR:
            textDirectionHeuristic = TextDirectionHeuristics.FIRSTSTRONG_LTR;
            break;
        case TEXT_DIRECTION_FIRST_STRONG_RTL:
            textDirectionHeuristic = TextDirectionHeuristics.FIRSTSTRONG_RTL;
            break;
    }
    CharSequence text = view.getText();
    return textDirectionHeuristic.isRtl(text, 0, text.length());
}
 
Example 7
Source Project: Telegram-FOSS   Source File: StaticLayoutEx.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void init() {
    if (initialized) {
        return;
    }

    try {
        final Class<?> textDirClass;
        if (Build.VERSION.SDK_INT >= 18) {
            textDirClass = TextDirectionHeuristic.class;
            sTextDirection = TextDirectionHeuristics.FIRSTSTRONG_LTR;
        } else {
            ClassLoader loader = StaticLayoutEx.class.getClassLoader();
            textDirClass = loader.loadClass(TEXT_DIR_CLASS);
            Class<?> textDirsClass = loader.loadClass(TEXT_DIRS_CLASS);
            sTextDirection = textDirsClass.getField(TEXT_DIR_FIRSTSTRONG_LTR).get(textDirsClass);
        }

        final Class<?>[] signature = new Class[]{
                CharSequence.class,
                int.class,
                int.class,
                TextPaint.class,
                int.class,
                Layout.Alignment.class,
                textDirClass,
                float.class,
                float.class,
                boolean.class,
                TextUtils.TruncateAt.class,
                int.class,
                int.class
        };

        sConstructor = StaticLayout.class.getDeclaredConstructor(signature);
        sConstructor.setAccessible(true);
        sConstructorArgs = new Object[signature.length];
        initialized = true;
    } catch (Throwable e) {
        FileLog.e(e);
    }
}
 
Example 8
Source Project: Telegram   Source File: StaticLayoutEx.java    License: GNU General Public License v2.0 4 votes vote down vote up
public static void init() {
    if (initialized) {
        return;
    }

    try {
        final Class<?> textDirClass;
        if (Build.VERSION.SDK_INT >= 18) {
            textDirClass = TextDirectionHeuristic.class;
            sTextDirection = TextDirectionHeuristics.FIRSTSTRONG_LTR;
        } else {
            ClassLoader loader = StaticLayoutEx.class.getClassLoader();
            textDirClass = loader.loadClass(TEXT_DIR_CLASS);
            Class<?> textDirsClass = loader.loadClass(TEXT_DIRS_CLASS);
            sTextDirection = textDirsClass.getField(TEXT_DIR_FIRSTSTRONG_LTR).get(textDirsClass);
        }

        final Class<?>[] signature = new Class[]{
                CharSequence.class,
                int.class,
                int.class,
                TextPaint.class,
                int.class,
                Layout.Alignment.class,
                textDirClass,
                float.class,
                float.class,
                boolean.class,
                TextUtils.TruncateAt.class,
                int.class,
                int.class
        };

        sConstructor = StaticLayout.class.getDeclaredConstructor(signature);
        sConstructor.setAccessible(true);
        sConstructorArgs = new Object[signature.length];
        initialized = true;
    } catch (Throwable e) {
        FileLog.e(e);
    }
}