30 import android.os.Parcel; |
30 import android.os.Parcel; |
31 import android.os.Parcelable; |
31 import android.os.Parcelable; |
32 import android.preference.PreferenceManager; |
32 import android.preference.PreferenceManager; |
33 |
33 |
34 public class DownloadPackage implements Parcelable{ |
34 public class DownloadPackage implements Parcelable{ |
35 private String url_without_suffix; |
35 private String url_without_suffix; |
36 private String pathToStore; |
36 private String pathToStore; |
37 private String representation; |
37 private String representation; |
38 private String description; |
38 private String description; |
39 private int versionNumber; |
39 private int versionNumber; |
40 private final Status status; |
40 private final Status status; |
41 private int uniqueId; |
41 private int uniqueId; |
42 |
42 |
43 |
43 |
44 public DownloadPackage(Parcel src){ |
44 public DownloadPackage(Parcel src){ |
45 url_without_suffix = src.readString(); |
45 url_without_suffix = src.readString(); |
46 pathToStore = src.readString(); |
46 pathToStore = src.readString(); |
47 representation = src.readString(); |
47 representation = src.readString(); |
48 versionNumber = src.readInt(); |
48 versionNumber = src.readInt(); |
49 status = Status.values()[src.readInt()]; |
49 status = Status.values()[src.readInt()]; |
50 description = src.readString(); |
50 description = src.readString(); |
51 uniqueId = src.readInt(); |
51 uniqueId = src.readInt(); |
52 } |
52 } |
53 |
53 |
54 public DownloadPackage(Context c, String _url_without_suffix, String path, int version, String _representation, String _description, int _uniqueId){ |
54 public DownloadPackage(Context c, String _url_without_suffix, String path, int version, String _representation, String _description, int _uniqueId){ |
55 url_without_suffix = _url_without_suffix; |
55 url_without_suffix = _url_without_suffix; |
56 pathToStore = path; |
56 pathToStore = path; |
57 representation = _representation; |
57 representation = _representation; |
58 versionNumber = version; |
58 versionNumber = version; |
59 description = _description; |
59 description = _description; |
60 uniqueId = _uniqueId; |
60 uniqueId = _uniqueId; |
61 |
61 |
62 |
62 |
63 //determine if the user has already downloaded this version |
63 //determine if the user has already downloaded this version |
64 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(c); |
64 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(c); |
65 int currentVersion = sharedPref.getInt(representation, -1); |
65 int currentVersion = sharedPref.getInt(representation, -1); |
66 if(currentVersion == versionNumber) status = Status.CURRENTVERSION; |
66 if(currentVersion == versionNumber) status = Status.CURRENTVERSION; |
67 else if (currentVersion < versionNumber) status = Status.NEWERVERSION; |
67 else if (currentVersion < versionNumber) status = Status.NEWERVERSION; |
68 else status = Status.OLDERVERSION; |
68 else status = Status.OLDERVERSION; |
69 } |
69 } |
70 |
70 |
71 public Status getStatus(){ |
71 public Status getStatus(){ |
72 return status; |
72 return status; |
73 } |
73 } |
74 |
74 |
75 public String getURL(){ |
75 public String getURL(){ |
76 return url_without_suffix; |
76 return url_without_suffix; |
77 } |
77 } |
78 |
78 |
79 public String getPathToStore(){ |
79 public String getPathToStore(){ |
80 return pathToStore; |
80 return pathToStore; |
81 } |
81 } |
82 |
82 |
83 public String toString(){ |
83 public String toString(){ |
84 return representation; |
84 return representation; |
85 } |
85 } |
86 |
86 |
87 public int describeContents() { |
87 public int describeContents() { |
88 return 0; |
88 return 0; |
89 } |
89 } |
90 public int getId(){ |
90 public int getId(){ |
91 return uniqueId; |
91 return uniqueId; |
92 } |
92 } |
93 |
93 |
94 public void writeToParcel(Parcel dest, int flags) { |
94 public void writeToParcel(Parcel dest, int flags) { |
95 dest.writeString(url_without_suffix); |
95 dest.writeString(url_without_suffix); |
96 dest.writeString(pathToStore); |
96 dest.writeString(pathToStore); |
97 dest.writeString(representation); |
97 dest.writeString(representation); |
98 dest.writeInt(versionNumber); |
98 dest.writeInt(versionNumber); |
99 dest.writeInt(status.ordinal()); |
99 dest.writeInt(status.ordinal()); |
100 dest.writeString(description); |
100 dest.writeString(description); |
101 } |
101 } |
102 |
102 |
103 public static final Parcelable.Creator<DownloadPackage> CREATOR = new Parcelable.Creator<DownloadPackage>() { |
103 public static final Parcelable.Creator<DownloadPackage> CREATOR = new Parcelable.Creator<DownloadPackage>() { |
104 public DownloadPackage createFromParcel(Parcel source) { |
104 public DownloadPackage createFromParcel(Parcel source) { |
105 return new DownloadPackage(source); |
105 return new DownloadPackage(source); |
106 } |
106 } |
107 public DownloadPackage[] newArray(int size) { |
107 public DownloadPackage[] newArray(int size) { |
108 return new DownloadPackage[size]; |
108 return new DownloadPackage[size]; |
109 } |
109 } |
110 }; |
110 }; |
111 |
111 |
112 /* |
112 /* |
113 * We enter with a XmlPullParser.Start_tag with name "task" |
113 * We enter with a XmlPullParser.Start_tag with name "task" |
114 */ |
114 */ |
115 public static DownloadPackage getTaskFromXML(Context c, XmlPullParser xmlPuller) throws XmlPullParserException, IOException{ |
115 public static DownloadPackage getTaskFromXML(Context c, XmlPullParser xmlPuller) throws XmlPullParserException, IOException{ |
116 String url = null; |
116 String url = null; |
117 String path = null; |
117 String path = null; |
118 String representation = null; |
118 String representation = null; |
119 String description = null; |
119 String description = null; |
120 int uniqueId = -1; |
120 int uniqueId = -1; |
121 int version = -1; |
121 int version = -1; |
122 |
122 |
123 int eventType = DownloadPackage.getEventType(xmlPuller);//get the next token, should be a start tag |
123 int eventType = DownloadPackage.getEventType(xmlPuller);//get the next token, should be a start tag |
124 while(eventType != XmlPullParser.END_DOCUMENT){ |
124 while(eventType != XmlPullParser.END_DOCUMENT){ |
125 switch(eventType){ |
125 switch(eventType){ |
126 case XmlPullParser.START_TAG: |
126 case XmlPullParser.START_TAG: |
127 String name = xmlPuller.getName().toLowerCase(); |
127 String name = xmlPuller.getName().toLowerCase(); |
128 if(DownloadPackage.getEventType(xmlPuller) == XmlPullParser.TEXT){ |
128 if(DownloadPackage.getEventType(xmlPuller) == XmlPullParser.TEXT){ |
129 String text = xmlPuller.getText().trim(); |
129 String text = xmlPuller.getText().trim(); |
130 if(name.equals("url")){ |
130 if(name.equals("url")){ |
131 url = text; |
131 url = text; |
132 }else if(name.equals("version")){ |
132 }else if(name.equals("version")){ |
133 try{ |
133 try{ |
134 version = Integer.parseInt(text); |
134 version = Integer.parseInt(text); |
135 }catch (NumberFormatException e){ |
135 }catch (NumberFormatException e){ |
136 e.printStackTrace(); |
136 e.printStackTrace(); |
137 version = -1; |
137 version = -1; |
138 } |
138 } |
139 }else if(name.equals("path")){ |
139 }else if(name.equals("path")){ |
140 path = FileUtils.getDataPathFile(c, text).getAbsolutePath(); |
140 path = FileUtils.getDataPathFile(c, text).getAbsolutePath(); |
141 }else if(name.equals("representation")){ |
141 }else if(name.equals("representation")){ |
142 representation = text; |
142 representation = text; |
143 }else if(name.equals("description")){ |
143 }else if(name.equals("description")){ |
144 description = text; |
144 description = text; |
145 }else if(name.equals("uniqueid")){ |
145 }else if(name.equals("uniqueid")){ |
146 try{ |
146 try{ |
147 uniqueId = Integer.parseInt(text); |
147 uniqueId = Integer.parseInt(text); |
148 }catch (NumberFormatException e){ |
148 }catch (NumberFormatException e){ |
149 e.printStackTrace(); |
149 e.printStackTrace(); |
150 version = -1; |
150 version = -1; |
151 } |
151 } |
152 } |
152 } |
153 } |
153 } |
154 DownloadPackage.getEventType(xmlPuller);//endtag |
154 DownloadPackage.getEventType(xmlPuller);//endtag |
155 break; |
155 break; |
156 case XmlPullParser.END_TAG: |
156 case XmlPullParser.END_TAG: |
157 if(xmlPuller.getName().toLowerCase().equals("task") && url != null && path != null && version != -1 && representation != null){ |
157 if(xmlPuller.getName().toLowerCase().equals("task") && url != null && path != null && version != -1 && representation != null){ |
158 return new DownloadPackage(c, url, path, version, representation, description, uniqueId); |
158 return new DownloadPackage(c, url, path, version, representation, description, uniqueId); |
159 }else{ |
159 }else{ |
160 throw new XmlPullParserException("XML download parsing: missing tags"); |
160 throw new XmlPullParserException("XML download parsing: missing tags"); |
161 } |
161 } |
162 case XmlPullParser.TEXT: |
162 case XmlPullParser.TEXT: |
163 throw new XmlPullParserException("Wrong tag recieved got TEXT : " + xmlPuller.getText()); |
163 throw new XmlPullParserException("Wrong tag recieved got TEXT : " + xmlPuller.getText()); |
164 default: |
164 default: |
165 throw new XmlPullParserException("Wrong tag recieved got: " + eventType); |
165 throw new XmlPullParserException("Wrong tag recieved got: " + eventType); |
166 } |
166 } |
167 eventType = DownloadPackage.getEventType(xmlPuller); |
167 eventType = DownloadPackage.getEventType(xmlPuller); |
168 } |
168 } |
169 throw new XmlPullParserException("Xml: unexpected endofdocument tag"); |
169 throw new XmlPullParserException("Xml: unexpected endofdocument tag"); |
170 } |
170 } |
171 |
171 |
172 /** |
172 /** |
173 * Skips whitespaces.. |
173 * Skips whitespaces.. |
174 */ |
174 */ |
175 private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
175 private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
176 int eventType = xmlPuller.next(); |
176 int eventType = xmlPuller.next(); |
177 while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){ |
177 while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){ |
178 eventType = xmlPuller.next(); |
178 eventType = xmlPuller.next(); |
179 } |
179 } |
180 return eventType; |
180 return eventType; |
181 } |
181 } |
182 } |
182 } |
183 |
183 |
184 enum Status{ |
184 enum Status{ |
185 CURRENTVERSION, NEWERVERSION, OLDERVERSION; |
185 CURRENTVERSION, NEWERVERSION, OLDERVERSION; |
186 } |
186 } |