OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sys_info.h" | 5 #include "base/sys_info.h" |
6 | 6 |
7 #include <sys/system_properties.h> | 7 #include <sys/system_properties.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/string_piece.h" | 11 #include "base/string_piece.h" |
12 #include "base/stringprintf.h" | |
12 | 13 |
13 namespace { | 14 namespace { |
14 | 15 |
16 // Default to ICS when the version number string doesn't make sense. | |
17 // TODO(yfriedman): Keep this reasonably up to date. | |
18 static const int kPublicAndroidMajorVersion = 4; | |
19 static const int kPublicAndroidMinorVersion = 0; | |
20 static const int kPublicAndroidBugfixVersion = 3; | |
21 | |
22 // If we have no clue what version of Android we're on, use 1.0.0. | |
23 static const int kUnknownMajorVersion = 1; | |
24 static const int kUnknownMinorVersion = 0; | |
25 static const int kUnknownBugfixVersion = 0; | |
26 | |
27 // Parse out the OS version numbers from the system properties. | |
28 void GetOSVersionNumbers(const char* os_version_str, | |
29 int *major_version, | |
30 int *minor_version, | |
31 int *bugfix_version) { | |
32 if (strlen(os_version_str) > 0) { | |
33 // Try to parse out the version numbers. | |
34 int num_read = sscanf(os_version_str, "%d.%d.%d", major_version, | |
35 minor_version, bugfix_version); | |
36 | |
37 if (num_read == 0) { | |
38 // Either the user is using a build that doesn't have a proper version | |
39 // number, or the release doesn't have a version number yet (e.g. | |
40 // "Jellybean"). In this case, use the latest public Android version. | |
41 *major_version = kPublicAndroidMajorVersion; | |
42 *minor_version = kPublicAndroidMinorVersion; | |
43 *bugfix_version = kPublicAndroidBugfixVersion; | |
44 } else { | |
45 // If we don't have a full set of version numbers, make the extras 0. | |
46 if (num_read < 2) *minor_version = 0; | |
47 if (num_read < 3) *bugfix_version = 0; | |
48 } | |
49 } else { | |
50 // We don't have a string at all. | |
51 *major_version = kUnknownMajorVersion; | |
52 *minor_version = kUnknownMinorVersion; | |
53 *bugfix_version = kUnknownBugfixVersion; | |
54 } | |
55 } | |
56 | |
15 int ParseHeapSize(const base::StringPiece& str) { | 57 int ParseHeapSize(const base::StringPiece& str) { |
16 const int64 KB = 1024; | 58 const int64 KB = 1024; |
17 const int64 MB = 1024 * KB; | 59 const int64 MB = 1024 * KB; |
18 const int64 GB = 1024 * MB; | 60 const int64 GB = 1024 * MB; |
19 CHECK_GT(str.size(), 0u); | 61 CHECK_GT(str.size(), 0u); |
20 int64 factor = 1; | 62 int64 factor = 1; |
21 size_t length = str.size(); | 63 size_t length = str.size(); |
22 if (str[length - 1] == 'k') { | 64 if (str[length - 1] == 'k') { |
23 factor = KB; | 65 factor = KB; |
24 length--; | 66 length--; |
(...skipping 19 matching lines...) Expand all Loading... | |
44 int GetDalvikHeapSizeMB() { | 86 int GetDalvikHeapSizeMB() { |
45 char heap_size_str[PROP_VALUE_MAX]; | 87 char heap_size_str[PROP_VALUE_MAX]; |
46 __system_property_get("dalvik.vm.heapsize", heap_size_str); | 88 __system_property_get("dalvik.vm.heapsize", heap_size_str); |
47 return ParseHeapSize(heap_size_str); | 89 return ParseHeapSize(heap_size_str); |
48 } | 90 } |
49 | 91 |
50 } // anonymous namespace | 92 } // anonymous namespace |
51 | 93 |
52 namespace base { | 94 namespace base { |
53 | 95 |
96 void SysInfo::OperatingSystemVersionNumbers(int32* major_version, | |
97 int32* minor_version, | |
98 int32* bugfix_version) { | |
99 // Read the version number string out from the properties. | |
100 char os_version_str[PROP_VALUE_MAX]; | |
101 __system_property_get("ro.build.version.release", os_version_str); | |
102 | |
103 // Parse out the numbers. | |
104 int internal_major_version, internal_minor_version, internal_bugfix_version; | |
105 GetOSVersionNumbers(os_version_str, &internal_major_version, | |
106 &internal_minor_version, &internal_bugfix_version); | |
107 *major_version = internal_major_version; | |
108 *minor_version = internal_minor_version; | |
109 *bugfix_version = internal_bugfix_version; | |
110 } | |
111 | |
112 std::string SysInfo::OperatingSystemInformation() { | |
113 std::string android_info_str; | |
114 | |
115 // Append information about the OS version. | |
116 int32 version_major, version_minor, version_bugfix; | |
117 OperatingSystemVersionNumbers(&version_major, &version_minor, | |
118 &version_bugfix); | |
119 if (version_bugfix == 0) { | |
120 base::StringAppendF(&android_info_str, "%d.%d", version_major, | |
121 version_minor); | |
122 } else { | |
123 base::StringAppendF(&android_info_str, "%d.%d.%d", version_major, | |
124 version_minor, version_bugfix); | |
125 } | |
126 android_info_str += ";"; | |
127 | |
128 // Append information about the device. | |
129 char os_version_codename_str[PROP_VALUE_MAX]; | |
130 __system_property_get("ro.build.version.codename", os_version_codename_str); | |
131 if (strcmp("REL", os_version_codename_str) == 0) { | |
132 char device_model_str[PROP_VALUE_MAX]; | |
133 __system_property_get("ro.product.model", device_model_str); | |
134 if (strlen(device_model_str) > 0) { | |
135 android_info_str += " " + std::string(device_model_str); | |
136 } | |
137 } | |
138 | |
139 // Append the build ID. | |
140 char os_build_id_str[PROP_VALUE_MAX]; | |
141 __system_property_get("ro.build.id", os_build_id_str); | |
142 if (strlen(os_build_id_str) > 0) { | |
willchan no longer on Chromium
2012/08/14 17:51:09
You don't need strlen, just see if (os_build_id_st
gone
2012/08/14 17:58:37
Done.
| |
143 android_info_str += " Build/" + std::string(os_build_id_str); | |
144 } | |
145 | |
146 return android_info_str; | |
147 } | |
148 | |
54 int SysInfo::DalvikHeapSizeMB() { | 149 int SysInfo::DalvikHeapSizeMB() { |
55 static int heap_size = GetDalvikHeapSizeMB(); | 150 static int heap_size = GetDalvikHeapSizeMB(); |
56 return heap_size; | 151 return heap_size; |
57 } | 152 } |
58 | 153 |
59 } // namespace base | 154 } // namespace base |
OLD | NEW |