1 /*
2  * Copyright (C) 2007 Esmertec AG.
3  * Copyright (C) 2007 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.google.android.mms.pdu;
19 
20 import android.compat.annotation.UnsupportedAppUsage;
21 
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.Vector;
25 
26 public class PduBody {
27     private Vector<PduPart> mParts = null;
28 
29     private Map<String, PduPart> mPartMapByContentId = null;
30     private Map<String, PduPart> mPartMapByContentLocation = null;
31     private Map<String, PduPart> mPartMapByName = null;
32     private Map<String, PduPart> mPartMapByFileName = null;
33 
34     /**
35      * Constructor.
36      */
37     @UnsupportedAppUsage
PduBody()38     public PduBody() {
39         mParts = new Vector<PduPart>();
40 
41         mPartMapByContentId = new HashMap<String, PduPart>();
42         mPartMapByContentLocation  = new HashMap<String, PduPart>();
43         mPartMapByName = new HashMap<String, PduPart>();
44         mPartMapByFileName = new HashMap<String, PduPart>();
45     }
46 
putPartToMaps(PduPart part)47     private void putPartToMaps(PduPart part) {
48         // Put part to mPartMapByContentId.
49         byte[] contentId = part.getContentId();
50         if(null != contentId) {
51             mPartMapByContentId.put(new String(contentId), part);
52         }
53 
54         // Put part to mPartMapByContentLocation.
55         byte[] contentLocation = part.getContentLocation();
56         if(null != contentLocation) {
57             String clc = new String(contentLocation);
58             mPartMapByContentLocation.put(clc, part);
59         }
60 
61         // Put part to mPartMapByName.
62         byte[] name = part.getName();
63         if(null != name) {
64             String clc = new String(name);
65             mPartMapByName.put(clc, part);
66         }
67 
68         // Put part to mPartMapByFileName.
69         byte[] fileName = part.getFilename();
70         if(null != fileName) {
71             String clc = new String(fileName);
72             mPartMapByFileName.put(clc, part);
73         }
74     }
75 
76     /**
77      * Appends the specified part to the end of this body.
78      *
79      * @param part part to be appended
80      * @return true when success, false when fail
81      * @throws NullPointerException when part is null
82      */
83     @UnsupportedAppUsage
addPart(PduPart part)84     public boolean addPart(PduPart part) {
85         if(null == part) {
86             throw new NullPointerException();
87         }
88 
89         putPartToMaps(part);
90         return mParts.add(part);
91     }
92 
93     /**
94      * Inserts the specified part at the specified position.
95      *
96      * @param index index at which the specified part is to be inserted
97      * @param part part to be inserted
98      * @throws NullPointerException when part is null
99      */
100     @UnsupportedAppUsage
addPart(int index, PduPart part)101     public void addPart(int index, PduPart part) {
102         if(null == part) {
103             throw new NullPointerException();
104         }
105 
106         putPartToMaps(part);
107         mParts.add(index, part);
108     }
109 
110     /**
111      * Removes the part at the specified position.
112      *
113      * @param index index of the part to return
114      * @return part at the specified index
115      */
116     @UnsupportedAppUsage
removePart(int index)117     public PduPart removePart(int index) {
118         return mParts.remove(index);
119     }
120 
121     /**
122      * Remove all of the parts.
123      */
removeAll()124     public void removeAll() {
125         mParts.clear();
126     }
127 
128     /**
129      * Get the part at the specified position.
130      *
131      * @param index index of the part to return
132      * @return part at the specified index
133      */
134     @UnsupportedAppUsage
getPart(int index)135     public PduPart getPart(int index) {
136         return mParts.get(index);
137     }
138 
139     /**
140      * Get the index of the specified part.
141      *
142      * @param part the part object
143      * @return index the index of the first occurrence of the part in this body
144      */
145     @UnsupportedAppUsage
getPartIndex(PduPart part)146     public int getPartIndex(PduPart part) {
147         return mParts.indexOf(part);
148     }
149 
150     /**
151      * Get the number of parts.
152      *
153      * @return the number of parts
154      */
155     @UnsupportedAppUsage
getPartsNum()156     public int getPartsNum() {
157         return mParts.size();
158     }
159 
160     /**
161      * Get pdu part by content id.
162      *
163      * @param cid the value of content id.
164      * @return the pdu part.
165      */
166     @UnsupportedAppUsage
getPartByContentId(String cid)167     public PduPart getPartByContentId(String cid) {
168         return mPartMapByContentId.get(cid);
169     }
170 
171     /**
172      * Get pdu part by Content-Location. Content-Location of part is
173      * the same as filename and name(param of content-type).
174      *
175      * @param fileName the value of filename.
176      * @return the pdu part.
177      */
178     @UnsupportedAppUsage
getPartByContentLocation(String contentLocation)179     public PduPart getPartByContentLocation(String contentLocation) {
180         return mPartMapByContentLocation.get(contentLocation);
181     }
182 
183     /**
184      * Get pdu part by name.
185      *
186      * @param fileName the value of filename.
187      * @return the pdu part.
188      */
189     @UnsupportedAppUsage
getPartByName(String name)190     public PduPart getPartByName(String name) {
191         return mPartMapByName.get(name);
192     }
193 
194     /**
195      * Get pdu part by filename.
196      *
197      * @param fileName the value of filename.
198      * @return the pdu part.
199      */
200     @UnsupportedAppUsage
getPartByFileName(String filename)201     public PduPart getPartByFileName(String filename) {
202         return mPartMapByFileName.get(filename);
203     }
204 }
205