Example usage for android.content Context grantUriPermission

List of usage examples for android.content Context grantUriPermission

Introduction

In this page you can find the example usage for android.content Context grantUriPermission.

Prototype

public abstract void grantUriPermission(String toPackage, Uri uri, @Intent.GrantUriMode int modeFlags);

Source Link

Document

Grant permission to access a specific Uri to another package, regardless of whether that package has general permission to access the Uri's content provider.

Usage

From source file:ru.dublgis.androidhelpers.DesktopUtils.java

public static boolean sendEmail(final Context ctx, final String to, final String subject, final String body,
        final String attach_file, final boolean force_content_provider, final String authorities) {
    //Log.d(TAG, "Will send email with subject \"" +
    //    subject + "\" to \"" + to + "\" with attach_file = \"" + attach_file + "\"" +
    //    ", force_content_provider = " + force_content_provider +
    //    ", authorities = \"" + authorities + "\"");
    try {/* w w w.  j a  v  a2  s. c  om*/
        // TODO: support multiple recipients
        String[] recipients = new String[] { to };

        final Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", to, null));
        List<ResolveInfo> resolveInfos = ctx.getPackageManager().queryIntentActivities(intent, 0);
        Intent chooserIntent = null;
        List<Intent> intentList = new ArrayList<Intent>();

        Intent i = new Intent(Intent.ACTION_SEND);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setType("message/rfc822");
        i.putExtra(Intent.EXTRA_EMAIL, recipients);
        i.putExtra(Intent.EXTRA_SUBJECT, subject);
        i.putExtra(Intent.EXTRA_TEXT, body);

        Uri workaround_grant_permission_for_uri = null;
        if (attach_file != null && !attach_file.isEmpty()) {
            if (!force_content_provider && android.os.Build.VERSION.SDK_INT < 23) {
                i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(attach_file)));
            } else {
                // Android 6+: going the longer route.
                // For more information, please see:
                // http://stackoverflow.com/questions/32981194/android-6-cannot-share-files-anymore
                i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                workaround_grant_permission_for_uri = FileProvider.getUriForFile(ctx, authorities,
                        new File(attach_file));
                i.putExtra(Intent.EXTRA_STREAM, workaround_grant_permission_for_uri);
            }
        }

        for (ResolveInfo resolveInfo : resolveInfos) {
            String packageName = resolveInfo.activityInfo.packageName;
            String name = resolveInfo.activityInfo.name;

            // Some mail clients will not read the URI unless this is done.
            // See here: https://stackoverflow.com/questions/24467696/android-file-provider-permission-denial
            if (workaround_grant_permission_for_uri != null) {
                try {
                    ctx.grantUriPermission(packageName, workaround_grant_permission_for_uri,
                            Intent.FLAG_GRANT_READ_URI_PERMISSION);
                } catch (final Throwable e) {
                    Log.e(TAG, "grantUriPermission error: ", e);
                }
            }

            Intent fakeIntent = (Intent) i.clone();
            fakeIntent.setComponent(new ComponentName(packageName, name));
            if (chooserIntent == null) {
                chooserIntent = fakeIntent;
            } else {
                intentList.add(fakeIntent);
            }
        }

        if (chooserIntent == null) {
            chooserIntent = Intent.createChooser(i, null // "Select email application."
            );
            chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else if (!intentList.isEmpty()) {
            Intent fakeIntent = chooserIntent;
            chooserIntent = new Intent(Intent.ACTION_CHOOSER);
            chooserIntent.putExtra(Intent.EXTRA_INTENT, fakeIntent);
            Intent[] extraIntents = intentList.toArray(new Intent[intentList.size()]);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
        }

        ctx.startActivity(chooserIntent);
        return true;
    } catch (final Throwable e) {
        Log.e(TAG, "sendEmail exception: ", e);
        return false;
    }
}

From source file:ru.dublgis.androidhelpers.DesktopUtils.java

public static boolean sendEmail(final Context ctx, final String to, final String subject, final String body,
        final String[] attachment, final String authorities) {
    try {/*  ww w  .ja v a2 s .co m*/
        final String[] recipients = new String[] { to };

        final Intent intent = new Intent(
                attachment.length > 1 ? Intent.ACTION_SEND_MULTIPLE : Intent.ACTION_SENDTO);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setType("message/rfc822");
        intent.putExtra(Intent.EXTRA_EMAIL, recipients);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, body);

        boolean grant_permissions_workaround = false;
        final ArrayList<Uri> uri = new ArrayList<>();
        if (attachment.length > 0) {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                for (final String fileName : attachment) {
                    uri.add(Uri.fromFile(new File(fileName)));
                }
            } else {
                // Android 6+: going the longer route.
                // For more information, please see:
                // http://stackoverflow.com/questions/32981194/android-6-cannot-share-files-anymore
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                grant_permissions_workaround = true;
                for (final String fileName : attachment) {
                    uri.add(FileProvider.getUriForFile(ctx, authorities, new File(fileName)));
                }
            }
            // Should not put array with only one element into intent because of a bug in GMail.
            if (uri.size() == 1) {
                intent.putExtra(Intent.EXTRA_STREAM, uri.get(0));
            } else {
                intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uri);
            }
        }

        final IntentResolverInfo mailtoIntentResolvers = new IntentResolverInfo(ctx.getPackageManager());
        mailtoIntentResolvers
                .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto", to, null)));

        final Intent chooserIntent;

        if (mailtoIntentResolvers.isEmpty()) {
            chooserIntent = Intent.createChooser(intent, null);
        } else {
            final IntentResolverInfo messageIntentResolvers = new IntentResolverInfo(ctx.getPackageManager());
            messageIntentResolvers
                    .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("sms", "", null)));
            messageIntentResolvers
                    .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mms", "", null)));
            messageIntentResolvers
                    .appendResolvers(new Intent(Intent.ACTION_SENDTO, Uri.fromParts("tel", "", null)));

            mailtoIntentResolvers.removeSamePackages(messageIntentResolvers.getResolveInfos());

            final List<Intent> intentList = new ArrayList<>();

            for (final ActivityInfo activityInfo : mailtoIntentResolvers.getResolveInfos()) {
                final String packageName = activityInfo.getPackageName();
                final String name = activityInfo.getName();

                // Some mail clients will not read the URI unless this is done.
                // See here: https://stackoverflow.com/questions/24467696/android-file-provider-permission-denial
                if (grant_permissions_workaround) {
                    for (int i = 0; i < uri.size(); ++i) {
                        try {
                            ctx.grantUriPermission(packageName, uri.get(i),
                                    Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        } catch (final Throwable e) {
                            Log.e(TAG, "grantUriPermission error: ", e);
                        }
                    }
                }

                final Intent cloneIntent = (Intent) intent.clone();
                cloneIntent.setComponent(new ComponentName(packageName, name));
                intentList.add(cloneIntent);
            }

            final Intent targetIntent = intentList.get(0);
            intentList.remove(0);

            chooserIntent = Intent.createChooser(targetIntent, null);
            if (!intentList.isEmpty()) {
                final Intent[] extraIntents = intentList.toArray(new Intent[intentList.size()]);
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
                    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
                } else {
                    chooserIntent.putExtra(Intent.EXTRA_ALTERNATE_INTENTS, extraIntents);
                }
            }
        }

        chooserIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        ctx.startActivity(chooserIntent);

        return true;
    } catch (final Throwable exception) {
        Log.e(TAG, "sendEmail exception: ", exception);
        return false;
    }
}