blob: 8f053ae2cfb19155edf56d974a2791a595c49691 [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
Kim Hansen086964d2013-12-10 11:33:28 +000019import android.content.res.Resources;
20import android.text.TextUtils;
Jose Pascoal40916302015-02-06 18:43:47 +000021import android.util.Log;
Kim Hansen086964d2013-12-10 11:33:28 +000022
Jose Pascoal8a31c8d2014-10-17 17:05:29 +010023import com.fairphone.updater.R;
24
Filipe Gonçalves4c204d82015-01-23 15:09:19 +000025import java.util.ArrayList;
Jose Pascoal40916302015-02-06 18:43:47 +000026import java.util.List;
Maarten Derks4bb57892016-01-26 10:52:53 +010027import java.util.regex.Matcher;
28import java.util.regex.Pattern;
Filipe Gonçalves4c204d82015-01-23 15:09:19 +000029
Tiago Costa6d360e22014-12-15 14:32:51 +000030public class Version extends DownloadableItem implements Comparable<Version>
Jose Pascoal810950b2014-10-09 17:16:08 +010031{
Jose Pascoal40916302015-02-06 18:43:47 +000032 private static final String TAG = Version.class.getSimpleName();
33
34 private static final String DEPENDENCY_SEPARATOR = ",";
Kim Hansen086964d2013-12-10 11:33:28 +000035
Jose Pascoal1ee56e22014-05-21 16:58:20 +010036 public static final String IMAGE_TYPE_AOSP = "AOSP";
Kim Hansen086964d2013-12-10 11:33:28 +000037
Jose Pascoal1ee56e22014-05-21 16:58:20 +010038 public static final String IMAGE_TYPE_FAIRPHONE = "FAIRPHONE";
Kim Hansen086964d2013-12-10 11:33:28 +000039
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000040 private String mImageType;
Kim Hansen086964d2013-12-10 11:33:28 +000041
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000042 private String mAndroidVersion;
Kim Hansen086964d2013-12-10 11:33:28 +000043
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000044 private String mBetaStatus;
Filipe Gonçalves4c204d82015-01-23 15:09:19 +000045
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000046 private boolean mErasePartitionsWarning;
Jose Pascoal810950b2014-10-09 17:16:08 +010047
Jose Pascoalcfc2dd42015-02-09 18:00:05 +000048 private final List<Integer> mDependencies;
Jose Pascoal40916302015-02-06 18:43:47 +000049
Jose Pascoal810950b2014-10-09 17:16:08 +010050 public Version()
51 {
Jose Pascoal40916302015-02-06 18:43:47 +000052 super();
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +000053 mDependencies = new ArrayList<>();
Jose Pascoal2666b892014-10-02 21:05:08 +010054 mAndroidVersion = "";
Jose Pascoal2666b892014-10-02 21:05:08 +010055 mImageType = IMAGE_TYPE_FAIRPHONE;
Jose Pascoalcfc2dd42015-02-09 18:00:05 +000056 mErasePartitionsWarning = false;
Filipe Gonçalves4c204d82015-01-23 15:09:19 +000057 mBetaStatus = "";
Jose Pascoal1ee56e22014-05-21 16:58:20 +010058 }
Kim Hansen086964d2013-12-10 11:33:28 +000059
Jose Pascoal060ab282015-02-09 19:56:08 +000060 public Version(Version other)
61 {
62 super(other);
63 mDependencies = other.mDependencies;
64 mAndroidVersion = other.mAndroidVersion;
65 mImageType = other.mImageType;
66 mErasePartitionsWarning = other.hasEraseAllPartitionWarning();
67 mBetaStatus = other.mBetaStatus;
68 }
69
Jose Pascoalcfc2dd42015-02-09 18:00:05 +000070 public void setEraseAllPartitionWarning()
Jose Pascoal810950b2014-10-09 17:16:08 +010071 {
Jose Pascoalcfc2dd42015-02-09 18:00:05 +000072 mErasePartitionsWarning = true;
Tiago Costa554587b2014-06-26 18:39:40 +010073 }
Jose Pascoal810950b2014-10-09 17:16:08 +010074
Tiago Costa6d360e22014-12-15 14:32:51 +000075 public boolean hasEraseAllPartitionWarning()
Jose Pascoal810950b2014-10-09 17:16:08 +010076 {
Tiago Costa6d360e22014-12-15 14:32:51 +000077 return mErasePartitionsWarning;
Jose Pascoal1ee56e22014-05-21 16:58:20 +010078 }
Jose Pascoal9be8f0d2014-03-10 11:22:41 +000079
Tiago Costa6d360e22014-12-15 14:32:51 +000080 public void setAndroidVersion(String mAndroid)
Jose Pascoal810950b2014-10-09 17:16:08 +010081 {
Tiago Costa6d360e22014-12-15 14:32:51 +000082 this.mAndroidVersion = mAndroid;
Jose Pascoal1ee56e22014-05-21 16:58:20 +010083 }
84
Jose Pascoal810950b2014-10-09 17:16:08 +010085 public void setImageType(String imageType)
86 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +010087 mImageType = imageType;
88 }
89
Jose Pascoal810950b2014-10-09 17:16:08 +010090 public String getImageType()
91 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +010092 return mImageType;
93 }
Jose Pascoal810950b2014-10-09 17:16:08 +010094
Filipe Gonçalves4c204d82015-01-23 15:09:19 +000095 public void setBetaStatus(String betaStatus)
96 {
97 mBetaStatus = betaStatus;
98 }
99
100 public String getBetaStatus()
101 {
102 return mBetaStatus;
103 }
104
Jose Pascoal810950b2014-10-09 17:16:08 +0100105 public String getImageTypeDescription(Resources resources)
106 {
Jose Pascoala8604492014-06-23 19:12:08 +0100107 return Version.getImageTypeDescription(mImageType, resources);
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100108 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100109
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000110 private static String getImageTypeDescription(String imageType, Resources resources)
Jose Pascoal810950b2014-10-09 17:16:08 +0100111 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100112 String description = resources.getString(R.string.fairphone);
Jose Pascoalaa579a82014-11-05 22:17:16 +0000113 if (!TextUtils.isEmpty(imageType))
Jose Pascoal810950b2014-10-09 17:16:08 +0100114 {
Jose Pascoalaa579a82014-11-05 22:17:16 +0000115 if (imageType.equalsIgnoreCase(IMAGE_TYPE_AOSP))
116 {
117 description = resources.getString(R.string.android);
118 }
119 if (imageType.equalsIgnoreCase(IMAGE_TYPE_FAIRPHONE))
120 {
121 description = resources.getString(R.string.fairphone);
122 }
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100123 }
124 return description;
125 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100126
Jose Pascoalcdd82042015-02-06 13:04:26 +0000127// --Commented out by Inspection START (06/02/2015 12:25):
128// public String getAndroidVersion(Resources resources)
129// {
130// String retVal = "";
131// if (!TextUtils.isEmpty(mAndroidVersion))
132// {
133// retVal = resources.getString(R.string.android) + " " + mAndroidVersion;
134// }
135// return retVal;
136// }
137// --Commented out by Inspection STOP (06/02/2015 12:25)
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000138
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100139 @Override
Filipe Gonçalvesb31d5862015-02-04 17:28:58 +0000140 public int compareTo(@SuppressWarnings("NullableProblems") Version another)
Jose Pascoal810950b2014-10-09 17:16:08 +0100141 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100142 int retVal;
Jose Pascoal810950b2014-10-09 17:16:08 +0100143 if (another != null)
144 {
Maarten Derksf17e6db2016-02-09 15:10:53 +0100145 if (!this.getId().equals(another.getId()) && this.mImageType.equalsIgnoreCase(another.mImageType))
Jose Pascoal810950b2014-10-09 17:16:08 +0100146 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100147 retVal = 1;
Jose Pascoal810950b2014-10-09 17:16:08 +0100148 }
Maarten Derksf17e6db2016-02-09 15:10:53 +0100149 else if (this.getId() == another.getId() && this.mImageType.equalsIgnoreCase(another.mImageType))
Jose Pascoal810950b2014-10-09 17:16:08 +0100150 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100151 retVal = 0;
Jose Pascoal810950b2014-10-09 17:16:08 +0100152 }
153 else
154 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100155 retVal = -1;
156 }
Jose Pascoal810950b2014-10-09 17:16:08 +0100157 }
158 else
159 {
Jose Pascoal1ee56e22014-05-21 16:58:20 +0100160 retVal = 1;
161 }
162 return retVal;
163 }
Tiago Costa6d360e22014-12-15 14:32:51 +0000164
Jose Pascoal40916302015-02-06 18:43:47 +0000165 public void setVersionDependencies(String dependencyList)
166 {
167 if (TextUtils.isEmpty(dependencyList))
168 {
169 mDependencies.clear();
170 }
171 else
172 {
173 String[] dependencies = dependencyList.split(DEPENDENCY_SEPARATOR);
174 for (String dependency : dependencies)
175 {
176 try
177 {
178 mDependencies.add(Integer.valueOf(dependency));
179 } catch (NumberFormatException e)
180 {
Jose Pascoalcfc2dd42015-02-09 18:00:05 +0000181 Log.e(TAG, "Invalid dependency: " + e.getLocalizedMessage());
Jose Pascoal40916302015-02-06 18:43:47 +0000182 }
183 }
184 }
185 }
186
Jose Pascoal060ab282015-02-09 19:56:08 +0000187// --Commented out by Inspection START (09/02/2015 19:47):
188// List<Integer> getVersionDependencies()
Jose Pascoal40916302015-02-06 18:43:47 +0000189// {
190// return mDependencies;
191// }
Jose Pascoal060ab282015-02-09 19:56:08 +0000192// --Commented out by Inspection STOP (09/02/2015 19:47)
Maarten Derks4bb57892016-01-26 10:52:53 +0100193
194 /**
Maarten Derksf17e6db2016-02-09 15:10:53 +0100195 * This method retrieves the int.int.int part of the fingerprint
196 * @return the version as string or empty string if no version was found
Maarten Derks4bb57892016-01-26 10:52:53 +0100197 */
Maarten Derksf17e6db2016-02-09 15:10:53 +0100198 public String getBuildNumberFromId(){
199 String id = getId();
200 Pattern pattern = Pattern.compile(".*?\\d+.*?(\\d+)(\\.)(\\d+)(\\.)(\\d+)", Pattern.CASE_INSENSITIVE | Pattern.DOTALL); // is this pattern sufficient?
201 Matcher matcher = pattern.matcher(id); // get version number from fingerprint
Maarten Derks4bb57892016-01-26 10:52:53 +0100202 if (matcher.find()) {
203 return matcher.group(1) + "." +matcher.group(3) + "." + matcher.group(5);
204 }
Maarten Derksf17e6db2016-02-09 15:10:53 +0100205 Log.d(TAG,String.format("Failed to determine version number from fingerprint: %s",id));
206 return ""; /* we don't know what version is here */
Maarten Derks4bb57892016-01-26 10:52:53 +0100207 }
208
209 /**
Maarten Derksf17e6db2016-02-09 15:10:53 +0100210 * This method constructs the current version type from the device fingerprint
211 * @return A human (english) version type to be displayed on the screen
Maarten Derks4bb57892016-01-26 10:52:53 +0100212 */
Maarten Derksf17e6db2016-02-09 15:10:53 +0100213 public String getCurrentImageType() {
214 String id = getId();
215 if(id.contains("gms")) {
216 return "Fairphone OS";
217 } else if (id.contains("sibon")) {
Maarten Derksc4e95202016-05-24 09:33:19 +0200218 return "Fairphone Open";
Maarten Derksf17e6db2016-02-09 15:10:53 +0100219 } else if (id.contains("AOSP+")) {
220 return "Fairphone Internal";
Maarten Derks4bb57892016-01-26 10:52:53 +0100221 } else {
Maarten Derksf17e6db2016-02-09 15:10:53 +0100222 // we do have a version but.. we don't know about it. return the full "number"
223 return id;
Maarten Derks4bb57892016-01-26 10:52:53 +0100224 }
225 }
Maarten Derksf17e6db2016-02-09 15:10:53 +0100226
227 public String getHumanReadableName() {
228 return getName() + " " + getBuildNumber();
229 }
Kim Hansen086964d2013-12-10 11:33:28 +0000230}