2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.1 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file StBluetoothPresentationModel.cpp
19 * @brief This is the implementation file for Bluetooth Presentation Model class.
24 #include "StBluetoothPresentationModel.h"
26 using namespace Tizen::App;
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Net::Bluetooth;
31 static const int BLUETOOTH_DISCOVERABLE_TIME_2_MINUTES = 120;
32 static const int BLUETOOTH_DISCOVERABLE_TIME_5_MINUTES = 300;
33 static const int BLUETOOTH_DISCOVERABLE_TIME_1_HOUR = 3600;
35 BluetoothPresentationModel* BluetoothPresentationModel::__pBluetoothPresentationModelInstance = null;
37 BluetoothPresentationModel::BluetoothPresentationModel(void)
38 : __pBluetoothMgr(null)
39 , __pPairedRemoteDeviceList(null)
40 , __pFoundRemoteDeviceList(null)
41 , __pBluetoothPresentationModelEventListener(null)
42 , __bluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_BASE)
46 BluetoothPresentationModel::~BluetoothPresentationModel(void)
48 delete __pBluetoothMgr;
50 __pBluetoothPresentationModelEventListener = null;
51 RemoveFoundRemoteDeviceList();
52 RemovePairedRemoteDeviceList();
55 BluetoothPresentationModel*
56 BluetoothPresentationModel::GetInstance(void)
58 if (__pBluetoothPresentationModelInstance == null)
63 return __pBluetoothPresentationModelInstance;
67 BluetoothPresentationModel::Construct(void)
69 __pBluetoothMgr = new (std::nothrow) BluetoothManager();
70 result r = __pBluetoothMgr->Construct(*this);
73 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
74 delete __pBluetoothMgr;
75 __pBluetoothMgr = null;
79 __pBluetoothMgr->SetBluetoothDeviceListener(this);
81 if (IsBluetoothActivated() == true)
83 UpdatePairedRemoteDeviceList();
90 BluetoothPresentationModel::CreateInstance(void)
92 __pBluetoothPresentationModelInstance = new (std::nothrow) BluetoothPresentationModel();
93 result r = __pBluetoothPresentationModelInstance->Construct();
96 delete __pBluetoothPresentationModelInstance;
97 __pBluetoothPresentationModelInstance = null;
101 std::atexit(DestroyInstance);
105 BluetoothPresentationModel::DestroyInstance(void)
107 delete __pBluetoothPresentationModelInstance;
108 __pBluetoothPresentationModelInstance = null;
112 BluetoothPresentationModel::UpdatePairedRemoteDeviceList(void)
114 RemovePairedRemoteDeviceList();
116 __pPairedRemoteDeviceList = __pBluetoothMgr->GetPairedDeviceListN();
117 if (__pPairedRemoteDeviceList == null)
119 AppLogDebug("__pPairedRemoteDeviceList is null");
124 BluetoothPresentationModel::RemovePairedRemoteDeviceList(void)
126 if (__pPairedRemoteDeviceList == null)
128 AppLogDebug("__pPairedRemoteDeviceList is null");
132 __pPairedRemoteDeviceList->RemoveAll();
133 delete __pPairedRemoteDeviceList;
134 __pPairedRemoteDeviceList = null;
138 BluetoothPresentationModel::InitFoundRemoteDeviceList(void)
140 __pFoundRemoteDeviceList = new (std::nothrow) ArrayList(SingleObjectDeleter);
142 result r = __pFoundRemoteDeviceList->Construct();
145 AppLogDebug("Construct fail [%s]", GetErrorMessage(r));
146 delete __pFoundRemoteDeviceList;
147 __pFoundRemoteDeviceList = null;
154 BluetoothPresentationModel::RemoveFoundRemoteDeviceList(void)
156 if (__pFoundRemoteDeviceList == null)
158 AppLogDebug("__pFoundRemoteDeviceList is null");
162 delete __pFoundRemoteDeviceList;
163 __pFoundRemoteDeviceList = null;
167 BluetoothPresentationModel::GetFoundRemoteDeviceListCount(void)
169 int foundDeviceListCount = 0;
171 if (IsBluetoothActivated() == false || __pFoundRemoteDeviceList == null)
173 if (__pFoundRemoteDeviceList == null)
175 AppLogDebug("__pFoundRemoteDeviceList is null");
179 AppLogDebug("Bluetooth is Deactivated");
181 return foundDeviceListCount;
184 foundDeviceListCount = __pFoundRemoteDeviceList->GetCount();
185 return foundDeviceListCount;
189 BluetoothPresentationModel::SetBluetoothVisibleMode(BluetoothVisibleMode mode)
191 int discoveryTime = 0;
192 BluetoothDiscoverableMode discoverablemode = BT_DISC_MODE_NOT_DISCOVERABLE;
196 case BLUETOOTH_VISIBLE_MDOE_2_MINUTES:
198 discoveryTime = BLUETOOTH_DISCOVERABLE_TIME_2_MINUTES;
199 discoverablemode = BT_DISC_MODE_TIME_LIMITED_DISCOVERABLE;
203 case BLUETOOTH_VISIBLE_MODE_5_MINUTES:
205 discoveryTime = BLUETOOTH_DISCOVERABLE_TIME_5_MINUTES;
206 discoverablemode = BT_DISC_MODE_TIME_LIMITED_DISCOVERABLE;
210 case BLUETOOTH_VISIBLE_MODE_1_HOUR:
212 discoveryTime = BLUETOOTH_DISCOVERABLE_TIME_1_HOUR;
213 discoverablemode = BT_DISC_MODE_TIME_LIMITED_DISCOVERABLE;
217 case BLUETOOTH_VISIBLE_MODE_ALWAYS_ON:
219 discoverablemode = BT_DISC_MODE_DISCOVERABLE;
227 return __pBluetoothMgr->SetDiscoverableMode(discoverablemode, discoveryTime);
231 BluetoothPresentationModel::GetBluetoothVisibleMode(void)
233 BluetoothVisibleMode visibleMode = BLUETOOTH_VISIBLE_MODE_BASE;
235 BluetoothDiscoverableMode discoverableMode = __pBluetoothMgr->GetDiscoverableMode();
236 switch (discoverableMode)
238 case BT_DISC_MODE_NOT_DISCOVERABLE:
240 visibleMode = BLUETOOTH_VISIBLE_MODE_OFF;
244 case BT_DISC_MODE_DISCOVERABLE:
246 visibleMode = BLUETOOTH_VISIBLE_MODE_ALWAYS_ON;
258 BluetoothPresentationModel::GetBluetoothRemainingVisibleTime(void)
260 String remainingTime;
261 AppResource* pAppResource = Application::GetInstance()->GetAppResource();
263 BluetoothDiscoverableMode mode = __pBluetoothMgr->GetDiscoverableMode();
266 case BT_DISC_MODE_NOT_DISCOVERABLE:
268 pAppResource->GetString(L"IDS_ST_BODY_OFF", remainingTime);
272 case BT_DISC_MODE_DISCOVERABLE:
274 pAppResource->GetString(L"IDS_ST_BODY_ALWAYS_ON", remainingTime);
278 case BT_DISC_MODE_TIME_LIMITED_DISCOVERABLE:
280 int remainingSec = __pBluetoothMgr->GetRemainingTimeAsDiscoverable();
281 TimeSpan timeSpan(0, 0, remainingSec);
283 remainingTime.Append(Integer::ToString(timeSpan.GetMinutes()));
284 remainingTime.Append(L":");
285 remainingTime.Append(Integer::ToString(timeSpan.GetSeconds()));
293 return remainingTime;
297 BluetoothPresentationModel::SetEventListener(const IBluetoothPresentationModelEventListener* pEventListener)
299 __pBluetoothPresentationModelEventListener = pEventListener;
303 BluetoothPresentationModel::ActivateBluetooth(void)
305 result r = __pBluetoothMgr->Activate();
308 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_ACTIVATING);
315 BluetoothPresentationModel::DeactivateBluetooth(void)
317 if (__pBluetoothMgr->IsActivated() == false)
319 AppLogDebug("Already is Deactivated");
320 return E_INVALID_OPERATION;
323 result r = __pBluetoothMgr->Deactivate();
326 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_DEACTIVATING);
333 BluetoothPresentationModel::StartBluetoothDiscovery(void)
335 result r = __pBluetoothMgr->StartDiscovery();
338 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_DISCOVERY_STARTING);
345 BluetoothPresentationModel::CancelBluetoothDiscovery(void)
347 if (__pBluetoothMgr->IsDiscoveryInProgress() == false)
349 AppLogDebug("__pBluetoothMgr is not perform to Discovery");
353 result r = __pBluetoothMgr->CancelDiscovery();
356 if (__bluetoothEventInProgress != BLUETOOTH_PRESENTATION_MODEL_EVENT_DISCOVERY_EXIT)
358 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_CANCEL_DISCOVERYING);
366 BluetoothPresentationModel::PairToFoundBluetoothDeviceAt(int index)
368 if (__pFoundRemoteDeviceList == null)
370 AppLogDebug("__pFoundRemoteDeviceList is null");
374 BluetoothDevice* pRemoteDevice = static_cast<BluetoothDevice*>(__pFoundRemoteDeviceList->GetAt(index));
375 if (pRemoteDevice == null)
377 AppLogDebug("GetAt fail[%d]", index);
381 result r = __pBluetoothMgr->Pair(*pRemoteDevice);
384 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_PAIRING);
391 BluetoothPresentationModel::UnpairToPairedBluetoothDeviceAt(int index)
393 if (__pPairedRemoteDeviceList == null)
395 AppLogDebug("__pPairedRemoteDeviceList is null");
399 BluetoothDevice* pRemoteDevice = static_cast<BluetoothDevice*>(__pPairedRemoteDeviceList->GetAt(index));
400 if (pRemoteDevice == null)
402 AppLogDebug("GetAt fail[%d]", index);
406 result r = __pBluetoothMgr->Unpair(*pRemoteDevice);
409 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_UNPAIRING);
416 BluetoothPresentationModel::SetBluetoothDiscoverableMode(Tizen::Net::Bluetooth::BluetoothDiscoverableMode discoverableMode, int seconds)
418 result r = __pBluetoothMgr->SetDiscoverableMode(discoverableMode, seconds);
421 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_VISIBLE_MODE_CHANGING);
428 BluetoothPresentationModel::CancelBluetoothPair(void)
430 if (GetBluetoothEventInProgress() != BLUETOOTH_PRESENTATION_MODEL_EVENT_PAIRING)
432 AppLogDebug("__pBluetoothMgr is not pairing");
436 result r = __pBluetoothMgr->CancelPair();
439 SetBluetoothEventInProgress(BLUETOOTH_PRESENTATION_MODEL_EVENT_PAIR_CANCELLING);
446 BluetoothPresentationModel::GetBluetoothLocalMacAddress(void)
448 String localMacAddress;
450 localMacAddress = __pBluetoothMgr->GetLocalDeviceAddress();
451 return localMacAddress;
455 BluetoothPresentationModel::IsBluetoothActivated(void)
457 return __pBluetoothMgr->IsActivated();
461 BluetoothPresentationModel::IsBluetoothDiscoveryDone(void)
463 return __pBluetoothMgr->IsDiscoveryInProgress();
467 BluetoothPresentationModel::GetBluetoothLocalDeviceName(void)
469 return __pBluetoothMgr->GetLocalDeviceName();
473 BluetoothPresentationModel::GetFoundDeviceListAt(int index)
475 String foundDevieName;
476 if ((IsBluetoothActivated() == false)
477 || (__pFoundRemoteDeviceList == null))
479 if (__pFoundRemoteDeviceList == null)
481 AppLogDebug("__pFoundRemoteDeviceList is null");
485 AppLogDebug("Bluetooth is Deactivated");
487 return foundDevieName;
490 BluetoothDevice* pRemoteDevice = static_cast<BluetoothDevice*>(__pFoundRemoteDeviceList->GetAt(index));
491 if (pRemoteDevice == null)
493 AppLogDebug("GetAt fail[%d]", index);
494 return foundDevieName;
497 return pRemoteDevice->GetName();
500 Tizen::Net::Bluetooth::BluetoothMajorDeviceClassType
501 BluetoothPresentationModel::GetMajorDeviceClasstypeInFoundDeviceAt(int index)
503 BluetoothMajorDeviceClassType majorDeviceClassType = BT_COD_MAJ_DEV_CLS_UNCLASSIFIED;
504 if ((IsBluetoothActivated() == false)
505 || (__pFoundRemoteDeviceList == null))
507 if (__pFoundRemoteDeviceList == null)
509 AppLogDebug("__pFoundRemoteDeviceList is null");
513 AppLogDebug("Bluetooth is Deactivated");
515 SetLastResult(E_FAILURE);
516 return majorDeviceClassType;
519 BluetoothDevice* pRemoteDevice = static_cast<BluetoothDevice*>(__pFoundRemoteDeviceList->GetAt(index));
520 if (pRemoteDevice == null)
522 AppLogDebug("GetAt fail[%d]", index);
523 SetLastResult(E_FAILURE);
524 return majorDeviceClassType;
527 return pRemoteDevice->GetMajorDeviceClassType();
531 BluetoothPresentationModel::GetPairedDeviceNameAt(int index)
533 String pairedDeviceName;
534 if (__pPairedRemoteDeviceList == null)
536 AppLogDebug("__pPairedRemoteDeviceList is null");
537 SetLastResult(E_FAILURE);
538 return pairedDeviceName;
541 if (IsBluetoothActivated() == false)
543 AppLogDebug("Bluetooth is Deactivated");
544 SetLastResult(E_FAILURE);
545 return pairedDeviceName;
548 BluetoothDevice* pRemoteDevice = static_cast<BluetoothDevice*>(__pPairedRemoteDeviceList->GetAt(index));
549 if (pRemoteDevice == null)
551 AppLogDebug("GetAt fail[%d]", index);
552 SetLastResult(E_FAILURE);
553 return pairedDeviceName;
556 return pRemoteDevice->GetName();
559 Tizen::Net::Bluetooth::BluetoothMajorDeviceClassType
560 BluetoothPresentationModel::GetMajorDeviceClassTypeInPairedDeviceAt(int index)
562 BluetoothMajorDeviceClassType majorDeviceClassType = BT_COD_MAJ_DEV_CLS_UNCLASSIFIED;
564 if (IsBluetoothActivated() == false)
566 AppLogDebug("Bluetooth is Deactivated");
567 SetLastResult(E_FAILURE);
568 return majorDeviceClassType;
571 BluetoothDevice* pRemoteDevice = static_cast<BluetoothDevice*>(__pPairedRemoteDeviceList->GetAt(index));
572 if (pRemoteDevice == null)
574 AppLogDebug("GetAt fail[%d]", index);
575 SetLastResult(E_FAILURE);
576 return majorDeviceClassType;
579 return pRemoteDevice->GetMajorDeviceClassType();
583 BluetoothPresentationModel::GetPairedDeviceListCount(void)
585 int pairedDeviceListCount = 0;
587 if (IsBluetoothActivated() == false)
589 AppLogDebug("Bluetooth is Deactivated");
590 SetLastResult(E_FAILURE);
591 return pairedDeviceListCount;
594 pairedDeviceListCount = __pPairedRemoteDeviceList->GetCount();
595 return pairedDeviceListCount;
599 BluetoothPresentationModel::FireIBluetoothPresentationModelEventListener(BluetoothPresentationModelEvent bluetoothEvent, result r, Tizen::Net::Bluetooth::BluetoothDevice* pDevice)
601 if (__pBluetoothPresentationModelEventListener == null)
603 AppLogDebug("__pBluetoothPresentationModelEventListener is null");
607 SetBluetoothEventInProgress(bluetoothEvent);
608 (const_cast <IBluetoothPresentationModelEventListener*>(__pBluetoothPresentationModelEventListener))->OnBluetoothPresentationModelEventCompletedN(bluetoothEvent, r, pDevice);
614 BluetoothPresentationModel::SetBluetoothEventInProgress(BluetoothPresentationModelEvent lastbluetoothEvent)
616 __bluetoothEventInProgress = lastbluetoothEvent;
619 BluetoothPresentationModelEvent
620 BluetoothPresentationModel::GetBluetoothEventInProgress(void)
622 return __bluetoothEventInProgress;
626 BluetoothPresentationModel::OnBluetoothActivated(result r)
628 UpdatePairedRemoteDeviceList();
629 InitFoundRemoteDeviceList();
630 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_ACTIVATED, r);
634 BluetoothPresentationModel::OnBluetoothDeactivated(result r)
638 RemoveFoundRemoteDeviceList();
639 RemovePairedRemoteDeviceList();
641 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_DEACTIVATED, r);
645 BluetoothPresentationModel::OnBluetoothDiscoveryDone(bool isCompleted)
647 BluetoothPresentationModelEvent bluetoothPresentationModelEvent = BLUETOOTH_PRESENTATION_MODEL_EVENT_DISCOVERY_CANCELED;
648 if (isCompleted == true)
650 bluetoothPresentationModelEvent = BLUETOOTH_PRESENTATION_MODEL_EVENT_DISCOVERY_DONE;
653 if (__bluetoothEventInProgress != BLUETOOTH_PRESENTATION_MODEL_EVENT_DISCOVERY_EXIT)
655 FireIBluetoothPresentationModelEventListener(bluetoothPresentationModelEvent);
660 BluetoothPresentationModel::OnBluetoothDiscoveryStarted(result r)
662 InitFoundRemoteDeviceList();
663 if (__bluetoothEventInProgress != BLUETOOTH_PRESENTATION_MODEL_EVENT_DISCOVERY_EXIT)
665 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_DISCOVERY_STARTED, r);
670 BluetoothPresentationModel::OnBluetoothPairingFailed(result r)
672 AppLogDebug("OnBluetoothPairingFailed [%s]", GetErrorMessage(r));
673 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_PAIRING_FAILED, r);
677 BluetoothPresentationModel::OnBluetoothPaired(const Tizen::Net::Bluetooth::BluetoothDevice& pairedDevice)
679 AppLogDebug("OnBluetoothPaired");
681 if (__pFoundRemoteDeviceList != null)
683 __pFoundRemoteDeviceList->Remove(pairedDevice, true);
686 UpdatePairedRemoteDeviceList();
687 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_PAIRED);
691 BluetoothPresentationModel::OnBluetoothRemoteDeviceFoundN(Tizen::Net::Bluetooth::BluetoothDevice* pFoundDevice)
693 result r = E_FAILURE;
694 bool isExistDevice = false;
695 if (__pFoundRemoteDeviceList != null)
697 if (__pPairedRemoteDeviceList != null)
699 isExistDevice = __pPairedRemoteDeviceList->Contains(*pFoundDevice);
702 if (isExistDevice == false)
704 r = __pFoundRemoteDeviceList->Add(*pFoundDevice);
708 AppLogDebug("Add fail[%s]", GetErrorMessage(r));
713 AppLogDebug("Already paired device[%ls]", pFoundDevice->GetName().GetPointer());
718 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_REMOTE_DEVICE_FOUND, r);
722 BluetoothPresentationModel::OnBluetoothUnpaired(const Tizen::Net::Bluetooth::BluetoothDevice& unpairedDevice)
724 if (__pFoundRemoteDeviceList == null)
726 InitFoundRemoteDeviceList();
729 BluetoothDevice* pDevice = new (std::nothrow) BluetoothDevice(unpairedDevice);
732 __pFoundRemoteDeviceList->Add(*pDevice);
735 UpdatePairedRemoteDeviceList();
736 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_UNPAIRED);
740 BluetoothPresentationModel::OnBluetoothDiscoverableModeChanged(Tizen::Net::Bluetooth::BluetoothDiscoverableMode mode)
742 FireIBluetoothPresentationModelEventListener(BLUETOOTH_PRESENTATION_MODEL_EVENT_VISIBLE_MODE_CHANGED);