Attachment.java

  1/*
  2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
  3 *
  4 * Redistribution and use in source and binary forms, with or without modification,
  5 * are permitted provided that the following conditions are met:
  6 *
  7 * 1. Redistributions of source code must retain the above copyright notice, this
  8 * list of conditions and the following disclaimer.
  9 *
 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
 11 * this list of conditions and the following disclaimer in the documentation and/or
 12 * other materials provided with the distribution.
 13 *
 14 * 3. Neither the name of the copyright holder nor the names of its contributors
 15 * may be used to endorse or promote products derived from this software without
 16 * specific prior written permission.
 17 *
 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28 */
 29
 30package eu.siacs.conversations.ui.util;
 31
 32import android.content.ClipData;
 33import android.content.Context;
 34import android.content.Intent;
 35import android.net.Uri;
 36import android.os.Parcel;
 37import android.os.Parcelable;
 38
 39import com.google.common.base.MoreObjects;
 40
 41import org.jetbrains.annotations.NotNull;
 42
 43import java.io.File;
 44import java.util.ArrayList;
 45import java.util.Collections;
 46import java.util.List;
 47import java.util.UUID;
 48
 49import eu.siacs.conversations.utils.Compatibility;
 50import eu.siacs.conversations.utils.MimeUtils;
 51
 52public class Attachment implements Parcelable {
 53
 54    Attachment(Parcel in) {
 55        uri = in.readParcelable(Uri.class.getClassLoader());
 56        mime = in.readString();
 57        uuid = UUID.fromString(in.readString());
 58        type = Type.valueOf(in.readString());
 59    }
 60
 61    @Override
 62    public void writeToParcel(Parcel dest, int flags) {
 63        dest.writeParcelable(uri, flags);
 64        dest.writeString(mime);
 65        dest.writeString(uuid.toString());
 66        dest.writeString(type.toString());
 67    }
 68
 69    @Override
 70    public int describeContents() {
 71        return 0;
 72    }
 73
 74    public static final Creator<Attachment> CREATOR = new Creator<Attachment>() {
 75        @Override
 76        public Attachment createFromParcel(Parcel in) {
 77            return new Attachment(in);
 78        }
 79
 80        @Override
 81        public Attachment[] newArray(int size) {
 82            return new Attachment[size];
 83        }
 84    };
 85
 86    public String getMime() {
 87        return mime;
 88    }
 89
 90    public Type getType() {
 91        return type;
 92    }
 93
 94    @NotNull
 95    @Override
 96    public String toString() {
 97        return MoreObjects.toStringHelper(this)
 98                .add("uri", uri)
 99                .add("type", type)
100                .add("uuid", uuid)
101                .add("mime", mime)
102                .toString();
103    }
104
105    public enum Type {
106        FILE, IMAGE, LOCATION, RECORDING
107    }
108
109    private final Uri uri;
110    private final Type type;
111    private final UUID uuid;
112    private final String mime;
113
114    private Attachment(UUID uuid, Uri uri, Type type, String mime) {
115        this.uri = uri;
116        this.type = type;
117        this.mime = mime;
118        this.uuid = uuid;
119    }
120
121    private Attachment(Uri uri, Type type, String mime) {
122        this.uri = uri;
123        this.type = type;
124        this.mime = mime;
125        this.uuid = UUID.randomUUID();
126    }
127
128    public static boolean canBeSendInband(final List<Attachment> attachments) {
129        for (Attachment attachment : attachments) {
130            if (attachment.type != Type.LOCATION) {
131                return false;
132            }
133        }
134        return true;
135    }
136
137    public static List<Attachment> of(final Context context, Uri uri, Type type) {
138        final String mime = type == Type.LOCATION ? null : MimeUtils.guessMimeTypeFromUri(context, uri);
139        return Collections.singletonList(new Attachment(uri, type, mime));
140    }
141
142    public static List<Attachment> of(final Context context, List<Uri> uris, final String type) {
143        final List<Attachment> attachments = new ArrayList<>();
144        for (final Uri uri : uris) {
145            if (uri == null) {
146                continue;
147            }
148            final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, type);
149            attachments.add(new Attachment(uri, mime != null && isImage(mime) ? Type.IMAGE : Type.FILE, mime));
150        }
151        return attachments;
152    }
153
154    public static Attachment of(UUID uuid, final File file, String mime) {
155        return new Attachment(uuid, Uri.fromFile(file), mime != null && (isImage(mime) || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime);
156    }
157
158    public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
159        List<Attachment> uris = new ArrayList<>();
160        if (intent == null) {
161            return uris;
162        }
163        final String contentType = intent.getType();
164        final Uri data = intent.getData();
165        if (data == null) {
166            final ClipData clipData = intent.getClipData();
167            if (clipData != null) {
168                for (int i = 0; i < clipData.getItemCount(); ++i) {
169                    final Uri uri = clipData.getItemAt(i).getUri();
170                    final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType);
171                    uris.add(new Attachment(uri, type, mime));
172                }
173            }
174        } else {
175            final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, data, contentType);
176            uris.add(new Attachment(data, type, mime));
177        }
178        return uris;
179    }
180
181    public boolean renderThumbnail() {
182        return type == Type.IMAGE || (type == Type.FILE && mime != null && renderFileThumbnail(mime));
183    }
184
185    private static boolean renderFileThumbnail(final String mime) {
186        return mime.startsWith("video/")
187                || isImage(mime)
188                || "application/pdf".equals(mime);
189    }
190
191    public Uri getUri() {
192        return uri;
193    }
194
195    public UUID getUuid() {
196        return uuid;
197    }
198
199    private static boolean isImage(final String mime) {
200        return mime.startsWith("image/") && !mime.equals("image/svg+xml");
201    }
202}