Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 1 | /* |
| 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 Pascoal | 7bf83a0 | 2014-10-13 18:30:18 +0100 | [diff] [blame] | 17 | package com.fairphone.updater.data; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 18 | |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
Filipe Gonçalves | ad527aa | 2015-01-12 15:13:35 +0000 | [diff] [blame] | 21 | import android.os.Build; |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 22 | import android.os.Environment; |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 23 | import android.util.Log; |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 24 | import android.util.Pair; |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 25 | |
Jose Pascoal | 8a31c8d | 2014-10-17 17:05:29 +0100 | [diff] [blame] | 26 | import com.fairphone.updater.R; |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 27 | import com.fairphone.updater.tools.Utils; |
| 28 | |
| 29 | import org.xmlpull.v1.XmlPullParser; |
| 30 | import org.xmlpull.v1.XmlPullParserException; |
| 31 | import org.xmlpull.v1.XmlPullParserFactory; |
| 32 | |
| 33 | import java.io.File; |
| 34 | import java.io.FileInputStream; |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 35 | import java.io.FileNotFoundException; |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 36 | import java.io.IOException; |
| 37 | import java.io.InputStreamReader; |
| 38 | import java.util.Locale; |
Jose Pascoal | 8a31c8d | 2014-10-17 17:05:29 +0100 | [diff] [blame] | 39 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 40 | public class VersionParserHelper |
| 41 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 42 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 43 | private static final String TAG = VersionParserHelper.class.getSimpleName(); |
| 44 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 45 | private static final String CURRENT_VERSION_NUMBER = "fairphone.ota.version.number"; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 46 | 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çalves | 4c204d8 | 2015-01-23 15:09:19 +0000 | [diff] [blame] | 48 | private static final String CURRENT_BETA_STATUS = "fairphone.ota.beta"; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 49 | 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çalves | bf5e08c | 2014-12-15 18:02:17 +0000 | [diff] [blame] | 51 | private static final String CURRENT_VERSION_BUILD_DATE = "ro.build.date.utc"; |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 52 | private static final String CURRENT_VERSION_ID = "ro.build.version.incremental"; // for FP2 |
Maarten Derks | bb28776 | 2015-10-12 17:22:45 +0200 | [diff] [blame] | 53 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 54 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 55 | private static Version version; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 56 | public static Version getDeviceVersion(Context context) |
| 57 | { |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 58 | if (version == null){ |
| 59 | Version versionBuilder = new Version(); |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 60 | String[] supportedDevices = context.getResources().getString(R.string.knownFPDevices).split(";"); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 61 | String modelWithoutSpaces = Build.MODEL.replaceAll("\\s", ""); |
| 62 | boolean knownFPDevice = false; |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 63 | for (String device : supportedDevices) { |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 64 | knownFPDevice = knownFPDevice || device.equals(modelWithoutSpaces); |
| 65 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 66 | |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 67 | 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)); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 90 | } |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 91 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 92 | 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 Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 96 | |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 97 | Version versionData = UpdaterData.getInstance().getVersion(versionBuilder.getImageType(), versionBuilder.getId()); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 98 | versionBuilder.setThumbnailLink(versionData != null ? versionData.getThumbnailLink() : ""); |
| 99 | versionBuilder.setReleaseNotes(Locale.getDefault().getLanguage(), versionData != null ? versionData.getReleaseNotes(Locale.getDefault().getLanguage()) : ""); |
| 100 | version = versionBuilder; |
| 101 | } |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 102 | |
| 103 | return version; |
| 104 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 105 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 106 | private static String getSystemData(Context context, String property, boolean useDefaults) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 107 | { |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 108 | String result; |
| 109 | switch (property) { |
| 110 | case CURRENT_VERSION_NUMBER: |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 111 | result = Utils.getprop(CURRENT_VERSION_NUMBER, useDefaults ? String.valueOf(context.getResources().getString(R.string.defaultVersionId)) : ""); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 112 | 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 Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 131 | case CURRENT_VERSION_ID: |
| 132 | result = Utils.getprop(CURRENT_VERSION_ID, useDefaults ? "" : ""); // TODO: define default value for fingerprint |
Maarten Derks | bb28776 | 2015-10-12 17:22:45 +0200 | [diff] [blame] | 133 | break; |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 134 | default: |
| 135 | result = ""; |
| 136 | break; |
| 137 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 138 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 139 | return result; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public static Version getLatestVersion(Context context) |
| 143 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 144 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 145 | Version latest = null; |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 146 | Resources resources = context.getResources(); |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 147 | 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 Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 152 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 153 | if (fis != null) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 154 | { |
| 155 | try |
| 156 | { |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 157 | latest = parseLatestXML(context, fis); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 158 | } catch (XmlPullParserException e) |
| 159 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 160 | Log.e(TAG, "Could not start the XML parser", e); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 161 | } catch (IOException e) |
| 162 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 163 | Log.e(TAG, "Invalid data in File", e); |
| 164 | // remove the files |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 165 | removeConfigFiles(context); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 168 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 169 | removeZipContents(context); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 170 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 171 | return latest; |
| 172 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 173 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 174 | // @formatter:off |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 175 | public enum XML_LEVEL_TAGS |
| 176 | { |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 177 | NONE, AOSP, FAIRPHONE, STORES |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 178 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 179 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 180 | public enum XML_TAGS |
| 181 | { |
Jose Pascoal | cdd8204 | 2015-02-06 13:04:26 +0000 | [diff] [blame] | 182 | /*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 Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 183 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 184 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 185 | // @formatter:on |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 186 | |
Filipe | 72b5a7f | 2015-09-01 08:59:54 +0000 | [diff] [blame] | 187 | 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 Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 193 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 194 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 195 | Version version = null; |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 196 | Store store = null; |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 197 | Pair<Version, Store> result; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 198 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 199 | UpdaterData update = UpdaterData.getInstance(); |
| 200 | update.resetUpdaterData(); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 201 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 202 | XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); |
| 203 | factory.setNamespaceAware(true); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 204 | |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 205 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 206 | XmlPullParser xpp = factory.newPullParser(); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 207 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 208 | xpp.setInput(new InputStreamReader(fis)); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 209 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 210 | int eventType = xpp.getEventType(); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 211 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 212 | XML_LEVEL_TAGS currentTag = XML_LEVEL_TAGS.NONE; |
| 213 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 214 | while (eventType != XmlPullParser.END_DOCUMENT) |
| 215 | { |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 216 | String tagName; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 217 | switch (eventType) |
| 218 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 219 | case XmlPullParser.START_DOCUMENT: |
| 220 | break; |
| 221 | case XmlPullParser.START_TAG: |
| 222 | tagName = xpp.getName(); |
| 223 | currentTag = getCurrentXmlElement(tagName, currentTag); |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 224 | result = readStartTag(xpp, currentTag, tagName, update, version, store); |
| 225 | version = result.first; |
| 226 | store = result.second; |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 227 | break; |
| 228 | case XmlPullParser.END_TAG: |
| 229 | tagName = xpp.getName(); |
| 230 | currentTag = getCurrentXmlElement(tagName, currentTag); |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 231 | result = readEndTag(currentTag, tagName, update, version, store); |
| 232 | version = result.first; |
| 233 | store = result.second; |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 234 | break; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 235 | default: |
| 236 | break; |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | eventType = xpp.next(); |
| 240 | } |
Jose Pascoal | ad4ea57 | 2014-12-16 20:31:48 +0000 | [diff] [blame] | 241 | fis.close(); |
| 242 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 243 | removeZipContents(context); |
Filipe Gonçalves | ad527aa | 2015-01-12 15:13:35 +0000 | [diff] [blame] | 244 | return update.getLatestVersion(getSystemData(context, CURRENT_VERSION_IMAGE_TYPE, true)); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 245 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 246 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 247 | private static Pair<Version, Store> readStartTag(XmlPullParser xpp, XML_LEVEL_TAGS currentTag, String tagName, UpdaterData update, Version version, Store store) throws XmlPullParserException, IOException |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 248 | { |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 249 | Version updateVersion = null; |
| 250 | Store updateStore = null; |
| 251 | switch (currentTag) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 252 | { |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 253 | 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 Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 284 | } |
Jose Pascoal | ad4ea57 | 2014-12-16 20:31:48 +0000 | [diff] [blame] | 285 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 286 | 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 Pascoal | ad4ea57 | 2014-12-16 20:31:48 +0000 | [diff] [blame] | 366 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 367 | if (tagName.equalsIgnoreCase(XML_TAGS.VERSION.name())) |
| 368 | { |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 369 | updateVersion.setId(xpp.getAttributeValue(0)); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 370 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 371 | else if (tagName.equalsIgnoreCase(XML_TAGS.ANDROID_VERSION.name())) |
| 372 | { |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 373 | updateVersion.setAndroidVersion(xpp.nextText()); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 374 | } |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 375 | else if (tagName.equalsIgnoreCase(XML_TAGS.DEPENDENCIES.name())) |
| 376 | { |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 377 | updateVersion.setVersionDependencies(xpp.nextText()); |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 378 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 379 | else if (tagName.equalsIgnoreCase(XML_TAGS.ERASE_DATA_WARNING.name())) |
| 380 | { |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 381 | updateVersion.setEraseAllPartitionWarning(); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 384 | return updateVersion; |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 385 | } |
Jose Pascoal | ad4ea57 | 2014-12-16 20:31:48 +0000 | [diff] [blame] | 386 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 387 | private static Store readStore(Store store, XmlPullParser xpp, String tagName) throws XmlPullParserException, IOException |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 388 | { |
| 389 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 390 | Store updateStore; |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 391 | if (store == null) |
| 392 | { |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 393 | updateStore = new Store(); |
| 394 | } |
| 395 | else |
| 396 | { |
| 397 | updateStore = new Store(store); |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 398 | } |
Jose Pascoal | ad4ea57 | 2014-12-16 20:31:48 +0000 | [diff] [blame] | 399 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 400 | readDownloadableItem(updateStore, xpp, tagName); |
Jose Pascoal | ad4ea57 | 2014-12-16 20:31:48 +0000 | [diff] [blame] | 401 | |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 402 | if (tagName.equalsIgnoreCase(XML_TAGS.STORE.name())) |
| 403 | { |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame^] | 404 | updateStore.setId(xpp.getAttributeValue(0)); |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 405 | } |
| 406 | else if (tagName.equalsIgnoreCase(XML_TAGS.SHOW_DISCLAIMER.name())) |
| 407 | { |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 408 | updateStore.setShowDisclaimer(); |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 411 | return updateStore; |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 412 | } |
| 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 Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 424 | 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 Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 449 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 450 | private static XML_LEVEL_TAGS getCurrentXmlElement(String tagName, XML_LEVEL_TAGS current) |
| 451 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 452 | XML_LEVEL_TAGS retval = current; |
| 453 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 454 | if (tagName.equalsIgnoreCase(XML_LEVEL_TAGS.AOSP.name())) |
| 455 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 456 | retval = XML_LEVEL_TAGS.AOSP; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 457 | } |
| 458 | else if (tagName.equalsIgnoreCase(XML_LEVEL_TAGS.FAIRPHONE.name())) |
| 459 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 460 | retval = XML_LEVEL_TAGS.FAIRPHONE; |
| 461 | } |
Jose Pascoal | 6860a9d | 2014-12-16 11:58:16 +0000 | [diff] [blame] | 462 | else if (tagName.equalsIgnoreCase(XML_LEVEL_TAGS.STORES.name())) |
| 463 | { |
| 464 | retval = XML_LEVEL_TAGS.STORES; |
| 465 | } |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 466 | return retval; |
| 467 | } |
| 468 | |
Filipe Gonçalves | 837e157 | 2015-01-23 17:52:35 +0000 | [diff] [blame] | 469 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 470 | |
Jose Pascoal | c2545cc | 2014-12-18 16:51:52 +0000 | [diff] [blame] | 471 | public static void removeConfigFiles(Context context) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 472 | { |
| 473 | Resources resources = context.getResources(); |
| 474 | String filePath = |
| 475 | Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + resources.getString(R.string.configFilename); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 476 | |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 477 | removeFile(filePath + resources.getString(R.string.config_zip)); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 478 | removeZipContents(context); |
| 479 | } |
| 480 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 481 | private static void removeZipContents(Context context) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 482 | { |
| 483 | Resources resources = context.getResources(); |
| 484 | String filePath = |
| 485 | Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + resources.getString(R.string.configFilename); |
| 486 | |
Jose Pascoal | fd3e43c | 2014-05-22 20:29:17 +0100 | [diff] [blame] | 487 | removeFile(filePath + resources.getString(R.string.config_xml)); |
| 488 | removeFile(filePath + resources.getString(R.string.config_sig)); |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 491 | private static void removeFile(String filePath) |
| 492 | { |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 493 | File file = new File(filePath); |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 494 | if (file.exists()) |
| 495 | { |
Jose Pascoal | 46fdb06 | 2015-02-05 18:59:32 +0000 | [diff] [blame] | 496 | final boolean notDeleted = !file.delete(); |
| 497 | if (notDeleted) { |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 498 | Log.d(TAG, "Couldn't delete file: " + file.getAbsolutePath()); |
| 499 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 502 | |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 503 | } |