author | Medo <smaxein@googlemail.com> |
Sat, 18 Aug 2012 00:47:51 +0200 | |
changeset 7508 | 763d3961400b |
parent 6842 | 2e6391f33204 |
child 7584 | 7831c84cc644 |
permissions | -rw-r--r-- |
6350 | 1 |
/* |
2 |
* Hedgewars for Android. An Android port of Hedgewars, a free turn based strategy game |
|
6700 | 3 |
* Copyright (c) 2011-2012 Richard Deurwaarder <xeli@xelification.com> |
6350 | 4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*/ |
|
18 |
||
19 |
package org.hedgewars.hedgeroid.Downloader; |
|
20 |
||
21 |
import java.io.IOException; |
|
22 |
||
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
6842
diff
changeset
|
23 |
import org.hedgewars.hedgeroid.util.FileUtils; |
6350 | 24 |
import org.xmlpull.v1.XmlPullParser; |
25 |
import org.xmlpull.v1.XmlPullParserException; |
|
26 |
||
27 |
import android.content.Context; |
|
28 |
import android.content.SharedPreferences; |
|
29 |
import android.os.Parcel; |
|
30 |
import android.os.Parcelable; |
|
31 |
import android.preference.PreferenceManager; |
|
32 |
||
33 |
public class DownloadPackage implements Parcelable{ |
|
34 |
private String url_without_suffix; |
|
35 |
private String pathToStore; |
|
36 |
private String representation; |
|
37 |
private String description; |
|
38 |
private int versionNumber; |
|
39 |
private final Status status; |
|
40 |
private int uniqueId; |
|
41 |
||
42 |
||
43 |
public DownloadPackage(Parcel src){ |
|
44 |
url_without_suffix = src.readString(); |
|
45 |
pathToStore = src.readString(); |
|
46 |
representation = src.readString(); |
|
47 |
versionNumber = src.readInt(); |
|
48 |
status = Status.values()[src.readInt()]; |
|
49 |
description = src.readString(); |
|
50 |
uniqueId = src.readInt(); |
|
51 |
} |
|
52 |
||
53 |
public DownloadPackage(Context c, String _url_without_suffix, String path, int version, String _representation, String _description, int _uniqueId){ |
|
54 |
url_without_suffix = _url_without_suffix; |
|
55 |
pathToStore = path; |
|
56 |
representation = _representation; |
|
57 |
versionNumber = version; |
|
58 |
description = _description; |
|
59 |
uniqueId = _uniqueId; |
|
60 |
||
61 |
||
62 |
//determine if the user has already downloaded this version |
|
63 |
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(c); |
|
64 |
int currentVersion = sharedPref.getInt(representation, -1); |
|
65 |
if(currentVersion == versionNumber) status = Status.CURRENTVERSION; |
|
66 |
else if (currentVersion < versionNumber) status = Status.NEWERVERSION; |
|
67 |
else status = Status.OLDERVERSION; |
|
68 |
} |
|
69 |
||
70 |
public Status getStatus(){ |
|
71 |
return status; |
|
72 |
} |
|
73 |
||
74 |
public String getURL(){ |
|
75 |
return url_without_suffix; |
|
76 |
} |
|
77 |
||
78 |
public String getPathToStore(){ |
|
79 |
return pathToStore; |
|
80 |
} |
|
81 |
||
82 |
public String toString(){ |
|
83 |
return representation; |
|
84 |
} |
|
85 |
||
86 |
public int describeContents() { |
|
87 |
return 0; |
|
88 |
} |
|
89 |
public int getId(){ |
|
90 |
return uniqueId; |
|
91 |
} |
|
92 |
||
93 |
public void writeToParcel(Parcel dest, int flags) { |
|
94 |
dest.writeString(url_without_suffix); |
|
95 |
dest.writeString(pathToStore); |
|
96 |
dest.writeString(representation); |
|
97 |
dest.writeInt(versionNumber); |
|
98 |
dest.writeInt(status.ordinal()); |
|
99 |
dest.writeString(description); |
|
100 |
} |
|
101 |
||
102 |
public static final Parcelable.Creator<DownloadPackage> CREATOR = new Parcelable.Creator<DownloadPackage>() { |
|
103 |
public DownloadPackage createFromParcel(Parcel source) { |
|
104 |
return new DownloadPackage(source); |
|
105 |
} |
|
106 |
public DownloadPackage[] newArray(int size) { |
|
107 |
return new DownloadPackage[size]; |
|
108 |
} |
|
109 |
}; |
|
110 |
||
111 |
/* |
|
112 |
* We enter with a XmlPullParser.Start_tag with name "task" |
|
113 |
*/ |
|
114 |
public static DownloadPackage getTaskFromXML(Context c, XmlPullParser xmlPuller) throws XmlPullParserException, IOException{ |
|
115 |
String url = null; |
|
116 |
String path = null; |
|
117 |
String representation = null; |
|
118 |
String description = null; |
|
119 |
int uniqueId = -1; |
|
120 |
int version = -1; |
|
121 |
||
122 |
int eventType = DownloadPackage.getEventType(xmlPuller);//get the next token, should be a start tag |
|
123 |
while(eventType != XmlPullParser.END_DOCUMENT){ |
|
124 |
switch(eventType){ |
|
125 |
case XmlPullParser.START_TAG: |
|
126 |
String name = xmlPuller.getName().toLowerCase(); |
|
127 |
if(DownloadPackage.getEventType(xmlPuller) == XmlPullParser.TEXT){ |
|
128 |
String text = xmlPuller.getText().trim(); |
|
129 |
if(name.equals("url")){ |
|
130 |
url = text; |
|
131 |
}else if(name.equals("version")){ |
|
132 |
try{ |
|
133 |
version = Integer.parseInt(text); |
|
134 |
}catch (NumberFormatException e){ |
|
135 |
e.printStackTrace(); |
|
136 |
version = -1; |
|
137 |
} |
|
138 |
}else if(name.equals("path")){ |
|
7508
763d3961400b
Hedgeroid: Frantic scrabbling toward the deadline
Medo <smaxein@googlemail.com>
parents:
6842
diff
changeset
|
139 |
path = FileUtils.getDataPath(c) + text; |
6350 | 140 |
}else if(name.equals("representation")){ |
141 |
representation = text; |
|
142 |
}else if(name.equals("description")){ |
|
143 |
description = text; |
|
144 |
}else if(name.equals("uniqueid")){ |
|
145 |
try{ |
|
146 |
uniqueId = Integer.parseInt(text); |
|
147 |
}catch (NumberFormatException e){ |
|
148 |
e.printStackTrace(); |
|
149 |
version = -1; |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
DownloadPackage.getEventType(xmlPuller);//endtag |
|
154 |
break; |
|
155 |
case XmlPullParser.END_TAG: |
|
156 |
if(xmlPuller.getName().toLowerCase().equals("task") && url != null && path != null && version != -1 && representation != null){ |
|
157 |
return new DownloadPackage(c, url, path, version, representation, description, uniqueId); |
|
158 |
}else{ |
|
159 |
throw new XmlPullParserException("XML download parsing: missing tags"); |
|
160 |
} |
|
161 |
case XmlPullParser.TEXT: |
|
162 |
throw new XmlPullParserException("Wrong tag recieved got TEXT : " + xmlPuller.getText()); |
|
163 |
default: |
|
164 |
throw new XmlPullParserException("Wrong tag recieved got: " + eventType); |
|
165 |
} |
|
166 |
eventType = DownloadPackage.getEventType(xmlPuller); |
|
167 |
} |
|
168 |
throw new XmlPullParserException("Xml: unexpected endofdocument tag"); |
|
169 |
} |
|
170 |
||
171 |
/** |
|
172 |
* Skips whitespaces.. |
|
173 |
*/ |
|
174 |
private static int getEventType(XmlPullParser xmlPuller)throws XmlPullParserException, IOException{ |
|
175 |
int eventType = xmlPuller.next(); |
|
176 |
while(eventType == XmlPullParser.TEXT && xmlPuller.isWhitespace()){ |
|
177 |
eventType = xmlPuller.next(); |
|
178 |
} |
|
179 |
return eventType; |
|
180 |
} |
|
181 |
} |
|
182 |
||
183 |
enum Status{ |
|
184 |
CURRENTVERSION, NEWERVERSION, OLDERVERSION; |
|
185 |
} |