Example usage for android.content Intent clone

List of usage examples for android.content Intent clone

Introduction

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

Prototype

@Override
    public Object clone() 

Source Link

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 {//from ww  w . java 2  s.  c o m
        // 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:com.doplgangr.secrecy.views.FileViewer.java

private Intent generateCustomChooserIntent(Intent prototype, ArrayList<Uri> uris) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
    Intent chooserIntent;//w  w  w  .  j  ava 2s  .c  o m

    Intent dummy = new Intent(prototype.getAction());
    dummy.setType(prototype.getType());
    List<ResolveInfo> resInfo = context.getPackageManager().queryIntentActivities(dummy, 0);

    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            if (resolveInfo.activityInfo == null
                    || resolveInfo.activityInfo.packageName.equalsIgnoreCase("com.doplgangr.secrecy"))
                continue;

            HashMap<String, String> info = new HashMap<String, String>();
            info.put("packageName", resolveInfo.activityInfo.packageName);
            info.put("className", resolveInfo.activityInfo.name);
            info.put("simpleName",
                    String.valueOf(resolveInfo.activityInfo.loadLabel(context.getPackageManager())));
            intentMetaInfo.add(info);
            for (Uri uri : uris)
                context.grantUriPermission(resolveInfo.activityInfo.packageName, uri,
                        Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        if (!intentMetaInfo.isEmpty()) {
            // sorting for nice readability
            Collections.sort(intentMetaInfo, new Comparator<HashMap<String, String>>() {
                @Override
                public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
                    return map.get("simpleName").compareTo(map2.get("simpleName"));
                }
            });

            // create the custom intent list
            for (HashMap<String, String> metaInfo : intentMetaInfo) {
                Intent targetedShareIntent = (Intent) prototype.clone();
                targetedShareIntent.setPackage(metaInfo.get("packageName"));
                targetedShareIntent.setClassName(metaInfo.get("packageName"), metaInfo.get("className"));
                targetedShareIntents.add(targetedShareIntent);
            }
            chooserIntent = Intent.createChooser(targetedShareIntents.remove(targetedShareIntents.size() - 1),
                    CustomApp.context.getString(R.string.Dialog__send_file));
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                    targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));
            return chooserIntent;
        }
    }

    return new Intent(Intent.ACTION_SEND); //Unable to do anything. Duh.
}

From source file:de.mrapp.android.bottomsheet.BottomSheet.java

/**
 * Adds the apps, which are able to handle a specific intent, as items to the bottom sheet. This
 * causes all previously added items to be removed. When an item is clicked, the corresponding
 * app is started.//  w w  w.j a  va2 s .com
 *
 * @param activity
 *         The activity, the bottom sheet belongs to, as an instance of the class {@link
 *         Activity}. The activity may not be null
 * @param intent
 *         The intent as an instance of the class {@link Intent}. The intent may not be null
 */
public final void setIntent(@NonNull final Activity activity, @NonNull final Intent intent) {
    ensureNotNull(activity, "The activity may not be null");
    ensureNotNull(intent, "The intent may not be null");
    removeAllItems();
    PackageManager packageManager = activity.getPackageManager();
    List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);

    for (int i = 0; i < resolveInfos.size(); i++) {
        ResolveInfo resolveInfo = resolveInfos.get(i);
        addItem(i, resolveInfo.loadLabel(packageManager), resolveInfo.loadIcon(packageManager));
    }

    setOnItemClickListener(createIntentClickListener(activity, (Intent) intent.clone(), resolveInfos));
}

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 {/*from  w w  w .j a va  2s.  c om*/
        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;
    }
}

From source file:com.codename1.impl.android.AndroidImplementation.java

/**
 * Adds actions to a push notification.  This is called by the Push broadcast receiver probably before 
 * Codename One is initialized/*from w  w  w  .j a v a  2 s .  c  o m*/
 * @param provider Reference to the app's main class which implements PushActionsProvider
 * @param categoryId The category ID of the push notification.
 * @param builder The builder for the push notification.
 * @param targetIntent The target intent... this should go to the app's main Activity.
 * @param context The current context (inside the Broadcast receiver).
 * @throws IOException 
 */
public static void addActionsToNotification(PushActionsProvider provider, String categoryId,
        NotificationCompat.Builder builder, Intent targetIntent, Context context) throws IOException {
    // NOTE:  THis will likely run when the main activity isn't running so we won't have
    // access to any display properties... just native Android APIs will be accessible.

    PushActionCategory category = null;
    PushActionCategory[] categories;
    if (provider != null) {
        categories = provider.getPushActionCategories();
    } else {
        categories = getInstalledPushActionCategories(context);
    }
    for (PushActionCategory candidateCategory : categories) {
        if (categoryId.equals(candidateCategory.getId())) {
            category = candidateCategory;
            break;
        }
    }
    if (category == null) {
        return;
    }

    int requestCode = 1;
    for (PushAction action : category.getActions()) {
        Intent newIntent = (Intent) targetIntent.clone();
        newIntent.putExtra("pushActionId", action.getId());
        PendingIntent contentIntent = PendingIntent.getActivity(context, requestCode++, newIntent,
                PendingIntent.FLAG_CANCEL_CURRENT);
        try {
            int iconId = 0;
            try {
                iconId = Integer.parseInt(action.getIcon());
            } catch (Exception ex) {
            }
            //android.app.Notification.Action.Builder actionBuilder = new android.app.Notification.Action.Builder(iconId, action.getTitle(), contentIntent);

            System.out
                    .println("Adding action " + action.getId() + ", " + action.getTitle() + ", icon=" + iconId);
            if (ActionWrapper.BuilderWrapper.isSupported()) {
                // We need to take this abstracted "wrapper" approach because the Action.Builder class, and RemoteInput class
                // aren't available until API 22.
                // These classes use reflection to provide support for these classes safely.
                ActionWrapper.BuilderWrapper actionBuilder = new ActionWrapper.BuilderWrapper(iconId,
                        action.getTitle(), contentIntent);
                if (action.getTextInputPlaceholder() != null && RemoteInputWrapper.isSupported()) {
                    RemoteInputWrapper.BuilderWrapper remoteInputBuilder = new RemoteInputWrapper.BuilderWrapper(
                            action.getId() + "$Result");
                    remoteInputBuilder.setLabel(action.getTextInputPlaceholder());

                    RemoteInputWrapper remoteInput = remoteInputBuilder.build();
                    actionBuilder.addRemoteInput(remoteInput);
                }
                ActionWrapper actionWrapper = actionBuilder.build();
                new NotificationCompatWrapper.BuilderWrapper(builder).addAction(actionWrapper);
            } else {
                builder.addAction(iconId, action.getTitle(), contentIntent);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}