android.text.Annotation Java Examples

The following examples show how to use android.text.Annotation. 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: ConverterHtmlToText.java    From Android-RTEditor with Apache License 2.0 6 votes vote down vote up
/**
 * When we come upon an ignored tag, we mark it with an Annotation object with a specific key
 * and value as above. We don't really need to be checking these values since Html.fromHtml()
 * doesn't use Annotation spans, but we should do it now to be safe in case they do start using
 * it in the future.
 *
 * @param opening If this is an opening tag or not.
 * @param output  Spannable string that we're working with.
 */
private void handleIgnoredTag(boolean opening, Editable output) {
    int len = output.length();
    if (opening) {
        output.setSpan(new Annotation(IGNORED_ANNOTATION_KEY, IGNORED_ANNOTATION_VALUE), len,
                len, Spanned.SPAN_MARK_MARK);
    } else {
        Object start = getOpeningAnnotation(output);
        if (start != null) {
            int where = output.getSpanStart(start);
            // Remove the temporary Annotation span.
            output.removeSpan(start);
            // Delete everything between the start of the Annotation and the end of the string
            // (what we've generated so far).
            output.delete(where, len);
        }
    }
}
 
Example #2
Source File: ConverterHtmlToText.java    From memoir with Apache License 2.0 6 votes vote down vote up
/**
 * When we come upon an ignored tag, we mark it with an Annotation object with a specific key
 * and value as above. We don't really need to be checking these values since Html.fromHtml()
 * doesn't use Annotation spans, but we should do it now to be safe in case they do start using
 * it in the future.
 *
 * @param opening If this is an opening tag or not.
 * @param output  Spannable string that we're working with.
 */
private void handleIgnoredTag(boolean opening, Editable output) {
    int len = output.length();
    if (opening) {
        output.setSpan(new Annotation(IGNORED_ANNOTATION_KEY, IGNORED_ANNOTATION_VALUE), len,
                len, Spanned.SPAN_MARK_MARK);
    } else {
        Object start = getOpeningAnnotation(output);
        if (start != null) {
            int where = output.getSpanStart(start);
            // Remove the temporary Annotation span.
            output.removeSpan(start);
            // Delete everything between the start of the Annotation and the end of the string
            // (what we've generated so far).
            output.delete(where, len);
        }
    }
}
 
Example #3
Source File: ConverterHtmlToText.java    From memoir with Apache License 2.0 6 votes vote down vote up
/**
 * When we come upon an ignored tag, we mark it with an Annotation object with a specific key
 * and value as above. We don't really need to be checking these values since Html.fromHtml()
 * doesn't use Annotation spans, but we should do it now to be safe in case they do start using
 * it in the future.
 *
 * @param opening If this is an opening tag or not.
 * @param output  Spannable string that we're working with.
 */
private void handleIgnoredTag(boolean opening, Editable output) {
    int len = output.length();
    if (opening) {
        output.setSpan(new Annotation(IGNORED_ANNOTATION_KEY, IGNORED_ANNOTATION_VALUE), len,
                len, Spanned.SPAN_MARK_MARK);
    } else {
        Object start = getOpeningAnnotation(output);
        if (start != null) {
            int where = output.getSpanStart(start);
            // Remove the temporary Annotation span.
            output.removeSpan(start);
            // Delete everything between the start of the Annotation and the end of the string
            // (what we've generated so far).
            output.delete(where, len);
        }
    }
}
 
Example #4
Source File: ConverterHtmlToText.java    From memoir with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch the matching opening Annotation object and verify that it's the one added by us.
 *
 * @param output Spannable string we're working with.
 * @return Starting Annotation object.
 */
private Object getOpeningAnnotation(Editable output) {
    Object[] objs = output.getSpans(0, output.length(), Annotation.class);
    for (int i = objs.length - 1; i >= 0; i--) {
        Annotation span = (Annotation) objs[i];
        if (output.getSpanFlags(objs[i]) == Spanned.SPAN_MARK_MARK
                && span.getKey().equals(IGNORED_ANNOTATION_KEY)
                && span.getValue().equals(IGNORED_ANNOTATION_VALUE)) {
            return objs[i];
        }
    }
    return null;
}
 
Example #5
Source File: RecipientsEditor.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private static String getAnnotation(Annotation[] a, String key) {
    for (Annotation anA : a) {
        if (anA.getKey().equals(key)) {
            return anA.getValue();
        }
    }

    return "";
}
 
Example #6
Source File: RecipientsEditor.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private static String getFieldAt(String field, Spanned sp, int start, int end,
        Context context) {
    Annotation[] a = sp.getSpans(start, end, Annotation.class);
    String fieldValue = getAnnotation(a, field);
    if (TextUtils.isEmpty(fieldValue)) {
        fieldValue = TextUtils.substring(sp, start, end);
    }
    return fieldValue;

}
 
Example #7
Source File: RecipientsEditor.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
private static int getSpanLength(Spanned sp, int start, int end, Context context) {
    // TODO: there's a situation where the span can lose its annotations:
    //   - add an auto-complete contact
    //   - add another auto-complete contact
    //   - delete that second contact and keep deleting into the first
    //   - we lose the annotation and can no longer get the span.
    // Need to fix this case because it breaks auto-complete contacts with commas in the name.
    Annotation[] a = sp.getSpans(start, end, Annotation.class);
    if (a.length > 0) {
        return sp.getSpanEnd(a[0]);
    }
    return 0;
}
 
Example #8
Source File: RecipientsEditor.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
public static CharSequence contactToToken(Recipient c) {
  String name       = c.getName();
  String number     = c.getNumber();
  SpannableString s = new SpannableString(RecipientsFormatter.formatNameAndNumber(name, number));
  int len           = s.length();

  if (len == 0) {
    return s;
  }

  s.setSpan(new Annotation("number", c.getNumber()), 0, len,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  return s;
}
 
Example #9
Source File: ConverterHtmlToText.java    From Android-RTEditor with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch the matching opening Annotation object and verify that it's the one added by us.
 *
 * @param output Spannable string we're working with.
 * @return Starting Annotation object.
 */
private Object getOpeningAnnotation(Editable output) {
    Object[] objs = output.getSpans(0, output.length(), Annotation.class);
    for (int i = objs.length - 1; i >= 0; i--) {
        Annotation span = (Annotation) objs[i];
        if (output.getSpanFlags(objs[i]) == Spanned.SPAN_MARK_MARK
                && span.getKey().equals(IGNORED_ANNOTATION_KEY)
                && span.getValue().equals(IGNORED_ANNOTATION_VALUE)) {
            return objs[i];
        }
    }
    return null;
}
 
Example #10
Source File: ConverterHtmlToText.java    From memoir with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch the matching opening Annotation object and verify that it's the one added by us.
 *
 * @param output Spannable string we're working with.
 * @return Starting Annotation object.
 */
private Object getOpeningAnnotation(Editable output) {
    Object[] objs = output.getSpans(0, output.length(), Annotation.class);
    for (int i = objs.length - 1; i >= 0; i--) {
        Annotation span = (Annotation) objs[i];
        if (output.getSpanFlags(objs[i]) == Spanned.SPAN_MARK_MARK
                && span.getKey().equals(IGNORED_ANNOTATION_KEY)
                && span.getValue().equals(IGNORED_ANNOTATION_VALUE)) {
            return objs[i];
        }
    }
    return null;
}
 
Example #11
Source File: RecipientsEditor.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private static String getAnnotation(Annotation[] a, String key) {
    for (int i = 0; i < a.length; i++) {
        if (a[i].getKey().equals(key)) {
            return a[i].getValue();
        }
    }

    return "";
}
 
Example #12
Source File: RecipientsEditor.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private static String getFieldAt(String field, Spanned sp, int start, int end,
        Context context) {
    Annotation[] a = sp.getSpans(start, end, Annotation.class);
    String fieldValue = getAnnotation(a, field);
    if (TextUtils.isEmpty(fieldValue)) {
        fieldValue = TextUtils.substring(sp, start, end);
    }
    return fieldValue;

}
 
Example #13
Source File: RecipientsEditor.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
private static int getSpanLength(Spanned sp, int start, int end, Context context) {
    // TODO: there's a situation where the span can lose its annotations:
    //   - add an auto-complete contact
    //   - add another auto-complete contact
    //   - delete that second contact and keep deleting into the first
    //   - we lose the annotation and can no longer get the span.
    // Need to fix this case because it breaks auto-complete contacts with commas in the name.
    Annotation[] a = sp.getSpans(start, end, Annotation.class);
    if (a.length > 0) {
        return sp.getSpanEnd(a[0]);
    }
    return 0;
}
 
Example #14
Source File: RecipientsEditor.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
public static CharSequence contactToToken(Recipient c) {
  String name       = c.getName();
  String number     = c.getAddress().serialize();
  SpannableString s = new SpannableString(RecipientsFormatter.formatNameAndNumber(name, number));
  int len           = s.length();

  if (len == 0) {
    return s;
  }

  s.setSpan(new Annotation("number", c.getAddress().serialize()), 0, len,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  return s;
}
 
Example #15
Source File: RecipientsEditor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static String getAnnotation(Annotation[] a, String key) {
    for (int i = 0; i < a.length; i++) {
        if (a[i].getKey().equals(key)) {
            return a[i].getValue();
        }
    }

    return "";
}
 
Example #16
Source File: RecipientsEditor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static String getFieldAt(String field, Spanned sp, int start, int end,
        Context context) {
    Annotation[] a = sp.getSpans(start, end, Annotation.class);
    String fieldValue = getAnnotation(a, field);
    if (TextUtils.isEmpty(fieldValue)) {
        fieldValue = TextUtils.substring(sp, start, end);
    }
    return fieldValue;

}
 
Example #17
Source File: RecipientsEditor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private static int getSpanLength(Spanned sp, int start, int end, Context context) {
    // TODO: there's a situation where the span can lose its annotations:
    //   - add an auto-complete contact
    //   - add another auto-complete contact
    //   - delete that second contact and keep deleting into the first
    //   - we lose the annotation and can no longer get the span.
    // Need to fix this case because it breaks auto-complete contacts with commas in the name.
    Annotation[] a = sp.getSpans(start, end, Annotation.class);
    if (a.length > 0) {
        return sp.getSpanEnd(a[0]);
    }
    return 0;
}
 
Example #18
Source File: RecipientsEditor.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public static CharSequence contactToToken(@NonNull Context context, @NonNull Recipient c) {
  String name       = c.getDisplayName(context);
  String number     = c.getE164().or(c.getEmail()).or("");
  SpannableString s = new SpannableString(RecipientsFormatter.formatNameAndNumber(name, number));
  int len           = s.length();

  if (len == 0) {
    return s;
  }

  s.setSpan(new Annotation("number", number), 0, len,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

  return s;
}
 
Example #19
Source File: RecipientsAdapter.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public final CharSequence convertToString(Cursor cursor) {
    String name = cursor.getString(RecipientsAdapter.NAME_INDEX);
    int type = cursor.getInt(RecipientsAdapter.TYPE_INDEX);
    String number = cursor.getString(RecipientsAdapter.NUMBER_INDEX).trim();

    String label = cursor.getString(RecipientsAdapter.LABEL_INDEX);
    CharSequence displayLabel = mContactAccessor.phoneTypeToString(mContext, type, label);

    if (number.length() == 0) {
        return number;
    }

    if (name == null) {
        name = "";
    } else {
        // Names with commas are the bane of the recipient editor's existence.
        // We've worked around them by using spans, but there are edge cases
        // where the spans get deleted. Furthermore, having commas in names
        // can be confusing to the user since commas are used as separators
        // between recipients. The best solution is to simply remove commas
        // from names.
        name = name.replace(", ", " ")
                   .replace(",", " ");  // Make sure we leave a space between parts of names.
    }

    String nameAndNumber = RecipientsFormatter.formatNameAndNumber(name, number);

    SpannableString out = new SpannableString(nameAndNumber);
    int len = out.length();

    if (!TextUtils.isEmpty(name)) {
        out.setSpan(new Annotation("name", name), 0, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        out.setSpan(new Annotation("name", number), 0, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    String person_id = cursor.getString(RecipientsAdapter.CONTACT_ID_INDEX);
    out.setSpan(new Annotation("person_id", person_id), 0, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    out.setSpan(new Annotation("label", displayLabel.toString()), 0, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);              
    out.setSpan(new Annotation("number", number), 0, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return out;
}
 
Example #20
Source File: RecipientsAdapter.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
@Override
public final CharSequence convertToString(Cursor cursor) {
    String name = cursor.getString(RecipientsAdapter.NAME_INDEX);
    int type = cursor.getInt(RecipientsAdapter.TYPE_INDEX);
    String number = cursor.getString(RecipientsAdapter.NUMBER_INDEX).trim();

    String label = cursor.getString(RecipientsAdapter.LABEL_INDEX);
    CharSequence displayLabel = mContactAccessor.phoneTypeToString(mContext, type, label);

    if (number.length() == 0) {
        return number;
    }

    if (name == null) {
        name = "";
    } else {
        // Names with commas are the bane of the recipient editor's existence.
        // We've worked around them by using spans, but there are edge cases
        // where the spans get deleted. Furthermore, having commas in names
        // can be confusing to the user since commas are used as separators
        // between recipients. The best solution is to simply remove commas
        // from names.
        name = name.replace(", ", " ")
                   .replace(",", " ");  // Make sure we leave a space between parts of names.
    }

    String nameAndNumber = RecipientsFormatter.formatNameAndNumber(name, number);

    SpannableString out = new SpannableString(nameAndNumber);
    int len = out.length();

    if (!TextUtils.isEmpty(name)) {
        out.setSpan(new Annotation("name", name), 0, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    } else {
        out.setSpan(new Annotation("name", number), 0, len,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    }

    String person_id = cursor.getString(RecipientsAdapter.CONTACT_ID_INDEX);
    out.setSpan(new Annotation("person_id", person_id), 0, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    out.setSpan(new Annotation("label", displayLabel.toString()), 0, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    out.setSpan(new Annotation("number", number), 0, len,
                Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return out;
}