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