Java Code Examples for android.content.ClipboardManager#getPrimaryClipDescription()

The following examples show how to use android.content.ClipboardManager#getPrimaryClipDescription() . 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: CustomThemeListingActivity.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void importTheme() {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard != null) {
        // If it does contain data, decide if you can handle the data.
        if (!clipboard.hasPrimaryClip()) {
            Snackbar.make(coordinatorLayout, R.string.no_data_in_clipboard, Snackbar.LENGTH_SHORT).show();
        } else if (clipboard.getPrimaryClipDescription() != null &&
                !clipboard.getPrimaryClipDescription().hasMimeType(MIMETYPE_TEXT_PLAIN)) {
            // since the clipboard has data but it is not plain text
            Snackbar.make(coordinatorLayout, R.string.no_data_in_clipboard, Snackbar.LENGTH_SHORT).show();
        } else if (clipboard.getPrimaryClip() != null) {
            ClipData.Item item = clipboard.getPrimaryClip().getItemAt(0);
            String json = item.getText().toString();
            try {
                CustomTheme customTheme = new Gson().fromJson(json, CustomTheme.class);
                checkDuplicateAndImportTheme(customTheme, true);
            } catch (JsonSyntaxException e) {
                Snackbar.make(coordinatorLayout, R.string.parse_theme_failed, Snackbar.LENGTH_SHORT).show();
            }
        }
    }
}
 
Example 2
Source File: ClipboardManagerUtil.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
public static CharSequence getText() {
    android.text.ClipboardManager clipboardManager = ServiceUtil.getClipboardManager();
    if (APILevel.require(11)) {
        ClipboardManager cm = (ClipboardManager) clipboardManager;
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        if (clipData != null
                && description != null
                && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
            return clipData.getItemAt(0).getText();
        else
            return null;
    } else {
        return clipboardManager.getText();
    }
}
 
Example 3
Source File: ClipboardUtils.java    From XposedSmsCode with GNU General Public License v3.0 6 votes vote down vote up
public static void clearClipboard(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (cm == null) {
        XLog.e("Clear failed, clipboard manager is null");
        return;
    }
    if(cm.hasPrimaryClip()) {
        ClipDescription cd = cm.getPrimaryClipDescription();
        if (cd != null) {
            if (cd.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                cm.setPrimaryClip(ClipData.newPlainText("Copy text", ""));
                XLog.i("Clear clipboard succeed");
            }
        }
    }
}
 
Example 4
Source File: ClipboardUtils.java    From AdBlockedWebView-Android with MIT License 6 votes vote down vote up
public static CharSequence getText(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        if (clipData != null && description != null && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
            return clipData.getItemAt(0).getText();
        } else {
            return "";
        }
    } else {
        //noinspection deprecation
        return cm.getText();
    }
}
 
Example 5
Source File: ClipboardManagerUtil.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
public static boolean hasText() {
    android.text.ClipboardManager clipboardManager = ServiceUtil.getClipboardManager();
    if (APILevel.require(11)) {
        ClipboardManager cm = (ClipboardManager) clipboardManager;
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        return clipData != null
                && description != null
                && (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN));
    } else {
        return clipboardManager.hasText();
    }
}
 
Example 6
Source File: ClipboardUtils.java    From SmsCode with GNU General Public License v3.0 5 votes vote down vote up
public static void clearClipboard(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (cm == null) {
        return;
    }
    if (cm.hasPrimaryClip()) {
        ClipDescription cd = cm.getPrimaryClipDescription();
        if (cd != null) {
            if (cd.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                cm.setPrimaryClip(ClipData.newPlainText("Copy text", ""));
            }
        }
    }
}
 
Example 7
Source File: ClipboardUtils.java    From AdBlockedWebView-Android with MIT License 5 votes vote down vote up
public static boolean hasText(Context context) {
    ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        ClipDescription description = cm.getPrimaryClipDescription();
        ClipData clipData = cm.getPrimaryClip();
        return clipData != null
                && description != null
                && (description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN));
    } else {
        //noinspection deprecation
        return cm.hasText();
    }
}
 
Example 8
Source File: Utils.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
public static String readFromClipboard() {
    ClipboardManager clipboard = (ClipboardManager) App.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard.hasPrimaryClip()) {
        android.content.ClipDescription description = clipboard.getPrimaryClipDescription();
        android.content.ClipData data = clipboard.getPrimaryClip();
        if (data != null && description != null && description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
            return String.valueOf(data.getItemAt(0).getText());
    }
    return null;
}