Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Arrays;

import android.annotation.SuppressLint;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.ClipboardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Log;

public class Main {
    private static android.text.ClipboardManager oldClipboard = null;
    private static ClipboardManager clipboard = null;

    /**
     * Return text content of clipboard as individual lines 
     * @param ctx
     * @return
     */
    @SuppressLint("NewApi")
    private static ArrayList<String> getTextLines(Context ctx) {

        String EOL = "\\r?\\n|\\r";

        if (checkForText(ctx)) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);

                // Gets the clipboard as text.
                CharSequence cs = item.getText();
                if (cs == null) { // item might be an URI
                    Uri pasteUri = item.getUri();
                    if (pasteUri != null) { // FIXME untested
                        try {
                            Log.d("ClipboardUtils", "Clipboard contains an uri");
                            ContentResolver cr = ctx.getContentResolver();
                            String uriMimeType = cr.getType(pasteUri);
                            //               pasteData = resolveUri(pasteUri);
                            // If the return value is not null, the Uri is a content Uri
                            if (uriMimeType != null) {

                                // Does the content provider offer a MIME type that the current application can use?
                                if (uriMimeType.equals(ClipDescription.MIMETYPE_TEXT_PLAIN)) {

                                    // Get the data from the content provider.
                                    Cursor pasteCursor = cr.query(pasteUri, null, null, null, null);

                                    // If the Cursor contains data, move to the first record
                                    if (pasteCursor != null) {
                                        if (pasteCursor.moveToFirst()) {
                                            String pasteData = pasteCursor.getString(0);
                                            return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                                        }
                                        // close the Cursor
                                        pasteCursor.close();
                                    }
                                }
                            }
                        } catch (Exception e) { // FIXME given that the above is unteted, cath all here
                            Log.e("ClipboardUtils", "Resolving URI failed " + e);
                            e.printStackTrace();
                            return null;
                        }
                    }
                } else {
                    Log.d("ClipboardUtils", "Clipboard contains text");
                    String pasteData = cs.toString();
                    return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                }
            } else {
                // Gets the clipboard as text.
                @SuppressWarnings("deprecation")
                CharSequence cs = oldClipboard.getText();
                if (cs != null) {
                    String pasteData = cs.toString();
                    if (pasteData != null) { // should always be the case
                        return new ArrayList<String>(Arrays.asList(pasteData.split(EOL)));
                    }
                }
            }
            Log.e("ClipboardUtils", "Clipboard contains an invalid data type");
        }
        return null;
    }

    /**
     * Return true if there is text in the clipboard
     * @param ctx
     * @return
     */
    @SuppressWarnings("deprecation")
    @SuppressLint("NewApi")
    public static boolean checkForText(Context ctx) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (clipboard == null) {
                clipboard = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
            }
            return clipboard.hasPrimaryClip();
        } else {
            if (oldClipboard == null) {
                oldClipboard = (android.text.ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);
            }
            return oldClipboard.hasText();
        }
    }
}