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 java.io.File;
 40import java.util.ArrayList;
 41import java.util.Collections;
 42import java.util.List;
 43import java.util.UUID;
 44
 45import eu.siacs.conversations.utils.Compatibility;
 46import eu.siacs.conversations.utils.MimeUtils;
 47
 48public class Attachment implements Parcelable {
 49
 50    Attachment(Parcel in) {
 51        uri = in.readParcelable(Uri.class.getClassLoader());
 52        mime = in.readString();
 53        uuid = UUID.fromString(in.readString());
 54        type = Type.valueOf(in.readString());
 55    }
 56
 57    @Override
 58    public void writeToParcel(Parcel dest, int flags) {
 59        dest.writeParcelable(uri, flags);
 60        dest.writeString(mime);
 61        dest.writeString(uuid.toString());
 62        dest.writeString(type.toString());
 63    }
 64
 65    @Override
 66    public int describeContents() {
 67        return 0;
 68    }
 69
 70    public static final Creator<Attachment> CREATOR = new Creator<Attachment>() {
 71        @Override
 72        public Attachment createFromParcel(Parcel in) {
 73            return new Attachment(in);
 74        }
 75
 76        @Override
 77        public Attachment[] newArray(int size) {
 78            return new Attachment[size];
 79        }
 80    };
 81
 82    public String getMime() {
 83        return mime;
 84    }
 85
 86    public Type getType() {
 87        return type;
 88    }
 89
 90    public enum Type {
 91        FILE, IMAGE, LOCATION, RECORDING
 92    }
 93
 94    private final Uri uri;
 95    private final Type type;
 96    private final UUID uuid;
 97    private final String mime;
 98
 99    private Attachment(UUID uuid, Uri uri, Type type, String mime) {
100        this.uri = uri;
101        this.type = type;
102        this.mime = mime;
103        this.uuid = uuid;
104    }
105
106    private Attachment(Uri uri, Type type, String mime) {
107        this.uri = uri;
108        this.type = type;
109        this.mime = mime;
110        this.uuid = UUID.randomUUID();
111    }
112
113    public static boolean canBeSendInband(final List<Attachment> attachments) {
114        for (Attachment attachment : attachments) {
115            if (attachment.type != Type.LOCATION) {
116                return false;
117            }
118        }
119        return true;
120    }
121
122    public static List<Attachment> of(final Context context, Uri uri, Type type) {
123        final String mime = type == Type.LOCATION ? null : MimeUtils.guessMimeTypeFromUri(context, uri);
124        return Collections.singletonList(new Attachment(uri, type, mime));
125    }
126
127    public static List<Attachment> of(final Context context, List<Uri> uris) {
128        List<Attachment> attachments = new ArrayList<>();
129        for (Uri uri : uris) {
130            final String mime = MimeUtils.guessMimeTypeFromUri(context, uri);
131            attachments.add(new Attachment(uri, mime != null && isImage(mime) ? Type.IMAGE : Type.FILE, mime));
132        }
133        return attachments;
134    }
135
136    public static Attachment of(UUID uuid, final File file, String mime) {
137        return new Attachment(uuid, Uri.fromFile(file), mime != null && (isImage(mime) || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime);
138    }
139
140    public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
141        List<Attachment> uris = new ArrayList<>();
142        if (intent == null) {
143            return uris;
144        }
145        final String contentType = intent.getType();
146        final Uri data = intent.getData();
147        if (data == null) {
148            final ClipData clipData = intent.getClipData();
149            if (clipData != null) {
150                for (int i = 0; i < clipData.getItemCount(); ++i) {
151                    final Uri uri = clipData.getItemAt(i).getUri();
152                    final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType);
153                    uris.add(new Attachment(uri, type, mime));
154                }
155            }
156        } else {
157            final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, data, contentType);
158            uris.add(new Attachment(data, type, mime));
159        }
160        return uris;
161    }
162
163    public boolean renderThumbnail() {
164        return type == Type.IMAGE || (type == Type.FILE && mime != null && renderFileThumbnail(mime));
165    }
166
167    private static boolean renderFileThumbnail(final String mime) {
168        return mime.startsWith("video/")
169                || isImage(mime)
170                || (Compatibility.runsTwentyOne() && "application/pdf".equals(mime));
171    }
172
173    public Uri getUri() {
174        return uri;
175    }
176
177    public UUID getUuid() {
178        return uuid;
179    }
180
181    private static boolean isImage(final String mime) {
182        return mime.startsWith("image/") && !mime.equals("image/svg+xml");
183    }
184}