1 /*
2 * Copyright (C) 2013 Samsung System LSI
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *      http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package com.android.bluetooth.map;
16 
17 import java.io.IOException;
18 import java.io.StringWriter;
19 import java.io.UnsupportedEncodingException;
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import com.android.internal.util.FastXmlSerializer;
24 
25 import org.xmlpull.v1.XmlSerializer;
26 
27 import android.util.Log;
28 import android.util.Xml;
29 
30 public class BluetoothMapMessageListing {
31     private boolean hasUnread = false;
32     private static final String TAG = "BluetoothMapMessageListing";
33     private static final boolean D = BluetoothMapService.DEBUG;
34 
35     private List<BluetoothMapMessageListingElement> list;
36 
BluetoothMapMessageListing()37     public BluetoothMapMessageListing(){
38      list = new ArrayList<BluetoothMapMessageListingElement>();
39     }
add(BluetoothMapMessageListingElement element)40     public void add(BluetoothMapMessageListingElement element) {
41         list.add(element);
42         /* update info regarding whether the list contains unread messages */
43         if (element.getReadBool())
44         {
45             hasUnread = true;
46         }
47     }
48 
49     /**
50      * Used to fetch the number of BluetoothMapMessageListingElement elements in the list.
51      * @return the number of elements in the list.
52      */
getCount()53     public int getCount() {
54         if(list != null)
55         {
56             return list.size();
57         }
58         return 0;
59     }
60 
61     /**
62      * does the list contain any unread messages
63      * @return true if unread messages have been added to the list, else false
64      */
hasUnread()65     public boolean hasUnread()
66     {
67         return hasUnread;
68     }
69 
70 
71     /**
72      *  returns the entire list as a list
73      * @return list
74      */
getList()75     public List<BluetoothMapMessageListingElement> getList(){
76         return list;
77     }
78 
79     /**
80      * Encode the list of BluetoothMapMessageListingElement(s) into a UTF-8
81      * formatted XML-string in a trimmed byte array
82      *
83      * @return a reference to the encoded byte array.
84      * @throws UnsupportedEncodingException
85      *             if UTF-8 encoding is unsupported on the platform.
86      */
encode(boolean includeThreadId)87     public byte[] encode(boolean includeThreadId) throws UnsupportedEncodingException {
88         StringWriter sw = new StringWriter();
89         XmlSerializer xmlMsgElement = new FastXmlSerializer();
90         try {
91             xmlMsgElement.setOutput(sw);
92             xmlMsgElement.startDocument("UTF-8", true);
93             xmlMsgElement.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
94             xmlMsgElement.startTag(null, "MAP-msg-listing");
95             xmlMsgElement.attribute(null, "version", "1.0");
96             // Do the XML encoding of list
97             for (BluetoothMapMessageListingElement element : list) {
98                 element.encode(xmlMsgElement, includeThreadId); // Append the list element
99             }
100             xmlMsgElement.endTag(null, "MAP-msg-listing");
101             xmlMsgElement.endDocument();
102         } catch (IllegalArgumentException e) {
103             Log.w(TAG, e);
104         } catch (IllegalStateException e) {
105             Log.w(TAG, e);
106         } catch (IOException e) {
107             Log.w(TAG, e);
108         }
109         return sw.toString().getBytes("UTF-8");
110     }
111 
sort()112     public void sort() {
113         Collections.sort(list);
114     }
115 
segment(int count, int offset)116     public void segment(int count, int offset) {
117         count = Math.min(count, list.size() - offset);
118         if (count > 0) {
119             list = list.subList(offset, offset + count);
120             if(list == null) {
121                 list = new ArrayList<BluetoothMapMessageListingElement>(); // Return an empty list
122             }
123         } else {
124             if(offset > list.size()) {
125                list = new ArrayList<BluetoothMapMessageListingElement>();
126                Log.d(TAG, "offset greater than list size. Returning empty list");
127             } else {
128                list = list.subList(offset, list.size());
129             }
130         }
131     }
132 }
133