radio_utils.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/android/radio_utils.h"
  5. #include "base/base_jni_headers/RadioUtils_jni.h"
  6. #include "third_party/abseil-cpp/absl/types/optional.h"
  7. namespace base {
  8. namespace android {
  9. namespace {
  10. bool InitializeIsSupported() {
  11. JNIEnv* env = AttachCurrentThread();
  12. return Java_RadioUtils_isSupported(env);
  13. }
  14. } // namespace
  15. bool RadioUtils::IsSupported() {
  16. static const bool kIsSupported = InitializeIsSupported();
  17. return kIsSupported;
  18. }
  19. RadioConnectionType RadioUtils::GetConnectionType() {
  20. if (!IsSupported())
  21. return RadioConnectionType::kUnknown;
  22. JNIEnv* env = AttachCurrentThread();
  23. if (Java_RadioUtils_isWifiConnected(env)) {
  24. return RadioConnectionType::kWifi;
  25. } else {
  26. return RadioConnectionType::kCell;
  27. }
  28. }
  29. absl::optional<RadioSignalLevel> RadioUtils::GetCellSignalLevel() {
  30. if (!IsSupported())
  31. return absl::nullopt;
  32. JNIEnv* env = AttachCurrentThread();
  33. int signal_level = Java_RadioUtils_getCellSignalLevel(env);
  34. if (signal_level < 0) {
  35. return absl::nullopt;
  36. } else {
  37. return static_cast<RadioSignalLevel>(signal_level);
  38. }
  39. }
  40. absl::optional<RadioDataActivity> RadioUtils::GetCellDataActivity() {
  41. if (!IsSupported())
  42. return absl::nullopt;
  43. JNIEnv* env = AttachCurrentThread();
  44. return static_cast<RadioDataActivity>(
  45. Java_RadioUtils_getCellDataActivity(env));
  46. }
  47. } // namespace android
  48. } // namespace base