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

AttachmentUtil.java « util « deck « nextcloud « niedermann « it « java « main « src « app - github.com/stefan-niedermann/nextcloud-deck.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7b1fab982c52aa5abb07d3a5f4df59f6f98eb4cf (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package it.niedermann.nextcloud.deck.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.Toast;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.Px;

import com.nextcloud.android.sso.helper.VersionCheckHelper;
import com.nextcloud.android.sso.model.FilesAppType;

import java.util.Optional;

import it.niedermann.nextcloud.deck.DeckLog;
import it.niedermann.nextcloud.deck.R;
import it.niedermann.nextcloud.deck.model.Account;
import it.niedermann.nextcloud.deck.model.Attachment;
import it.niedermann.nextcloud.deck.model.enums.EAttachmentType;
import it.niedermann.nextcloud.deck.model.ocs.Version;

/**
 * Created by stefan on 07.03.20.
 */

public class AttachmentUtil {

    private AttachmentUtil() {
        throw new UnsupportedOperationException("This class must not get instantiated");
    }

    /**
     * @return a link to the thumbnail of the given {@link Attachment}.
     * If a thumbnail is not available (see {@link Version#supportsFileAttachments()}), a link to
     * the {@link Attachment} itself will be returned instead.
     */
    public static String getThumbnailUrl(@NonNull Account account, @NonNull Long cardRemoteId, @NonNull Attachment attachment, @Px int previewSize) {
        return getThumbnailUrl(account, cardRemoteId, attachment, previewSize, previewSize);
    }

    public static String getThumbnailUrl(@NonNull Account account, @NonNull Long cardRemoteId, @NonNull Attachment attachment, @Px int previewWidth, @Px int previewHeight) {
        return account.getServerDeckVersionAsObject().supportsFileAttachments() &&
                EAttachmentType.FILE.equals(attachment.getType()) &&
                attachment.getFileId() != null
                ? account.getUrl() + "/index.php/core/preview?fileId=" + attachment.getFileId() + "&x=" + previewWidth + "&y=" + previewHeight + "&a=true"
                : getRemoteOrLocalUrl(account.getUrl(), cardRemoteId, attachment);
    }

    /**
     * @return {@link AttachmentUtil#getDeck_1_0_RemoteUrl} or {@link Attachment#getLocalPath()} as fallback
     * in case this {@param attachment} has not yet been synced.
     */
    @Nullable
    private static String getRemoteOrLocalUrl(@NonNull String accountUrl, @Nullable Long cardRemoteId, @NonNull Attachment attachment) {
        return (attachment.getId() == null || cardRemoteId == null)
                ? attachment.getLocalPath()
                : getDeck_1_0_RemoteUrl(accountUrl, cardRemoteId, attachment.getId());
    }

    /**
     * Tries to open the given {@link Attachment} in the Nextcloud Android app with a fallback to the web browser.
     * Displays a toast on failure.
     */
    public static void openAttachment(@NonNull Account account, @NonNull Context context, @Nullable Long cardRemoteId, Attachment attachment) {
        if (cardRemoteId == null) {
            Toast.makeText(context, R.string.card_does_not_yet_exist, Toast.LENGTH_LONG).show();
            DeckLog.logError(new IllegalArgumentException("cardRemoteId must not be null."));
            return;
        }

        final var intent = generateNextcloudFilesIntent(context, account, attachment)
                .orElse(generateBrowserIntent(account, cardRemoteId, attachment));

        try {
            context.startActivity(intent);
        } catch (IllegalArgumentException e) {
            Toast.makeText(context, R.string.attachment_does_not_yet_exist, Toast.LENGTH_LONG).show();
            DeckLog.logError(new IllegalArgumentException("attachmentRemoteId must not be null."));
        }
    }

    private static Optional<Intent> generateNextcloudFilesIntent(@NonNull Context context, @NonNull Account account, Attachment attachment) {
        final var packageManager = context.getPackageManager();

        for (final var type : FilesAppType.values()) {
            try {
                if (VersionCheckHelper.getNextcloudFilesVersionCode(context, type) > 30110000) {
                    final var intent = new Intent(Intent.ACTION_VIEW)
                            .setClassName(type.packageId, "com.owncloud.android.ui.activity.FileDisplayActivity")
                            .putExtra("KEY_FILE_ID", String.valueOf(attachment.getFileId()))
                            .putExtra("KEY_ACCOUNT", account.getName());

                    if (packageManager.resolveActivity(intent, 0) != null) {
                        return Optional.of(intent);
                    }
                }
            } catch (PackageManager.NameNotFoundException ignored) {
            }
        }

        return Optional.empty();
    }

    private static Intent generateBrowserIntent(@NonNull Account account, long cardRemoteId, @NonNull Attachment attachment) {
        return new Intent(Intent.ACTION_VIEW)
                .setData(Uri.parse(getCopyDownloadUrl(account, cardRemoteId, attachment)));
    }

    public static String getCopyDownloadUrl(@NonNull Account account, @NonNull Long cardRemoteId, @NonNull Attachment attachment) {
        if (attachment.getId() == null) {
            throw new IllegalArgumentException("attachment id must not be null");
        }

        return (attachment.getFileId() != null)
                ? account.getUrl() + "/f/" + attachment.getFileId()
                : getDeck_1_0_RemoteUrl(account.getUrl(), cardRemoteId, attachment.getId());
    }

    /**
     * Attention! This does only work for attachments of type {@link EAttachmentType#DECK_FILE} which are a legacy of Deck API 1.0
     */
    @Deprecated
    private static String getDeck_1_0_RemoteUrl(@NonNull String accountUrl, @NonNull Long cardRemoteId, @NonNull Long attachmentRemoteId) {
        return accountUrl + "/index.php/apps/deck/cards/" + cardRemoteId + "/attachment/" + attachmentRemoteId;
    }

    @DrawableRes
    public static int getIconForMimeType(@NonNull String mimeType) {
        if (TextUtils.isEmpty(mimeType)) {
            return R.drawable.ic_attach_file_24dp;
        } else if (MimeTypeUtil.isAudio(mimeType)) {
            return R.drawable.ic_music_note_grey600_24dp;
        } else if (MimeTypeUtil.isVideo(mimeType)) {
            return R.drawable.ic_local_movies_grey600_24dp;
        } else if (MimeTypeUtil.isPdf(mimeType)) {
            return R.drawable.ic_baseline_picture_as_pdf_24;
        } else if (MimeTypeUtil.isContact(mimeType)) {
            return R.drawable.ic_baseline_contact_mail_24;
        } else {
            return R.drawable.ic_attach_file_24dp;
        }
    }

}