1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.bluetooth.avrcpcontroller;
18 
19 import android.util.Log;
20 
21 import java.util.Date;
22 import java.util.Objects;
23 
24 /**
25  * Represents BIP attachment metadata arriving from a GetImageProperties request.
26  *
27  * Content type is the only spec-required field.
28  *
29  * Examples:
30  *   <attachment content-type="text/plain" name="ABCD1234.txt" size="5120"/>
31  *   <attachment content-type="audio/basic" name="ABCD1234.wav" size="102400"/>
32  */
33 public class BipAttachmentFormat {
34     private static final String TAG = "avrcpcontroller.BipAttachmentFormat";
35 
36     /**
37      * MIME content type of the image attachment, i.e. "text/plain"
38      *
39      * This is required by the specification
40      */
41     private final String mContentType;
42 
43     /**
44      * MIME character set of the image attachment, i.e. "ISO-8859-1"
45      */
46     private final String mCharset;
47 
48     /**
49      * File name of the image attachment
50      *
51      * This is required by the specification
52      */
53     private final String mName;
54 
55     /**
56      * Size of the image attachment in bytes
57      */
58     private final int mSize;
59 
60     /**
61      * Date the image attachment was created
62      */
63     private final BipDateTime mCreated;
64 
65     /**
66      * Date the image attachment was last modified
67      */
68     private final BipDateTime mModified;
69 
BipAttachmentFormat(String contentType, String charset, String name, String size, String created, String modified)70     public BipAttachmentFormat(String contentType, String charset, String name, String size,
71             String created, String modified) {
72         if (contentType == null) {
73             throw new ParseException("ContentType is required and must be valid");
74         }
75         if (name == null) {
76             throw new ParseException("Name is required and must be valid");
77         }
78 
79         mContentType = contentType;
80         mName = name;
81         mCharset = charset;
82         mSize = parseInt(size);
83 
84         BipDateTime bipCreated = null;
85         try {
86             bipCreated = new BipDateTime(created);
87         } catch (ParseException e) {
88             bipCreated = null;
89         }
90         mCreated = bipCreated;
91 
92         BipDateTime bipModified = null;
93         try {
94             bipModified = new BipDateTime(modified);
95         } catch (ParseException e) {
96             bipModified = null;
97         }
98         mModified = bipModified;
99     }
100 
BipAttachmentFormat(String contentType, String charset, String name, int size, Date created, Date modified)101     public BipAttachmentFormat(String contentType, String charset, String name, int size,
102             Date created, Date modified) {
103         mContentType = Objects.requireNonNull(contentType, "Content-Type cannot be null");
104         mName = Objects.requireNonNull(name, "Name cannot be null");
105         mCharset = charset;
106         mSize = size;
107         mCreated = created != null ? new BipDateTime(created) : null;
108         mModified = modified != null ? new BipDateTime(modified) : null;
109     }
110 
parseInt(String s)111     private static int parseInt(String s) {
112         if (s == null) return -1;
113         try {
114             return Integer.parseInt(s);
115         } catch (NumberFormatException e) {
116             Log.e(TAG, "Invalid number format for '" + s + "'");
117         }
118         return -1;
119     }
120 
getContentType()121     public String getContentType() {
122         return mContentType;
123     }
124 
getName()125     public String getName() {
126         return mName;
127     }
128 
getCharset()129     public String getCharset() {
130         return mCharset;
131     }
132 
getSize()133     public int getSize() {
134         return mSize;
135     }
136 
getCreatedDate()137     public BipDateTime getCreatedDate() {
138         return mCreated;
139     }
140 
getModifiedDate()141     public BipDateTime getModifiedDate() {
142         return mModified;
143     }
144 
145     @Override
equals(Object o)146     public boolean equals(Object o) {
147         if (o == this) return true;
148         if (!(o instanceof BipAttachmentFormat)) return false;
149 
150         BipAttachmentFormat a = (BipAttachmentFormat) o;
151         return a.getContentType() == getContentType()
152                 && a.getName() == getName()
153                 && a.getCharset() == getCharset()
154                 && a.getSize() == getSize()
155                 && a.getCreatedDate() == getCreatedDate()
156                 && a.getModifiedDate() == getModifiedDate();
157     }
158 
159     @Override
toString()160     public String toString() {
161         StringBuilder sb = new StringBuilder();
162         sb.append("<attachment");
163         sb.append(" content-type=\"" + mContentType + "\"");
164         if (mCharset != null) sb.append(" charset=\"" + mCharset + "\"");
165         sb.append(" name=\"" + mName + "\"");
166         if (mSize > -1) sb.append(" size=\"" + mSize + "\"");
167         if (mCreated != null) sb.append(" created=\"" + mCreated.toString() + "\"");
168         if (mModified != null) sb.append(" modified=\"" + mModified.toString() + "\"");
169         sb.append(" />");
170         return sb.toString();
171     }
172 }
173