android.text.TextDirectionHeuristics Java Examples

The following examples show how to use android.text.TextDirectionHeuristics. 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 File: TimeUtil.java    From AndroidWallet with GNU General Public License v3.0 6 votes vote down vote up
public static String getTimeZoneText(TimeZone tz, boolean includeName) {
    Date now = new Date();

    SimpleDateFormat gmtFormatter = new SimpleDateFormat("ZZZZ");
    gmtFormatter.setTimeZone(tz);
    String gmtString = gmtFormatter.format(now);
    BidiFormatter bidiFormatter = BidiFormatter.getInstance();
    Locale l = Locale.getDefault();
    boolean isRtl = TextUtils.getLayoutDirectionFromLocale(l) == View.LAYOUT_DIRECTION_RTL;
    gmtString = bidiFormatter.unicodeWrap(gmtString,
            isRtl ? TextDirectionHeuristics.RTL : TextDirectionHeuristics.LTR);

    if (!includeName) {
        return gmtString;
    }

    return gmtString;
}
 
Example #2
Source File: StaticLayoutProxy.java    From TextLayoutBuilder with 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 #3
Source File: HtmlEx.java    From FairEmail with GNU General Public License v3.0 5 votes vote down vote up
private /* static */ String getTextDirection(Spanned text, int start, int end) {
    if (TextDirectionHeuristics.FIRSTSTRONG_LTR.isRtl(text, start, end - start)) {
        return " dir=\"rtl\"";
    } else {
        return " dir=\"ltr\"";
    }
}
 
Example #4
Source File: StaticLayoutEx.java    From TelePlus-Android with 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 #5
Source File: StaticLayoutEx.java    From TelePlus-Android with 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 #6
Source File: StaticLayoutManager.java    From FastTextView with Apache License 2.0 4 votes vote down vote up
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void initLayout(Context context, CharSequence source, CharSequence longString) {

  textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
  textPaint.density = context.getResources().getDisplayMetrics().density;
  textPaint.setTextSize(Util.fromDPtoPix(context, Util.TEXT_SIZE_DP));

  textDir = TextDirectionHeuristics.LTR;

  alignment = Layout.Alignment.ALIGN_NORMAL;

  hardCodeWidth = Util.getScreenWidth(context);

  longStringLayout = new StaticLayout(longString, textPaint, hardCodeWidth, alignment, 1.0f, 0f, true);

  dummyCanvas = new Canvas();

  longStringLayout.draw(dummyCanvas);

  for (int i = 0; i < layout.length; i++) {
    layout[i] = new StaticLayout(TestSpan.getSpanString(i), textPaint, hardCodeWidth, alignment, 1.0f, 0f, true);
    layout[i].draw(dummyCanvas);
  }
}
 
Example #7
Source File: StaticLayoutBuilderCompat.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
/** 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 #8
Source File: StaticLayoutBuilderCompat.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
/**
 * 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 #9
Source File: CustomTypefaceSpan.java    From edx-app-android with 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 #10
Source File: StaticLayoutEx.java    From Telegram-FOSS with 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 #11
Source File: StaticLayoutEx.java    From Telegram with 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);
    }
}