-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils_android_version.go
55 lines (48 loc) · 1.35 KB
/
utils_android_version.go
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
package go_android_utils
import (
"errors"
"strconv"
"strings"
)
func (version AndroidVersion) IsValid() bool {
if version == 0 {
return false
}
_, result := AndroidVersion_name[int32(version)]
return result
}
func AndroidVersionFromVersionString(versionStr string) (AndroidVersion, error) {
var err error
// 6 => 6.0, 6.1 => 6.1
versionSplits := strings.Split(versionStr, ".")
if len(versionSplits) == 1 {
versionSplits = append(versionSplits, "0")
}
val, ok := AndroidVersion_value["V"+strings.ToUpper(strings.Join(versionSplits[:2], "_"))]
if !ok {
err = ErrAndroidVersionVersionUnsupported
val = 0
}
return AndroidVersion(val), err
}
func AndroidVersionFromSDKString(sdkStr string) (AndroidVersion, error) {
sdk, err := strconv.ParseInt(sdkStr, 10, 64)
if err == nil {
_, ok := AndroidVersion_name[int32(sdk)]
if !ok || sdk < 1 {
err = ErrAndroidVersionSDKUnsupported
}
return AndroidVersion(int(sdk)), err
}
return AndroidVersion(0), err
}
func (version AndroidVersion) ToAndroidVersion() string {
return strings.ReplaceAll(version.String()[1:], "_", ".")
}
func (version AndroidVersion) ToAndroidSDK() string {
return strconv.Itoa(int(version))
}
var (
ErrAndroidVersionSDKUnsupported = errors.New("the supplied SDK is unsupported")
ErrAndroidVersionVersionUnsupported = errors.New("the supplied version is unsupported")
)