text: // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  // Use of this source code is governed by a BSD-style license that can be
  // found in the LICENSE file.
  
  #include "chrome/browser/chromeos/cros/cellular_data_plan.h"
  
  #include "base/i18n/time_formatting.h"
  #include "base/string_number_conversions.h"
  #include "base/utf_string_conversions.h"
  #include "chrome/common/time_format.h"
  #include "grit/generated_resources.h"
  #include "ui/base/l10n/l10n_util.h"
  #include "ui/base/text/bytes_formatting.h"
  
  namespace chromeos {
  
  // Cellular network is considered low data when less than 60 minues.
  const int kCellularDataLowSecs = 60 * 60;
  
  // Cellular network is considered low data when less than 30 minues.
  const int kCellularDataVeryLowSecs = 30 * 60;
  
  // Cellular network is considered low data when less than 100MB.
  const int kCellularDataLowBytes = 100 * 1024 * 1024;
  
  // Cellular network is considered very low data when less than 50MB.
  const int kCellularDataVeryLowBytes = 50 * 1024 * 1024;
  
  CellularDataPlan::CellularDataPlan()
      : plan_name("Unknown"),
        plan_type(CELLULAR_DATA_PLAN_UNLIMITED),
        plan_data_bytes(0),
        data_bytes_used(0) {
  }
  
  CellularDataPlan::~CellularDataPlan() {}
  
  string16 CellularDataPlan::GetPlanDesciption() const {
    switch (plan_type) {
      case chromeos::CELLULAR_DATA_PLAN_UNLIMITED: {
        return l10n_util::GetStringFUTF16(
            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PURCHASE_UNLIMITED_DATA,
            base::TimeFormatFriendlyDate(plan_start_time));
        break;
      }
      case chromeos::CELLULAR_DATA_PLAN_METERED_PAID: {
        return l10n_util::GetStringFUTF16(
                  IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_PURCHASE_DATA,
                  ui::FormatBytes(plan_data_bytes),
                  base::TimeFormatFriendlyDate(plan_start_time));
      }
      case chromeos::CELLULAR_DATA_PLAN_METERED_BASE: {
        return l10n_util::GetStringFUTF16(
                  IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_RECEIVED_FREE_DATA,
                  ui::FormatBytes(plan_data_bytes),
                  base::TimeFormatFriendlyDate(plan_start_time));
      default:
        break;
      }
    }
    return string16();
  }
  
  string16 CellularDataPlan::GetRemainingWarning() const {
    if (plan_type == chromeos::CELLULAR_DATA_PLAN_UNLIMITED) {
      // Time based plan. Show nearing expiration and data expiration.
      if (remaining_time().InSeconds() <= chromeos::kCellularDataVeryLowSecs) {
        return GetPlanExpiration();
      }
    } else if (plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_PAID ||
               plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_BASE) {
      // Metered plan. Show low data and out of data.
      if (remaining_data() <= chromeos::kCellularDataVeryLowBytes) {
        int64 remaining_mbytes = remaining_data() / (1024 * 1024);
        return l10n_util::GetStringFUTF16(
            IDS_NETWORK_DATA_REMAINING_MESSAGE,
            UTF8ToUTF16(base::Int64ToString(remaining_mbytes)));
      }
    }
    return string16();
  }
  
  string16 CellularDataPlan::GetDataRemainingDesciption() const {
    int64 remaining_bytes = remaining_data();
    switch (plan_type) {
      case chromeos::CELLULAR_DATA_PLAN_UNLIMITED: {
        return l10n_util::GetStringUTF16(
            IDS_OPTIONS_SETTINGS_INTERNET_OPTIONS_UNLIMITED);
      }
      case chromeos::CELLULAR_DATA_PLAN_METERED_PAID: {
        return ui::FormatBytes(remaining_bytes);
      }
      case chromeos::CELLULAR_DATA_PLAN_METERED_BASE: {
        return ui::FormatBytes(remaining_bytes);
      }
      default:
        break;
    }
    return string16();
  }
  
  string16 CellularDataPlan::GetUsageInfo() const {
    if (plan_type == chromeos::CELLULAR_DATA_PLAN_UNLIMITED) {
      // Time based plan. Show nearing expiration and data expiration.
      return GetPlanExpiration();
    } else if (plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_PAID ||
               plan_type == chromeos::CELLULAR_DATA_PLAN_METERED_BASE) {
      // Metered plan. Show low data and out of data.
      int64 remaining_bytes = remaining_data();
      if (remaining_bytes == 0) {
        return l10n_util::GetStringUTF16(
            IDS_NETWORK_DATA_NONE_AVAILABLE_MESSAGE);
      } else if (remaining_bytes < 1024 * 1024) {
        return l10n_util::GetStringUTF16(
            IDS_NETWORK_DATA_LESS_THAN_ONE_MB_AVAILABLE_MESSAGE);
      } else {
        int64 remaining_mb = remaining_bytes / (1024 * 1024);
        return l10n_util::GetStringFUTF16(
            IDS_NETWORK_DATA_MB_AVAILABLE_MESSAGE,
            UTF8ToUTF16(base::Int64ToString(remaining_mb)));
      }
    }
    return string16();
  }
  
  std::string CellularDataPlan::GetUniqueIdentifier() const {
    // A cellular plan is uniquely described by the union of name, type,
    // start time, end time, and max bytes.
    // So we just return a union of all these variables.
    return plan_name + "|" +
        base::Int64ToString(plan_type) + "|" +
        base::Int64ToString(plan_start_time.ToInternalValue()) + "|" +
        base::Int64ToString(plan_end_time.ToInternalValue()) + "|" +
        base::Int64ToString(plan_data_bytes);
  }
  
  base::TimeDelta CellularDataPlan::remaining_time() const {
    base::TimeDelta time = plan_end_time - base::Time::Now();
    return time.InMicroseconds() < 0 ? base::TimeDelta() : time;
  }
  
  int64 CellularDataPlan::remaining_minutes() const {
    return remaining_time().InMinutes();
  }
  
  int64 CellularDataPlan::remaining_data() const {
    int64 data = plan_data_bytes - data_bytes_used;
    return data < 0 ? 0 : data;
  }
  
  string16 CellularDataPlan::GetPlanExpiration() const {
    return TimeFormat::TimeRemaining(remaining_time());
  }
  
  }  // namespace chromeos
  