public boolean onDrag(View v, DragEvent event) { final int action = event.getAction(); switch (action) { case DragEvent.ACTION_DRAG_STARTED: if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { ((ImageView) v).setColorFilter(Color.BLUE); Toast.makeText(DragAndDropActivity.this, "ACTION_DRAG_STARTED", Toast.LENGTH_SHORT) .show(); v.invalidate(); return true; } else { return false; } case DragEvent.ACTION_DRAG_ENTERED: ((ImageView) v).setColorFilter(Color.GREEN); Toast.makeText(DragAndDropActivity.this, "ACTION_DRAG_ENTERED", Toast.LENGTH_SHORT) .show(); v.invalidate(); return true; case DragEvent.ACTION_DRAG_LOCATION: return true; case DragEvent.ACTION_DRAG_EXITED: ((ImageView) v).setColorFilter(Color.RED); Toast.makeText(DragAndDropActivity.this, "ACTION_DRAG_EXITED", Toast.LENGTH_SHORT).show(); v.invalidate(); return true; case DragEvent.ACTION_DROP: ClipData.Item item = event.getClipData().getItemAt(0); CharSequence dragData = item.getText(); Toast.makeText( DragAndDropActivity.this, "Dragged data is " + dragData, Toast.LENGTH_SHORT) .show(); ((ImageView) v).clearColorFilter(); v.invalidate(); return true; case DragEvent.ACTION_DRAG_ENDED: ((ImageView) v).clearColorFilter(); v.invalidate(); if (event.getResult()) { Toast.makeText(DragAndDropActivity.this, "The drop was handled.", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(DragAndDropActivity.this, "The drop didn't work.", Toast.LENGTH_SHORT) .show(); } ; return true; default: Log.e("DragDrop Example", "Unknown action type received by OnDragListener."); break; } ; return false; };
@Override protected void onResume() { super.onResume(); final int entryCount = getSupportFragmentManager().getBackStackEntryCount(); final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); if (entryCount == 0 && clipboardManager.hasPrimaryClip() && clipboardManager.getPrimaryClip().getItemCount() > 0) { final ClipData primaryClip = clipboardManager.getPrimaryClip(); final ClipData.Item item = primaryClip.getItemAt(0); final String text = item.coerceToText(this).toString(); if (text.trim().length() > 0) { final Snackbar snackbar = Snackbar.with(this) .text(getString(R.string.paste_from_clipboard_prompt)) .actionLabel(getString(R.string.paste_clipboard_action)) .actionColorResource(R.color.sharelock_orange) .actionListener( new ActionClickListener() { @Override public void onActionClicked(Snackbar snackbar) { bus.postSticky(new ClipboardSecretEvent(text)); clipboardManager.setPrimaryClip(ClipData.newPlainText("", "")); } }) .duration(Snackbar.SnackbarDuration.LENGTH_INDEFINITE); SnackbarManager.show(snackbar); } } }
private void processDrop(DragEvent event) { final ClipData data = event.getClipData(); final int N = data.getItemCount(); for (int i = 0; i < N; i++) { ClipData.Item item = data.getItemAt(i); String text = item.coerceToText(getContext()).toString(); if (this.tile.type.equalsIgnoreCase(Constants.BLANK)) { String[] tileArgs = text.split(";"); this.setTile( new Tile( this.tile.posOnBoard, tileArgs[1], Integer.parseInt(tileArgs[0]), MoveDirection.NONE, tileArgs[4], Constants.stringToBmp.get(tileArgs[1]))); Constants.droppedOnBlank = true; // Communicator.getInstance(null, // null).sendMoveTile(tileArgs[1]); ClientCommunicator.getInstance(getContext(), null).sendMoveRequset(tile); } else { Constants.droppedOnBlank = false; } } }
private final void revokeItemLocked(ClipData.Item item) { if (item.getUri() != null) { revokeUriLocked(item.getUri()); } Intent intent = item.getIntent(); if (intent != null && intent.getData() != null) { revokeUriLocked(intent.getData()); } }
private final void grantItemLocked(ClipData.Item item, String pkg) { if (item.getUri() != null) { grantUriLocked(item.getUri(), pkg); } Intent intent = item.getIntent(); if (intent != null && intent.getData() != null) { grantUriLocked(intent.getData(), pkg); } }
private final void checkItemOwnerLocked(ClipData.Item item, int uid) { if (item.getUri() != null) { checkUriOwnerLocked(item.getUri(), uid); } Intent intent = item.getIntent(); if (intent != null && intent.getData() != null) { checkUriOwnerLocked(intent.getData(), uid); } }
/** * 粘贴【剪切板】功能 * * @return 返回剪贴板的内容 */ @TargetApi(Build.VERSION_CODES.HONEYCOMB) public String paste() { // 粘贴部分【本案例不需要粘贴】 ClipboardManager myClipboard; myClipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); // 需要做的下一件事就是通过调用ClipData类的数据方法的相应类型来实例化ClipData对象。如果文本数据在newPlainText方法被调用。必须将数据设置为剪贴板管理器对象的剪辑。 ClipData clipData = myClipboard.getPrimaryClip(); ClipData.Item item = clipData.getItemAt(0); return item.getText().toString(); }
@Override public boolean onDrag(View v, DragEvent event) { // Defines a variable to store the action type for the incoming event final int action = event.getAction(); switch (action) { case DragEvent.ACTION_DRAG_STARTED: // Log.d(TAG, "onDrag() ACTION_DRAG_STARTED"); return true; case DragEvent.ACTION_DRAG_ENTERED: Log.d(TAG, "onDrag() ACTION_DRAG_ENTERED"); return true; case DragEvent.ACTION_DRAG_LOCATION: // Log.d(TAG, "onDrag() ACTION_DRAG_LOCATION"); return true; case DragEvent.ACTION_DRAG_EXITED: Log.d(TAG, "onDrag() ACTION_DRAG_EXITED"); return true; case DragEvent.ACTION_DROP: Log.d(TAG, "onDrag() ACTION_DROP"); // Gets the item containing the dragged data ClipData.Item item = event.getClipData().getItemAt(0); String sku = item.getText().toString(); if (v.getId() == R.id.basketImageView) { Toast.makeText(this, "Dropped " + sku, Toast.LENGTH_LONG).show(); } return true; case DragEvent.ACTION_DRAG_ENDED: // Log.d(TAG, "onDrag() ACTION_DRAG_ENDED"); return true; // An unknown action type was received. default: Log.e(TAG, "onDrag() Unknown DragEvent"); break; } Log.d(TAG, "onDrag() Exit with false"); return false; }
@TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static String getImageUrl(final Context context) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) return null; final ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); final ClipData primaryClip = cm.getPrimaryClip(); if (primaryClip.getItemCount() > 0) { final ClipData.Item item = primaryClip.getItemAt(0); final CharSequence styledText = item.coerceToStyledText(context); if (styledText instanceof Spanned) { final Spanned spanned = (Spanned) styledText; final ImageSpan[] imageSpans = spanned.getSpans(0, spanned.length(), ImageSpan.class); if (imageSpans.length == 1) return imageSpans[0].getSource(); } } return null; }
void pasteIntent() { ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); if (cm.hasPrimaryClip() == false) { Toast.makeText(this, "Clipboard Empty", 0).show(); return; } if (cm.getPrimaryClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_INTENT) == false) { Toast.makeText(this, "Clip is not intent", 0).show(); return; } ClipData clip = cm.getPrimaryClip(); ClipData.Item item = clip.getItemAt(0); Intent intent = item.getIntent(); if (intent != null) { startActivity(intent); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // setContentView(R.layout.activity_test_hook_binder); try { BinderHookHelper.hookClipboardService(); } catch (Exception e) { e.printStackTrace(); } EditText editText = new EditText(this); setContentView(editText); ClipboardManager manager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); if (manager.hasPrimaryClip()) { ClipData data = manager.getPrimaryClip(); ClipData.Item item = data.getItemAt(0); editText.setText(item.getText().toString()); } }
private void readClipboard() { ClipboardManager cm = (ClipboardManager) getApplicationContext().getSystemService(Context.CLIPBOARD_SERVICE); ClipData cmContent = cm.getPrimaryClip(); if (cmContent == null) { return; } ClipData.Item item = cmContent.getItemAt(0); if (item != null) { String url = item.coerceToText(this).toString(); boolean a = !TextUtils.isEmpty(url) && !url.equals(SettingUtility.getLastFoundWeiboAccountLink()); boolean b = Utility.isWeiboAccountIdLink(url) || Utility.isWeiboAccountDomainLink(url); if (a && b) { OpenWeiboAccountLinkDialog dialog = new OpenWeiboAccountLinkDialog(url); dialog.show(getSupportFragmentManager(), ""); SettingUtility.setLastFoundWeiboAccountLink(url); } } }
/** * Return the content text. If there is intent in clip data, display intent, or display uri if uri * included, or html and text at last. * * @return the content in string. */ public String getDiaplayText() { String ret = null; if (this.isCoerceText()) { ret = this.getContent(); } else { ClipData.Item item = this.rawContent.getItemAt(0); CharSequence text = item.getText(); String html = item.getHtmlText(); Intent intent = item.getIntent(); Uri uri = item.getUri(); if (intent != null) { ret = intent.toUri(Intent.URI_INTENT_SCHEME); } else if (uri != null) { ret = uri.toString(); } else if (html != null) { ret = html; } else if (text != null) { ret = text.toString(); } } return ret; }
public void PasteFromClipboard() { ClipData cdata = mClipBoard.getPrimaryClip(); ClipData.Item item = cdata.getItemAt(0); this.setText(item.getText().toString()); }
/** * Required for reacting to selected bitmaps from galleries. Loads and prepares bitmaps * asynchronously, showing progress on the fly and an indeterminate progress bar. * * @param requestCode The request code of the activity result started for intent. * @param resultCode The result code. Does nothing if not ok. * @param data The returned data. */ public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK || requestCode != WorkshopView.PICK_IMAGES_FOR_BUNDLE || data == null) { return; } ClipData clipData = data.getClipData(); Uri[] selectedImageUris; if (clipData == null) { selectedImageUris = new Uri[] {data.getData()}; } else { selectedImageUris = new Uri[clipData.getItemCount()]; for (int i = 0; i < clipData.getItemCount(); i++) { ClipData.Item curr = clipData.getItemAt(i); selectedImageUris[i] = curr.getUri(); } } new AsyncTask<Uri, SelectedBitmap, Void>() { @Override public void onPreExecute() { ensureTempDirectory(); mBitmapsSelectCount++; if (mBitmapsSelectCount > 0) { mBitmapsSelectProgress.setVisibility(View.VISIBLE); } } @Override protected Void doInBackground(Uri... params) { String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME }; for (Uri selectedImages : params) { if (selectedImages == null) { continue; } Cursor cursor = null; try { cursor = mActivity.getContentResolver().query(selectedImages, columns, null, null, null); if (cursor == null || cursor.isAfterLast()) { continue; // empty cursor } cursor.moveToFirst(); int pathIndex = cursor.getColumnIndexOrThrow(columns[0]); int sizeIndex = cursor.getColumnIndexOrThrow(columns[1]); int nameIndex = cursor.getColumnIndex(columns[2]); while (!cursor.isAfterLast()) { String picturePath = cursor.getString(pathIndex); int sizeKB = cursor.getInt(sizeIndex) / (1024); if (picturePath == null) { InputStream input = null; try { input = mActivity.getContentResolver().openInputStream(selectedImages); } catch (FileNotFoundException e1) { Log.e( "HomeStuff", "File not found when trying to decode bitmap from stream: " + e1); } String bitmapName = null; if (nameIndex != -1) { bitmapName = cursor.getString(nameIndex); } if (TextUtils.isEmpty(bitmapName)) { bitmapName = String.valueOf(System.currentTimeMillis()); } File bitmapFileInTemp = new File(mTempDirectory, bitmapName); Bitmap bitmap = ImageUtil.loadBitmap( input, IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT, BitmapUtil.MODE_FIT_NO_GROW); if (ImageUtil.saveToFile(bitmap, bitmapFileInTemp, null, COMPRESSION)) { publishProgress(new SelectedBitmap(bitmap, bitmapFileInTemp, sizeKB, mBuffer)); } } else { File path = new File(picturePath); String bitmapName = path.getName(); if (TextUtils.isEmpty(bitmapName) && nameIndex != -1) { bitmapName = cursor.getString(nameIndex); } File targetPath = new File(mTempDirectory, bitmapName); Bitmap bitmap = ImageUtil.loadBitmap( path, IMAGE_MAX_WIDTH, IMAGE_MAX_HEIGHT, BitmapUtil.MODE_FIT_NO_GROW); if (ImageUtil.saveToFile(bitmap, targetPath, null, COMPRESSION)) { publishProgress(new SelectedBitmap(bitmap, targetPath, sizeKB, mBuffer)); } } cursor.moveToNext(); } } catch (Exception e) { Log.e("HomeStuff", "Error retrieving images from cursor " + cursor + ": " + e); } finally { if (cursor != null) { cursor.close(); } } } return null; } @Override public void onProgressUpdate(SelectedBitmap... bitmap) { if (!isImageSelected(bitmap[0].mPathInTemp.getAbsolutePath())) { synchronized (mSelectedBitmaps) { mSelectedBitmaps.add(bitmap[0]); } } mSaveResult = ImageXmlWriter.RESULT_NONE; fillSelectedBitmapsData(); updateStatus(); } @Override public void onPostExecute(Void nothing) { mSaveResult = ImageXmlWriter.RESULT_NONE; Log.d( "HomeStuff", "Finished loading bitmaps (Processes still loading: " + (mBitmapsSelectCount - 1) + ")."); mBitmapsSelectCount--; if (mBitmapsSelectCount <= 0) { mBitmapsSelectProgress.setVisibility(View.INVISIBLE); } applyOrigin(); fillSelectedBitmapsData(); updateStatus(); } }.execute(selectedImageUris); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { switch (requestCode) { case REQUEST_CODE_TAKEN_PHOTO_CAMERA: { if (data != null) { final Bitmap bitmap = (Bitmap) data.getExtras().get("data"); onImageTaken(bitmap); break; } } case REQUEST_CODE_TAKEN_PHOTO_GALLERY: { if (data != null) { if (data.getParcelableArrayListExtra("uris") != null) { List<Uri> uriList = data.getParcelableArrayListExtra("uris"); for (int i = 0; i < uriList.size(); i++) { new GetBitmapFromUriTask( this, uriList.get(i), new GetBitmapFromUriTask.IOnImageTakenListener() { @Override public void onImageTaken(final Bitmap pBitmap) { MainActivity.this.onImageTaken(pBitmap); } }) .execute(); } } else if (data.getData() == null) { ClipData clipData = data.getClipData(); for (int i = 0; i < clipData.getItemCount(); i++) { ClipData.Item item = clipData.getItemAt(i); new GetBitmapFromUriTask( this, item.getUri(), new GetBitmapFromUriTask.IOnImageTakenListener() { @Override public void onImageTaken(final Bitmap pBitmap) { MainActivity.this.onImageTaken(pBitmap); } }) .execute(); } } else { new GetBitmapFromUriTask( this, data.getData(), new GetBitmapFromUriTask.IOnImageTakenListener() { @Override public void onImageTaken(final Bitmap pBitmap) { MainActivity.this.onImageTaken(pBitmap); } }) .execute(); } break; } } } } }