OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 |
| 5 #include "chrome/browser/chromeos/cros/cellular_data_plan.h" |
| 6 |
| 7 #include "base/i18n/time_formatting.h" |
| 8 #include "base/string_number_conversions.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/common/time_format.h" |
| 11 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 #include "ui/base/text/bytes_formatting.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 // Cellular network is considered low data when less than 60 minues. |
| 18 const int kCellularDataLowSecs = 60 * 60; |
| 19 |
| 20 // Cellular network is considered low data when less than 30 minues. |
| 21 const int kCellularDataVeryLowSecs = 30 * 60; |
| 22 |
| 23 // Cellular network is considered low data when less than 100MB. |
| 24 const int kCellularDataLowBytes = 100 * 1024 * 1024; |
| 25 |
| 26 // Cellular network is considered very low data when less than 50MB. |
| 27 const int kCellularDataVeryLowBytes = 50 * 1024 * 1024; |
| 28 |
| 29 CellularDataPlan::CellularDataPlan() |
| 30 : plan_name("Unknown"), |
| 31 plan_type(CELLULAR_DATA_PLAN_UNLIMITED), |
| 32 plan_data_bytes(0), |
| 33 data_bytes_used(0) { |
| 34 } |
| 35 |
| 36 CellularDataPlan::CellularDataPlan(const CellularDataPlanInfo &plan) |
| 37 : plan_name(plan.plan_name ? plan.plan_name : ""), |
| 38 plan_type(plan.plan_type), |
| 39 update_time(base::Time::FromInternalValue(plan.update_time)), |
| 40 plan_start_time(base::Time::FromInternalValue(plan.plan_start_time)), |
| 41 plan_end_time(base::Time::FromInternalValue(plan.plan_end_time)), |
| 42 plan_data_bytes(plan.plan_data_bytes), |
| 43 data_bytes_used(plan.data_bytes_used) { |
| 44 } |
| 45 |
| 46 CellularDataPlan::~CellularDataPlan() {} |
| 47 |
| 48 string16 CellularDataPlan::GetPlanDesciption() const { |
| 49 switch (plan_type) { |
| 50 case chromeos::CELLULAR_DATA_PLAN_UNLIMITED: { |
| 51 return l10n_util::GetStringFUTF16( |
| 52 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PURCHASE_UNLIMITED_DATA, |
| 53 base::TimeFormatFriendlyDate(plan_start_time)); |
| 54 break; |
| 55 } |
| 56 case chromeos::CELLULAR_DATA_PLAN_METERED_PAID: { |
| 57 return l10n_util::GetStringFUTF16( |
| 58 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PURCHASE_DATA, |
| 59 ui::FormatBytes(plan_data_bytes), |
| 60 base::TimeFormatFriendlyDate(plan_start_time)); |
| 61 } |
| 62 case chromeos::CELLULAR_DATA_PLAN_METERED_BASE: { |
| 63 return l10n_util::GetStringFUTF16( |
| 64 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_RECEIVED_FREE_DATA, |
| 65 ui::FormatBytes(plan_data_bytes), |
| 66 base::TimeFormatFriendlyDate(plan_start_time)); |
| 67 default: |
| 68 break; |
| 69 } |
| 70 } |
| 71 return string16(); |
| 72 } |
| 73 |
| 74 string16 CellularDataPlan::GetRemainingWarning() const { |
| 75 if (plan_type == chromeos::CELLULAR_DATA_PLAN_UNLIMITED) { |
| 76 // Time based plan. Show nearing expiration and data expiration. |
| 77 if (remaining_time().InSeconds() <= chromeos::kCellularDataVeryLowSecs) { |
| 78 return GetPlanExpiration(); |
| 79 } |
| 80 } else if (plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_PAID || |
| 81 plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_BASE) { |
| 82 // Metered plan. Show low data and out of data. |
| 83 if (remaining_data() <= chromeos::kCellularDataVeryLowBytes) { |
| 84 int64 remaining_mbytes = remaining_data() / (1024 * 1024); |
| 85 return l10n_util::GetStringFUTF16( |
| 86 IDS_NETWORK_DATA_REMAINING_MESSAGE, |
| 87 UTF8ToUTF16(base::Int64ToString(remaining_mbytes))); |
| 88 } |
| 89 } |
| 90 return string16(); |
| 91 } |
| 92 |
| 93 string16 CellularDataPlan::GetDataRemainingDesciption() const { |
| 94 int64 remaining_bytes = remaining_data(); |
| 95 switch (plan_type) { |
| 96 case chromeos::CELLULAR_DATA_PLAN_UNLIMITED: { |
| 97 return l10n_util::GetStringUTF16( |
| 98 IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_UNLIMITED); |
| 99 } |
| 100 case chromeos::CELLULAR_DATA_PLAN_METERED_PAID: { |
| 101 return ui::FormatBytes(remaining_bytes); |
| 102 } |
| 103 case chromeos::CELLULAR_DATA_PLAN_METERED_BASE: { |
| 104 return ui::FormatBytes(remaining_bytes); |
| 105 } |
| 106 default: |
| 107 break; |
| 108 } |
| 109 return string16(); |
| 110 } |
| 111 |
| 112 string16 CellularDataPlan::GetUsageInfo() const { |
| 113 if (plan_type == chromeos::CELLULAR_DATA_PLAN_UNLIMITED) { |
| 114 // Time based plan. Show nearing expiration and data expiration. |
| 115 return GetPlanExpiration(); |
| 116 } else if (plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_PAID || |
| 117 plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_BASE) { |
| 118 // Metered plan. Show low data and out of data. |
| 119 int64 remaining_bytes = remaining_data(); |
| 120 if (remaining_bytes == 0) { |
| 121 return l10n_util::GetStringUTF16( |
| 122 IDS_NETWORK_DATA_NONE_AVAILABLE_MESSAGE); |
| 123 } else if (remaining_bytes < 1024 * 1024) { |
| 124 return l10n_util::GetStringUTF16( |
| 125 IDS_NETWORK_DATA_LESS_THAN_ONE_MB_AVAILABLE_MESSAGE); |
| 126 } else { |
| 127 int64 remaining_mb = remaining_bytes / (1024 * 1024); |
| 128 return l10n_util::GetStringFUTF16( |
| 129 IDS_NETWORK_DATA_MB_AVAILABLE_MESSAGE, |
| 130 UTF8ToUTF16(base::Int64ToString(remaining_mb))); |
| 131 } |
| 132 } |
| 133 return string16(); |
| 134 } |
| 135 |
| 136 std::string CellularDataPlan::GetUniqueIdentifier() const { |
| 137 // A cellular plan is uniquely described by the union of name, type, |
| 138 // start time, end time, and max bytes. |
| 139 // So we just return a union of all these variables. |
| 140 return plan_name + "|" + |
| 141 base::Int64ToString(plan_type) + "|" + |
| 142 base::Int64ToString(plan_start_time.ToInternalValue()) + "|" + |
| 143 base::Int64ToString(plan_end_time.ToInternalValue()) + "|" + |
| 144 base::Int64ToString(plan_data_bytes); |
| 145 } |
| 146 |
| 147 base::TimeDelta CellularDataPlan::remaining_time() const { |
| 148 base::TimeDelta time = plan_end_time - base::Time::Now(); |
| 149 return time.InMicroseconds() < 0 ? base::TimeDelta() : time; |
| 150 } |
| 151 |
| 152 int64 CellularDataPlan::remaining_minutes() const { |
| 153 return remaining_time().InMinutes(); |
| 154 } |
| 155 |
| 156 int64 CellularDataPlan::remaining_data() const { |
| 157 int64 data = plan_data_bytes - data_bytes_used; |
| 158 return data < 0 ? 0 : data; |
| 159 } |
| 160 |
| 161 string16 CellularDataPlan::GetPlanExpiration() const { |
| 162 return TimeFormat::TimeRemaining(remaining_time()); |
| 163 } |
| 164 |
| 165 } // namespace chromeos |
OLD | NEW |