BluetoothMapMessageListing.java revision 5079ae05a3fa7cf09d2d8e564b71382dfc70a1ae
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*/
15package com.android.bluetooth.map;
16
17import java.io.IOException;
18import java.io.StringWriter;
19import java.io.UnsupportedEncodingException;
20import java.util.ArrayList;
21import java.util.Collections;
22import java.util.List;
23import com.android.internal.util.FastXmlSerializer;
24
25import org.xmlpull.v1.XmlSerializer;
26
27import android.util.Log;
28import android.util.Xml;
29
30public class BluetoothMapMessageListing {
31    private boolean hasUnread = false;
32    private static final String TAG = "BluetoothMapMessageListing";
33    private List<BluetoothMapMessageListingElement> list;
34
35    public BluetoothMapMessageListing(){
36     list = new ArrayList<BluetoothMapMessageListingElement>();
37    }
38    public void add(BluetoothMapMessageListingElement element) {
39        list.add(element);
40        /* update info regarding whether the list contains unread messages */
41        if (element.getRead().equalsIgnoreCase("no"))
42        {
43            hasUnread = true;
44        }
45    }
46
47    /**
48     * Used to fetch the number of BluetoothMapMessageListingElement elements in the list.
49     * @return the number of elements in the list.
50     */
51    public int getCount() {
52        if(list != null)
53        {
54            return list.size();
55        }
56        return 0;
57    }
58
59    /**
60     * does the list contain any unread messages
61     * @return true if unread messages have been added to the list, else false
62     */
63    public boolean hasUnread()
64    {
65        return hasUnread;
66    }
67
68    /**
69     * Encode the list of BluetoothMapMessageListingElement(s) into a UTF-8
70     * formatted XML-string in a trimmed byte array
71     *
72     * @return a reference to the encoded byte array.
73     * @throws UnsupportedEncodingException
74     *             if UTF-8 encoding is unsupported on the platform.
75     */
76    public byte[] encode() throws UnsupportedEncodingException {
77        StringWriter sw = new StringWriter();
78        XmlSerializer xmlMsgElement = new FastXmlSerializer();
79        try {
80            xmlMsgElement.setOutput(sw);
81            xmlMsgElement.startDocument("UTF-8", true);
82            xmlMsgElement.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
83            xmlMsgElement.startTag(null, "MAP-msg-listing");
84            xmlMsgElement.attribute(null, "version", "1.0");
85            // Do the XML encoding of list
86            for (BluetoothMapMessageListingElement element : list) {
87                element.encode(xmlMsgElement); // Append the list element
88            }
89            xmlMsgElement.endTag(null, "MAP-msg-listing");
90            xmlMsgElement.endDocument();
91        } catch (IllegalArgumentException e) {
92            Log.w(TAG, e.toString());
93        } catch (IllegalStateException e) {
94            Log.w(TAG, e.toString());
95        } catch (IOException e) {
96            Log.w(TAG, e.toString());
97        }
98        return sw.toString().getBytes("UTF-8");
99    }
100
101    public void sort() {
102        Collections.sort(list);
103    }
104
105    public void segment(int count, int offset) {
106        count = Math.min(count, list.size());
107        if (offset + count <= list.size()) {
108            list = list.subList(offset, offset + count);
109        } else {
110            if(offset > list.size()) {
111               list = new ArrayList<BluetoothMapMessageListingElement>();
112               Log.d(TAG, "offset greater than list size. Returning empty list");
113            } else {
114               list = list.subList(offset, list.size());
115            }
116        }
117    }
118}
119