Java Code Examples for android.content.Intent#clone()

The following examples show how to use android.content.Intent#clone() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TimedNotificationPublisher.java    From OsmGo with MIT License 5 votes vote down vote up
private void rescheduleNotificationIfNeeded(Context context, Intent intent, int id) {
  String dateString = intent.getStringExtra(CRON_KEY);
  if (dateString != null) {
    DateMatch date = DateMatch.fromMatchString(dateString);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    long trigger = date.nextTrigger(new Date());
    Intent clone = (Intent) intent.clone();
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, clone, PendingIntent.FLAG_CANCEL_CURRENT);
    alarmManager.set(AlarmManager.RTC, trigger, pendingIntent);
  }
}
 
Example 2
Source File: InAppBrowser.java    From cordova-plugin-inappbrowser with Apache License 2.0 5 votes vote down vote up
/**
 * Opens the intent, providing a chooser that excludes the current app to avoid
 * circular loops.
 */
private void openExternalExcludeCurrentApp(Intent intent) {
    String currentPackage = cordova.getActivity().getPackageName();
    boolean hasCurrentPackage = false;

    PackageManager pm = cordova.getActivity().getPackageManager();
    List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
    ArrayList<Intent> targetIntents = new ArrayList<Intent>();

    for (ResolveInfo ri : activities) {
        if (!currentPackage.equals(ri.activityInfo.packageName)) {
            Intent targetIntent = (Intent)intent.clone();
            targetIntent.setPackage(ri.activityInfo.packageName);
            targetIntents.add(targetIntent);
        }
        else {
            hasCurrentPackage = true;
        }
    }

    // If the current app package isn't a target for this URL, then use
    // the normal launch behavior
    if (hasCurrentPackage == false || targetIntents.size() == 0) {
        this.cordova.getActivity().startActivity(intent);
    }
    // If there's only one possible intent, launch it directly
    else if (targetIntents.size() == 1) {
        this.cordova.getActivity().startActivity(targetIntents.get(0));
    }
    // Otherwise, show a custom chooser without the current app listed
    else if (targetIntents.size() > 0) {
        Intent chooser = Intent.createChooser(targetIntents.remove(targetIntents.size()-1), null);
        chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[] {}));
        this.cordova.getActivity().startActivity(chooser);
    }
}
 
Example 3
Source File: ShareIntent.java    From react-native-share with MIT License 4 votes vote down vote up
public Intent excludeChooserIntent(Intent prototype, ReadableMap options) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    List<HashMap<String, String>> intentMetaInfo = new ArrayList<HashMap<String, String>>();
    Intent chooserIntent;

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

    if (!resInfo.isEmpty()) {
        for (ResolveInfo resolveInfo : resInfo) {
            if (resolveInfo.activityInfo == null || options.getArray("excludedActivityTypes").toString().contains(resolveInfo.activityInfo.packageName))
                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(this.reactContext.getPackageManager())));
            intentMetaInfo.add(info);
        }

        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), "share");
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
            return chooserIntent;
        }
    }

    return Intent.createChooser(prototype, "Share");
}
 
Example 4
Source File: FileViewer.java    From secrecy with Apache License 2.0 4 votes vote down vote up
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;

    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.
}
 
Example 5
Source File: BrowserIntentBuilder.java    From wear-notify-for-reddit with Apache License 2.0 4 votes vote down vote up
@Nullable public Intent build(String title, Intent target) {
    // find all the browsers installed - don't use the reddit url because that will include 3rd party reddit clients
    final Intent dummy = new Intent(Intent.ACTION_VIEW);
    dummy.setData(Uri.parse(DEFAULT_URL));

    final List<ResolveInfo> resInfo = mPackageManager.queryIntentActivities(dummy, 0);

    final List<HashMap<String, String>> metaInfo = new ArrayList<>();
    for (ResolveInfo ri : resInfo) {
        if (ri.activityInfo == null) {
            continue;
        }

        final HashMap<String, String> info = new HashMap<>();
        info.put(PACKAGE_NAME, ri.activityInfo.packageName);
        info.put(CLASS_NAME, ri.activityInfo.name);
        info.put(SIMPLE_NAME, String.valueOf(ri.activityInfo.loadLabel(mPackageManager)));
        metaInfo.add(info);
    }

    if (metaInfo.isEmpty()) {
        return null;
    }

    // sort items by display name
    Collections.sort(metaInfo, new Comparator<HashMap<String, String>>() {
        @Override
        public int compare(HashMap<String, String> map, HashMap<String, String> map2) {
            return map.get(SIMPLE_NAME).compareTo(map2.get(SIMPLE_NAME));
        }
    });

    // create the custom intent list
    final List<Intent> targetedIntents = new ArrayList<>();
    for (HashMap<String, String> mi : metaInfo) {
        Intent targetedShareIntent = (Intent) target.clone();
        targetedShareIntent.setPackage(mi.get(PACKAGE_NAME));
        targetedShareIntent.setClassName(mi.get(PACKAGE_NAME), mi.get(CLASS_NAME));
        targetedIntents.add(targetedShareIntent);
    }

    final Intent chooserIntent = Intent.createChooser(targetedIntents.get(0), title);
    targetedIntents.remove(0);

    final Parcelable[] targetedIntentsParcelable = targetedIntents.toArray(new Parcelable[targetedIntents
            .size()]);
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedIntentsParcelable);

    return chooserIntent;
}