blob: 5334b7fdb8ba0c19da002869f4d4c27d687c9b32 [file] [log] [blame]
Maarten Derksdaf5ecb2016-06-07 15:51:53 +02001package com.fairphone.updater.data;
2
3import android.content.Context;
4import android.content.res.Resources;
5import android.os.Build;
6import android.os.Environment;
7import android.util.Log;
8
9import com.fairphone.updater.R;
10import com.fairphone.updater.tools.Utils;
11import com.fairphone.updater.tools.XmlParser;
12
13import org.xmlpull.v1.XmlPullParserException;
14
15import java.io.File;
16import java.io.FileInputStream;
17import java.io.FileNotFoundException;
18import java.io.IOException;
19import java.util.Locale;
20
21public class VersionParserHelper {
22
23 private static final String TAG = VersionParserHelper.class.getSimpleName();
24
25 private static final String CURRENT_VERSION_NUMBER = "fairphone.ota.version.number";
26 private static final String CURRENT_VERSION_NAME = "fairphone.ota.version.name";
27 private static final String CURRENT_VERSION_BUILD_NUMBER = "fairphone.ota.build_number";
28 private static final String CURRENT_VERSION_IMAGE_TYPE = "fairphone.ota.image_type";
29 private static final String CURRENT_VERSION_ID = "ro.build.version.incremental"; // for FP2
30
31
32 private static Version version;
33 public static Version getDeviceVersion(Context context)
34 {
35 if (version == null){
36 Version versionBuilder = new Version();
37 String[] supportedDevices = context.getResources().getString(R.string.knownFPDevices).split(";");
38 String modelWithoutSpaces = Build.MODEL.replaceAll("\\s", "");
39 boolean knownFPDevice = false;
40 for (String device : supportedDevices) {
41 knownFPDevice = knownFPDevice || device.equals(modelWithoutSpaces);
42 }
43
44 if(modelWithoutSpaces.equals(context.getResources().getString(R.string.FP2Model))) {
45 // FP2
46 try {
47 versionBuilder.setId(getSystemData(context, CURRENT_VERSION_ID, knownFPDevice));
48 } catch (NumberFormatException e) {
49 String defaultVersionId = context.getResources().getString(R.string.defaultVersionId);
50 Log.w(TAG, "Error parsing current version id. Defaulting to " + defaultVersionId + ": " + e.getLocalizedMessage());
51 versionBuilder.setId(defaultVersionId);
52 }
53 versionBuilder.setName(versionBuilder.getCurrentImageType());
54 versionBuilder.setBuildNumber(versionBuilder.getBuildNumberFromId());
55 } else {
56 // FP1(U)
57 try
58 {
59 versionBuilder.setId(getSystemData(context, CURRENT_VERSION_NUMBER, knownFPDevice) );
60 } catch (NumberFormatException e) {
61 String defaultVersionNumber = context.getResources().getString(R.string.defaultVersionId);
62 Log.w(TAG, "Error parsing current version number. Defaulting to " + defaultVersionNumber + ": " + e.getLocalizedMessage());
63 versionBuilder.setId(defaultVersionNumber);
64 }
65 versionBuilder.setName(getSystemData(context, CURRENT_VERSION_NAME, knownFPDevice));
66 versionBuilder.setBuildNumber(getSystemData(context, CURRENT_VERSION_BUILD_NUMBER, knownFPDevice));
67 }
68
69 versionBuilder.setImageType(getSystemData(context, CURRENT_VERSION_IMAGE_TYPE, knownFPDevice));
70
71 Version versionData = UpdaterData.getInstance().getVersion(versionBuilder.getImageType(), versionBuilder.getId());
72 versionBuilder.setThumbnailLink(versionData != null ? versionData.getThumbnailLink() : "");
73 versionBuilder.setReleaseNotes(Locale.getDefault().getLanguage(), versionData != null ? versionData.getReleaseNotes(Locale.getDefault().getLanguage()) : "");
74 version = versionBuilder;
75 }
76
77 return version;
78 }
79
80 private static String getSystemData(Context context, String property, boolean useDefaults)
81 {
82 String result;
83 switch (property) {
84 case CURRENT_VERSION_NUMBER:
85 result = Utils.getprop(CURRENT_VERSION_NUMBER, useDefaults ? String.valueOf(context.getResources().getString(R.string.defaultVersionId)) : "");
86 break;
87 case CURRENT_VERSION_NAME:
88 result = Utils.getprop(CURRENT_VERSION_NAME, useDefaults ? context.getResources().getString(R.string.defaultVersionName) : "");
89 break;
90 case CURRENT_VERSION_BUILD_NUMBER:
91 result = Utils.getprop(CURRENT_VERSION_BUILD_NUMBER, useDefaults ? context.getResources().getString(R.string.defaultBuildNumber) : "");
92 break;
93 case CURRENT_VERSION_IMAGE_TYPE:
94 result = Utils.getprop(CURRENT_VERSION_IMAGE_TYPE, useDefaults ? context.getResources().getString(R.string.defaultImageType) : "");
95 break;
96 case CURRENT_VERSION_ID:
97 result = Utils.getprop(CURRENT_VERSION_ID, useDefaults ? "" : ""); // TODO: define default value for fingerprint
98 break;
99 default:
100 result = "";
101 break;
102 }
103
104 return result;
105 }
106
107 public static Version getLatestVersion(Context context)
108 {
109
110 Version latest = null;
111 Resources resources = context.getResources();
112 FileInputStream fis = null;
113 try {
114 fis = context.openFileInput(resources.getString(R.string.configFilename) + resources.getString(R.string.config_xml));
115 } catch (FileNotFoundException e){
116 }
117
118 if (fis != null)
119 {
120 try
121 {
122 XmlParser xmlParser = new XmlParser();
123 UpdaterData updaterData = xmlParser.parse(fis);
124 latest = updaterData.getLatestVersion(getSystemData(context, CURRENT_VERSION_IMAGE_TYPE, true));
125 } catch (XmlPullParserException e)
126 {
127 Log.e(TAG, "Could not start the XML parser", e);
128 } catch (IOException e)
129 {
130 Log.e(TAG, "Invalid data in File", e);
131 // remove the files
132 removeConfigFiles(context);
133 }
134 }
135
136 removeZipContents(context);
137
138 return latest;
139 }
140
141 public static void removeConfigFiles(Context context)
142 {
143 Resources resources = context.getResources();
144 String filePath =
145 Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + resources.getString(R.string.configFilename);
146
147 removeFile(filePath + resources.getString(R.string.config_zip));
148 removeZipContents(context);
149 }
150
151 private static void removeZipContents(Context context)
152 {
153 Resources resources = context.getResources();
154 String filePath =
155 Environment.getExternalStorageDirectory() + resources.getString(R.string.updaterFolder) + resources.getString(R.string.configFilename);
156
157 removeFile(filePath + resources.getString(R.string.config_xml));
158 removeFile(filePath + resources.getString(R.string.config_sig));
159 }
160
161 private static void removeFile(String filePath)
162 {
163 File file = new File(filePath);
164 if (file.exists())
165 {
166 final boolean notDeleted = !file.delete();
167 if (notDeleted) {
168 Log.d(TAG, "Couldn't delete file: " + file.getAbsolutePath());
169 }
170 }
171 }
172}