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

The following examples show how to use android.content.ClipboardManager#getPrimaryClip() . These examples are extracted from open source projects. 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 Project: SimpleProject   File: VerificationCodeLayout.java    License: MIT License 6 votes vote down vote up
@Override
public boolean onTextContextMenuItem(int id) {
	if (id == android.R.id.paste) {
		ClipboardManager cm = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
		if (cm == null) {
			return super.onTextContextMenuItem(id);
		}

		ClipData clipData = cm.getPrimaryClip();
		if (clipData != null && clipData.getItemCount() > 0) {
			String content =  clipData.getItemAt(0).coerceToText(getContext()).toString();
			if (listener != null) {
				listener.onPaste(content);
			}
		}
	}

	return super.onTextContextMenuItem(id);
}
 
Example 2
@VisibleForTesting
public boolean canPasteAsPlainText() {
    // String resource "paste_as_plain_text" only exist in O.
    // Also this is an O feature, we need to make it consistant with TextView.
    if (!BuildInfo.isAtLeastO()) return false;
    if (!mCanEditRichlyForPastePopup) return false;
    ClipboardManager clipMgr =
            (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
    if (!clipMgr.hasPrimaryClip()) return false;

    ClipData clipData = clipMgr.getPrimaryClip();
    ClipDescription description = clipData.getDescription();
    CharSequence text = clipData.getItemAt(0).getText();
    boolean isPlainType = description.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
    // On Android, Spanned could be copied to Clipboard as plain_text MIME type, but in some
    // cases, Spanned could have text format, we need to show "paste as plain text" when
    // that happens.
    if (isPlainType && (text instanceof Spanned)) {
        Spanned spanned = (Spanned) text;
        if (hasStyleSpan(spanned)) return true;
    }
    return description.hasMimeType(ClipDescription.MIMETYPE_TEXT_HTML);
}
 
Example 3
Source Project: ProjectX   File: ClipboardHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 粘贴
 *
 * @param context Context
 * @return 数据,可能为空
 */
public <T> T paste(Context context) {
    if (mMatcher == null)
        return null;
    final ClipboardManager manager = (ClipboardManager)
            context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (manager == null)
        return null;
    final ClipData clip = manager.getPrimaryClip();
    if (clip == null)
        return null;
    if (clip.getItemCount() <= 0)
        return null;
    final ClipData.Item item = clip.getItemAt(0);
    final Uri uri = item.getUri();
    if (uri == null)
        return null;
    final int index = mMatcher.match(uri);
    final Adapter<?> adapter = getAdapter(index);
    if (adapter == null)
        return null;
    return onPaste(context, clip, adapter);
}
 
Example 4
public void pasteFromClipboard(View button) {
    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard != null && clipboard.hasPrimaryClip()) {
        ClipData clipData = clipboard.getPrimaryClip();
        Uri uri;
        CharSequence url;
        if ( clipData != null && clipData.getItemCount() > 0 ) {
            if ((uri = clipData.getItemAt(0).getUri()) != null) {
                urlEdit.setText(uri.toString());
            }
            else if ((url = clipData.getItemAt(0).getText()) != null ) {
                urlEdit.setText(url.toString());
            }
            startPoint(button);
        }
    }
}
 
Example 5
@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 6
public static CharSequence getLastClipContent(Context context) {
    ClipboardManager clipboard =
        (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = clipboard.getPrimaryClip();
    if (clipData != null) {
        ClipData.Item item = clipData.getItemAt(0);
        if (item != null) {
            return item.coerceToText(context);
        }
    }

    return null;
}
 
Example 7
Source Project: android-common   File: ClipboardUtil.java    License: Apache License 2.0 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static String getLatestText(Context context) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return String.valueOf(clip.getItemAt(0).coerceToText(context));
    }
    return null;
}
 
Example 8
@Override
public void pasteSource() {
    ClipboardManager clipboard = (ClipboardManager) mView.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = clipboard != null ? clipboard.getPrimaryClip() : null;
    if (clipData != null && clipData.getItemCount() > 0) {
        setText(String.valueOf(clipData.getItemAt(0).getText()));
    }
}
 
Example 9
Source Project: zone-sdk   File: ClipboardUtil.java    License: MIT License 5 votes vote down vote up
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static String getText(Context context, int index) {
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null && clip.getItemCount() > index) {
        return String.valueOf(clip.getItemAt(0).coerceToText(context));
    }
    return null;
}
 
Example 10
public Map<String, Integer> clipboardData() {
    Map<String, Integer> result = null;
    // Log.d(TAG, "clipboardData: ");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        try {
            ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            ClipData clipData = clipboardManager.getPrimaryClip();
            if (clipData.getItemCount() > 0) {
                ClipData.Item item = clipData.getItemAt(0);
                String tabSepratedData = item.getText().toString();
                //Log.d(TAG, "clipboardData: " + tabSepratedData);
                //validate tabSepratedData and get its data
                //unique id bae73ae068dacc6cb659d1fb231e7b11 i.e LastLauncher-ColorSniffer MD5-128

                String[] line = tabSepratedData.split("\n");//get each line

                Map<String, Integer> colorsAndId = new ArrayMap<>(); // map to put all values in key and values format
                // iterate over every line
                for (String entry : line) {
                    String[] activityIdAndColor = entry.split("\t");// split line into id and color
                    int color = Color.parseColor(activityIdAndColor[1]);
                    colorsAndId.put(activityIdAndColor[0], color);// put id and color to map

                    //   Log.d(TAG, "clipboardData: app:" + activityIdAndColor[0] + "  color==" + color);

                }
                setAppsColorFromClipboard(colorsAndId);
                result = colorsAndId;// return map
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // return empty null/
    return result;
}
 
Example 11
Source Project: clipboard   File: ClipboardModule.java    License: MIT License 5 votes vote down vote up
@ReactMethod
public void getString(Promise promise) {
  try {
    ClipboardManager clipboard = getClipboardService();
    ClipData clipData = clipboard.getPrimaryClip();
    if (clipData != null && clipData.getItemCount() >= 1) {
      ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0);
      promise.resolve("" + firstItem.getText());
    } else {
      promise.resolve("");
    }
  } catch (Exception e) {
    promise.reject(e);
  }
}
 
Example 12
private void setNumberFromClipBoard() {
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
        ClipboardManager clipboardManager = android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M ? (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE) : (ClipboardManager) getView().getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clipData = clipboardManager.getPrimaryClip();
        if (clipData.getItemCount() > 0) {
            ClipData.Item item = clipData.getItemAt(0);
            String text = item.getText().toString();
            //text='+'+text.replaceAll("/[^0-9]/", "");
            mPhoneInput.setPhoneNumber(text);
           // isFromClipBoard = true;
        }
    }

}
 
Example 13
@Override
public void pasteSource() {
    ClipboardManager clipboard = (ClipboardManager) mView.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clipData = clipboard != null ? clipboard.getPrimaryClip() : null;
    if (clipData != null && clipData.getItemCount() > 0) {
        setText(String.valueOf(clipData.getItemAt(0).getText()));
    }
}
 
Example 14
@ReactMethod
public void get(Callback cb) {
    try {
        ReactApplicationContext reactContext = getReactApplicationContext();

        ClipboardManager clipboard = (ClipboardManager)reactContext.getSystemService(reactContext.CLIPBOARD_SERVICE);

        android.content.ClipData clipData = clipboard.getPrimaryClip();

        if(clipData == null){
            cb.invoke("");
            return;
        }

        if (clipData.getItemCount() >= 1) {
            ClipData.Item clipDataItem = clipboard.getPrimaryClip().getItemAt(0);

            String data = "" + clipDataItem.getText();

            cb.invoke(data);
        } else {
            cb.invoke("");
            return;
        }

    } catch(Exception e) {
        Log.w("ClipboardAndroid", "Error in GET operation: " + e.getMessage());
    }
}
 
Example 15
Source Project: QPM   File: ClipboardUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取剪贴板的意图
 *
 * @return 剪贴板的意图
 */
public static Intent getIntent() {
    ClipboardManager cm = (ClipboardManager) Utils.getApp().getSystemService(Context.CLIPBOARD_SERVICE);
    //noinspection ConstantConditions
    ClipData clip = cm.getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return clip.getItemAt(0).getIntent();
    }
    return null;
}
 
Example 16
Source Project: mongol-library   File: MongolEditText.java    License: MIT License 5 votes vote down vote up
public void pasteText() {
    Context context = getContext();
    ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
    if (clipboard == null) return;
    ClipData clip = clipboard.getPrimaryClip();
    if (clip == null) return;
    ClipData.Item item = clip.getItemAt(0);
    if (item == null) return;
    CharSequence text = item.getText();
    if (text == null) return;
    int start = getSelectionStart();
    int end = getSelectionEnd();
    mTextStorage.replace(start, end, text);
}
 
Example 17
Source Project: Android-UtilCode   File: ClipboardUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取剪贴板的uri
 *
 * @return 剪贴板的uri
 */
public static Uri getUri() {
    ClipboardManager clipboard = (ClipboardManager) Utils.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
    ClipData clip = clipboard.getPrimaryClip();
    if (clip != null && clip.getItemCount() > 0) {
        return clip.getItemAt(0).getUri();
    }
    return null;
}
 
Example 18
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 19
public static CharSequence getText(Context context) {
  ClipboardManager clipboard = getManager(context);
  ClipData clip = clipboard.getPrimaryClip();
  return hasText(context) ? clip.getItemAt(0).coerceToText(context) : null;
}
 
Example 20
public static boolean hasText(Context context) {
  ClipboardManager clipboard = getManager(context);
  ClipData clip = clipboard.getPrimaryClip();
  return clip != null && clip.getItemCount() > 0;
}