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