1fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie/*
2fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* Copyright (C) 2013 Samsung System LSI
3fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* Licensed under the Apache License, Version 2.0 (the "License");
4fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* you may not use this file except in compliance with the License.
5fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* You may obtain a copy of the License at
6fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie*
7fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie*      http://www.apache.org/licenses/LICENSE-2.0
8fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie*
9fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* Unless required by applicable law or agreed to in writing, software
10fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* distributed under the License is distributed on an "AS IS" BASIS,
11fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* See the License for the specific language governing permissions and
13fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie* limitations under the License.
14fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie*/
15fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xiepackage com.android.bluetooth.map;
16fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
17fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport java.io.IOException;
18fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport java.io.StringWriter;
19fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport java.io.UnsupportedEncodingException;
20fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport java.util.ArrayList;
21fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport java.util.Collections;
22fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport java.util.List;
23fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
24fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport org.xmlpull.v1.XmlSerializer;
25fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
26fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport android.util.Log;
27fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xieimport android.util.Xml;
28fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
29fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xiepublic class BluetoothMapMessageListing {
3070be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz    private boolean hasUnread = false;
31fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    private static final String TAG = "BluetoothMapMessageListing";
32fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    private List<BluetoothMapMessageListingElement> list;
33fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
34fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    public BluetoothMapMessageListing(){
35fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     list = new ArrayList<BluetoothMapMessageListingElement>();
36fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    }
37fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    public void add(BluetoothMapMessageListingElement element) {
38fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        list.add(element);
3970be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        /* update info regarding whether the list contains unread messages */
4070be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        if (element.getRead().equalsIgnoreCase("no"))
4170be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        {
4270be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz            hasUnread = true;
4370be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        }
44fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    }
45fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
46fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    /**
47fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     * Used to fetch the number of BluetoothMapMessageListingElement elements in the list.
48fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     * @return the number of elements in the list.
49fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     */
50fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    public int getCount() {
5170be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        if(list != null)
5270be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        {
5370be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz            return list.size();
5470be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        }
5570be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        return 0;
5670be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz    }
5770be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz
5870be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz    /**
5970be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz     * does the list contain any unread messages
6070be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz     * @return true if unread messages have been added to the list, else false
6170be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz     */
6270be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz    public boolean hasUnread()
6370be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz    {
6470be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz        return hasUnread;
65fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    }
6670be005a18a35ec5fcb46152f0dfbe82156efa3aKim Schulz
67fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    /**
68fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     * Encode the list of BluetoothMapMessageListingElement(s) into a UTF-8
69fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     * formatted XML-string in a trimmed byte array
70fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     *
71fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     * @return a reference to the encoded byte array.
72fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     * @throws UnsupportedEncodingException
73fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     *             if UTF-8 encoding is unsupported on the platform.
74fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie     */
75fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    public byte[] encode() throws UnsupportedEncodingException {
76fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        StringWriter sw = new StringWriter();
77fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        XmlSerializer xmlMsgElement = Xml.newSerializer();
78fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        try {
79fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            xmlMsgElement.setOutput(sw);
80fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            xmlMsgElement.startDocument(null, null);
81fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            xmlMsgElement.startTag("", "MAP-msg-listing");
82fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            xmlMsgElement.attribute("", "version", "1.0");
83fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            // Do the XML encoding of list
84fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            for (BluetoothMapMessageListingElement element : list) {
85fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie                element.encode(xmlMsgElement); // Append the list element
86fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            }
87fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            xmlMsgElement.endTag("", "MAP-msg-listing");
88fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            xmlMsgElement.endDocument();
89fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        } catch (IllegalArgumentException e) {
90fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            Log.w(TAG, e.toString());
91fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        } catch (IllegalStateException e) {
92fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            Log.w(TAG, e.toString());
93fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        } catch (IOException e) {
94fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            Log.w(TAG, e.toString());
95fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        }
96fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        return sw.toString().getBytes("UTF-8");
97fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    }
98fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
99fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    public void sort() {
100fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        Collections.sort(list);
101fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    }
102fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie
103fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    public void segment(int count, int offset) {
104fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        count = Math.min(count, list.size());
105fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        if (offset + count <= list.size()) {
106fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            list = list.subList(offset, offset + count);
107fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        } else {
108fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie            list = null;
109fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie        }
110fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie    }
111fd6603b8bf9ed72dcc8bd59aaef3209251b6e17cMatthew Xie}
112