OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "athena/system/status_icon_container_view.h" |
| 6 |
| 7 #include "athena/resources/athena_resources.h" |
| 8 #include "athena/system/network_selector.h" |
| 9 #include "base/bind.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chromeos/dbus/dbus_thread_manager.h" |
| 14 #include "chromeos/dbus/power_manager/power_supply_properties.pb.h" |
| 15 #include "chromeos/dbus/power_manager_client.h" |
| 16 #include "chromeos/dbus/update_engine_client.h" |
| 17 #include "chromeos/network/network_state.h" |
| 18 #include "chromeos/network/network_state_handler.h" |
| 19 #include "chromeos/network/network_state_handler_observer.h" |
| 20 #include "chromeos/network/network_type_pattern.h" |
| 21 #include "extensions/shell/common/version.h" |
| 22 #include "ui/aura/window.h" |
| 23 #include "ui/base/resource/resource_bundle.h" |
| 24 #include "ui/gfx/image/image.h" |
| 25 #include "ui/gfx/image/image_skia.h" |
| 26 #include "ui/gfx/image/image_skia_operations.h" |
| 27 #include "ui/views/controls/image_view.h" |
| 28 #include "ui/views/controls/label.h" |
| 29 #include "ui/views/layout/box_layout.h" |
| 30 |
| 31 namespace athena { |
| 32 namespace { |
| 33 |
| 34 views::Label* CreateLabel(const std::string& text) { |
| 35 views::Label* label = new views::Label(base::UTF8ToUTF16(text)); |
| 36 label->SetEnabledColor(SK_ColorWHITE); |
| 37 label->SetAutoColorReadabilityEnabled(false); |
| 38 label->SetFontList(gfx::FontList().DeriveWithStyle(gfx::Font::BOLD)); |
| 39 return label; |
| 40 } |
| 41 |
| 42 } // namespace |
| 43 |
| 44 class StatusIconContainerView::PowerStatus |
| 45 : public chromeos::PowerManagerClient::Observer { |
| 46 public: |
| 47 explicit PowerStatus(views::ImageView* icon) : icon_(icon) { |
| 48 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->AddObserver( |
| 49 this); |
| 50 chromeos::DBusThreadManager::Get() |
| 51 ->GetPowerManagerClient() |
| 52 ->RequestStatusUpdate(); |
| 53 } |
| 54 |
| 55 virtual ~PowerStatus() { |
| 56 chromeos::DBusThreadManager::Get()->GetPowerManagerClient()->RemoveObserver( |
| 57 this); |
| 58 } |
| 59 |
| 60 private: |
| 61 const gfx::ImageSkia GetPowerIcon( |
| 62 const power_manager::PowerSupplyProperties& proto) const { |
| 63 // Width and height of battery images. |
| 64 const int kBatteryImageHeight = 25; |
| 65 const int kBatteryImageWidth = 25; |
| 66 |
| 67 // Number of different power states. |
| 68 const int kNumPowerImages = 15; |
| 69 |
| 70 gfx::Image all = ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| 71 IDR_AURA_UBER_TRAY_POWER_SMALL); |
| 72 int horiz_offset = IsCharging(proto) ? 1 : 0; |
| 73 int vert_offset = -1; |
| 74 if (proto.battery_percent() >= 100) { |
| 75 vert_offset = kNumPowerImages - 1; |
| 76 } else { |
| 77 vert_offset = static_cast<int>((kNumPowerImages - 1) * |
| 78 proto.battery_percent() / 100); |
| 79 vert_offset = std::max(std::min(vert_offset, kNumPowerImages - 2), 0); |
| 80 } |
| 81 gfx::Rect region(horiz_offset * kBatteryImageWidth, |
| 82 vert_offset * kBatteryImageHeight, |
| 83 kBatteryImageWidth, |
| 84 kBatteryImageHeight); |
| 85 return gfx::ImageSkiaOperations::ExtractSubset(*all.ToImageSkia(), region); |
| 86 } |
| 87 |
| 88 bool IsCharging(const power_manager::PowerSupplyProperties& proto) const { |
| 89 return proto.external_power() != |
| 90 power_manager::PowerSupplyProperties_ExternalPower_DISCONNECTED; |
| 91 } |
| 92 |
| 93 // chromeos::PowerManagerClient::Observer: |
| 94 virtual void PowerChanged( |
| 95 const power_manager::PowerSupplyProperties& proto) OVERRIDE { |
| 96 icon_->SetImage(GetPowerIcon(proto)); |
| 97 } |
| 98 |
| 99 views::ImageView* icon_; |
| 100 |
| 101 DISALLOW_COPY_AND_ASSIGN(PowerStatus); |
| 102 }; |
| 103 |
| 104 class StatusIconContainerView::NetworkStatus |
| 105 : public chromeos::NetworkStateHandlerObserver { |
| 106 public: |
| 107 explicit NetworkStatus(views::Label* label) : label_(label) { |
| 108 chromeos::NetworkStateHandler* handler = |
| 109 chromeos::NetworkHandler::Get()->network_state_handler(); |
| 110 handler->AddObserver(this, FROM_HERE); |
| 111 } |
| 112 |
| 113 virtual ~NetworkStatus() { |
| 114 chromeos::NetworkStateHandler* handler = |
| 115 chromeos::NetworkHandler::Get()->network_state_handler(); |
| 116 handler->RemoveObserver(this, FROM_HERE); |
| 117 } |
| 118 |
| 119 private: |
| 120 void Update() { |
| 121 std::string status = "<unknown>"; |
| 122 chromeos::NetworkStateHandler* handler = |
| 123 chromeos::NetworkHandler::Get()->network_state_handler(); |
| 124 const chromeos::NetworkState* network = handler->DefaultNetwork(); |
| 125 if (!network) { |
| 126 network = handler->ConnectedNetworkByType( |
| 127 chromeos::NetworkTypePattern::NonVirtual()); |
| 128 } |
| 129 if (network) { |
| 130 status = base::StringPrintf( |
| 131 "%s (%s)", network->ip_address().c_str(), network->name().c_str()); |
| 132 } |
| 133 label_->SetText(base::UTF8ToUTF16(status)); |
| 134 } |
| 135 |
| 136 // chromeos::NetworkStateHandlerObserver: |
| 137 virtual void DefaultNetworkChanged( |
| 138 const chromeos::NetworkState* network) OVERRIDE { |
| 139 Update(); |
| 140 } |
| 141 |
| 142 virtual void NetworkConnectionStateChanged( |
| 143 const chromeos::NetworkState* network) OVERRIDE { |
| 144 Update(); |
| 145 } |
| 146 |
| 147 virtual void NetworkPropertiesUpdated( |
| 148 const chromeos::NetworkState* network) OVERRIDE { |
| 149 Update(); |
| 150 } |
| 151 |
| 152 views::Label* label_; |
| 153 |
| 154 DISALLOW_COPY_AND_ASSIGN(NetworkStatus); |
| 155 }; |
| 156 |
| 157 void StartUpdateCallback( |
| 158 chromeos::UpdateEngineClient::UpdateCheckResult result) { |
| 159 VLOG(1) << "Callback from RequestUpdateCheck, result " << result; |
| 160 } |
| 161 |
| 162 class StatusIconContainerView::UpdateStatus |
| 163 : public chromeos::UpdateEngineClient::Observer { |
| 164 public: |
| 165 explicit UpdateStatus(views::ImageView* icon) : icon_(icon) { |
| 166 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->AddObserver( |
| 167 this); |
| 168 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()-> |
| 169 RequestUpdateCheck(base::Bind(StartUpdateCallback)); |
| 170 } |
| 171 |
| 172 virtual ~UpdateStatus() { |
| 173 chromeos::DBusThreadManager::Get()->GetUpdateEngineClient()->RemoveObserver( |
| 174 this); |
| 175 } |
| 176 |
| 177 // chromeos::UpdateEngineClient::Observer: |
| 178 virtual void UpdateStatusChanged( |
| 179 const chromeos::UpdateEngineClient::Status& status) OVERRIDE { |
| 180 if (status.status != |
| 181 chromeos::UpdateEngineClient::UPDATE_STATUS_UPDATED_NEED_REBOOT) { |
| 182 return; |
| 183 } |
| 184 icon_->SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( |
| 185 IDR_AURA_UBER_TRAY_UPDATE)); |
| 186 } |
| 187 |
| 188 private: |
| 189 views::ImageView* icon_; |
| 190 |
| 191 DISALLOW_COPY_AND_ASSIGN(UpdateStatus); |
| 192 }; |
| 193 |
| 194 StatusIconContainerView::StatusIconContainerView( |
| 195 aura::Window* system_modal_container) |
| 196 : system_modal_container_(system_modal_container) { |
| 197 const int kHorizontalSpacing = 10; |
| 198 const int kVerticalSpacing = 3; |
| 199 const int kBetweenChildSpacing = 10; |
| 200 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal, |
| 201 kHorizontalSpacing, |
| 202 kVerticalSpacing, |
| 203 kBetweenChildSpacing)); |
| 204 |
| 205 std::string version_text = |
| 206 base::StringPrintf("%s (Build %s)", PRODUCT_VERSION, LAST_CHANGE); |
| 207 AddChildView(CreateLabel(version_text)); |
| 208 |
| 209 AddChildView(CreateLabel("Network:")); |
| 210 views::Label* network_label = CreateLabel(std::string()); |
| 211 AddChildView(network_label); |
| 212 network_status_.reset(new NetworkStatus(network_label)); |
| 213 |
| 214 views::ImageView* battery_view = new views::ImageView(); |
| 215 AddChildView(battery_view); |
| 216 power_status_.reset(new PowerStatus(battery_view)); |
| 217 |
| 218 views::ImageView* update_view = new views::ImageView(); |
| 219 AddChildView(update_view); |
| 220 update_status_.reset(new UpdateStatus(update_view)); |
| 221 } |
| 222 |
| 223 StatusIconContainerView::~StatusIconContainerView() { |
| 224 } |
| 225 |
| 226 bool StatusIconContainerView::OnMousePressed(const ui::MouseEvent& event) { |
| 227 CreateNetworkSelector(system_modal_container_); |
| 228 return true; |
| 229 } |
| 230 |
| 231 void StatusIconContainerView::OnGestureEvent(ui::GestureEvent* event) { |
| 232 if (event->type() == ui::ET_GESTURE_TAP) { |
| 233 CreateNetworkSelector(system_modal_container_); |
| 234 event->SetHandled(); |
| 235 } |
| 236 } |
| 237 |
| 238 void StatusIconContainerView::ChildPreferredSizeChanged(views::View* child) { |
| 239 PreferredSizeChanged(); |
| 240 } |
| 241 |
| 242 } // namespace athena |
OLD | NEW |