TextDirectionHeuristic getTextDirectionHeuristic() { if (hasPasswordTransformationMethod()) { // passwords fields should be LTR return TextDirectionHeuristics.LTR; } // Always need to resolve layout direction first final boolean defaultIsRtl = (getLayoutDirection() == LAYOUT_DIRECTION_RTL); // Now, we can select the heuristic switch (getTextDirection()) { default: case TEXT_DIRECTION_FIRST_STRONG: return (defaultIsRtl ? TextDirectionHeuristics.FIRSTSTRONG_RTL : TextDirectionHeuristics.FIRSTSTRONG_LTR); case TEXT_DIRECTION_ANY_RTL: return TextDirectionHeuristics.ANYRTL_LTR; case TEXT_DIRECTION_LTR: return TextDirectionHeuristics.LTR; case TEXT_DIRECTION_RTL: return TextDirectionHeuristics.RTL; case TEXT_DIRECTION_LOCALE: return TextDirectionHeuristics.LOCALE; } }
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("tmessages", e); } }
@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); } }
@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()); }
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); } }