append Clip Description to Clipboard - Android Android OS

Android examples for Android OS:Clipboard

Description

append Clip Description to Clipboard

Demo Code


//package com.java2s;

import android.content.ClipDescription;

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;

    private static void appendClipDescription(StringBuilder sb,
            ClipDescription desc) {/*  w  w w.j  a va  2 s. c o  m*/
        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 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 appendInt(StringBuilder sb, int i) {
        sb.append(hexStringFromInt(FLAG_INT, NUM_LEN)).append(
                hexStringFromInt(i, NUM_LEN));
    }

    /**
     * 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);
    }
}

Related Tutorials