androidx.core.app.ShareCompat Java Examples

The following examples show how to use androidx.core.app.ShareCompat. 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: DetailActivity.java    From android-popular-movies-app with Apache License 2.0 6 votes vote down vote up
/**
 * Uses the ShareCompat Intent builder to create our share intent for sharing.
 * Return the newly created intent.
 *
 * @return The Intent to use to start our share.
 */
private Intent createShareIntent() {
    // Text message to share
    String shareText = getString(R.string.check_out) + mMovie.getTitle()
            + getString(R.string.new_line) + SHARE_URL + mMovie.getId();
    // If there is the first trailer, add the first trailer's YouTube URL to the text message
    if (mFirstVideoUrl != null) {
        shareText += getString(R.string.new_line) + getString(R.string.youtube_trailer)
                + mFirstVideoUrl;
    }

    // Create share intent
    Intent shareIntent = ShareCompat.IntentBuilder.from(this)
            .setType(SHARE_INTENT_TYPE_TEXT)
            .setText(shareText)
            .setChooserTitle(getString(R.string.chooser_title))
            .createChooserIntent();
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
    return shareIntent;
}
 
Example #2
Source File: DynamicFileUtils.java    From dynamic-utils with Apache License 2.0 6 votes vote down vote up
/**
 * Share multiple files.
 *
 * @param activity The activity to create the intent chooser.
 * @param title The title for the intent chooser.
 * @param subject The subject for the intent chooser.
 * @param uris The content uris to be shared.
 * @param mimeType The mime type of the file.
 */
public static void shareFiles(@NonNull Activity activity, @Nullable String title,
        @Nullable String subject, @NonNull Uri[] uris, @Nullable String mimeType) {
    ShareCompat.IntentBuilder intentBuilder =
            ShareCompat.IntentBuilder
                    .from(activity)
                    .setSubject(subject != null ? subject : title)
                    .setType(mimeType != null ? mimeType : "*/*")
                    .setChooserTitle(title);

    for (Uri uri : uris) {
        intentBuilder.addStream(uri);
    }

    Intent shareBackup = intentBuilder.createChooserIntent()
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    activity.startActivity(shareBackup);
}
 
Example #3
Source File: WallabagFileProvider.java    From android-app with GNU General Public License v3.0 6 votes vote down vote up
public static boolean shareFile(@NonNull Activity activity, @NonNull File file) {
    try {
        Uri uri = getUriForFile(activity, file);

        ShareCompat.IntentBuilder shareBuilder = ShareCompat.IntentBuilder.from(activity)
                .setStream(uri)
                .setType(activity.getContentResolver().getType(uri));

        shareBuilder.getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        shareBuilder.startChooser();

        return true;
    } catch (Exception e) {
        Log.w(TAG, "Error sharing file", e);
    }

    return false;
}
 
Example #4
Source File: ShareHelper.java    From simple_share with MIT License 5 votes vote down vote up
public void share(Map params) {
        this.intentBuilder = ShareCompat.IntentBuilder.from(this.registrar.activity());
        this.params = params;
        if (checkKey("title")) {
            String title = (String) params.get("title");
            intentBuilder.setChooserTitle(title);
        }
        if (checkKey("msg")) {
            intentBuilder.setText((String) params.get("msg"));
            intentBuilder.setType("text/plain");
        }
        if (checkKey("subject")) {
            intentBuilder.setSubject((String) params.get("subject"));
        }
        if (checkKey("uri")) {
            FileHelper fileHelper = getFileHelper(params);
            if (fileHelper.isFile()) {
//                final String SHARED_PROVIDER_AUTHORITY = registrar.context().getPackageName() + ".adv_provider";
                final Uri uri = (Uri) fileHelper.getUri();
                intentBuilder.setType(fileHelper.getType());
                intentBuilder.setStream(uri);

                List<ResolveInfo> resInfoList = this.registrar.context().getPackageManager().queryIntentActivities(intentBuilder.getIntent(), PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    this.registrar.context().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
        }

        openChooser();
    }
 
Example #5
Source File: SubmitDebugLogActivity.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
private void presentResultDialog(@NonNull String url) {
  AlertDialog.Builder builder = new AlertDialog.Builder(this)
                                               .setTitle(R.string.SubmitDebugLogActivity_success)
                                               .setCancelable(false)
                                               .setNeutralButton(R.string.SubmitDebugLogActivity_ok, (d, w) -> finish())
                                               .setPositiveButton(R.string.SubmitDebugLogActivity_share, (d, w) -> {
                                                 ShareCompat.IntentBuilder.from(this)
                                                                          .setText(url)
                                                                          .setType("text/plain")
                                                                          .setEmailTo(new String[] { "[email protected]" })
                                                                          .startChooser();
                                               });

  TextView textView = new TextView(builder.getContext());
  textView.setText(getResources().getString(R.string.SubmitDebugLogActivity_copy_this_url_and_add_it_to_your_issue, url));
  textView.setMovementMethod(LinkMovementMethod.getInstance());
  textView.setOnLongClickListener(v -> {
    Util.copyToClipboard(this, url);
    Toast.makeText(this, R.string.SubmitDebugLogActivity_copied_to_clipboard, Toast.LENGTH_SHORT).show();
    return true;
  });

  LinkifyCompat.addLinks(textView, Linkify.WEB_URLS);
  ViewUtil.setPadding(textView, (int) ThemeUtil.getThemedDimen(this, R.attr.dialogPreferredPadding));

  builder.setView(textView);
  builder.show();
}
 
Example #6
Source File: ShareHelper.java    From simple_share with MIT License 5 votes vote down vote up
public void share(Map params) {
        this.intentBuilder = ShareCompat.IntentBuilder.from(this.registrar.activity());
        this.params = params;
        if (checkKey("title")) {
            String title = (String) params.get("title");
            intentBuilder.setChooserTitle(title);
        }
        if (checkKey("msg")) {
            intentBuilder.setText((String) params.get("msg"));
            intentBuilder.setType("text/plain");
        }
        if (checkKey("subject")) {
            intentBuilder.setSubject((String) params.get("subject"));
        }
        if (checkKey("uri")) {
            FileHelper fileHelper = getFileHelper(params);
            if (fileHelper.isFile()) {
//                final String SHARED_PROVIDER_AUTHORITY = registrar.context().getPackageName() + ".adv_provider";
                final Uri uri = (Uri) fileHelper.getUri();
                intentBuilder.setType(fileHelper.getType());
                intentBuilder.setStream(uri);

                List<ResolveInfo> resInfoList = this.registrar.context().getPackageManager().queryIntentActivities(intentBuilder.getIntent(), PackageManager.MATCH_DEFAULT_ONLY);
                for (ResolveInfo resolveInfo : resInfoList) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    this.registrar.context().grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                }
            }
        }

        openChooser();
    }
 
Example #7
Source File: DetailsActivity.java    From PopularMovies with MIT License 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_share: {
            MovieDetails movieDetails = mViewModel.getResult().getValue().data;
            Intent shareIntent = ShareCompat.IntentBuilder.from(this)
                    .setType("text/plain")
                    .setSubject(movieDetails.getMovie().getTitle() + " movie trailer")
                    .setText("Check out " + movieDetails.getMovie().getTitle() + " movie trailer at " +
                            Uri.parse(Constants.YOUTUBE_WEB_URL +
                                    movieDetails.getTrailers().get(0).getKey())
                    )
                    .createChooserIntent();

            int flags = Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_MULTIPLE_TASK;
            if (Build.VERSION.SDK_INT >= 21)
                flags |= Intent.FLAG_ACTIVITY_NEW_DOCUMENT;

            shareIntent.addFlags(flags);
            if (shareIntent.resolveActivity(getPackageManager()) != null) {
                startActivity(shareIntent);
            }
            return true;
        }
        case R.id.action_favorite: {
            mViewModel.onFavoriteClicked();
            invalidateOptionsMenu();
            return true;
        }
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example #8
Source File: DynamicFileUtils.java    From dynamic-utils with Apache License 2.0 5 votes vote down vote up
/**
 * Share file according to the mime type.
 *
 * @param activity The activity to create the intent chooser.
 * @param title The title for the intent chooser.
 * @param subject The subject for the intent chooser.
 * @param file The file to be shared.
 * @param mimeType The mime type of the file.
 */
public static void shareFile(@NonNull Activity activity, @Nullable String title,
        @Nullable String subject, @NonNull File file, @NonNull String mimeType) {
    Intent shareBackup = ShareCompat.IntentBuilder
            .from(activity)
            .setType(mimeType)
            .setSubject(subject != null ? subject : title)
            .setStream(getUriFromFile(activity, file))
            .setChooserTitle(title)
            .createChooserIntent()
            .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
                    | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

    activity.startActivity(shareBackup);
}
 
Example #9
Source File: Share.java    From intra42 with Apache License 2.0 5 votes vote down vote up
static public void shareString(Activity activity, String textToShare) {

        if (activity == null || textToShare == null)
            return;

        ShareCompat.IntentBuilder
                .from(activity)
                .setText(textToShare)
                .setType("text/plain") // most general text sharing MIME type
                .setChooserTitle(R.string.share_using)
                .startChooser();
    }
 
Example #10
Source File: FeedbackFragment.java    From hash-checker with Apache License 2.0 4 votes vote down vote up
private void sendEmail(
        @NonNull String text,
        @NonNull String email
) {
    Intent emailIntent = new Intent(
            Intent.ACTION_SEND
    );
    emailIntent.putExtra(
            Intent.EXTRA_EMAIL,
            new String[] { email }
    );

    String subject = getString(R.string.common_app_name);
    emailIntent.putExtra(
            Intent.EXTRA_SUBJECT,
            subject
    );
    emailIntent.putExtra(
            Intent.EXTRA_TEXT,
            text
    );

    Intent selectorIntent = new Intent(
            Intent.ACTION_SENDTO
    );
    selectorIntent.setData(
            Uri.parse(
                    "mailto:"
            )
    );
    emailIntent.setSelector(
        selectorIntent
    );

    String chooseMessage = String.format(
            "%s:",
            getString(R.string.message_email_app_chooser)
    );

    try {
        startActivity(
                Intent.createChooser(
                        emailIntent,
                        chooseMessage
                )
        );
    } catch (ActivityNotFoundException e) {
        L.e(e);
        ShareCompat.IntentBuilder
                .from(getActivity())
                .setText("message/rfc822")
                .addEmailTo(email)
                .setSubject(subject)
                .setText(text)
                .setChooserTitle(chooseMessage)
                .startChooser();
    }
}
 
Example #11
Source File: AboutActivity.java    From zephyr with MIT License 4 votes vote down vote up
@OnClick(R.id.about_export_logs)
public void onClickExportLogsBtn(View view) {
    mLogger.log(LogLevel.INFO, LOG_TAG, "Exporting logs...");
    ZephyrExecutors.getDiskExecutor().execute(() -> {
        File file = new File(getCacheDir(), "logs/zephyr-logs.txt");
        if (!file.exists()) {
            file.getParentFile().mkdirs();
        }

        FileWriter writer;
        try {
            writer = new FileWriter(file);

            for (LogEntry logEntry : mLogger.getLogs()) {
                writer.write(logEntry.toString() + "\n");
            }

            writer.close();
        } catch (IOException e) {
            mLogger.log(LogLevel.ERROR, LOG_TAG, "Error while exporting logs!", e);
            return;
        }

        Uri contentUri = FileProvider.getUriForFile(AboutActivity.this, BuildConfig.APPLICATION_ID + ".provider", file);

        Intent intent = ShareCompat.IntentBuilder.from(this)
                .setType("text/plain")
                .setStream(contentUri)
                .setChooserTitle(R.string.about_export_logs)
                .createChooserIntent()
                .addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        List<ResolveInfo> resolvedIntentActivities = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
            String packageName = resolvedIntentInfo.activityInfo.packageName;
            grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }

        startActivity(intent);
    });
}
 
Example #12
Source File: IntentUtils.java    From android-utils with Apache License 2.0 2 votes vote down vote up
/**
 * Share text.
 *
 * @param activity the activity
 * @param title    the title
 * @param textData the text data
 */
public static void shareText(Activity activity, String title, String textData) {
    ShareCompat.IntentBuilder.from(activity).setType("text/plain").setChooserTitle(title)
            .setText(textData).startChooser();
}