Bluetooth: Added settings to remember last used device.

Signed-off-by: Matt Madia <mattmadia@gmail.com>
This commit is contained in:
Tri-Edge AI 2012-12-16 19:11:54 +02:00 committed by Matt Madia
parent 9741a1b5b0
commit e381b02559
9 changed files with 257 additions and 88 deletions

View File

@ -77,7 +77,7 @@ BluetoothApplication::MessageReceived(BMessage* message)
BMessageRunner::StartSending(be_app_messenger,
new BMessage('Xtmp'), 2 * 1000000, 1);
} else {
fWindow = new BluetoothWindow(BRect(100, 100, 550, 420));
fWindow = new BluetoothWindow(BRect(100, 100, 750, 420));
fWindow->Show();
}
break;

View File

@ -0,0 +1,56 @@
/*
* Copyright 2008-2009, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes@gmail.com>
* Copyright 2012-2013, Tri-Edge AI <triedgeai@gmail.com>
*
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "BluetoothSettings.h"
BluetoothSettings::BluetoothSettings()
{
find_directory(B_USER_SETTINGS_DIRECTORY, &fPath);
fPath.Append("Bluetooth_settings", true);
}
BluetoothSettings::~BluetoothSettings()
{
}
void
BluetoothSettings::Defaults()
{
Data.PickedDevice = bdaddrUtils::NullAddress();
}
void
BluetoothSettings::Load()
{
fFile = new BFile(fPath.Path(), B_READ_ONLY);
if (fFile->InitCheck() == B_OK) {
fFile->Read(&Data, sizeof(Data));
// TODO: Add more settings here.
} else
Defaults();
delete fFile;
}
void
BluetoothSettings::Save()
{
fFile = new BFile(fPath.Path(), B_WRITE_ONLY | B_CREATE_FILE);
if (fFile->InitCheck() == B_OK) {
fFile->Write(&Data, sizeof(Data));
// TODO: Add more settings here.
}
delete fFile;
}

View File

@ -0,0 +1,37 @@
/*
* Copyright 2008-2009, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes@gmail.com>
* Copyright 2012-2013, Tri-Edge AI <triedgeai@gmail.com>
*
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef BLUETOOTH_SETTINGS_H
#define BLUETOOTH_SETTINGS_H
#include <bluetooth/bdaddrUtils.h>
#include <bluetooth/LocalDevice.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
class BluetoothSettings
{
public:
struct {
bdaddr_t PickedDevice;
} Data;
BluetoothSettings();
~BluetoothSettings();
void Defaults();
void Load();
void Save();
private:
BPath fPath;
BFile* fFile;
};
#endif // BLUETOOTH_SETTINGS_H

View File

@ -1,11 +1,18 @@
/*
* Copyright 2008-09, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes_at_gmail.com>
* Copyright 2008-2009, Oliver Ruiz Dorantes <oliver.ruiz.dorantes@gmail.com>
* Copyright 2012-2013, Tri-Edge AI, <triedgeai@gmail.com>
*
* All rights reserved. Distributed under the terms of the MIT License.
*/
#include "BluetoothSettingsView.h"
#include <stdio.h>
#include <stdlib.h>
#include "defs.h"
#include "BluetoothSettings.h"
#include "BluetoothWindow.h"
#include "ExtendedLocalDeviceView.h"
#include <bluetooth/LocalDevice.h>
#include <Box.h>
#include <Catalog.h>
@ -19,12 +26,8 @@
#include <String.h>
#include <TextView.h>
#include <bluetooth/LocalDevice.h>
#include "ExtendedLocalDeviceView.h"
#include "defs.h"
#include "BluetoothWindow.h"
#include <stdio.h>
#include <stdlib.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Settings view"
@ -45,13 +48,15 @@ static const char* kLaptopLabel = B_TRANSLATE_MARK("Laptop");
static const char* kHandheldLabel = B_TRANSLATE_MARK("Handheld");
static const char* kPhoneLabel = B_TRANSLATE_MARK("Smart phone");
// #pragma mark -
BluetoothSettingsView::BluetoothSettingsView(const char* name)
: BView(name, 0),
:
BView(name, 0),
fLocalDevicesMenu(NULL)
{
fSettings.Load();
_BuildConnectionPolicy();
fPolicyMenuField = new BMenuField("policy",
B_TRANSLATE("Incoming connections policy:"), fPolicyMenu);
@ -65,10 +70,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name)
fInquiryTimeControl->SetHashMarkCount(255 / 15);
fInquiryTimeControl->SetEnabled(true);
// hinting menu
_BuildClassMenu();
fClassMenuField = new BMenuField("class", B_TRANSLATE("Identify host as:"),
fClassMenu);
fExtDeviceView = new ExtendedLocalDeviceView(BRect(0, 0, 5, 5), NULL);
// localdevices menu
_BuildLocalDevicesMenu();
@ -76,13 +78,20 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name)
B_TRANSLATE("Local devices found on system:"),
fLocalDevicesMenu);
fExtDeviceView = new ExtendedLocalDeviceView(BRect(0, 0, 5, 5), NULL);
SetLayout(new BGroupLayout(B_VERTICAL));
if (ActiveLocalDevice != NULL) {
fExtDeviceView->SetLocalDevice(ActiveLocalDevice);
fExtDeviceView->SetEnabled(true);
}
// hinting menu
_BuildClassMenu();
fClassMenuField = new BMenuField("class", B_TRANSLATE("Identify host as:"),
fClassMenu);
// controls pane
AddChild(BGridLayoutBuilder(10, 10)
.Add(fClassMenuField->CreateLabelLayoutItem(), 0, 0)
.Add(fClassMenuField->CreateMenuBarLayoutItem(), 1, 0)
@ -107,7 +116,7 @@ BluetoothSettingsView::BluetoothSettingsView(const char* name)
BluetoothSettingsView::~BluetoothSettingsView()
{
fSettings.Save();
}
@ -133,44 +142,52 @@ BluetoothSettingsView::MessageReceived(BMessage* message)
case kMsgLocalSwitched:
{
LocalDevice* lDevice;
if (message->FindPointer("LocalDevice", (void**) &lDevice) == B_OK) {
// Device integrity should be rechecked
fExtDeviceView->SetLocalDevice(lDevice);
fExtDeviceView->SetEnabled(true);
ActiveLocalDevice = lDevice;
if (message->FindPointer("LocalDevice",
(void**)&lDevice) == B_OK) {
_MarkLocalDevice(lDevice);
}
break;
}
break;
/*
To be fixed :)
// TODO: To be fixed. :)
/*
case kMsgSetConnectionPolicy:
{
//uint8 Policy;
//if (message->FindInt8("Policy", (int8*)&Policy) == B_OK)
break;
}
case kMsgSetInquiryTime:
{
break;
}*/
}
*/
case kMsgSetDeviceClass:
{
uint8 deviceClass;
if (message->FindInt8("DeviceClass", (int8*)&deviceClass) == B_OK) {
if (message->FindInt8("DeviceClass",
(int8*)&deviceClass) == B_OK) {
if (deviceClass == 5)
_SetDeviceClass(2, 3, 0x72);
else
_SetDeviceClass(1, deviceClass, 0x72);
}
break;
}
case kMsgRefresh:
{
_BuildLocalDevicesMenu();
fLocalDevicesMenu->SetTargetForItems(this);
break;
break;
}
default:
BView::MessageReceived(message);
}
@ -178,17 +195,18 @@ BluetoothSettingsView::MessageReceived(BMessage* message)
bool
BluetoothSettingsView::_SetDeviceClass(uint8 major, uint8 minor, uint16 service)
BluetoothSettingsView::_SetDeviceClass(uint8 major, uint8 minor,
uint16 service)
{
bool haveRun = true;
DeviceClass devClass;
devClass.SetRecord(major, minor, service);
DeviceClass devClass(major, minor, service);
if (ActiveLocalDevice != NULL)
ActiveLocalDevice->SetDeviceClass(devClass);
else
haveRun = false;
return haveRun;
}
@ -217,13 +235,16 @@ BluetoothSettingsView::_BuildConnectionPolicy()
fPolicyMenu->AddItem(item);
}
void
BluetoothSettingsView::_BuildClassMenu()
{
BMessage* message = NULL;
BMenuItem* item = NULL;
DeviceClass devClass;
if (ActiveLocalDevice != NULL) {
devClass = ActiveLocalDevice->GetDeviceClass();
}
fClassMenu = new BPopUpMenu(B_TRANSLATE("Identify us as..."));
@ -232,25 +253,45 @@ BluetoothSettingsView::_BuildClassMenu()
item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kDesktopLabel), message);
fClassMenu->AddItem(item);
if (devClass.MajorDeviceClass() == 1 &&
devClass.MinorDeviceClass() == 1)
item->SetMarked(true);
message = new BMessage(kMsgSetDeviceClass);
message->AddInt8("DeviceClass", 2);
item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kServerLabel), message);
fClassMenu->AddItem(item);
if (devClass.MajorDeviceClass() == 1 &&
devClass.MinorDeviceClass() == 2)
item->SetMarked(true);
message = new BMessage(kMsgSetDeviceClass);
message->AddInt8("DeviceClass", 3);
item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kLaptopLabel), message);
fClassMenu->AddItem(item);
if (devClass.MajorDeviceClass() == 1 &&
devClass.MinorDeviceClass() == 3)
item->SetMarked(true);
message = new BMessage(kMsgSetDeviceClass);
message->AddInt8("DeviceClass", 4);
item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kHandheldLabel), message);
fClassMenu->AddItem(item);
if (devClass.MajorDeviceClass() == 1 &&
devClass.MinorDeviceClass() == 4)
item->SetMarked(true);
message = new BMessage(kMsgSetDeviceClass);
message->AddInt8("DeviceClass", 5);
item = new BMenuItem(B_TRANSLATE_NOCOLLECT(kPhoneLabel), message);
fClassMenu->AddItem(item);
if (devClass.MajorDeviceClass() == 2 &&
devClass.MinorDeviceClass() == 3)
item->SetMarked(true);
}
@ -260,20 +301,47 @@ BluetoothSettingsView::_BuildLocalDevicesMenu()
LocalDevice* lDevice;
if (!fLocalDevicesMenu)
fLocalDevicesMenu = new BPopUpMenu(B_TRANSLATE("Pick LocalDevice..."));
fLocalDevicesMenu = new BPopUpMenu(B_TRANSLATE("Pick device..."));
for (uint32 index = 0; index < LocalDevice::GetLocalDeviceCount(); index++) {
while (fLocalDevicesMenu->CountItems() > 0) {
BMenuItem* item = fLocalDevicesMenu->RemoveItem(0L);
if (item != NULL) {
delete item;
}
}
ActiveLocalDevice = NULL;
for (uint32 i = 0; i < LocalDevice::GetLocalDeviceCount(); i++) {
lDevice = LocalDevice::GetLocalDevice();
if (lDevice != NULL) {
// TODO Check if they already exists
if (lDevice != NULL) {
BMessage* message = new BMessage(kMsgLocalSwitched);
message->AddPointer("LocalDevice", lDevice);
BMenuItem* item = new BMenuItem((lDevice->GetFriendlyName().String()),
message);
BMenuItem* item = new BMenuItem(
(lDevice->GetFriendlyName().String()), message);
if (bdaddrUtils::Compare(lDevice->GetBluetoothAddress(),
fSettings.Data.PickedDevice)) {
item->SetMarked(true);
ActiveLocalDevice = lDevice;
}
fLocalDevicesMenu->AddItem(item);
}
}
}
void
BluetoothSettingsView::_MarkLocalDevice(LocalDevice* lDevice)
{
// TODO: Device integrity should be rechecked.
fExtDeviceView->SetLocalDevice(lDevice);
fExtDeviceView->SetEnabled(true);
ActiveLocalDevice = lDevice;
fSettings.Data.PickedDevice = lDevice->GetBluetoothAddress();
}

View File

@ -1,49 +1,58 @@
/*
* Copyright 2008-09, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes_at_gmail.com>
* Copyright 2008-2009, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes@gmail.com>
* Copyright 2012-2013, Tri-Edge AI, <triedgeai@gmail.com>
*
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef BLUETOOTH_SETTINGS_VIEW_H
#define BLUETOOTH_SETTINGS_VIEW_H
#include "BluetoothSettings.h"
#include <View.h>
class BluetoothSettings;
class ExtendedLocalDeviceView;
class LocalDevice;
class BBox;
class BMenuField;
class BPopUpMenu;
class BSlider;
class ExtendedLocalDeviceView;
class BluetoothSettingsView : public BView {
public:
BluetoothSettingsView(const char* name);
virtual ~BluetoothSettingsView();
BluetoothSettingsView(const char* name);
virtual ~BluetoothSettingsView();
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message);
virtual void AttachedToWindow();
virtual void MessageReceived(BMessage* message);
private:
void _BuildConnectionPolicy();
void _BuildClassMenu();
void _BuildLocalDevicesMenu();
bool _SetDeviceClass(uint8 major, uint8 minor
, uint16 service);
void _BuildConnectionPolicy();
void _BuildClassMenu();
void _BuildLocalDevicesMenu();
bool _SetDeviceClass(uint8 major, uint8 minor,
uint16 service);
void _MarkLocalDevice(LocalDevice* lDevice);
protected:
float fDivider;
BluetoothSettings fSettings;
BMenuField* fPolicyMenuField;
BPopUpMenu* fPolicyMenu;
BMenuField* fClassMenuField;
BPopUpMenu* fClassMenu;
BMenuField* fLocalDevicesMenuField;
BPopUpMenu* fLocalDevicesMenu;
float fDivider;
ExtendedLocalDeviceView* fExtDeviceView;
BMenuField* fPolicyMenuField;
BPopUpMenu* fPolicyMenu;
BMenuField* fClassMenuField;
BPopUpMenu* fClassMenu;
BMenuField* fLocalDevicesMenuField;
BPopUpMenu* fLocalDevicesMenu;
BSlider* fInquiryTimeControl;
ExtendedLocalDeviceView* fExtDeviceView;
BSlider* fInquiryTimeControl;
};

View File

@ -89,7 +89,6 @@ BluetoothWindow::BluetoothWindow(BRect frame)
// tabView->AddTab(fConnChan);
tabView->AddTab(fSettingsView);
fRevertButton->SetEnabled(false);
AddChild(BGroupLayoutBuilder(B_VERTICAL, 0)

View File

@ -2,37 +2,36 @@
* Copyright 2008-09, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes_at_gmail.com>
* All rights reserved. Distributed under the terms of the MIT License.
*/
#ifndef BLUETOOTH_WINDOW_H
#define BLUETOOTH_WINDOW_H
#include "BluetoothSettingsView.h"
#include <Application.h>
#include <Button.h>
#include <Window.h>
#include <Message.h>
#include <TabView.h>
#include "BluetoothSettingsView.h"
class BluetoothSettingsView;
class RemoteDevicesView;
class ConnChanView;
class BluetoothWindow : public BWindow
{
class BluetoothWindow : public BWindow {
public:
BluetoothWindow(BRect frame);
BluetoothWindow(BRect frame);
bool QuitRequested(void);
void MessageReceived(BMessage *message);
private:
RemoteDevicesView* fRemoteDevices;
ConnChanView* fConnChan;
BButton* fDefaultsButton;
BButton* fRevertButton;
BMenuBar* fMenubar;
BluetoothSettingsView* fSettingsView;
RemoteDevicesView* fRemoteDevices;
ConnChanView* fConnChan;
BButton* fDefaultsButton;
BButton* fRevertButton;
BMenuBar* fMenubar;
BluetoothSettingsView* fSettingsView;
};
#endif

View File

@ -72,7 +72,7 @@ ExtendedLocalDeviceView::SetLocalDevice(LocalDevice* lDevice)
fDevice = lDevice;
SetName(lDevice->GetFriendlyName().String());
fDeviceView->SetBluetoothDevice(lDevice);
ClearDevice();
int value = fDevice->GetDiscoverable();
@ -110,12 +110,12 @@ void
ExtendedLocalDeviceView::MessageReceived(BMessage* message)
{
printf("ExtendedLocalDeviceView::MessageReceived\n");
if (fDevice == NULL) {
printf("ExtendedLocalDeviceView::Device missing\n");
return;
}
if (message->WasDropped()) {
}
@ -157,7 +157,7 @@ void
ExtendedLocalDeviceView::SetEnabled(bool value)
{
printf("ExtendedLocalDeviceView::SetEnabled\n");
fVisible->SetEnabled(value);
fAuthentication->SetEnabled(value);
fDiscoverable->SetEnabled(value);
@ -168,7 +168,7 @@ void
ExtendedLocalDeviceView::ClearDevice()
{
printf("ExtendedLocalDeviceView::ClearDevice\n");
fVisible->SetValue(false);
fAuthentication->SetValue(false);
fDiscoverable->SetValue(false);

View File

@ -8,6 +8,7 @@ AddResources Bluetooth : bluetooth-pref.rdef ;
Preference Bluetooth :
BluetoothDeviceView.cpp
BluetoothMain.cpp
BluetoothSettings.cpp
BluetoothSettingsView.cpp
BluetoothWindow.cpp
DeviceListItem.cpp