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