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