List of usage examples for android.content ClipData newRawUri
static public ClipData newRawUri(CharSequence label, Uri uri)
From source file:com.android.cts.intent.sender.ContentTest.java
/** * The intent receiver will try to read uriNotGranted. * Inside the same user, this uri can be read if the receiver has the * com.android.cts.managedprofile.permission.SAMPLE permission. But since we cross * user-boundaries, it should not be able to (only uri grants work accross users for apps * without special permission)./*from w w w .j a v a 2 s .c o m*/ * We also grant uriGranted to the receiver (this uri belongs to the same content provider as * uriNotGranted), to enforce that even if an app has permission to one uri of a * ContentProvider, it still cannot access a uri it does not have access to. */ public void testAppPermissionsDontWorkAcrossProfiles() throws Exception { // The FileProvider does not allow to use app permissions. So we need to use another // ContentProvider. Uri uriGranted = getBasicContentProviderUri("uri_granted"); Uri uriNotGranted = getBasicContentProviderUri("uri_not_granted"); // Granting uriGranted to the receiver // Using a persistable permission so that it is kept even after we restart the receiver // activity with another intent. grantPersistableReadPermission(uriGranted); Intent notGrant = new Intent(ACTION_READ_FROM_URI); notGrant.setClipData(ClipData.newRawUri("", uriNotGranted)); final Intent result = mActivity.getCrossProfileResult(notGrant); assertNotNull(result); // The receiver did not have permission to read the uri. So it should have caught a security // exception. assertTrue(result.getBooleanExtra("extra_caught_security_exception", false)); }
From source file:com.android.messaging.ui.UIIntentsImpl.java
/** * Get an intent which takes you to a conversation *//*from w w w. j av a 2 s. c om*/ private Intent getConversationActivityIntent(final Context context, final String conversationId, final MessageData draft, final boolean withCustomTransition) { final Intent intent = new Intent(context, ConversationActivity.class); // Always try to reuse the same ConversationActivity in the current task so that we don't // have two conversation activities in the back stack. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Otherwise we're starting a new conversation if (conversationId != null) { intent.putExtra(UI_INTENT_EXTRA_CONVERSATION_ID, conversationId); } if (draft != null) { intent.putExtra(UI_INTENT_EXTRA_DRAFT_DATA, draft); // If draft attachments came from an external content provider via a share intent, we // need to propagate the URI permissions through to ConversationActivity. This requires // putting the URIs into the ClipData (setData also works, but accepts only one URI). ClipData clipData = null; for (final MessagePartData partData : draft.getParts()) { if (partData.isAttachment()) { final Uri uri = partData.getContentUri(); if (clipData == null) { clipData = ClipData.newRawUri("Attachments", uri); } else { clipData.addItem(new ClipData.Item(uri)); } } } if (clipData != null) { intent.setClipData(clipData); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } } if (withCustomTransition) { intent.putExtra(UI_INTENT_EXTRA_WITH_CUSTOM_TRANSITION, true); } if (!(context instanceof Activity)) { // If the caller supplies an application context, and not an activity context, we must // include this flag intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } return intent; }
From source file:com.silentcircle.contacts.utils.ContactPhotoUtils19.java
/** * Adds common extras to gallery intents. * * @param intent The intent to add extras to. * @param photoUri The uri of the file to save the image to. *//*from w w w . j a va2 s .c o m*/ @TargetApi(Build.VERSION_CODES.KITKAT) public static void addPhotoPickerExtras(Intent intent, Uri photoUri) { intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, photoUri)); }
From source file:com.android.cts.intent.sender.IntentSenderTest.java
/** * Ensure that sender is only able to send data that it has access to. *///from w w w . j av a 2 s .co m public void testSecurity() throws Exception { // Pick a URI that neither of us have access to; it doens't matter if // its missing, since we expect a SE before a FNFE. final Uri uri = Uri.parse("content://media/external/images/media/10240"); final Intent intent = new Intent(ACTION_READ_FROM_URI); intent.setClipData(ClipData.newRawUri("", uri)); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // We're expecting to run into a security exception final Intent result = mActivity.getResult(intent); if (result == null) { // This is fine; probably of a SecurityException when off in the // system somewhere. } else { // But if we somehow came through, make sure they threw. assertTrue(result.getBooleanExtra("extra_caught_security_exception", false)); } }
From source file:com.google.android.apps.forscience.whistlepunk.PictureUtils.java
public static void launchExternalViewer(Activity activity, String fileUrl) { Intent intent = new Intent(Intent.ACTION_VIEW); String extension = MimeTypeMap.getFileExtensionFromUrl(fileUrl); String type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); if (!TextUtils.isEmpty(type)) { String filePath = fileUrl; if (filePath.startsWith("file:")) { filePath = filePath.substring("file:".length()); }/*from w w w . ja va 2 s . c o m*/ Uri photoUri = FileProvider.getUriForFile(activity, activity.getPackageName(), new File(filePath)); intent.setDataAndType(photoUri, type); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { // Needed to avoid security exception on KitKat. intent.setClipData(ClipData.newRawUri(null, photoUri)); } intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); try { activity.startActivity(intent); } catch (ActivityNotFoundException e) { Log.e(TAG, "No activity found to handle this " + fileUrl + " type " + type); } } else { Log.w(TAG, "Could not find mime type for " + fileUrl); } }
From source file:com.android.cts.intent.sender.ContentTest.java
/** * Ensure that sender is only able to send data that it has access to. *///w ww . j ava2 s. co m public void testSecurity() throws Exception { // Pick a URI that neither of us have access to; it doens't matter if // its missing, since we expect a SE before a FNFE. final Uri uri = Uri.parse("content://media/external/images/media/10240"); final Intent intent = new Intent(ACTION_READ_FROM_URI); intent.setClipData(ClipData.newRawUri("", uri)); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // We're expecting to run into a security exception final Intent result = mActivity.getCrossProfileResult(intent); if (result == null) { // This is fine; probably of a SecurityException when off in the // system somewhere. } else { // But if we somehow came through, make sure they threw. assertTrue(result.getBooleanExtra("extra_caught_security_exception", false)); } }
From source file:com.android.cts.intent.sender.IntentSenderTest.java
private void grantPersistableReadPermission(Uri uri) throws Exception { Intent grantPersistable = new Intent(ACTION_TAKE_PERSISTABLE_URI_PERMISSION); grantPersistable.setClipData(ClipData.newRawUri("", uri)); grantPersistable/* ww w .ja v a 2 s.c o m*/ .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); mActivity.getResult(grantPersistable); }
From source file:com.android.cts.intent.sender.ContentTest.java
private void grantPersistableReadPermission(Uri uri) throws Exception { Intent grantPersistable = new Intent(ACTION_TAKE_PERSISTABLE_URI_PERMISSION); grantPersistable.setClipData(ClipData.newRawUri("", uri)); grantPersistable/*from ww w. j a v a2s.com*/ .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION); mActivity.getCrossProfileResult(grantPersistable); }
From source file:com.android.gallery3d.gadget.WidgetConfigure.java
private void setChoosenPhoto(final Intent data) { AsyncTask.execute(new Runnable() { @Override/*from w ww . ja va 2 s . c o m*/ public void run() { Resources res = getResources(); float width = res.getDimension(R.dimen.appwidget_width); float height = res.getDimension(R.dimen.appwidget_height); // We try to crop a larger image (by scale factor), but there is still // a bound on the binder limit. float scale = Math.min(WIDGET_SCALE_FACTOR, MAX_WIDGET_SIDE / Math.max(width, height)); int widgetWidth = Math.round(width * scale); int widgetHeight = Math.round(height * scale); File cropSrc = new File(getCacheDir(), "crop_source.png"); File cropDst = new File(getCacheDir(), "crop_dest.png"); mPickedItem = data.getData(); if (!copyUriToFile(mPickedItem, cropSrc)) { setResult(Activity.RESULT_CANCELED); finish(); return; } mCropSrc = FileProvider.getUriForFile(WidgetConfigure.this, "com.android.gallery3d.fileprovider", new File(cropSrc.getAbsolutePath())); mCropDst = FileProvider.getUriForFile(WidgetConfigure.this, "com.android.gallery3d.fileprovider", new File(cropDst.getAbsolutePath())); Intent request = new Intent(CropActivity.CROP_ACTION).putExtra(CropExtras.KEY_OUTPUT_X, widgetWidth) .putExtra(CropExtras.KEY_OUTPUT_Y, widgetHeight) .putExtra(CropExtras.KEY_ASPECT_X, widgetWidth) .putExtra(CropExtras.KEY_ASPECT_Y, widgetHeight) .putExtra(CropExtras.KEY_SCALE_UP_IF_NEEDED, true).putExtra(CropExtras.KEY_SCALE, true) .putExtra(CropExtras.KEY_RETURN_DATA, false).setDataAndType(mCropSrc, "image/*") .addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); request.putExtra(MediaStore.EXTRA_OUTPUT, mCropDst); request.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, mCropDst)); startActivityForResult(request, REQUEST_CROP_IMAGE); } }); }
From source file:com.android.settings.users.EditUserPhotoController.java
private void appendOutputExtra(Intent intent, Uri pictureUri) { intent.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri); intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setClipData(ClipData.newRawUri(MediaStore.EXTRA_OUTPUT, pictureUri)); }