-
Notifications
You must be signed in to change notification settings - Fork 27
/
AndroidVersions.cs
187 lines (160 loc) · 6.69 KB
/
AndroidVersions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Xml.Linq;
namespace Xamarin.Android.Tools
{
public class AndroidVersions
{
List<AndroidVersion> installedVersions = new List<AndroidVersion> ();
public IReadOnlyList<string> FrameworkDirectories { get; }
public AndroidVersion? MaxStableVersion { get; private set; }
public AndroidVersion? MinStableVersion { get; private set; }
public IReadOnlyList<AndroidVersion> InstalledBindingVersions { get; private set; }
public AndroidVersions (IEnumerable<string> frameworkDirectories)
{
if (frameworkDirectories == null)
throw new ArgumentNullException (nameof (frameworkDirectories));
var dirs = new List<string> ();
foreach (var d in frameworkDirectories) {
if (!Directory.Exists (d))
throw new ArgumentException ($"`{d}` must be a directory!", nameof (frameworkDirectories));
var dp = d.TrimEnd (Path.DirectorySeparatorChar);
var dn = Path.GetFileName (dp);
// In "normal" use, `dp` will contain e.g. `...\MonoAndroid\v1.0`.
// We want the `MonoAndroid` dir, not the versioned dir.
var p = dn.StartsWith ("v", StringComparison.Ordinal) ? (Path.GetDirectoryName (dp) ?? "") : dp;
dirs.Add (Path.GetFullPath (p));
}
dirs = dirs.Distinct (StringComparer.OrdinalIgnoreCase)
.ToList ();
FrameworkDirectories = new ReadOnlyCollection<string> (dirs);
var versions = dirs.SelectMany (d => Directory.EnumerateFiles (d, "AndroidApiInfo.xml", SearchOption.AllDirectories))
.Select (file => AndroidVersion.Load (file));
LoadVersions (versions);
InstalledBindingVersions = new ReadOnlyCollection<AndroidVersion> (installedVersions);
}
public AndroidVersions (IEnumerable<AndroidVersion> versions)
{
if (versions == null)
throw new ArgumentNullException (nameof (versions));
FrameworkDirectories = new ReadOnlyCollection<string> (new string [0]);
LoadVersions (versions);
InstalledBindingVersions = new ReadOnlyCollection<AndroidVersion> (installedVersions);
}
void LoadVersions (IEnumerable<AndroidVersion> versions)
{
foreach (var version in versions) {
installedVersions.Add (version);
if (!version.Stable)
continue;
if (MaxStableVersion == null || (MaxStableVersion.TargetFrameworkVersion < version.TargetFrameworkVersion)) {
MaxStableVersion = version;
}
if (MinStableVersion == null || (MinStableVersion.TargetFrameworkVersion > version.TargetFrameworkVersion)) {
MinStableVersion = version;
}
}
}
public int? GetApiLevelFromFrameworkVersion (string frameworkVersion)
{
return installedVersions.FirstOrDefault (v => MatchesFrameworkVersion (v, frameworkVersion))?.ApiLevel ??
KnownVersions.FirstOrDefault (v => MatchesFrameworkVersion (v, frameworkVersion))?.ApiLevel;
}
static bool MatchesFrameworkVersion (AndroidVersion version, string frameworkVersion)
{
return version.FrameworkVersion == frameworkVersion ||
version.OSVersion == frameworkVersion;
}
public int? GetApiLevelFromId (string id)
{
return installedVersions.FirstOrDefault (v => MatchesId (v, id))?.ApiLevel ??
KnownVersions.FirstOrDefault (v => MatchesId (v, id))?.ApiLevel ??
(int.TryParse (id, out int apiLevel) ? apiLevel : default (int?));
}
static bool MatchesId (AndroidVersion version, string id)
{
return version.Id == id ||
(version.AlternateIds?.Contains (id) ?? false) ||
(version.ApiLevel.ToString () == id);
}
public string? GetIdFromApiLevel (int apiLevel)
{
return installedVersions.FirstOrDefault (v => v.ApiLevel == apiLevel)?.Id ??
KnownVersions.FirstOrDefault (v => v.ApiLevel == apiLevel)?.Id ??
apiLevel.ToString ();
}
// Sometimes, e.g. when new API levels are introduced, the "API level" is a letter, not a number,
// e.g. 'API-H' for API-11, 'API-O' for API-26, etc.
public string? GetIdFromApiLevel (string apiLevel)
{
if (int.TryParse (apiLevel, out var platform))
return GetIdFromApiLevel (platform);
return installedVersions.FirstOrDefault (v => MatchesId (v, apiLevel))?.Id ??
KnownVersions.FirstOrDefault (v => MatchesId (v, apiLevel))?.Id ??
apiLevel;
}
public string? GetIdFromFrameworkVersion (string frameworkVersion)
{
return installedVersions.FirstOrDefault (v => MatchesFrameworkVersion (v, frameworkVersion))?.Id ??
KnownVersions.FirstOrDefault (v => MatchesFrameworkVersion (v, frameworkVersion))?.Id;
}
public string? GetFrameworkVersionFromApiLevel (int apiLevel)
{
return installedVersions.FirstOrDefault (v => v.ApiLevel == apiLevel)?.FrameworkVersion ??
KnownVersions.FirstOrDefault (v => v.ApiLevel == apiLevel)?.FrameworkVersion;
}
public string? GetFrameworkVersionFromId (string id)
{
return installedVersions.FirstOrDefault (v => MatchesId (v, id))?.FrameworkVersion ??
KnownVersions.FirstOrDefault (v => MatchesId (v, id))?.FrameworkVersion;
}
public static readonly AndroidVersion [] KnownVersions = new [] {
new AndroidVersion (4, "1.6", "Donut"),
new AndroidVersion (5, "2.0", "Eclair"),
new AndroidVersion (6, "2.0.1", "Eclair"),
new AndroidVersion (7, "2.1", "Eclair"),
new AndroidVersion (8, "2.2", "Froyo"),
new AndroidVersion (10, "2.3", "Gingerbread"),
new AndroidVersion (11, "3.0", "Honeycomb") {
AlternateIds = new[]{ "H" },
},
new AndroidVersion (12, "3.1", "Honeycomb"),
new AndroidVersion (13, "3.2", "Honeycomb"),
new AndroidVersion (14, "4.0", "Ice Cream Sandwich"),
new AndroidVersion (15, "4.0.3", "Ice Cream Sandwich"),
new AndroidVersion (16, "4.1", "Jelly Bean"),
new AndroidVersion (17, "4.2", "Jelly Bean"),
new AndroidVersion (18, "4.3", "Jelly Bean"),
new AndroidVersion (19, "4.4", "Kit Kat"),
new AndroidVersion (20, "4.4.87", "Kit Kat + Wear support"),
new AndroidVersion (21, "5.0", "Lollipop") {
AlternateIds = new[]{ "L" },
},
new AndroidVersion (22, "5.1", "Lollipop"),
new AndroidVersion (23, "6.0", "Marshmallow") {
AlternateIds = new[]{ "M" },
},
new AndroidVersion (24, "7.0", "Nougat") {
AlternateIds = new[]{ "N" },
},
new AndroidVersion (25, "7.1", "Nougat"),
new AndroidVersion (26, "8.0", "Oreo") {
AlternateIds = new[]{ "O" },
},
new AndroidVersion (27, "8.1", "Oreo"),
new AndroidVersion (28, "9.0", "Pie") {
AlternateIds = new[]{ "P" },
},
new AndroidVersion (29, "10.0", "Android10") {
AlternateIds = new[]{ "Q" },
},
new AndroidVersion (30, "11.0") {
AlternateIds = new[]{ "R" },
},
};
}
}