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 22 import org.xmlpull.v1.XmlSerializer; 23 24 import android.util.Xml; 25 26 /** 27 * @author cbonde 28 * 29 */ 30 public class BluetoothMapFolderElement { 31 private String name; 32 private BluetoothMapFolderElement parent = null; 33 private ArrayList<BluetoothMapFolderElement> subFolders; 34 35 public BluetoothMapFolderElement( String name, BluetoothMapFolderElement parrent ){ 36 this.name = name; 37 this.parent = parrent; 38 subFolders = new ArrayList<BluetoothMapFolderElement>(); 39 } 40 41 public String getName() { 42 return name; 43 } 44 45 /** 46 * Fetch the parent folder. 47 * @return the parent folder or null if we are at the root folder. 48 */ 49 public BluetoothMapFolderElement getParent() { 50 return parent; 51 } 52 53 /** 54 * Fetch the root folder. 55 * @return the parent folder or null if we are at the root folder. 56 */ 57 public BluetoothMapFolderElement getRoot() { 58 BluetoothMapFolderElement rootFolder = this; 59 while(rootFolder.getParent() != null) 60 rootFolder = rootFolder.getParent(); 61 return rootFolder; 62 } 63 64 /** 65 * Add a folder. 66 * @param name the name of the folder to add. 67 * @return the added folder element. 68 */ 69 public BluetoothMapFolderElement addFolder(String name){ 70 BluetoothMapFolderElement newFolder = new BluetoothMapFolderElement(name, this); 71 subFolders.add(newFolder); 72 return newFolder; 73 } 74 75 /** 76 * Fetch the number of sub folders. 77 * @return returns the number of sub folders. 78 */ 79 public int getSubFolderCount(){ 80 return subFolders.size(); 81 } 82 83 /** 84 * Returns the subFolder element matching the supplied folder name. 85 * @param folderName the name of the subFolder to find. 86 * @return the subFolder element if found {@code null} otherwise. 87 */ 88 public BluetoothMapFolderElement getSubFolder(String folderName){ 89 for(BluetoothMapFolderElement subFolder : subFolders){ 90 if(subFolder.getName().equals(folderName)) 91 return subFolder; 92 } 93 return null; 94 } 95 96 public byte[] encode(int offset, int count) throws UnsupportedEncodingException { 97 StringWriter sw = new StringWriter(); 98 XmlSerializer xmlMsgElement = Xml.newSerializer(); 99 int i, stopIndex; 100 if(offset > subFolders.size()) 101 throw new IllegalArgumentException("FolderListingEncode: offset > subFolders.size()"); 102 103 stopIndex = offset + count; 104 if(stopIndex > subFolders.size()) 105 stopIndex = subFolders.size(); 106 107 try { 108 xmlMsgElement.setOutput(sw); 109 xmlMsgElement.startDocument(null, null); 110 xmlMsgElement.text("\n"); 111 xmlMsgElement.startTag("", "folder-listing"); 112 xmlMsgElement.attribute("", "version", "1.0"); 113 for(i = offset; i<stopIndex; i++) 114 { 115 xmlMsgElement.startTag("", "folder"); 116 xmlMsgElement.attribute("", "name", subFolders.get(i).getName()); 117 xmlMsgElement.endTag("", "folder"); 118 } 119 xmlMsgElement.endTag("", "folder-listing"); 120 xmlMsgElement.endDocument(); 121 } catch (IllegalArgumentException e) { 122 // TODO Auto-generated catch block 123 e.printStackTrace(); 124 } catch (IllegalStateException e) { 125 // TODO Auto-generated catch block 126 e.printStackTrace(); 127 } catch (IOException e) { 128 // TODO Auto-generated catch block 129 e.printStackTrace(); 130 } 131 return sw.toString().getBytes("UTF-8"); 132 } 133 } 134