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 29 public class BluetoothMapMessageListing { 30 private boolean hasUnread = false; 31 private static final String TAG = "BluetoothMapMessageListing"; 32 private static final boolean D = BluetoothMapService.DEBUG; 33 34 private List<BluetoothMapMessageListingElement> mList; 35 BluetoothMapMessageListing()36 public BluetoothMapMessageListing(){ 37 mList = new ArrayList<BluetoothMapMessageListingElement>(); 38 } add(BluetoothMapMessageListingElement element)39 public void add(BluetoothMapMessageListingElement element) { 40 mList.add(element); 41 /* update info regarding whether the list contains unread messages */ 42 if (element.getReadBool()) 43 { 44 hasUnread = true; 45 } 46 } 47 48 /** 49 * Used to fetch the number of BluetoothMapMessageListingElement elements in the list. 50 * @return the number of elements in the list. 51 */ getCount()52 public int getCount() { 53 if(mList != null) 54 { 55 return mList.size(); 56 } 57 return 0; 58 } 59 60 /** 61 * does the list contain any unread messages 62 * @return true if unread messages have been added to the list, else false 63 */ hasUnread()64 public boolean hasUnread() 65 { 66 return hasUnread; 67 } 68 69 70 /** 71 * returns the entire list as a list 72 * @return list 73 */ getList()74 public List<BluetoothMapMessageListingElement> getList(){ 75 return mList; 76 } 77 78 /** 79 * Encode the list of BluetoothMapMessageListingElement(s) into a UTF-8 80 * formatted XML-string in a trimmed byte array 81 * 82 * @param version the version as a string. 83 * Set the listing version to e.g. "1.0" or "1.1". 84 * To make this future proof, no check is added to validate the value, hence be careful. 85 * @return a reference to the encoded byte array. 86 * @throws UnsupportedEncodingException 87 * if UTF-8 encoding is unsupported on the platform. 88 */ 89 // TODO: Remove includeThreadId when MAP-IM is adopted encode(boolean includeThreadId, String version)90 public byte[] encode(boolean includeThreadId, String version) throws UnsupportedEncodingException { 91 StringWriter sw = new StringWriter(); 92 XmlSerializer xmlMsgElement = new FastXmlSerializer(); 93 try { 94 xmlMsgElement.setOutput(sw); 95 xmlMsgElement.startDocument("UTF-8", true); 96 xmlMsgElement.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); 97 xmlMsgElement.startTag(null, "MAP-msg-listing"); 98 xmlMsgElement.attribute(null, "version", version); 99 // Do the XML encoding of list 100 for (BluetoothMapMessageListingElement element : mList) { 101 element.encode(xmlMsgElement, includeThreadId); // Append the list element 102 } 103 xmlMsgElement.endTag(null, "MAP-msg-listing"); 104 xmlMsgElement.endDocument(); 105 } catch (IllegalArgumentException e) { 106 Log.w(TAG, e); 107 } catch (IllegalStateException e) { 108 Log.w(TAG, e); 109 } catch (IOException e) { 110 Log.w(TAG, e); 111 } 112 return sw.toString().getBytes("UTF-8"); 113 } 114 sort()115 public void sort() { 116 Collections.sort(mList); 117 } 118 segment(int count, int offset)119 public void segment(int count, int offset) { 120 count = Math.min(count, mList.size() - offset); 121 if (count > 0) { 122 mList = mList.subList(offset, offset + count); 123 if(mList == null) { 124 mList = new ArrayList<BluetoothMapMessageListingElement>(); // Return an empty list 125 } 126 } else { 127 if(offset > mList.size()) { 128 mList = new ArrayList<BluetoothMapMessageListingElement>(); 129 Log.d(TAG, "offset greater than list size. Returning empty list"); 130 } else { 131 mList = mList.subList(offset, mList.size()); 132 } 133 } 134 } 135 } 136