Java tutorial
//package com.java2s; //License from project: Apache License import java.net.URISyntaxException; import android.content.ClipData; import android.content.ClipDescription; import android.content.Intent; import android.net.Uri; import android.text.Html; public class Main { 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; /** * Restore {@link ClipData} from String * @param s * @return ClipData instance */ public static ClipData clipDataFromString(CharSequence s) { ClipData cd = null; if (s != null) { StringBuilder sb = new StringBuilder(s); CharSequence prefix = extract(sb); if (prefix != null) { ClipDescription desc = clipDescriptionFromString(sb); int n = extractInt(sb); ClipData.Item[] items = new ClipData.Item[n]; for (int i = 0; i < n; i++) { items[i] = itemFromString(sb); } if (n > 0) { cd = new ClipData(desc, items[0]); } for (int i = 1; i < n; i++) { cd.addItem(items[i]); } } } return cd; } private static CharSequence extract(StringBuilder data) { return extract(data, false); } private static CharSequence extract(StringBuilder data, boolean isSpanned) { CharSequence len = cut(data, 0, NUM_LEN); int l = intFromHexString(len); CharSequence ret = null; switch (l) { case FLAG_NULL: ret = null; break; case FLAG_SPANNED: ret = extract(data, true); break; default: ret = cut(data, 0, l); if (isSpanned) { ret = Html.fromHtml(ret.toString()); } } return ret; } private static ClipDescription clipDescriptionFromString(StringBuilder s) { CharSequence prefix = extract(s); ClipDescription desc = null; if (prefix != null) { String label = toString(extract(s)); int n = extractInt(s); String[] mimeTypes = new String[n]; for (int i = 0; i < n; i++) { mimeTypes[i] = toString(extract(s)); } desc = new ClipDescription(label, mimeTypes); } return desc; } private static int extractInt(StringBuilder data) { CharSequence flag = cut(data, 0, NUM_LEN); int n = intFromHexString(flag); if (n == FLAG_INT) { CharSequence cs = cut(data, 0, NUM_LEN); return intFromHexString(cs); } else { throw new IllegalArgumentException(String.format("%s is not a valid number string.", data.toString())); } } private static ClipData.Item itemFromString(StringBuilder s) { CharSequence prefix = extract(s); ClipData.Item item = null; if (prefix != null) { item = new ClipData.Item((extract(s)), toString(extract(s)), intentFromString(s), uriFromString(s)); } return item; } private static CharSequence cut(StringBuilder sb, int start, int end) { CharSequence cs = sb.subSequence(start, end); sb.delete(start, end); return cs; } /** * Convert the hex string to the integer value. * @param str * @return the integer value for the specified hex string */ public static int intFromHexString(CharSequence str) { int n = 0; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); int v = c <= '9' ? c - '0' : c - 'a' + 10; n <<= 4; n |= (v & 0x0f); } return n; } /** * toString method for values include null * @param cs * @return the string */ public static String toString(CharSequence cs) { return cs == null ? null : cs.toString(); } private static Intent intentFromString(StringBuilder s) { CharSequence prefix = extract(s); Intent intent = null; if (prefix != null) { try { String action = toString(extract(s)); String uri = toString(extract(s)); intent = Intent.parseUri(uri, Intent.URI_INTENT_SCHEME); intent.setAction(action); } catch (URISyntaxException e) { e.printStackTrace(); } } return intent; } private static Uri uriFromString(StringBuilder s) { String str = toString(extract(s)); Uri uri = str == null ? null : Uri.parse(str); return uri; } }