Java tutorial
//package com.java2s; //License from project: Apache License import android.content.ClipData; import android.content.ClipDescription; import android.content.Intent; import android.net.Uri; import android.text.Html; import android.text.Spanned; public class Main { private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', }; private final static int NUM_LEN = 8; private final static int FLAG_NULL = -1; private final static int FLAG_INT = -2; private final static int FLAG_SPANNED = -3; /** * Convert {@link ClipData} to String for storing. * @param clip * @return String for specifed clipdata */ public static CharSequence stringFromClipData(ClipData clip) { StringBuilder sb = new StringBuilder(); if (clip != null) { append(sb, "clip"); appendClipDescription(sb, clip.getDescription()); int n = clip.getItemCount(); appendInt(sb, n); for (int i = 0; i < n; i++) { appendItem(sb, clip.getItemAt(i)); } } else { append(sb, (String) null); } return sb; } private static void append(StringBuilder sb, CharSequence... data) { for (CharSequence d : data) { if (d != null) { if (d instanceof Spanned) { String s = Html.toHtml((Spanned) d); sb.append(hexStringFromInt(FLAG_SPANNED, NUM_LEN)); append(sb, s); } else { int len = d.length(); sb.append(hexStringFromInt(len, NUM_LEN)).append(d); } } else { sb.append(hexStringFromInt(FLAG_NULL, NUM_LEN)); } } } private static void appendClipDescription(StringBuilder sb, ClipDescription desc) { if (desc != null) { append(sb, "desc", desc.getLabel()); int n = desc.getMimeTypeCount(); appendInt(sb, n); for (int i = 0; i < n; i++) { append(sb, desc.getMimeType(i)); } } else { append(sb, (String) null); } } private static void appendInt(StringBuilder sb, int i) { sb.append(hexStringFromInt(FLAG_INT, NUM_LEN)).append(hexStringFromInt(i, NUM_LEN)); } private static void appendItem(StringBuilder sb, ClipData.Item item) { if (item != null) { append(sb, "item", item.getText(), item.getHtmlText()); appendIntent(sb, item.getIntent()); appendUri(sb, item.getUri()); } else { append(sb, (String) null); } } /** * Return the hex string for specified integer. * @param i * @param minWidth * @return the hex string like 0f3e */ public static String hexStringFromInt(int i, int minWidth) { final int len = 8; char[] buf = new char[len]; int cursor = len; do { buf[--cursor] = DIGITS[i & 0x0f]; } while ((i >>>= 4) != 0 || (len - cursor < minWidth)); return new String(buf, cursor, len - cursor); } private static void appendIntent(StringBuilder sb, Intent intent) { if (intent != null) { String uri = intent.toUri(Intent.URI_INTENT_SCHEME); String action = intent.getAction(); append(sb, "intent", action, uri); } else { append(sb, (String) null); } } private static void appendUri(StringBuilder sb, Uri uri) { append(sb, uri == null ? null : uri.toString()); } /** * toString method for values include null * @param cs * @return the string */ public static String toString(CharSequence cs) { return cs == null ? null : cs.toString(); } }