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.
5 #include "base/android/radio_utils.h"
7 #include "base/base_jni/RadioUtils_jni.h"
8 #include "third_party/abseil-cpp/absl/types/optional.h"
15 RadioUtils::OverrideForTesting* g_overrider_for_tests = nullptr;
17 bool InitializeIsSupported() {
18 JNIEnv* env = AttachCurrentThread();
19 return Java_RadioUtils_isSupported(env);
23 RadioUtils::OverrideForTesting::OverrideForTesting() {
24 DCHECK(!g_overrider_for_tests);
25 g_overrider_for_tests = this;
28 RadioUtils::OverrideForTesting::~OverrideForTesting() {
29 DCHECK(g_overrider_for_tests);
30 g_overrider_for_tests = nullptr;
33 bool RadioUtils::IsSupported() {
34 static const bool kIsSupported = InitializeIsSupported();
38 RadioConnectionType RadioUtils::GetConnectionType() {
39 if (g_overrider_for_tests) {
40 // If GetConnectionType is being used in tests
41 return g_overrider_for_tests->GetConnectionType();
44 return RadioConnectionType::kUnknown;
46 JNIEnv* env = AttachCurrentThread();
47 if (Java_RadioUtils_isWifiConnected(env)) {
48 return RadioConnectionType::kWifi;
50 return RadioConnectionType::kCell;
54 absl::optional<RadioSignalLevel> RadioUtils::GetCellSignalLevel() {
58 JNIEnv* env = AttachCurrentThread();
59 int signal_level = Java_RadioUtils_getCellSignalLevel(env);
60 if (signal_level < 0) {
63 return static_cast<RadioSignalLevel>(signal_level);
67 absl::optional<RadioDataActivity> RadioUtils::GetCellDataActivity() {
71 JNIEnv* env = AttachCurrentThread();
72 return static_cast<RadioDataActivity>(
73 Java_RadioUtils_getCellDataActivity(env));
76 } // namespace android