Example usage for android.content Intent setClipData

List of usage examples for android.content Intent setClipData

Introduction

In this page you can find the example usage for android.content Intent setClipData.

Prototype

public void setClipData(@Nullable ClipData clip) 

Source Link

Document

Set a ClipData associated with this Intent.

Usage

From source file:com.android.cts.intent.sender.ContentTest.java

/**
 * This method will send an intent to a receiver in another profile.
 * This intent will have a message in an extra, and a uri specified by the ClipData.
 * The receiver will read the message from the extra, and write it to the uri in
 * the ClipData./*from  ww  w .  j av  a2 s  .c  o  m*/
 */
public void testReceiverCanWrite() throws Exception {
    // It's the receiver of the intent that should write to the uri, not us. So, for now, we
    // write an empty string.
    Uri uri = getUriWithTextInFile("writing_test", "");
    assertTrue(uri != null);
    Intent intent = new Intent(ACTION_WRITE_TO_URI);
    intent.setClipData(ClipData.newRawUri("", uri));
    intent.putExtra("extra_message", MESSAGE);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

    mActivity.getCrossProfileResult(intent);
    assertEquals(MESSAGE, getFirstLineFromUri(uri));
}

From source file:com.android.cts.intent.sender.IntentSenderTest.java

/**
 * This method will send an intent to a receiver in another profile.
 * This intent will have a message in an extra, and a uri specified by the ClipData.
 * The receiver will read the message from the extra, and write it to the uri in
 * the ClipData.//from ww  w.j  a v  a  2 s  .c  o  m
 */
public void testReceiverCanWrite() throws Exception {
    // It's the receiver of the intent that should write to the uri, not us. So, for now, we
    // write an empty string.
    Uri uri = getUriWithTextInFile("writing_test", "");
    assertTrue(uri != null);
    Intent intent = new Intent(ACTION_WRITE_TO_URI);
    intent.setClipData(ClipData.newRawUri("", uri));
    intent.putExtra("extra_message", MESSAGE);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);

    mActivity.getResult(intent);
    assertEquals(MESSAGE, getFirstLineFromUri(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 a2  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.cts.intent.sender.IntentSenderTest.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  www.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.getResult(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.cts.intent.sender.ContentTest.java

/**
 * Ensure that sender is only able to send data that it has access to.
 *//*  w  ww  .  j a v  a 2s.com*/
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

/**
 * Ensure that sender is only able to send data that it has access to.
 *//*from w  w  w.  j a  va2s  .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.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  av a  2  s  .co m*/
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
    mActivity.getCrossProfileResult(grantPersistable);
}

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 . j  a  va 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.browser.UploadHandler.java

private Intent createCameraIntent(Uri contentUri) {
    if (contentUri == null)
        throw new IllegalArgumentException();
    mCapturedMedia = contentUri;//from  w w w.  j  av a  2 s .c o m
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedMedia);
    intent.setClipData(ClipData.newUri(mController.getActivity().getContentResolver(), FILE_PROVIDER_AUTHORITY,
            mCapturedMedia));
    return intent;
}

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));
}