blob: 2df0e43e541e9f183edf1070a0bfa016ce60fec1 [file] [log] [blame]
Kim Hansen086964d2013-12-10 11:33:28 +00001/*
2 * Copyright (C) 2013 Fairphone Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jose Pascoal7bf83a02014-10-13 18:30:18 +010017package com.fairphone.updater.data;
Kim Hansen086964d2013-12-10 11:33:28 +000018
Jose Pascoalfd3e43c2014-05-22 20:29:17 +010019import android.content.Context;
20import android.content.res.Resources;
Filipe Gonçalvesad527aa2015-01-12 15:13:35 +000021import android.os.Build;
Jose Pascoalfd3e43c2014-05-22 20:29:17 +010022import android.os.Environment;
Jose Pascoalfd3e43c2014-05-22 20:29:17 +010023import android.util.Log;
Jose Pascoal060ab282015-02-09 19:56:08 +000024import android.util.Pair;
Jose Pascoalfd3e43c2014-05-22 20:29:17 +010025
Jose Pascoal8a31c8d2014-10-17 17:05:29 +010026import com.fairphone.updater.R;
Filipe Gonçalves837e1572015-01-23 17:52:35 +000027import com.fairphone.updater.tools.Utils;
28
29import org.xmlpull.v1.XmlPullParser;
30import org.xmlpull.v1.XmlPullParserException;
31import org.xmlpull.v1.XmlPullParserFactory;
32
33import java.io.File;
34import java.io.FileInputStream;
Filipe72b5a7f2015-09-01 08:59:54 +000035import java.io.FileNotFoundException;
Filipe Gonçalves837e1572015-01-23 17:52:35 +000036import java.io.IOException;
37import java.io.InputStreamReader;
38import java.util.Locale;
Jose Pascoal8a31c8d2014-10-17 17:05:29 +010039
Jose Pascoal810950b2014-10-09 17:16:08 +010040public class VersionParserHelper
41{
Kim Hansen086964d2013-12-10 11:33:28 +000042
Jose Pascoal810950b2014-10-09 17:16:08 +010043 private static final String TAG = VersionParserHelper.class.getSimpleName();
44
Jose Pascoal1ee56e22014-05-21 16:58:20 +010045 private static final String CURRENT_VERSION_NUMBER = "fairphone.ota.version.number";
Jose Pascoal810950b2014-10-09 17:16:08 +010046 private static final String CURRENT_VERSION_NAME = "fairphone.ota.version.name";
47 private static final String CURRENT_VERSION_BUILD_NUMBER = "fairphone.ota.build_number";
Filipe Gonçalves4c204d82015-01-23 15:09:19 +000048 private static final String CURRENT_BETA_STATUS = "fairphone.ota.beta";
Jose Pascoal810950b2014-10-09 17:16:08 +010049 private static final String CURRENT_ANDROID_VERSION = "fairphone.ota.android_version";
50 private static final String CURRENT_VERSION_IMAGE_TYPE = "fairphone.ota.image_type";
Filipe Gonçalvesbf5e08c2014-12-15 18:02:17 +000051 private static final String CURRENT_VERSION_BUILD_DATE = "ro.build.date.utc";
Maarten Derksf17e6db2016-02-09 15:10:53 +010052 private static final String CURRENT_VERSION_ID = "ro.build.version.incremental"; // for FP2
Maarten Derksbb287762015-10-12 17:22:45 +020053
Jose Pascoal810950b2014-10-09 17:16:08 +010054
Filipe72b5a7f2015-09-01 08:59:54 +000055 private static Version version;
Jose Pascoal810950b2014-10-09 17:16:08 +010056 public static Version getDeviceVersion(Context context)
57 {
Filipe72b5a7f2015-09-01 08:59:54 +000058 if (version == null){
59 Version versionBuilder = new Version();
Maarten Derksf17e6db2016-02-09 15:10:53 +010060 String[] supportedDevices = context.getResources().getString(R.string.knownFPDevices).split(";");
Filipe72b5a7f2015-09-01 08:59:54 +000061 String modelWithoutSpaces = Build.MODEL.replaceAll("\\s", "");
62 boolean knownFPDevice = false;
Maarten Derksf17e6db2016-02-09 15:10:53 +010063 for (String device : supportedDevices) {
Filipe72b5a7f2015-09-01 08:59:54 +000064 knownFPDevice = knownFPDevice || device.equals(modelWithoutSpaces);
65 }
Kim Hansen086964d2013-12-10 11:33:28 +000066
Maarten Derksf17e6db2016-02-09 15:10:53 +010067 if(modelWithoutSpaces.equals(context.getResources().getString(R.string.FP2Model))) {
68 // FP2
69 try {
70 versionBuilder.setId(getSystemData(context, CURRENT_VERSION_ID, knownFPDevice));
71 } catch (NumberFormatException e) {
72 String defaultVersionId = context.getResources().getString(R.string.defaultVersionId);
73 Log.w(TAG, "Error parsing current version id. Defaulting to " + defaultVersionId + ": " + e.getLocalizedMessage());
74 versionBuilder.setId(defaultVersionId);
75 }
76 versionBuilder.setName(versionBuilder.getCurrentImageType());
77 versionBuilder.setBuildNumber(versionBuilder.getBuildNumberFromId());
78 } else {
79 // FP1(U)
80 try
81 {
82 versionBuilder.setId(getSystemData(context, CURRENT_VERSION_NUMBER, knownFPDevice) );
83 } catch (NumberFormatException e) {
84 String defaultVersionNumber = context.getResources().getString(R.string.defaultVersionId);
85 Log.w(TAG, "Error parsing current version number. Defaulting to " + defaultVersionNumber + ": " + e.getLocalizedMessage());
86 versionBuilder.setId(defaultVersionNumber);
87 }
88 versionBuilder.setName(getSystemData(context, CURRENT_VERSION_NAME, knownFPDevice));
89 versionBuilder.setBuildNumber(getSystemData(context, CURRENT_VERSION_BUILD_NUMBER, knownFPDevice));
Filipe72b5a7f2015-09-01 08:59:54 +000090 }
Maarten Derksf17e6db2016-02-09 15:10:53 +010091
Filipe72b5a7f2015-09-01 08:59:54 +000092 versionBuilder.setAndroidVersion(getSystemData(context, CURRENT_ANDROID_VERSION, knownFPDevice));
93 versionBuilder.setImageType(getSystemData(context, CURRENT_VERSION_IMAGE_TYPE, knownFPDevice));
94 versionBuilder.setReleaseDate(getSystemData(context, CURRENT_VERSION_BUILD_DATE, knownFPDevice));
95 versionBuilder.setBetaStatus(getSystemData(context, CURRENT_BETA_STATUS, knownFPDevice));
Jose Pascoal1ee56e22014-05-21 16:58:20 +010096
Maarten Derksf17e6db2016-02-09 15:10:53 +010097 Version versionData = UpdaterData.getInstance().getVersion(versionBuilder.getImageType(), versionBuilder.getId());
Filipe72b5a7f2015-09-01 08:59:54 +000098 versionBuilder.setThumbnailLink(versionData != null ? versionData.getThumbnailLink() : "");
99 versionBuilder.setReleaseNotes(Locale.getDefault().getLanguage(), versionData != null ? versionData.getReleaseNotes(Locale.getDefault().getLanguage()) : "");
100 version = versionBuilder;
101 }
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100102
103 return version;
104 }
Kim Hansen086964d2013-12-10 11:33:28 +0000105
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000106 private static String getSystemData(Context context, String property, boolean useDefaults)
Jose Pascoal810950b2014-10-09 17:16:08 +0100107 {
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000108 String result;
109 switch (property) {
110 case CURRENT_VERSION_NUMBER:
Maarten Derksf17e6db2016-02-09 15:10:53 +0100111 result = Utils.getprop(CURRENT_VERSION_NUMBER, useDefaults ? String.valueOf(context.getResources().getString(R.string.defaultVersionId)) : "");
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000112 break;
113 case CURRENT_VERSION_NAME:
114 result = Utils.getprop(CURRENT_VERSION_NAME, useDefaults ? context.getResources().getString(R.string.defaultVersionName) : "");
115 break;
116 case CURRENT_ANDROID_VERSION:
117 result = Utils.getprop(CURRENT_ANDROID_VERSION, useDefaults ? context.getResources().getString(R.string.defaultAndroidVersionNumber) : "");
118 break;
119 case CURRENT_VERSION_BUILD_NUMBER:
120 result = Utils.getprop(CURRENT_VERSION_BUILD_NUMBER, useDefaults ? context.getResources().getString(R.string.defaultBuildNumber) : "");
121 break;
122 case CURRENT_VERSION_IMAGE_TYPE:
123 result = Utils.getprop(CURRENT_VERSION_IMAGE_TYPE, useDefaults ? context.getResources().getString(R.string.defaultImageType) : "");
124 break;
125 case CURRENT_VERSION_BUILD_DATE:
126 result = Utils.getprop(CURRENT_VERSION_BUILD_DATE, useDefaults ? context.getResources().getString(R.string.defaultBuildDate) : "");
127 break;
128 case CURRENT_BETA_STATUS:
129 result = Utils.getprop(CURRENT_BETA_STATUS, useDefaults ? context.getResources().getString(R.string.defaultBetaStatus) : "0");
130 break;
Maarten Derksf17e6db2016-02-09 15:10:53 +0100131 case CURRENT_VERSION_ID:
132 result = Utils.getprop(CURRENT_VERSION_ID, useDefaults ? "" : ""); // TODO: define default value for fingerprint
Maarten Derksbb287762015-10-12 17:22:45 +0200133 break;
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000134 default:
135 result = "";
136 break;
137 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100138
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000139 return result;
Jose Pascoal810950b2014-10-09 17:16:08 +0100140 }
141
142 public static Version getLatestVersion(Context context)
143 {
Kim Hansen086964d2013-12-10 11:33:28 +0000144
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100145 Version latest = null;
Jose Pascoalfd3e43c2014-05-22 20:29:17 +0100146 Resources resources = context.getResources();
Filipe72b5a7f2015-09-01 08:59:54 +0000147 FileInputStream fis = null;
148 try {
149 fis = context.openFileInput(resources.getString(R.string.configFilename) + resources.getString(R.string.config_xml));
150 } catch (FileNotFoundException e){
151 }
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100152
Filipe72b5a7f2015-09-01 08:59:54 +0000153 if (fis != null)
Jose Pascoal810950b2014-10-09 17:16:08 +0100154 {
155 try
156 {
Filipe72b5a7f2015-09-01 08:59:54 +0000157 latest = parseLatestXML(context, fis);
Jose Pascoal810950b2014-10-09 17:16:08 +0100158 } catch (XmlPullParserException e)
159 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100160 Log.e(TAG, "Could not start the XML parser", e);
Jose Pascoal810950b2014-10-09 17:16:08 +0100161 } catch (IOException e)
162 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100163 Log.e(TAG, "Invalid data in File", e);
164 // remove the files
Jose Pascoalc2545cc2014-12-18 16:51:52 +0000165 removeConfigFiles(context);
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100166 }
167 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100168
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100169 removeZipContents(context);
Kim Hansen086964d2013-12-10 11:33:28 +0000170
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100171 return latest;
172 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100173
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100174 // @formatter:off
Jose Pascoal810950b2014-10-09 17:16:08 +0100175 public enum XML_LEVEL_TAGS
176 {
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000177 NONE, AOSP, FAIRPHONE, STORES
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100178 }
Kim Hansen086964d2013-12-10 11:33:28 +0000179
Jose Pascoal810950b2014-10-09 17:16:08 +0100180 public enum XML_TAGS
181 {
Jose Pascoalcdd82042015-02-06 13:04:26 +0000182 /*RELEASES,*/ AOSP, FAIRPHONE, VERSION, NAME, BUILD_NUMBER, ANDROID_VERSION, RELEASE_NOTES, RELEASE_DATE, MD5SUM, THUMBNAIL_LINK, UPDATE_LINK, ERASE_DATA_WARNING, DEPENDENCIES, /*STORES,*/ STORE, SHOW_DISCLAIMER
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100183 }
Kim Hansen086964d2013-12-10 11:33:28 +0000184
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100185 // @formatter:on
Kim Hansen086964d2013-12-10 11:33:28 +0000186
Filipe72b5a7f2015-09-01 08:59:54 +0000187 private static Version parseLatestXML(Context context, File latestFile) throws XmlPullParserException, IOException {
188 FileInputStream fis = new FileInputStream(latestFile);
189 return parseLatestXML(context, fis);
190 }
191
192 private static Version parseLatestXML(Context context, FileInputStream fis) throws XmlPullParserException, IOException
Jose Pascoal810950b2014-10-09 17:16:08 +0100193 {
Kim Hansen086964d2013-12-10 11:33:28 +0000194
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100195 Version version = null;
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000196 Store store = null;
Jose Pascoal060ab282015-02-09 19:56:08 +0000197 Pair<Version, Store> result;
Kim Hansen086964d2013-12-10 11:33:28 +0000198
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100199 UpdaterData update = UpdaterData.getInstance();
200 update.resetUpdaterData();
Kim Hansen086964d2013-12-10 11:33:28 +0000201
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100202 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
203 factory.setNamespaceAware(true);
Kim Hansen086964d2013-12-10 11:33:28 +0000204
Kim Hansen086964d2013-12-10 11:33:28 +0000205
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100206 XmlPullParser xpp = factory.newPullParser();
Kim Hansen086964d2013-12-10 11:33:28 +0000207
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100208 xpp.setInput(new InputStreamReader(fis));
Kim Hansen086964d2013-12-10 11:33:28 +0000209
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100210 int eventType = xpp.getEventType();
Kim Hansen086964d2013-12-10 11:33:28 +0000211
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100212 XML_LEVEL_TAGS currentTag = XML_LEVEL_TAGS.NONE;
213
Jose Pascoal810950b2014-10-09 17:16:08 +0100214 while (eventType != XmlPullParser.END_DOCUMENT)
215 {
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000216 String tagName;
Jose Pascoal810950b2014-10-09 17:16:08 +0100217 switch (eventType)
218 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100219 case XmlPullParser.START_DOCUMENT:
220 break;
221 case XmlPullParser.START_TAG:
222 tagName = xpp.getName();
223 currentTag = getCurrentXmlElement(tagName, currentTag);
Jose Pascoal060ab282015-02-09 19:56:08 +0000224 result = readStartTag(xpp, currentTag, tagName, update, version, store);
225 version = result.first;
226 store = result.second;
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100227 break;
228 case XmlPullParser.END_TAG:
229 tagName = xpp.getName();
230 currentTag = getCurrentXmlElement(tagName, currentTag);
Jose Pascoal060ab282015-02-09 19:56:08 +0000231 result = readEndTag(currentTag, tagName, update, version, store);
232 version = result.first;
233 store = result.second;
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100234 break;
Jose Pascoal40916302015-02-06 18:43:47 +0000235 default:
236 break;
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100237 }
238
239 eventType = xpp.next();
240 }
Jose Pascoalad4ea572014-12-16 20:31:48 +0000241 fis.close();
242
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100243 removeZipContents(context);
Filipe Gonçalvesad527aa2015-01-12 15:13:35 +0000244 return update.getLatestVersion(getSystemData(context, CURRENT_VERSION_IMAGE_TYPE, true));
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100245 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100246
Jose Pascoal060ab282015-02-09 19:56:08 +0000247 private static Pair<Version, Store> readStartTag(XmlPullParser xpp, XML_LEVEL_TAGS currentTag, String tagName, UpdaterData update, Version version, Store store) throws XmlPullParserException, IOException
Jose Pascoal810950b2014-10-09 17:16:08 +0100248 {
Jose Pascoal060ab282015-02-09 19:56:08 +0000249 Version updateVersion = null;
250 Store updateStore = null;
251 switch (currentTag)
Jose Pascoal810950b2014-10-09 17:16:08 +0100252 {
Jose Pascoal060ab282015-02-09 19:56:08 +0000253 case AOSP:
254 if (tagName.equalsIgnoreCase(XML_TAGS.AOSP.name()))
255 {
256 update.setLatestAOSPVersionNumber(xpp.getAttributeValue(0));
257 }
258 updateVersion = readVersion(version, xpp, tagName);
259 updateVersion.setImageType(Version.IMAGE_TYPE_AOSP);
260 break;
261 case FAIRPHONE:
262 if (tagName.equalsIgnoreCase(XML_TAGS.FAIRPHONE.name()))
263 {
264 update.setLatestFairphoneVersionNumber(xpp.getAttributeValue(0));
265 }
266 updateVersion = readVersion(version, xpp, tagName);
267 updateVersion.setImageType(Version.IMAGE_TYPE_FAIRPHONE);
268 break;
269 case STORES:
270 updateStore = readStore(store, xpp, tagName);
271 break;
272 case NONE:
273 default:
274 if(version != null)
275 {
276 updateVersion = new Version(version);
277 }
278
279 if(store != null)
280 {
281 updateStore = new Store(store);
282 }
283 break;
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100284 }
Jose Pascoalad4ea572014-12-16 20:31:48 +0000285
Jose Pascoal060ab282015-02-09 19:56:08 +0000286 return new Pair<>(updateVersion, updateStore);
287 }
288
289 private static Pair<Version, Store> readEndTag(XML_LEVEL_TAGS currentTag, String tagName, UpdaterData update, Version version, Store store) {
290 Version updateVersion = null;
291 Store updateStore = null;
292 switch (currentTag)
293 {
294 case AOSP:
295 if (tagName.equalsIgnoreCase(XML_TAGS.VERSION.name()))
296 {
297 update.addAOSPVersion(version);
298 updateVersion = null;
299 }
300 else
301 {
302 if(version != null)
303 {
304 updateVersion = new Version(version);
305 }
306 }
307 break;
308 case FAIRPHONE:
309 if (tagName.equalsIgnoreCase(XML_TAGS.VERSION.name()))
310 {
311 update.addFairphoneVersion(version);
312 updateVersion = null;
313 }
314 else
315 {
316 if(version != null)
317 {
318 updateVersion = new Version(version);
319 }
320 }
321 break;
322 case STORES:
323 if (tagName.equalsIgnoreCase(XML_TAGS.STORE.name()))
324 {
325 update.addAppStore(store);
326 updateStore = null;
327 }
328 else
329 {
330 if(store != null)
331 {
332 updateStore = new Store(store);
333 }
334 }
335 break;
336 case NONE:
337 default:
338 if(version != null)
339 {
340 updateVersion = new Version(version);
341 }
342
343 if(store != null)
344 {
345 updateStore = new Store(store);
346 }
347 break;
348 }
349
350 return new Pair<>(updateVersion, updateStore);
351 }
352
353 private static Version readVersion(Version version, XmlPullParser xpp, String tagName) throws XmlPullParserException, IOException
354 {
355 Version updateVersion;
356 if (version == null)
357 {
358 updateVersion = new Version();
359 }
360 else
361 {
362 updateVersion = new Version(version);
363 }
364
365 readDownloadableItem(updateVersion, xpp, tagName);
Jose Pascoalad4ea572014-12-16 20:31:48 +0000366
Jose Pascoal810950b2014-10-09 17:16:08 +0100367 if (tagName.equalsIgnoreCase(XML_TAGS.VERSION.name()))
368 {
Maarten Derksf17e6db2016-02-09 15:10:53 +0100369 updateVersion.setId(xpp.getAttributeValue(0));
Jose Pascoal810950b2014-10-09 17:16:08 +0100370 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100371 else if (tagName.equalsIgnoreCase(XML_TAGS.ANDROID_VERSION.name()))
372 {
Jose Pascoal060ab282015-02-09 19:56:08 +0000373 updateVersion.setAndroidVersion(xpp.nextText());
Jose Pascoal810950b2014-10-09 17:16:08 +0100374 }
Jose Pascoal40916302015-02-06 18:43:47 +0000375 else if (tagName.equalsIgnoreCase(XML_TAGS.DEPENDENCIES.name()))
376 {
Jose Pascoal060ab282015-02-09 19:56:08 +0000377 updateVersion.setVersionDependencies(xpp.nextText());
Jose Pascoal40916302015-02-06 18:43:47 +0000378 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100379 else if (tagName.equalsIgnoreCase(XML_TAGS.ERASE_DATA_WARNING.name()))
380 {
Jose Pascoal060ab282015-02-09 19:56:08 +0000381 updateVersion.setEraseAllPartitionWarning();
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100382 }
383
Jose Pascoal060ab282015-02-09 19:56:08 +0000384 return updateVersion;
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100385 }
Jose Pascoalad4ea572014-12-16 20:31:48 +0000386
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000387 private static Store readStore(Store store, XmlPullParser xpp, String tagName) throws XmlPullParserException, IOException
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000388 {
389
Jose Pascoal060ab282015-02-09 19:56:08 +0000390 Store updateStore;
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000391 if (store == null)
392 {
Jose Pascoal060ab282015-02-09 19:56:08 +0000393 updateStore = new Store();
394 }
395 else
396 {
397 updateStore = new Store(store);
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000398 }
Jose Pascoalad4ea572014-12-16 20:31:48 +0000399
Jose Pascoal060ab282015-02-09 19:56:08 +0000400 readDownloadableItem(updateStore, xpp, tagName);
Jose Pascoalad4ea572014-12-16 20:31:48 +0000401
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000402 if (tagName.equalsIgnoreCase(XML_TAGS.STORE.name()))
403 {
Maarten Derksf17e6db2016-02-09 15:10:53 +0100404 updateStore.setId(xpp.getAttributeValue(0));
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000405 }
406 else if (tagName.equalsIgnoreCase(XML_TAGS.SHOW_DISCLAIMER.name()))
407 {
Jose Pascoal060ab282015-02-09 19:56:08 +0000408 updateStore.setShowDisclaimer();
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000409 }
410
Jose Pascoal060ab282015-02-09 19:56:08 +0000411 return updateStore;
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000412 }
413
414 private static void readDownloadableItem(DownloadableItem item, XmlPullParser xpp, String tagName) throws XmlPullParserException, IOException
415 {
416 if (tagName.equalsIgnoreCase(XML_TAGS.NAME.name()))
417 {
418 item.setName(xpp.nextText());
419 }
420 else if (tagName.equalsIgnoreCase(XML_TAGS.BUILD_NUMBER.name()))
421 {
422 item.setBuildNumber(xpp.nextText());
423 }
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000424 else if (tagName.equalsIgnoreCase(XML_TAGS.RELEASE_NOTES.name()))
425 {
426 item.setReleaseNotes(Version.DEFAULT_NOTES_LANG, xpp.nextText());
427 }
428 else if (tagName.equalsIgnoreCase(XML_TAGS.RELEASE_NOTES.name() + "_" + Locale.getDefault().getLanguage()))
429 {
430 item.setReleaseNotes(Locale.getDefault().getLanguage(), xpp.nextText());
431 }
432 else if (tagName.equalsIgnoreCase(XML_TAGS.RELEASE_DATE.name()))
433 {
434 item.setReleaseDate(xpp.nextText());
435 }
436 else if (tagName.equalsIgnoreCase(XML_TAGS.MD5SUM.name()))
437 {
438 item.setMd5Sum(xpp.nextText());
439 }
440 else if (tagName.equalsIgnoreCase(XML_TAGS.THUMBNAIL_LINK.name()))
441 {
442 item.setThumbnailLink(xpp.nextText());
443 }
444 else if (tagName.equalsIgnoreCase(XML_TAGS.UPDATE_LINK.name()))
445 {
446 item.setDownloadLink(xpp.nextText());
447 }
448 }
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100449
Jose Pascoal810950b2014-10-09 17:16:08 +0100450 private static XML_LEVEL_TAGS getCurrentXmlElement(String tagName, XML_LEVEL_TAGS current)
451 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100452 XML_LEVEL_TAGS retval = current;
453
Jose Pascoal810950b2014-10-09 17:16:08 +0100454 if (tagName.equalsIgnoreCase(XML_LEVEL_TAGS.AOSP.name()))
455 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100456 retval = XML_LEVEL_TAGS.AOSP;
Jose Pascoal810950b2014-10-09 17:16:08 +0100457 }
458 else if (tagName.equalsIgnoreCase(XML_LEVEL_TAGS.FAIRPHONE.name()))
459 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100460 retval = XML_LEVEL_TAGS.FAIRPHONE;
461 }
Jose Pascoal6860a9d2014-12-16 11:58:16 +0000462 else if (tagName.equalsIgnoreCase(XML_LEVEL_TAGS.STORES.name()))
463 {
464 retval = XML_LEVEL_TAGS.STORES;
465 }
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100466 return retval;
467 }
468
Filipe Gonçalves837e1572015-01-23 17:52:35 +0000469
Jose Pascoal810950b2014-10-09 17:16:08 +0100470
Jose Pascoalc2545cc2014-12-18 16:51:52 +0000471 public static void removeConfigFiles(Context context)
Jose Pascoal810950b2014-10-09 17:16:08 +0100472 {
473 Resources resources = context.getResources();
474 String filePath =
475 Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + resources.getString(R.string.configFilename);
Kim Hansen086964d2013-12-10 11:33:28 +0000476
Jose Pascoalfd3e43c2014-05-22 20:29:17 +0100477 removeFile(filePath + resources.getString(R.string.config_zip));
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100478 removeZipContents(context);
479 }
480
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000481 private static void removeZipContents(Context context)
Jose Pascoal810950b2014-10-09 17:16:08 +0100482 {
483 Resources resources = context.getResources();
484 String filePath =
485 Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + resources.getString(R.string.configFilename);
486
Jose Pascoalfd3e43c2014-05-22 20:29:17 +0100487 removeFile(filePath + resources.getString(R.string.config_xml));
488 removeFile(filePath + resources.getString(R.string.config_sig));
Kim Hansen086964d2013-12-10 11:33:28 +0000489 }
490
Jose Pascoal810950b2014-10-09 17:16:08 +0100491 private static void removeFile(String filePath)
492 {
Kim Hansen086964d2013-12-10 11:33:28 +0000493 File file = new File(filePath);
Jose Pascoal810950b2014-10-09 17:16:08 +0100494 if (file.exists())
495 {
Jose Pascoal46fdb062015-02-05 18:59:32 +0000496 final boolean notDeleted = !file.delete();
497 if (notDeleted) {
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000498 Log.d(TAG, "Couldn't delete file: " + file.getAbsolutePath());
499 }
Kim Hansen086964d2013-12-10 11:33:28 +0000500 }
501 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100502
Kim Hansen086964d2013-12-10 11:33:28 +0000503}