phone_status_model.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 "ash/components/phonehub/phone_status_model.h"
  5. #include "ash/components/multidevice/logging/logging.h"
  6. namespace ash {
  7. namespace phonehub {
  8. bool PhoneStatusModel::MobileConnectionMetadata::operator==(
  9. const MobileConnectionMetadata& other) const {
  10. return signal_strength == other.signal_strength &&
  11. mobile_provider == other.mobile_provider;
  12. }
  13. bool PhoneStatusModel::MobileConnectionMetadata::operator!=(
  14. const MobileConnectionMetadata& other) const {
  15. return !(*this == other);
  16. }
  17. PhoneStatusModel::PhoneStatusModel(
  18. MobileStatus mobile_status,
  19. const absl::optional<MobileConnectionMetadata>& mobile_connection_metadata,
  20. ChargingState charging_state,
  21. BatterySaverState battery_saver_state,
  22. uint32_t battery_percentage)
  23. : mobile_status_(mobile_status),
  24. mobile_connection_metadata_(mobile_connection_metadata),
  25. charging_state_(charging_state),
  26. battery_saver_state_(battery_saver_state),
  27. battery_percentage_(battery_percentage) {
  28. if (battery_percentage_ > 100) {
  29. PA_LOG(WARNING) << "Provided battery percentage (" << battery_percentage_
  30. << "%) is >100%; setting to 100%.";
  31. battery_percentage_ = 100;
  32. }
  33. if (mobile_status_ != MobileStatus::kSimWithReception &&
  34. mobile_connection_metadata_.has_value()) {
  35. if (!mobile_connection_metadata_->mobile_provider.empty()) {
  36. // If the phone reports 0/4 bars but still reports a mobile provider, it
  37. // may be temporarily under-reporting the real signal strength shown on
  38. // the phone. See https://crbug.com/1163266 for details.
  39. PA_LOG(VERBOSE) << "Provided MobileStatus " << mobile_status_ << " "
  40. << "indicates no reception, but it is connected to a "
  41. << "mobile provider. Updating to one-bar reception.";
  42. mobile_status_ = MobileStatus::kSimWithReception;
  43. mobile_connection_metadata_->signal_strength = SignalStrength::kOneBar;
  44. } else {
  45. PA_LOG(WARNING) << "Provided MobileStatus " << mobile_status_ << " "
  46. << "indicates no reception, but MobileConnectionMetadata "
  47. << *mobile_connection_metadata_
  48. << " was provided. Clearing "
  49. << "metadata.";
  50. mobile_connection_metadata_.reset();
  51. }
  52. } else if (mobile_status_ == MobileStatus::kSimWithReception &&
  53. !mobile_connection_metadata_.has_value()) {
  54. PA_LOG(WARNING) << "MobileStatus is " << mobile_status_ << ", but no "
  55. << "connection metadata was provided. Changing status to "
  56. << MobileStatus::kSimButNoReception << ".";
  57. mobile_status_ = MobileStatus::kSimButNoReception;
  58. }
  59. }
  60. PhoneStatusModel::PhoneStatusModel(const PhoneStatusModel& other) = default;
  61. PhoneStatusModel::~PhoneStatusModel() = default;
  62. bool PhoneStatusModel::operator==(const PhoneStatusModel& other) const {
  63. return mobile_status_ == other.mobile_status_ &&
  64. mobile_connection_metadata_ == other.mobile_connection_metadata_ &&
  65. charging_state_ == other.charging_state_ &&
  66. battery_saver_state_ == other.battery_saver_state_ &&
  67. battery_percentage_ == other.battery_percentage_;
  68. }
  69. bool PhoneStatusModel::operator!=(const PhoneStatusModel& other) const {
  70. return !(*this == other);
  71. }
  72. std::ostream& operator<<(std::ostream& stream,
  73. PhoneStatusModel::MobileStatus mobile_status) {
  74. switch (mobile_status) {
  75. case PhoneStatusModel::MobileStatus::kNoSim:
  76. stream << "[No SIM]";
  77. break;
  78. case PhoneStatusModel::MobileStatus::kSimButNoReception:
  79. stream << "[SIM present; no reception]";
  80. break;
  81. case PhoneStatusModel::MobileStatus::kSimWithReception:
  82. stream << "[SIM present with reception]";
  83. break;
  84. }
  85. return stream;
  86. }
  87. std::ostream& operator<<(std::ostream& stream,
  88. PhoneStatusModel::SignalStrength signal_strength) {
  89. switch (signal_strength) {
  90. case PhoneStatusModel::SignalStrength::kZeroBars:
  91. stream << "[0 bars / 4]";
  92. break;
  93. case PhoneStatusModel::SignalStrength::kOneBar:
  94. stream << "[1 bar / 4]";
  95. break;
  96. case PhoneStatusModel::SignalStrength::kTwoBars:
  97. stream << "[2 bars / 4]";
  98. break;
  99. case PhoneStatusModel::SignalStrength::kThreeBars:
  100. stream << "[3 bars / 4]";
  101. break;
  102. case PhoneStatusModel::SignalStrength::kFourBars:
  103. stream << "[4 bars / 4]";
  104. break;
  105. }
  106. return stream;
  107. }
  108. std::ostream& operator<<(
  109. std::ostream& stream,
  110. PhoneStatusModel::MobileConnectionMetadata mobile_connection_metadata) {
  111. stream << "{SignalStrength: " << mobile_connection_metadata.signal_strength
  112. << ", MobileProvider: \"" << mobile_connection_metadata.mobile_provider
  113. << "\"}";
  114. return stream;
  115. }
  116. std::ostream& operator<<(std::ostream& stream,
  117. PhoneStatusModel::ChargingState charging_state) {
  118. switch (charging_state) {
  119. case PhoneStatusModel::ChargingState::kNotCharging:
  120. stream << "[Not charging]";
  121. break;
  122. case PhoneStatusModel::ChargingState::kChargingAc:
  123. stream << "[Charging via an AC adapter]";
  124. break;
  125. case PhoneStatusModel::ChargingState::kChargingUsb:
  126. stream << "[Charging via USB]";
  127. break;
  128. }
  129. return stream;
  130. }
  131. std::ostream& operator<<(
  132. std::ostream& stream,
  133. PhoneStatusModel::BatterySaverState battery_saver_state) {
  134. switch (battery_saver_state) {
  135. case PhoneStatusModel::BatterySaverState::kOff:
  136. stream << "[Battery Saver off]";
  137. break;
  138. case PhoneStatusModel::BatterySaverState::kOn:
  139. stream << "[Battery Saver on]";
  140. break;
  141. }
  142. return stream;
  143. }
  144. } // namespace phonehub
  145. } // namespace ash