summaryrefslogtreecommitdiffstats
path: root/ash/system/bluetooth/tray_bluetooth.cc
blob: 975895f5699e8957ddc53e478d4e2ca9f06b3e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// 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 "ash/system/bluetooth/tray_bluetooth.h"

#include "ash/shell.h"
#include "ash/system/tray/system_tray.h"
#include "ash/system/tray/system_tray_delegate.h"
#include "ash/system/tray/tray_constants.h"
#include "ash/system/tray/tray_item_more.h"
#include "ash/system/tray/tray_views.h"
#include "grit/ash_strings.h"
#include "grit/ui_resources.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "ui/views/controls/image_view.h"
#include "ui/views/controls/label.h"
#include "ui/views/layout/box_layout.h"

namespace {
const int kDeviceListHeight = 190;
}

namespace ash {
namespace internal {

namespace tray {

class BluetoothDefaultView : public TrayItemMore {
 public:
  explicit BluetoothDefaultView(SystemTrayItem* owner)
      : TrayItemMore(owner) {
    SetLayoutManager(new views::BoxLayout(views::BoxLayout::kHorizontal,
        kTrayPopupPaddingHorizontal, 0, kTrayPopupPaddingBetweenItems));
    ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();

    views::ImageView* icon = new FixedSizedImageView(0, kTrayPopupItemHeight);
    icon->SetImage(bundle.GetImageNamed(
        IDR_AURA_UBER_TRAY_BLUETOOTH).ToSkBitmap());
    AddChildView(icon);

    label_ = new views::Label;
    AddChildView(label_);
    UpdateLabel();

    AddMore();
  }

  virtual ~BluetoothDefaultView() {}

  void UpdateLabel() {
    ash::SystemTrayDelegate* delegate =
        ash::Shell::GetInstance()->tray_delegate();
    ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
    label_->SetText(rb.GetLocalizedString(delegate->GetBluetoothEnabled() ?
        IDS_ASH_STATUS_TRAY_BLUETOOTH_CONNECTED :
        IDS_ASH_STATUS_TRAY_BLUETOOTH_DISABLED));
  }

 private:
  views::Label* label_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothDefaultView);
};

class BluetoothDetailedView : public views::View,
                              public ViewClickListener {
 public:
  BluetoothDetailedView()
      : header_(NULL),
        add_device_(NULL),
        toggle_bluetooth_(NULL) {
    SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 1, 1, 1));
    set_background(views::Background::CreateSolidBackground(kBackgroundColor));

    BluetoothDeviceList list;
    Shell::GetInstance()->tray_delegate()->GetAvailableBluetoothDevices(&list);
    Update(list);
  }

  virtual ~BluetoothDetailedView() {}

  void Update(const BluetoothDeviceList& list) {
    RemoveAllChildViews(true);

    header_ = NULL;
    add_device_ = NULL;
    toggle_bluetooth_ = NULL;

    AppendHeaderEntry();
    AppendDeviceList(list);
    AppendSettingsEntries();

    Layout();
  }

 private:
  void AppendHeaderEntry() {
    header_ = CreateDetailedHeaderEntry(IDS_ASH_STATUS_TRAY_BLUETOOTH, this);
    AddChildView(header_);
  }

  void AppendDeviceList(const BluetoothDeviceList& list) {
    views::View* devices = new views::View;
    devices->SetLayoutManager(new views::BoxLayout(
        views::BoxLayout::kVertical, 0, 0, 1));

    for (size_t i = 0; i < list.size(); i++) {
      HoverHighlightView* container = new HoverHighlightView(this);
      container->AddLabel(list[i].display_name,
          list[i].connected ? gfx::Font::BOLD : gfx::Font::NORMAL);
      devices->AddChildView(container);
      device_map_[container] = list[i].address;
    }

    FixedSizedScrollView* scroller = new FixedSizedScrollView;
    scroller->set_border(views::Border::CreateSolidSidedBorder(1, 0, 1, 0,
        SkColorSetARGB(25, 0, 0, 0)));
    scroller->set_fixed_size(
        gfx::Size(devices->GetPreferredSize().width() +
                  scroller->GetScrollBarWidth(),
                  kDeviceListHeight));
    scroller->SetContentsView(devices);
    AddChildView(scroller);
  }

  void AppendSettingsEntries() {
    ash::SystemTrayDelegate* delegate =
        ash::Shell::GetInstance()->tray_delegate();
    HoverHighlightView* container = new HoverHighlightView(this);
    ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
    container->AddLabel(rb.GetLocalizedString(
        delegate->GetBluetoothEnabled() ?
            IDS_ASH_STATUS_TRAY_DISABLE_BLUETOOTH :
            IDS_ASH_STATUS_TRAY_ENABLE_BLUETOOTH), gfx::Font::NORMAL);
    AddChildView(container);
    toggle_bluetooth_ = container;

    container = new HoverHighlightView(this);
    container->AddLabel(rb.GetLocalizedString(
          IDS_ASH_STATUS_TRAY_BLUETOOTH_ADD_DEVICE), gfx::Font::NORMAL);
    AddChildView(container);
    add_device_ = container;
  }

  // Overridden from ViewClickListener.
  virtual void ClickedOn(views::View* sender) OVERRIDE {
    ash::SystemTrayDelegate* delegate =
        ash::Shell::GetInstance()->tray_delegate();
    if (sender == header_) {
      Shell::GetInstance()->tray()->ShowDefaultView();
    } else if (sender == toggle_bluetooth_) {
      delegate->ToggleBluetooth();
    } else if (sender == add_device_) {
      delegate->AddBluetoothDevice();
    } else {
      std::map<views::View*, std::string>::iterator find;
      find = device_map_.find(sender);
      if (find != device_map_.end()) {
        std::string device_id = find->second;
        delegate->ToggleBluetoothConnection(device_id);
      }
    }
  }

  std::map<views::View*, std::string> device_map_;
  views::View* header_;
  views::View* add_device_;
  views::View* toggle_bluetooth_;
  views::View* settings_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothDetailedView);
};

}  // namespace tray

TrayBluetooth::TrayBluetooth()
    : TrayImageItem(IDR_AURA_UBER_TRAY_BLUETOOTH) {
}

TrayBluetooth::~TrayBluetooth() {
}

bool TrayBluetooth::GetInitialVisibility() {
  // Hide at startup. If bluetooth is enabled, the tray-delegate will send a
  // notification, and this will be made visible again.
  return false;
}

views::View* TrayBluetooth::CreateDefaultView(user::LoginStatus status) {
  if (!Shell::GetInstance()->tray_delegate()->GetBluetoothAvailable())
    return NULL;
  default_.reset(new tray::BluetoothDefaultView(this));
  return default_.get();
}

views::View* TrayBluetooth::CreateDetailedView(user::LoginStatus status) {
  if (!Shell::GetInstance()->tray_delegate()->GetBluetoothAvailable())
    return NULL;
  detailed_.reset(new tray::BluetoothDetailedView);
  return detailed_.get();
}

void TrayBluetooth::DestroyDefaultView() {
  default_.reset();
}

void TrayBluetooth::DestroyDetailedView() {
  detailed_.reset();
}

void TrayBluetooth::OnBluetoothRefresh() {
  BluetoothDeviceList list;
  Shell::GetInstance()->tray_delegate()->GetAvailableBluetoothDevices(&list);
  if (default_.get())
    default_->UpdateLabel();
  if (detailed_.get())
    detailed_->Update(list);
}

}  // namespace internal
}  // namespace ash