Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ShareUtil.java « util « shared « notes « owncloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 115d18dd51aa20845cd48cd84fa0d3f74c60c494 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package it.niedermann.owncloud.notes.shared.util;

import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.net.MalformedURLException;
import java.net.URL;

import it.niedermann.android.markdown.MarkdownUtil;

import static android.content.ClipDescription.MIMETYPE_TEXT_PLAIN;

public class ShareUtil {
    public static void openShareDialog(@NonNull Context context, @Nullable String subject, @Nullable String text) {
        context.startActivity(Intent.createChooser(new Intent()
                .setAction(Intent.ACTION_SEND)
                .setType(MIMETYPE_TEXT_PLAIN)
                .putExtra(Intent.EXTRA_SUBJECT, subject)
                .putExtra(Intent.EXTRA_TITLE, subject)
                .putExtra(Intent.EXTRA_TEXT, text), subject));
    }

    public static String extractSharedText(@NonNull Intent intent) {
        final String text = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (intent.hasExtra(Intent.EXTRA_SUBJECT)) {
            final String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
            try {
                new URL(text);
                if (text != null && subject != null && !subject.trim().isEmpty()) {
                    return MarkdownUtil.getMarkdownLink(subject, text);
                } else {
                    return text;
                }
            } catch (MalformedURLException e) {
                if (subject != null && !subject.trim().isEmpty()) {
                    return subject + ": " + text;
                } else {
                    return text;
                }
            }
        } else {
            return text;
        }
    }
}