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 | |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 19 | import android.content.res.Resources; |
| 20 | import android.text.TextUtils; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 21 | import android.util.Log; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 22 | |
Jose Pascoal | 8a31c8d | 2014-10-17 17:05:29 +0100 | [diff] [blame] | 23 | import com.fairphone.updater.R; |
| 24 | |
Filipe Gonçalves | 4c204d8 | 2015-01-23 15:09:19 +0000 | [diff] [blame] | 25 | import java.util.ArrayList; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 26 | import java.util.List; |
Maarten Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 27 | import java.util.regex.Matcher; |
| 28 | import java.util.regex.Pattern; |
Filipe Gonçalves | 4c204d8 | 2015-01-23 15:09:19 +0000 | [diff] [blame] | 29 | |
Tiago Costa | 6d360e2 | 2014-12-15 14:32:51 +0000 | [diff] [blame] | 30 | public class Version extends DownloadableItem implements Comparable<Version> |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 31 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 32 | private static final String TAG = Version.class.getSimpleName(); |
| 33 | |
| 34 | private static final String DEPENDENCY_SEPARATOR = ","; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 35 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 36 | public static final String IMAGE_TYPE_AOSP = "AOSP"; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 37 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 38 | public static final String IMAGE_TYPE_FAIRPHONE = "FAIRPHONE"; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 39 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 40 | private String mImageType; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 41 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 42 | private String mAndroidVersion; |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 43 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 44 | private String mBetaStatus; |
Filipe Gonçalves | 4c204d8 | 2015-01-23 15:09:19 +0000 | [diff] [blame] | 45 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 46 | private boolean mErasePartitionsWarning; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 47 | |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 48 | private final List<Integer> mDependencies; |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 49 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 50 | public Version() |
| 51 | { |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 52 | super(); |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 53 | mDependencies = new ArrayList<>(); |
Jose Pascoal | 2666b89 | 2014-10-02 21:05:08 +0100 | [diff] [blame] | 54 | mAndroidVersion = ""; |
Jose Pascoal | 2666b89 | 2014-10-02 21:05:08 +0100 | [diff] [blame] | 55 | mImageType = IMAGE_TYPE_FAIRPHONE; |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 56 | mErasePartitionsWarning = false; |
Filipe Gonçalves | 4c204d8 | 2015-01-23 15:09:19 +0000 | [diff] [blame] | 57 | mBetaStatus = ""; |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 58 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 59 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 60 | 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 Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 70 | public void setEraseAllPartitionWarning() |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 71 | { |
Jose Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 72 | mErasePartitionsWarning = true; |
Tiago Costa | 554587b | 2014-06-26 18:39:40 +0100 | [diff] [blame] | 73 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 74 | |
Tiago Costa | 6d360e2 | 2014-12-15 14:32:51 +0000 | [diff] [blame] | 75 | public boolean hasEraseAllPartitionWarning() |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 76 | { |
Tiago Costa | 6d360e2 | 2014-12-15 14:32:51 +0000 | [diff] [blame] | 77 | return mErasePartitionsWarning; |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 78 | } |
Jose Pascoal | 9be8f0d | 2014-03-10 11:22:41 +0000 | [diff] [blame] | 79 | |
Tiago Costa | 6d360e2 | 2014-12-15 14:32:51 +0000 | [diff] [blame] | 80 | public void setAndroidVersion(String mAndroid) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 81 | { |
Tiago Costa | 6d360e2 | 2014-12-15 14:32:51 +0000 | [diff] [blame] | 82 | this.mAndroidVersion = mAndroid; |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 85 | public void setImageType(String imageType) |
| 86 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 87 | mImageType = imageType; |
| 88 | } |
| 89 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 90 | public String getImageType() |
| 91 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 92 | return mImageType; |
| 93 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 94 | |
Filipe Gonçalves | 4c204d8 | 2015-01-23 15:09:19 +0000 | [diff] [blame] | 95 | public void setBetaStatus(String betaStatus) |
| 96 | { |
| 97 | mBetaStatus = betaStatus; |
| 98 | } |
| 99 | |
| 100 | public String getBetaStatus() |
| 101 | { |
| 102 | return mBetaStatus; |
| 103 | } |
| 104 | |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 105 | public String getImageTypeDescription(Resources resources) |
| 106 | { |
Jose Pascoal | a860449 | 2014-06-23 19:12:08 +0100 | [diff] [blame] | 107 | return Version.getImageTypeDescription(mImageType, resources); |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 108 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 109 | |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 110 | private static String getImageTypeDescription(String imageType, Resources resources) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 111 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 112 | String description = resources.getString(R.string.fairphone); |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 113 | if (!TextUtils.isEmpty(imageType)) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 114 | { |
Jose Pascoal | aa579a8 | 2014-11-05 22:17:16 +0000 | [diff] [blame] | 115 | 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 Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 123 | } |
| 124 | return description; |
| 125 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 126 | |
Jose Pascoal | cdd8204 | 2015-02-06 13:04:26 +0000 | [diff] [blame] | 127 | // --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çalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 138 | |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 139 | @Override |
Filipe Gonçalves | b31d586 | 2015-02-04 17:28:58 +0000 | [diff] [blame] | 140 | public int compareTo(@SuppressWarnings("NullableProblems") Version another) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 141 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 142 | int retVal; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 143 | if (another != null) |
| 144 | { |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 145 | if (!this.getId().equals(another.getId()) && this.mImageType.equalsIgnoreCase(another.mImageType)) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 146 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 147 | retVal = 1; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 148 | } |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 149 | else if (this.getId() == another.getId() && this.mImageType.equalsIgnoreCase(another.mImageType)) |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 150 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 151 | retVal = 0; |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 152 | } |
| 153 | else |
| 154 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 155 | retVal = -1; |
| 156 | } |
Jose Pascoal | 810950b | 2014-10-09 17:16:08 +0100 | [diff] [blame] | 157 | } |
| 158 | else |
| 159 | { |
Jose Pascoal | 1ee56e2 | 2014-05-21 16:58:20 +0100 | [diff] [blame] | 160 | retVal = 1; |
| 161 | } |
| 162 | return retVal; |
| 163 | } |
Tiago Costa | 6d360e2 | 2014-12-15 14:32:51 +0000 | [diff] [blame] | 164 | |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 165 | 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 Pascoal | cfc2dd4 | 2015-02-09 18:00:05 +0000 | [diff] [blame] | 181 | Log.e(TAG, "Invalid dependency: " + e.getLocalizedMessage()); |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 187 | // --Commented out by Inspection START (09/02/2015 19:47): |
| 188 | // List<Integer> getVersionDependencies() |
Jose Pascoal | 4091630 | 2015-02-06 18:43:47 +0000 | [diff] [blame] | 189 | // { |
| 190 | // return mDependencies; |
| 191 | // } |
Jose Pascoal | 060ab28 | 2015-02-09 19:56:08 +0000 | [diff] [blame] | 192 | // --Commented out by Inspection STOP (09/02/2015 19:47) |
Maarten Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 193 | |
| 194 | /** |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 195 | * 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 Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 197 | */ |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 198 | 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 Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 202 | if (matcher.find()) { |
| 203 | return matcher.group(1) + "." +matcher.group(3) + "." + matcher.group(5); |
| 204 | } |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 205 | 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 Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | /** |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 210 | * 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 Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 212 | */ |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 213 | public String getCurrentImageType() { |
| 214 | String id = getId(); |
| 215 | if(id.contains("gms")) { |
| 216 | return "Fairphone OS"; |
| 217 | } else if (id.contains("sibon")) { |
Maarten Derks | c4a354c | 2016-05-24 08:54:16 +0200 | [diff] [blame^] | 218 | return "Fairphone Open OS"; |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 219 | } else if (id.contains("AOSP+")) { |
| 220 | return "Fairphone Internal"; |
Maarten Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 221 | } else { |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 222 | // we do have a version but.. we don't know about it. return the full "number" |
| 223 | return id; |
Maarten Derks | 4bb5789 | 2016-01-26 10:52:53 +0100 | [diff] [blame] | 224 | } |
| 225 | } |
Maarten Derks | f17e6db | 2016-02-09 15:10:53 +0100 | [diff] [blame] | 226 | |
| 227 | public String getHumanReadableName() { |
| 228 | return getName() + " " + getBuildNumber(); |
| 229 | } |
Kim Hansen | 086964d | 2013-12-10 11:33:28 +0000 | [diff] [blame] | 230 | } |