/** * Share with other applications (but tries to put applications with "Translate" in the package * name in the first place(s). */ public static void shareWithTranslate(Activity activity, String text) { Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, text); PackageManager pm = activity.getPackageManager(); List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0); List<LabeledIntent> intentList = new ArrayList<>(); for (ResolveInfo ri : resInfo) { if(ri.activityInfo.packageName.contains("translate")) { Intent intent = new Intent(); intent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name)); intent.putExtra(android.content.Intent.EXTRA_TEXT, text); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); intentList.add(new LabeledIntent(intent, ri.activityInfo.packageName, ri.loadLabel(pm), ri.icon)); } } LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]); Intent openInChooser = Intent.createChooser(sendIntent, "Translate"); openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); activity.startActivity(openInChooser); }
private static LabeledIntent[] getLabeledIntents(Context context) { Intent sendIntent = getShareIntent(context); PackageManager pm = context.getPackageManager(); List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0); List<LabeledIntent> intents = new ArrayList<>(); for (ResolveInfo ri : resInfo) { String packageName = ri.activityInfo.packageName; if (packageName.contains("twitter") || packageName.contains("facebook") || packageName.contains("whatsapp") || packageName.contains("plus") || packageName.contains("talk") || packageName.contains("viber")) { Intent intent = getShareIntent(context); intent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); intents.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon)); } } return intents.toArray(new LabeledIntent[intents.size()]); }
private static LabeledIntent buildLabeledIntent(Intent intent, ResolveInfo intentActivity) { LabeledIntent labeledIntent = new LabeledIntent(intent, intentActivity.resolvePackageName, intentActivity.labelRes, intentActivity.getIconResource()); labeledIntent.setPackage(getPackageName(intentActivity)); labeledIntent.setClassName(getPackageName(intentActivity), intentActivity.activityInfo.name); return labeledIntent; }
private void showFileAttach() { final List<Intent> allIntents = new ArrayList<Intent>(); // all openable... Intent contentIntent = new Intent(Intent.ACTION_GET_CONTENT); contentIntent.setType(SafeSlingerConfig.MIMETYPE_ADD_ATTACH); contentIntent.addCategory(Intent.CATEGORY_OPENABLE); // camera Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); SafeSlinger.setTempCameraFileUri(SSUtil.makeCameraOutputUri()); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, SafeSlinger.getTempCameraFileUri()); allIntents.add(cameraIntent); // audio recorder Intent recorderIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); recorderIntent.putExtra(MediaStore.EXTRA_OUTPUT, SSUtil.makeRecorderOutputUri()); allIntents.add(recorderIntent); // our custom browser if (SSUtil.isExternalStorageReadable()) { Intent filePickIntent = new Intent(HomeActivity.this, FilePickerActivity.class); LabeledIntent fileIntent = new LabeledIntent(filePickIntent, filePickIntent.getPackage(), R.string.menu_FileManager, R.drawable.ic_menu_directory); allIntents.add(fileIntent); } // Chooser of file system options. Intent chooserIntent = Intent.createChooser(contentIntent, getString(R.string.title_ChooseFileLoad)); chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[] {})); startActivityForResult(chooserIntent, VIEW_FILEATTACH_ID); }
@Override public Intent getShareIntent(Context context) { StorageManager storage = StorageManager.getInstance(context); File pdfFile = new File(storage.getPaperDirectory(getPaper()), getKey()); File papersDir = storage.get(StorageManager.PAPER); try { String pagePath = pdfFile.getCanonicalPath() .replace(papersDir.getCanonicalPath(), "papers"); Uri contentUri = Uri.parse("content://" + BuildConfig.APPLICATION_ID + ".streamprovider") .buildUpon() .appendEncodedPath(pagePath) .build(); //share Intent Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("application/pdf"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.putExtra(Intent.EXTRA_STREAM, contentUri); intent.putExtra(Intent.EXTRA_SUBJECT, getPaper().getTitelWithDate(context) + ": " + getTitle()); intent.putExtra(Intent.EXTRA_TEXT, getPaper().getTitelWithDate(context) + "\n" + getTitle()); //get extra intents to view pdf Intent viewIntent = new Intent(Intent.ACTION_VIEW); viewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); viewIntent.setData(contentUri); PackageManager packageManager = context.getPackageManager(); List<ResolveInfo> resInfo = packageManager.queryIntentActivities(viewIntent, 0); Intent[] extraIntents = new Intent[resInfo.size()]; for (int i = 0; i < resInfo.size(); i++) { // Extract the label, append it, and repackage it in a LabeledIntent ResolveInfo ri = resInfo.get(i); String packageName = ri.activityInfo.packageName; Intent extraViewIntent = new Intent(); extraViewIntent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); extraViewIntent.setAction(Intent.ACTION_VIEW); extraViewIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); extraViewIntent.setData(contentUri); CharSequence label = ri.loadLabel(packageManager); extraIntents[i] = new LabeledIntent(extraViewIntent, packageName, label, ri.icon); } Intent chooserIntent = Intent.createChooser(intent, context.getString(R.string.reader_action_share_open)); if (extraIntents.length > 0) chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); return chooserIntent; } catch (IOException e) { e.printStackTrace(); } return null; }
public Intent build(String chooserTitle, final Context context) throws NoShareIntentsException { if (_intents.isEmpty()) { throw new IllegalStateException("Please add shares!"); } final PackageManager packageManager = context.getPackageManager(); ArrayList<LabeledIntent> extraIntents = new ArrayList<>(_intents.size() * 2); Intent mainIntent = null; for (int i = 0; i < _intents.size(); ++i) { Intent intent = _intents.get(i); List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0); if (resolveInfoList == null) {//Some devices return null... continue; } if (mainIntent == null && resolveInfoList.isEmpty() == false) { mainIntent = intent; //This will be main chooser so we don't want duplicates continue; } for (ResolveInfo info : resolveInfoList) { // Extract the label, append it, and repackage it in a LabeledIntent String packageName = info.activityInfo.packageName; intent.setComponent(new ComponentName(packageName, info.activityInfo.name)); final LabeledIntent labeledIntent = new LabeledIntent(intent, packageName, info.loadLabel(packageManager), info.icon); extraIntents.add(labeledIntent); } } if (mainIntent == null) { Logger.e("No app can't handle such share request"); throw new NoShareIntentsException("No app can't handle such share request"); } final Intent chooser = Intent.createChooser(mainIntent, chooserTitle); chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents.toArray(new LabeledIntent[extraIntents.size()])); return chooser; }
@Override public void itemViewOnShare(MobiStegoItem mobiStegoItem) { PackageManager pm = getPackageManager(); Intent emailIntent = new Intent(Intent.ACTION_SEND); //emailIntent.setType("vnd.android.cursor.item/*"); emailIntent.setType("vnd.android.cursor.item/*"); Intent openInChooser = Intent.createChooser(new Intent(), getResources().getString(R.string.send)); File f = mobiStegoItem.getBitmap(); if (f != null) { List<ResolveInfo> resInfo = pm.queryIntentActivities(emailIntent, 0); List<LabeledIntent> intentList = new ArrayList<>(); for (int i = 0; i < resInfo.size(); i++) { // Extract the label, append it, and repackage it in a LabeledIntent ResolveInfo ri = resInfo.get(i); String packageName = ri.activityInfo.packageName; if(packageName.contains("whatsapp")||packageName.contains("telegram")) continue; Intent intent = new Intent(); intent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); intent.setAction(Intent.ACTION_SEND); intent.setType("vnd.android.cursor.item/*"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon)); } // convert intentList to array LabeledIntent[] extraIntents = intentList.toArray( new LabeledIntent[ intentList.size() ]); openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); startActivity(openInChooser); //startActivity(Intent.createChooser(emailIntent, getResources().getString(R.string.send))); } }
public static void openMailChooser(Context context, String text, String[] mails, String subject) { Intent mailIntent = new Intent(); mailIntent.setAction(Intent.ACTION_SEND); mailIntent.putExtra(Intent.EXTRA_TEXT, text); mailIntent.putExtra(Intent.EXTRA_EMAIL, mails); mailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); mailIntent.setType(INTENT_TYPE_MSG); PackageManager pm = context.getPackageManager(); Intent sendIntent = new Intent(Intent.ACTION_SEND); sendIntent.setType(INTENT_TYPE_TEXT); Intent openInChooser = Intent.createChooser(mailIntent, ""); List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0); List<LabeledIntent> intentList = new ArrayList<LabeledIntent>(); for (ResolveInfo ri : resInfo) { String packageName = ri.activityInfo.packageName; if (packageName.contains(PACKAGE_EMAIL)) { mailIntent.setPackage(packageName); } else if (packageName.contains(PACKAGE_MMS) || packageName.contains(PACKAGE_GMAIL)) { Intent intent = new Intent(); intent.setComponent(new ComponentName(packageName, ri.activityInfo.name)); intent.setAction(Intent.ACTION_SEND); intent.setType(INTENT_TYPE_TEXT); if (packageName.contains(PACKAGE_MMS)) { intent.putExtra("subject", subject); intent.putExtra("sms_body", text); intent.putExtra("address", mails[0]); intent.setType(INTENT_TYPE_MSG); } else if (packageName.contains(PACKAGE_GMAIL)) { intent.putExtra(Intent.EXTRA_TEXT, text); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_EMAIL, mails); intent.setType(INTENT_TYPE_MSG); } intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm), ri.icon)); } } LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]); openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents); context.startActivity(openInChooser); }