1 // Copyright 2023 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "components/metrics/gms_metrics_provider.h" 6 7 #include "base/logging.h" 8 9 #include "base/android/build_info.h" 10 #include "base/metrics/histogram_functions.h" 11 #include "base/strings/string_number_conversions.h" 12 13 namespace metrics { 14 namespace { 15 16 enum class GmsShortVersionCode { 17 kNotInstalled = 0, 18 kNotParsable = 1, 19 kOutOfRange = 2, 20 kMaxValue = kOutOfRange, 21 }; 22 23 // Minimum valid GMS Core version. 24 constexpr int kMinimumVersion = 200302000; 25 26 // Maximum valid GMS Core version. 27 constexpr int kMaxValidVersion = 301200000; 28 RecordGMSCoreVersionCode(int version)29void RecordGMSCoreVersionCode(int version) { 30 base::UmaHistogramSparse("Android.PlayServices.ShortVersion", version); 31 } 32 33 } // namespace 34 35 GmsMetricsProvider::GmsMetricsProvider() = default; 36 GmsMetricsProvider::~GmsMetricsProvider() = default; 37 ProvideHistograms()38bool GmsMetricsProvider::ProvideHistograms() { 39 int current_gms_core_version; 40 if (!base::StringToInt(GetGMSVersion(), ¤t_gms_core_version)) { 41 RecordGMSCoreVersionCode( 42 static_cast<int>(GmsShortVersionCode::kNotParsable)); 43 return true; 44 } 45 46 if (current_gms_core_version == 0) { 47 RecordGMSCoreVersionCode( 48 static_cast<int>(GmsShortVersionCode::kNotInstalled)); 49 return true; 50 } 51 52 // Get rid of old versions and garbage. 53 if (current_gms_core_version < kMinimumVersion || 54 current_gms_core_version > kMaxValidVersion) { 55 RecordGMSCoreVersionCode( 56 static_cast<int>(GmsShortVersionCode::kOutOfRange)); 57 return true; 58 } 59 // Get first four digits representing a year and a week. 60 int year_weak_code = current_gms_core_version / 100000; 61 // Get following two digits indicating build version, version greater or equal 62 // to 12 are stable releases. 63 bool is_stable_release = ((current_gms_core_version / 1000) % 100) >= 12; 64 65 // Log the current version in a YYWWV format, where 24031 would indicate 66 // 2024y03w stable release. 67 RecordGMSCoreVersionCode(year_weak_code * 10 + is_stable_release); 68 return true; 69 } 70 GetGMSVersion()71std::string GmsMetricsProvider::GetGMSVersion() { 72 base::android::BuildInfo* info = base::android::BuildInfo::GetInstance(); 73 return info->gms_version_code(); 74 } 75 76 } // namespace metrics