1 // Copyright 2020 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 "base/android/radio_utils.h" 6 7 #include "base/base_jni/RadioUtils_jni.h" 8 #include "third_party/abseil-cpp/absl/types/optional.h" 9 10 namespace base { 11 namespace android { 12 13 namespace { 14 15 RadioUtils::OverrideForTesting* g_overrider_for_tests = nullptr; 16 InitializeIsSupported()17bool InitializeIsSupported() { 18 JNIEnv* env = AttachCurrentThread(); 19 return Java_RadioUtils_isSupported(env); 20 } 21 } // namespace 22 OverrideForTesting()23RadioUtils::OverrideForTesting::OverrideForTesting() { 24 DCHECK(!g_overrider_for_tests); 25 g_overrider_for_tests = this; 26 } 27 ~OverrideForTesting()28RadioUtils::OverrideForTesting::~OverrideForTesting() { 29 DCHECK(g_overrider_for_tests); 30 g_overrider_for_tests = nullptr; 31 } 32 IsSupported()33bool RadioUtils::IsSupported() { 34 static const bool kIsSupported = InitializeIsSupported(); 35 return kIsSupported; 36 } 37 GetConnectionType()38RadioConnectionType RadioUtils::GetConnectionType() { 39 if (g_overrider_for_tests) { 40 // If GetConnectionType is being used in tests 41 return g_overrider_for_tests->GetConnectionType(); 42 } 43 if (!IsSupported()) 44 return RadioConnectionType::kUnknown; 45 46 JNIEnv* env = AttachCurrentThread(); 47 if (Java_RadioUtils_isWifiConnected(env)) { 48 return RadioConnectionType::kWifi; 49 } else { 50 return RadioConnectionType::kCell; 51 } 52 } 53 GetCellSignalLevel()54absl::optional<RadioSignalLevel> RadioUtils::GetCellSignalLevel() { 55 if (!IsSupported()) 56 return absl::nullopt; 57 58 JNIEnv* env = AttachCurrentThread(); 59 int signal_level = Java_RadioUtils_getCellSignalLevel(env); 60 if (signal_level < 0) { 61 return absl::nullopt; 62 } else { 63 return static_cast<RadioSignalLevel>(signal_level); 64 } 65 } 66 GetCellDataActivity()67absl::optional<RadioDataActivity> RadioUtils::GetCellDataActivity() { 68 if (!IsSupported()) 69 return absl::nullopt; 70 71 JNIEnv* env = AttachCurrentThread(); 72 return static_cast<RadioDataActivity>( 73 Java_RadioUtils_getCellDataActivity(env)); 74 } 75 76 } // namespace android 77 } // namespace base 78