use of android.text.TextPaint in project android_frameworks_base by crdroidandroid.
the class PackageItemInfo method loadSafeLabel.
/**
* Same as {@link #loadLabel(PackageManager)} with the addition that
* the returned label is safe for being presented in the UI since it
* will not contain new lines and the length will be limited to a
* reasonable amount. This prevents a malicious party to influence UI
* layout via the app label misleading the user into performing a
* detrimental for them action. If the label is too long it will be
* truncated and ellipsized at the end.
*
* @param pm A PackageManager from which the label can be loaded; usually
* the PackageManager from which you originally retrieved this item
* @return Returns a CharSequence containing the item's label. If the
* item does not have a label, its name is returned.
*
* @hide
*/
@SystemApi
@NonNull
public CharSequence loadSafeLabel(@NonNull PackageManager pm) {
// loadLabel() always returns non-null
String label = loadLabel(pm).toString();
// strip HTML tags to avoid <br> and other tags overwriting original message
String labelStr = Html.fromHtml(label).toString();
// If the label contains new line characters it may push the UI
// down to hide a part of it. Labels shouldn't have new line
// characters, so just truncate at the first time one is seen.
final int labelLength = labelStr.length();
int offset = 0;
while (offset < labelLength) {
final int codePoint = labelStr.codePointAt(offset);
final int type = Character.getType(codePoint);
if (type == Character.LINE_SEPARATOR || type == Character.CONTROL || type == Character.PARAGRAPH_SEPARATOR) {
labelStr = labelStr.substring(0, offset);
break;
}
// replace all non-break space to " " in order to be trimmed
if (type == Character.SPACE_SEPARATOR) {
labelStr = labelStr.substring(0, offset) + " " + labelStr.substring(offset + Character.charCount(codePoint));
}
offset += Character.charCount(codePoint);
}
labelStr = labelStr.trim();
if (labelStr.isEmpty()) {
return packageName;
}
TextPaint paint = new TextPaint();
paint.setTextSize(42);
return TextUtils.ellipsize(labelStr, paint, MAX_LABEL_SIZE_PX, TextUtils.TruncateAt.END);
}
use of android.text.TextPaint in project android_frameworks_base by crdroidandroid.
the class StaticLayoutTest method moveCursorToLeftCursorableOffset.
private void moveCursorToLeftCursorableOffset(EditorState state, TextPaint paint) {
assertEquals("The editor has selection", state.mSelectionStart, state.mSelectionEnd);
final Layout layout = builder().setText(state.mText.toString()).setPaint(paint).build();
final int newOffset = layout.getOffsetToLeftOf(state.mSelectionStart);
state.mSelectionStart = state.mSelectionEnd = newOffset;
}
use of android.text.TextPaint in project android_frameworks_base by crdroidandroid.
the class StaticLayoutTest method testEmojiOffset.
/**
* Tests for keycap, variation selectors, flags are in CTS.
* See {@link android.text.cts.StaticLayoutTest}.
*/
public void testEmojiOffset() {
EditorState state = new EditorState();
TextPaint paint = new TextPaint();
// Odd numbered regional indicator symbols.
// U+1F1E6 is REGIONAL INDICATOR SYMBOL LETTER A, U+1F1E8 is REGIONAL INDICATOR SYMBOL
// LETTER C.
state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+1F1E6 U+1F1E8 | U+1F1E6 U+1F1E8 U+1F1E6");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 | U+1F1E6");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6 |");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6 |");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 | U+1F1E6");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("U+1F1E6 U+1F1E8 | U+1F1E6 U+1F1E8 U+1F1E6");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("| U+1F1E6 U+1F1E8 U+1F1E6 U+1F1E8 U+1F1E6");
moveCursorToLeftCursorableOffset(state, paint);
// Zero width sequence
final String zwjSequence = "U+1F468 U+200D U+2764 U+FE0F U+200D U+1F468";
state.setByString("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
moveCursorToRightCursorableOffset(state, paint);
state.assertEquals(zwjSequence + " | " + zwjSequence + " " + zwjSequence);
moveCursorToRightCursorableOffset(state, paint);
state.assertEquals(zwjSequence + " " + zwjSequence + " | " + zwjSequence);
moveCursorToRightCursorableOffset(state, paint);
state.assertEquals(zwjSequence + " " + zwjSequence + " " + zwjSequence + " |");
moveCursorToRightCursorableOffset(state, paint);
state.assertEquals(zwjSequence + " " + zwjSequence + " " + zwjSequence + " |");
moveCursorToLeftCursorableOffset(state, paint);
state.assertEquals(zwjSequence + " " + zwjSequence + " | " + zwjSequence);
moveCursorToLeftCursorableOffset(state, paint);
state.assertEquals(zwjSequence + " | " + zwjSequence + " " + zwjSequence);
moveCursorToLeftCursorableOffset(state, paint);
state.assertEquals("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
moveCursorToLeftCursorableOffset(state, paint);
state.assertEquals("| " + zwjSequence + " " + zwjSequence + " " + zwjSequence);
moveCursorToLeftCursorableOffset(state, paint);
// Emoji modifiers
// U+261D is WHITE UP POINTING INDEX, U+1F3FB is EMOJI MODIFIER FITZPATRICK TYPE-1-2.
state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+261D U+1F3FB | U+261D U+1F3FB U+261D U+1F3FB");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+261D U+1F3FB U+261D U+1F3FB | U+261D U+1F3FB");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB |");
moveCursorToRightCursorableOffset(state, paint);
state.setByString("U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB |");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("U+261D U+1F3FB U+261D U+1F3FB | U+261D U+1F3FB");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("U+261D U+1F3FB | U+261D U+1F3FB U+261D U+1F3FB");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
moveCursorToLeftCursorableOffset(state, paint);
state.setByString("| U+261D U+1F3FB U+261D U+1F3FB U+261D U+1F3FB");
moveCursorToLeftCursorableOffset(state, paint);
}
use of android.text.TextPaint in project JustAndroid by chinaltz.
the class AbGraphicUtil method getDrawRowStr.
/**
* 解析成行.
* @param text the text
* @param maxWPix the max w pix
* @param paint the paint
* @return the draw row count
*/
public static List<String> getDrawRowStr(String text, int maxWPix, TextPaint paint) {
String[] texts = null;
if (text.indexOf("\n") != -1) {
texts = text.split("\n");
} else {
texts = new String[1];
texts[0] = text;
}
//共多少行
List<String> mStrList = new ArrayList<String>();
for (int i = 0; i < texts.length; i++) {
String textLine = texts[i];
//计算这个文本显示为几行
while (true) {
//可容纳的最后一个字的位置
int endIndex = subStringLength(textLine, maxWPix, paint);
if (endIndex <= 0) {
mStrList.add(textLine);
} else {
if (endIndex == textLine.length() - 1) {
mStrList.add(textLine);
} else {
mStrList.add(textLine.substring(0, endIndex + 1));
}
}
//获取剩下的
if (textLine.length() > endIndex + 1) {
//还有剩下的
textLine = textLine.substring(endIndex + 1);
} else {
break;
}
}
}
return mStrList;
}
use of android.text.TextPaint in project JustAndroid by chinaltz.
the class AbGraphicUtil method getDrawRowCount.
/**
*
* 获取这段文本多少行.
* @param text
* @param maxWPix
* @param paint
* @return
*/
public static int getDrawRowCount(String text, int maxWPix, TextPaint paint) {
String[] texts = null;
if (text.indexOf("\n") != -1) {
texts = text.split("\n");
} else {
texts = new String[1];
texts[0] = text;
}
//共多少行
List<String> mStrList = new ArrayList<String>();
for (int i = 0; i < texts.length; i++) {
String textLine = texts[i];
//计算这个文本显示为几行
while (true) {
//可容纳的最后一个字的位置
int endIndex = subStringLength(textLine, maxWPix, paint);
if (endIndex <= 0) {
mStrList.add(textLine);
} else {
if (endIndex == textLine.length() - 1) {
mStrList.add(textLine);
} else {
mStrList.add(textLine.substring(0, endIndex + 1));
}
}
//获取剩下的
if (textLine.length() > endIndex + 1) {
//还有剩下的
textLine = textLine.substring(endIndex + 1);
} else {
break;
}
}
}
return mStrList.size();
}
Aggregations