summaryrefslogtreecommitdiff
path: root/project/src/MainForm.cpp
blob: 0696f394544bcf42cce4b95d5bcf10eda0b11f8f (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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//
// Tizen Native SDK
// Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
//
// Licensed under the Flora License, Version 1.1 (the License);
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://floralicense.org/license/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an AS IS BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include <new>
#include <FNet.h>
#include <FApp.h>
#include <FIo.h>
#include <FMedia.h>
#include "MainForm.h"
#include "ProfileForm.h"
#include "FriendListForm.h"

using namespace Tizen::App;
using namespace Tizen::Base;
using namespace Tizen::Base::Collection;
using namespace Tizen::Base::Utility;
using namespace Tizen::Net::Bluetooth;
using namespace Tizen::Ui::Controls;
using namespace Tizen::Ui::Scenes;
using namespace Tizen::Io;
using namespace Tizen::Media;
using namespace Tizen::Graphics;

static const int FRIEND_FINDER_PROGRESS_HALF = 50;
const wchar_t* FRIEND_FINDER_USER_INFO_PATH = L"data/FriendFinderInfo.txt";
const byte FRIEND_FINDER_SERVICE_UUID[] = {0x56, 0x46, 0x59, 0xE0, 0x55, 0x72, 0x11, 0xE2, 0xBC, 0xFD, 0x08, 0x00, 0x20, 0x0C, 0x9A, 0x66};

MainForm::MainForm(void)
	: __pProfilePanel(null)
	, __pFriendProfileLabel(null)
	, __pConnectionStatusLabel(null)
	, __pRequestPopup(null)
	, __pConnectingPopup(null)
	, __pConnectingCancelButton(null)
	, __pConnectingProgress(null)
	, __pNoticePopup(null)
	, __pProfileForm(null)
	, __pFriendListForm(null)
	, __connManager(this)
	, __friendImagePath(L"")
{
}

MainForm::~MainForm(void)
{
}

bool
MainForm::Initialize(void)
{
	Construct(FORM_STYLE_NORMAL | FORM_STYLE_HEADER | FORM_STYLE_INDICATOR | FORM_STYLE_FOOTER);

	// initializes the ConnectionManager
	TryReturn(__connManager.Initialize(), false, "Bluetooth resources are not available.");
	return true;
}

result
MainForm::OnInitializing(void)
{
	result r = E_SUCCESS;
	Header* pHeader = GetHeader();
	Rectangle clientArea = GetClientAreaBounds();

	// generates the header and the footer
	if (pHeader != null)
	{
		pHeader->SetStyle(HEADER_STYLE_TITLE);
		pHeader->SetTitleText(L"Friend finder");
	}

	Footer* pFooter = GetFooter();

	if (pFooter != null)
	{
		pFooter->SetStyle(FOOTER_STYLE_BUTTON_TEXT);

		FooterItem footerItemProfile;
		footerItemProfile.Construct(ID_OPTION_PROFILE);
		footerItemProfile.SetText("My Profile");
		pFooter->AddItem(footerItemProfile);

		FooterItem footerItemFindFriend;
		footerItemFindFriend.Construct(ID_OPTION_FIND_FRIEND);
		footerItemFindFriend.SetText("Search");
		pFooter->AddItem(footerItemFindFriend);

		pFooter->AddActionEventListener(*this);
	}

	SetFormBackEventListener(this);

	// generates the centered label that let users know the status of connection with friends
	__pConnectionStatusLabel = new (std::nothrow) Label();
	__pConnectionStatusLabel->Construct(Rectangle(0, (clientArea.height / 2) - 110, clientArea.width, 150),
			L"No Connected Friend");
	__pConnectionStatusLabel->SetTextConfig(40, LABEL_TEXT_STYLE_NORMAL);
	__pConnectionStatusLabel->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
	__pConnectionStatusLabel->SetTextHorizontalAlignment(ALIGNMENT_CENTER);
	AddControl(*__pConnectionStatusLabel);

	return r;
}

void
MainForm::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
{
	SceneManager* pSceneManager = SceneManager::GetInstance();
	AppAssert(pSceneManager);

	switch (actionId)
	{
	case ID_BUTTON_ACCEPT_CONNECTION:
		ClearFriendProfile();
		__connManager.AcceptConnection();
		break;

	case ID_BUTTON_REJECT_CONNECTION:
		__connManager.RejectConnection();
		break;

	case ID_BUTTON_CANCEL_CONNECTION:
		__connManager.CancelConnection();
		break;

	case ID_BUTTON_NOTICE_POPUP_CLOSE:
		CloseNoticePopup();
		break;

	case ID_OPTION_PROFILE:
		pSceneManager->GoForward(ForwardSceneTransition(L"ProfileScene"));
		break;

	case ID_OPTION_FIND_FRIEND:
		pSceneManager->GoForward(ForwardSceneTransition(L"FriendListScene"));
		break;

	default:
		break;
	}
}

void
MainForm::OnTouchPressed(const Tizen::Ui::Control& source, const Tizen::Graphics::Point& currentPosition,
		const Tizen::Ui::TouchEventInfo& touchInfo)
{
	if (source.Equals(*__pFriendProfileLabel))
	{
		// shows the detailed image of the connected friend by using the Image AppControl.
		String uri = String(L"file://") + __friendImagePath;
		AppControl* __pImageAppControl = AppManager::FindAppControlN(L"tizen.imageviewer",
				L"http://tizen.org/appcontrol/operation/view");

		if (__pImageAppControl != null)
		{
			__pImageAppControl->Start(&uri, null, null, null);
			delete __pImageAppControl;
		}
	}
}

void
MainForm::OnFormBackRequested(Tizen::Ui::Controls::Form& source)
{
    Application* pApplication = Application::GetInstance();

    pApplication->Terminate();
}

void
MainForm::OnSceneActivatedN(const Tizen::Ui::Scenes::SceneId& previousSceneId,
		const Tizen::Ui::Scenes::SceneId& currentSceneId, Tizen::Base::Collection::IList* pArgs)
{
	if (pArgs != null)
	{
		__connManager.RequestConnection(*((BluetoothDevice*) pArgs->GetAt(0)));
	}
}

void
MainForm::OnSceneDeactivated(const Tizen::Ui::Scenes::SceneId& currentSceneId,
		const Tizen::Ui::Scenes::SceneId& nextSceneId)
{

}

void
MainForm::ShowFriendProfile(const Tizen::Base::String& friendName, const Tizen::Base::String& imagePath,
		const Tizen::Base::String& deviceName)
{
	const int editFieldHeight = 106;
	const int imageBoundryHeight = 320;
	int emptySpaceHeight = (GetClientAreaBounds().height - (2 * editFieldHeight + imageBoundryHeight)) / 4;
	int currentHeight = emptySpaceHeight;

	Image friendsImage;
	Bitmap* pThumbnailBitmap = null;
	EditField* pFriendNameField = null;
	EditField* pImageBoundry = null;
	EditField* pDeviceNameField = null;

	__pProfilePanel = new (std::nothrow) Panel();

	__pProfilePanel->Construct(Rectangle(0, 0, GetClientAreaBounds().width, GetClientAreaBounds().height));
	AddControl(*__pProfilePanel);

	__pConnectionStatusLabel->SetShowState(false);

	pFriendNameField = new (std::nothrow) EditField();
	pFriendNameField->Construct(Rectangle(80, currentHeight, 320, editFieldHeight), EDIT_FIELD_STYLE_NORMAL,
								INPUT_STYLE_FULLSCREEN, true, 30, GROUP_STYLE_SINGLE);
	pFriendNameField->SetTitleText(L"Friend Name:");
	pFriendNameField->SetText(friendName);
	pFriendNameField->SetKeypadEnabled(false);
	pFriendNameField->SetViewModeEnabled(true);
	__pProfilePanel->AddControl(*pFriendNameField);


	currentHeight += editFieldHeight + emptySpaceHeight;

	__friendImagePath = imagePath;

	friendsImage.Construct();
	pThumbnailBitmap = friendsImage.DecodeN(__friendImagePath, BITMAP_PIXEL_FORMAT_RGB565);
	TryReturnVoid(pThumbnailBitmap != null, "The thumbnail bitmap is null. [error type: %s]", GetErrorMessage(GetLastResult()));

	int bitmapWidth = pThumbnailBitmap->GetWidth();
	int bitmapHeight = pThumbnailBitmap->GetHeight();
	int basis = 300;

	if ((bitmapWidth > basis) || (bitmapHeight > basis))
	{
		if (bitmapWidth > bitmapHeight)
		{
			bitmapHeight = (bitmapHeight * basis) / bitmapWidth;
			bitmapWidth = basis;
		}
		else
		{
			bitmapWidth = (bitmapWidth * basis) / bitmapHeight;
			bitmapHeight = basis;
		}
		pThumbnailBitmap->Scale(Dimension(bitmapWidth, bitmapHeight));
	}

	pImageBoundry = new (std::nothrow) EditField();
	pImageBoundry->Construct(Rectangle(80, currentHeight, 320, 320), EDIT_FIELD_STYLE_NORMAL,
							 INPUT_STYLE_FULLSCREEN, true, 1, GROUP_STYLE_SINGLE);
	pImageBoundry->SetKeypadEnabled(false);
	pImageBoundry->SetViewModeEnabled(true);
	pImageBoundry->SetFocusable(false);
	pImageBoundry->SetEffectSoundEnabled(false);
	__pProfilePanel->AddControl(*pImageBoundry);

	currentHeight += 10;

	if (__pFriendProfileLabel != null)
	{
		__pFriendProfileLabel->SetBounds(Rectangle(((300 - bitmapWidth) / 2) + 90, ((300 - bitmapHeight) / 2) + currentHeight,
				bitmapWidth, bitmapHeight));
	}
	else
	{
		__pFriendProfileLabel = new (std::nothrow) Label();
		__pFriendProfileLabel->Construct(Rectangle(((300 - bitmapWidth) / 2) + 90, ((300 - bitmapHeight) / 2) + currentHeight,
				bitmapWidth, bitmapHeight), L"");
		__pFriendProfileLabel->SetBackgroundBitmap(*pThumbnailBitmap);
		__pFriendProfileLabel->AddTouchEventListener(*this);
		__pProfilePanel->AddControl(*__pFriendProfileLabel);
		__pProfilePanel->SetControlAlwaysOnTop(*__pFriendProfileLabel, true);
	}
	__pFriendProfileLabel->SetBackgroundBitmap(*pThumbnailBitmap);

	currentHeight += imageBoundryHeight + emptySpaceHeight - 10;

	pDeviceNameField = new (std::nothrow) EditField();
	pDeviceNameField->Construct(Rectangle(80, currentHeight, 320, editFieldHeight), EDIT_FIELD_STYLE_NORMAL,
								INPUT_STYLE_FULLSCREEN, true, 30, GROUP_STYLE_SINGLE);
	pDeviceNameField->SetTitleText(L"Device Name:");
	pDeviceNameField->SetText(deviceName);
	pDeviceNameField->SetKeypadEnabled(false);
	pDeviceNameField->SetViewModeEnabled(true);
	__pProfilePanel->AddControl(*pDeviceNameField);

	RequestRedraw();

	delete pThumbnailBitmap;
}

void
MainForm::ClearFriendProfile(void)
{
	if (__pProfilePanel != null)
	{
		RemoveControl(*__pProfilePanel);  // remove the panel from the MainForm. All Controls involved in the panel will be automatically deleted
		__pProfilePanel = null;
	}

	__pFriendProfileLabel = null;
	__pConnectionStatusLabel->SetShowState(true);

	RequestRedraw();
}

void
MainForm::ShowRequestPopup(const Tizen::Base::String& targetName)
{
	const int requestPopupHeight = 275;
	// hides the other popups
	CloseConnectingPopup();
	CloseNoticePopup();

	// generates the request popup that let users know other device requests the connection with this device
	__pRequestPopup = new (std::nothrow) Popup();
	__pRequestPopup->Construct(true, Dimension(GetClientAreaBounds().width - 10, requestPopupHeight));
	__pRequestPopup->SetTitleText(L"New Friend Request");

	Rectangle popupArea = __pRequestPopup->GetClientAreaBounds();

	Label* pRequestPopupLabel = new (std::nothrow) Label();
	pRequestPopupLabel->Construct(Rectangle(0, 0, popupArea.width, 100), L"");
	pRequestPopupLabel->SetText(targetName);
	__pRequestPopup->AddControl(*pRequestPopupLabel);

	Button* pRejectButton = new (std::nothrow) Button();
	pRejectButton->Construct(Rectangle(popupArea.x + 10, popupArea.height - 80, (popupArea.width / 2) - 20, 50),
			L"Reject");
	pRejectButton->SetActionId(ID_BUTTON_REJECT_CONNECTION);
	pRejectButton->AddActionEventListener(*this);
	__pRequestPopup->AddControl(*pRejectButton);

	Button* pAcceptButton = new (std::nothrow) Button();
	pAcceptButton->Construct(Rectangle((popupArea.width / 2) + 10, popupArea.height - 80, (popupArea.width / 2) - 20, 50), L"Accept");
	pAcceptButton->SetActionId(ID_BUTTON_ACCEPT_CONNECTION);
	pAcceptButton->AddActionEventListener(*this);
	__pRequestPopup->AddControl(*pAcceptButton);

	__pRequestPopup->SetShowState(true);
	__pRequestPopup->Show();
}

void
MainForm::CloseRequestPopup(void)
{
	delete __pRequestPopup;
	__pRequestPopup = null;
}

void
MainForm::ShowConnectingPopup(const Tizen::Base::String& targetName)
{
	const int connectingPopupHeight = 300;

	// hides the other popups
	CloseNoticePopup();
	CloseRequestPopup();

	// generates the connecting popup that let users know the progress of connecting procedure
	__pConnectingPopup = new (std::nothrow) Popup();
	__pConnectingPopup->Construct(true, Dimension(GetClientAreaBounds().width, connectingPopupHeight));
	__pConnectingPopup->SetTitleText(L"Connecting");

	Rectangle popupArea = __pConnectingPopup->GetClientAreaBounds();

	Label* pConnectingPopupLabel = new (std::nothrow) Label();
	pConnectingPopupLabel->Construct(Rectangle(0, 0, popupArea.width, 100), L"");
	pConnectingPopupLabel->SetText(targetName);
	__pConnectingPopup->AddControl(*pConnectingPopupLabel);

	__pConnectingProgress = new (std::nothrow) Progress;
	__pConnectingProgress->Construct(Rectangle(40, 80, __pConnectingPopup->GetWidth() - 100, 75), 0, 100);
	__pConnectingProgress->SetValue(0);
	__pConnectingPopup->AddControl(*__pConnectingProgress);

	__pConnectingCancelButton = new (std::nothrow) Button();
	__pConnectingCancelButton->Construct(Rectangle(90, popupArea.height - 80, popupArea.width - 180, 50), L"Cancel");
	__pConnectingCancelButton->SetActionId(ID_BUTTON_CANCEL_CONNECTION);
	__pConnectingCancelButton->AddActionEventListener(*this);
	__pConnectingCancelButton->SetEnabled(true);
	__pConnectingPopup->AddControl(*__pConnectingCancelButton);

	__pConnectingPopup->SetShowState(true);
	__pConnectingPopup->Show();
}

void
MainForm::CloseConnectingPopup(void)
{
	delete __pConnectingPopup;
	__pConnectingPopup = null;
}

void
MainForm::UpdateProgressBar(int progress)
{
	int currentProgress = __pConnectingProgress->GetValue();

	if (currentProgress < FRIEND_FINDER_PROGRESS_HALF)
	{
		__pConnectingProgress->SetValue(progress / 2);
	}
	else
	{
		__pConnectingProgress->SetValue(50 + progress / 2);
	}

	__pConnectingProgress->Draw();
	__pConnectingProgress->Show();
}

void
MainForm::EnableCancelButton(void)
{
	__pConnectingCancelButton->SetEnabled(true);
	__pConnectingCancelButton->RequestRedraw(true);
}

void
MainForm::DisableCancelButton(void)
{
	__pConnectingCancelButton->SetEnabled(false);
	__pConnectingCancelButton->RequestRedraw(true);
}

void
MainForm::ShowNoticePopup(const Tizen::Base::String& title, const Tizen::Base::String& description)
{
	const int noticePopupHeight = 275;

	// hides the other popups
	CloseConnectingPopup();
	CloseRequestPopup();

	// generates the notice popup that provides users various kinds of notification.
	__pNoticePopup = new (std::nothrow) Popup();
	__pNoticePopup->Construct(true, Dimension(GetClientAreaBounds().width - 10, noticePopupHeight));
	__pNoticePopup->SetTitleText(title);

	Rectangle popupArea = __pNoticePopup->GetClientAreaBounds();

	Label* pNoticePopupLabel = new (std::nothrow) Label();
	pNoticePopupLabel->Construct(Rectangle(5, 5, popupArea.width - 10, popupArea.height - 90), L"");
	pNoticePopupLabel->SetTextConfig(35, LABEL_TEXT_STYLE_NORMAL);
	pNoticePopupLabel->SetTextVerticalAlignment(ALIGNMENT_MIDDLE);
	pNoticePopupLabel->SetText(description);
	__pNoticePopup->AddControl(*pNoticePopupLabel);

	Button* pCloseButton = new (std::nothrow) Button();
	pCloseButton->Construct(Rectangle((popupArea.width / 2) - 80, popupArea.height - 80, 160, 50), L"Close");
	pCloseButton->SetActionId(ID_BUTTON_NOTICE_POPUP_CLOSE);
	pCloseButton->AddActionEventListener(*this);
	__pNoticePopup->AddControl(*pCloseButton);

	__pNoticePopup->SetShowState(true);
	__pNoticePopup->Show();
}

void
MainForm::CloseNoticePopup(void)
{
	delete __pNoticePopup;
	__pNoticePopup = null;
}