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;
38import android.util.Log;
39
40import java.io.File;
41import java.util.ArrayList;
42import java.util.Collections;
43import java.util.List;
44import java.util.UUID;
45
46import eu.siacs.conversations.Config;
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 public enum Type {
93 FILE, IMAGE, LOCATION, RECORDING
94 }
95
96 private final Uri uri;
97 private final Type type;
98 private final UUID uuid;
99 private final String mime;
100
101 private Attachment(UUID uuid, Uri uri, Type type, String mime) {
102 this.uri = uri;
103 this.type = type;
104 this.mime = mime;
105 this.uuid = uuid;
106 }
107
108 private Attachment(Uri uri, Type type, String mime) {
109 this.uri = uri;
110 this.type = type;
111 this.mime = mime;
112 this.uuid = UUID.randomUUID();
113 }
114
115 public static boolean canBeSendInband(final List<Attachment> attachments) {
116 for(Attachment attachment : attachments) {
117 if (attachment.type != Type.LOCATION) {
118 return false;
119 }
120 }
121 return true;
122 }
123
124 public static List<Attachment> of(final Context context, Uri uri, Type type) {
125 final String mime = type == Type.LOCATION ?null :MimeUtils.guessMimeTypeFromUri(context, uri);
126 return Collections.singletonList(new Attachment(uri, type, mime));
127 }
128
129 public static List<Attachment> of(final Context context, List<Uri> uris) {
130 List<Attachment> attachments = new ArrayList<>();
131 for(Uri uri : uris) {
132 final String mime = MimeUtils.guessMimeTypeFromUri(context, uri);
133 attachments.add(new Attachment(uri, mime != null && mime.startsWith("image/") ? Type.IMAGE : Type.FILE,mime));
134 }
135 return attachments;
136 }
137
138 public static Attachment of(UUID uuid, final File file, String mime) {
139 return new Attachment(uuid, Uri.fromFile(file),mime != null && (mime.startsWith("image/") || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime);
140 }
141
142 public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
143 List<Attachment> uris = new ArrayList<>();
144 if (intent == null) {
145 return uris;
146 }
147 final String contentType = intent.getType();
148 final Uri data = intent.getData();
149 if (data == null) {
150 final ClipData clipData = intent.getClipData();
151 if (clipData != null) {
152 for (int i = 0; i < clipData.getItemCount(); ++i) {
153 final Uri uri = clipData.getItemAt(i).getUri();
154 Log.d(Config.LOGTAG,"uri="+uri+" contentType="+contentType);
155 final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType);
156 Log.d(Config.LOGTAG,"mime="+mime);
157 uris.add(new Attachment(uri, type, mime));
158 }
159 }
160 } else {
161 final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, data, contentType);
162 uris.add(new Attachment(data, type, mime));
163 }
164 return uris;
165 }
166
167 public boolean renderThumbnail() {
168 return type == Type.IMAGE || (type == Type.FILE && mime != null && renderFileThumbnail(mime));
169 }
170
171 private static boolean renderFileThumbnail(final String mime) {
172 return mime.startsWith("video/")
173 || mime.startsWith("image/")
174 || (Compatibility.runsTwentyOne() && "application/pdf".equals(mime));
175 }
176
177 public Uri getUri() {
178 return uri;
179 }
180
181 public UUID getUuid() {
182 return uuid;
183 }
184}