summaryrefslogtreecommitdiff
path: root/lib-phone/ph-dialer/src/PhDialerView.cpp
blob: 1a2df0fc9b8adad0cdbfa852c62a3dc2b7fadafe (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
/*
 * Copyright (c) 2009-2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 "PhDialerView.h"
#include "PhDialerEntry.h"
#include "PhDialerKey.h"
#include "PhDialerLayout.h"
#include "PhDialerSpeeddialPopup.h"
#include "PhSpeedDialView.h"
#include "PhDialerSearchController.h"

#include "ContactsAppControl.h"
#include "ContactsDebug.h"
#include "PhStrings.h"
#include "WMenuPopup.h"

#include <app_control.h>
#include <contacts.h>
#include <feedback.h>

#define KEYPAD_ROWS 4
#define KEYPAD_COLS 3

#define UG_CALL_SETTING "setting-call-efl"
#define DIALER_LAYOUT_EDJ "phone/ph-dialer-layout.edj"

namespace
{
	const std::string layoutFilePath = ContactsCommon::getAppEdjePath(DIALER_LAYOUT_EDJ);
}

PhDialerView::PhDialerView()
	: m_Entry(nullptr), m_Controller(nullptr)
{
	feedback_initialize();
}

PhDialerView::~PhDialerView()
{
	feedback_deinitialize();
}

void PhDialerView::setPredictiveLayout(Evas_Object *layout)
{
	elm_object_part_content_set(getEvasObj(), PART_SWALLOW_PREDICTIVE, layout);
}

PhDialerEntry* PhDialerView::getEntry() const
{
	return m_Entry;
}

Evas_Object *PhDialerView::onCreate(Evas_Object *parent, void *viewParam)
{
	elm_theme_extension_add(nullptr, ContactsCommon::getAppEdjePath("common/custom_button_styles.edj").c_str());

	Evas_Object *layout = elm_layout_add(parent);
	Eina_Bool res = elm_layout_file_set(layout, layoutFilePath.c_str(), GROUP_DIALER);
	WPWARN(res != EINA_TRUE, "elm_layout_file_set() failed");

	m_Entry = new PhDialerEntry();
	m_Entry->create(layout, nullptr);
	elm_object_part_content_set(layout, PART_SWALLOW_ENTRY, m_Entry->getEvasObj());
	elm_object_part_content_set(layout, PART_SWALLOW_KEYPAD, createKeypad(layout));
	elm_object_part_content_set(layout, PART_SWALLOW_CALL, createCallButton(layout));
	elm_object_part_content_set(layout, PART_SWALLOW_BACKSPACE, createBackspaceButton(layout));

	m_Controller = new PhDialerSearchController();
	m_Controller->create(*this);

	m_Entry->setNumber(m_InitialNumber);

	return layout;
}

void PhDialerView::setNumber(std::string &number)
{
	m_InitialNumber = number;
	if(m_Entry) {
		m_Entry->setNumber(number);
	}
}

void PhDialerView::onPush(Elm_Object_Item *naviItem)
{
	enableMoreButton(naviItem, &PhDialerView::onMenuPressed, this);
}

void PhDialerView::onTabSelect(Elm_Object_Item *naviItem)
{
	Evas_Object *conf = getWindow()->getConformantEvasObj();
	elm_object_signal_emit(conf, "elm,state,virtualkeypad,disable", "");
	elm_object_signal_emit(conf, "elm,state,clipboard,disable", "");

	enableMoreButton(naviItem, &PhDialerView::onMenuPressed, this);
}

void PhDialerView::onTabUnselect(Elm_Object_Item *naviItem)
{
	Evas_Object *conf = getWindow()->getConformantEvasObj();
	elm_object_signal_emit(conf, "elm,state,virtualkeypad,enable", "");
	elm_object_signal_emit(conf, "elm,state,clipboard,enable", "");
}

bool PhDialerView::onTabPop()
{
	return true;
}

void PhDialerView::onTabScrollStart()
{
}

void PhDialerView::onTabScrollStop()
{
}

Evas_Object *PhDialerView::createKeypad(Evas_Object *parent)
{
	Evas_Object *table = elm_table_add(parent);
	elm_table_padding_set(table, 2, 2);

	int id = 0;
	for(int i = 0; i < KEYPAD_ROWS; ++i) {
		for(int j = 0; j < KEYPAD_COLS; ++j, ++id) {
			using namespace std::placeholders;

			PhDialerKey *key = new PhDialerKey((PhDialerKey::Id) id);
			key->create(table, nullptr);
			key->setOnPressed(std::bind(&PhDialerView::onKeyPressed, this, _1));
			key->setOnLongpressed(std::bind(&PhDialerView::onKeyLongpressed, this, _1));

			Evas_Object *button = key->getEvasObj();
			evas_object_size_hint_weight_set(button, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
			evas_object_size_hint_align_set(button, EVAS_HINT_FILL, EVAS_HINT_FILL);
			elm_table_pack(table, button, j, i, 1, 1);
			evas_object_show(button);
		}
	}

	return table;
}

Evas_Object *PhDialerView::createCallButton(Evas_Object *parent)
{
	Evas_Object *button = elm_button_add(parent);
	elm_object_style_set(button, "custom_circle");
	evas_object_smart_callback_add(button, "clicked",
			&PhDialerView::onCallPressed, this);

	Evas_Object *edje = elm_layout_edje_get(button);
	edje_object_color_class_set(edje, "button_normal", BUTTON_CALL_NORMAL, 0, 0, 0, 0, 0, 0, 0, 0);
	edje_object_color_class_set(edje, "button_pressed", BUTTON_CALL_PRESSED, 0, 0, 0, 0, 0, 0, 0, 0);

	Evas_Object *image = elm_image_add(button);
	Eina_Bool res = elm_image_file_set(image, layoutFilePath.c_str(), BUTTON_CALL);
	WPWARN(res != EINA_TRUE, "elm_layout_file_set() failed");
	elm_object_part_content_set(button, "elm.swallow.content", image);

	return button;
}

Evas_Object *PhDialerView::createBackspaceButton(Evas_Object *parent)
{
	using namespace std::placeholders;
	WButton *key = new WButton();
	key->create(parent, nullptr);
	key->setOnPressed(std::bind(&PhDialerView::onBackspacePressed, this, _1));
	key->setOnLongpressed(std::bind(&PhDialerView::onBackspaceLongpressed, this, _1));

	Evas_Object *button = key->getEvasObj();
	elm_object_style_set(button, "transparent");
	Evas_Object *image = elm_image_add(button);
	Eina_Bool res = elm_image_file_set(image, layoutFilePath.c_str(), BUTTON_BACKSPACE);
	WPWARN(res != EINA_TRUE, "elm_layout_file_set() failed");
	elm_object_part_content_set(button, "elm.swallow.content", image);

	return button;
}

void PhDialerView::onKeyPressed(WButton &button)
{
	PhDialerKey &key = static_cast<PhDialerKey&>(button);
	m_Entry->insert(key.getValue());
}

bool PhDialerView::onKeyLongpressed(WButton &button)
{
	PhDialerKey &key = static_cast<PhDialerKey&>(button);
	int id = key.getId();

	if (m_Entry->getNumber().empty()) {
		if (id >= PhDialerKey::ID_1 && id <= PhDialerKey::ID_9) {
			launchSpeeddial(key.getValue() - '0');
			return true;
		}
	}

	if (id == PhDialerKey::ID_0) {
		m_Entry->insert('+');
		return true;
	}

	return false;
}

void PhDialerView::onBackspacePressed(WButton &button)
{
	feedback_play(FEEDBACK_PATTERN_KEY_BACK);
	m_Entry->popBack();
}

bool PhDialerView::onBackspaceLongpressed(WButton &button)
{
	m_Entry->clear();
	return true;
}

void PhDialerView::onCallPressed(void *data, Evas_Object *obj, void *event_info)
{
	PhDialerView *view = (PhDialerView*) data;

	std::string number = view->m_Entry->getNumber();
	if (!number.empty()) {
		launchCall(number);
		view->m_Entry->clear();
	} else {
		view->m_Entry->setNumber(getLastNumber());
	}
}

void PhDialerView::onMenuPressed(void *data, Evas_Object *obj, void *event_info)
{
	PhDialerView *view = (PhDialerView*) data;

	WMenuPopup *popup = new WMenuPopup();
	popup->prepare(view->getWindow()->getEvasObj(),
			view->getNaviframe()->getEvasObj());

	if (!view->m_Entry->getNumber().empty()) {
		popup->appendItem(PAT_(PH_KPD_BUTTON_SEND_MESSAGE),
				std::bind(&PhDialerView::onSendMessage, view));
		popup->appendItem(PAT_(PH_KPD_OPT_ADD_2_SECOND_PAUSE_ABB),
				std::bind(&PhDialerView::onAddPause, view));
		popup->appendItem(PAT_(PH_KPD_OPT_ADD_WAIT_ABB),
				std::bind(&PhDialerView::onAddWait, view));
	}

	popup->appendItem(PAT_(PH_KPD_OPT_SPEED_DIAL_SETTINGS_ABB2),
			std::bind(&PhDialerView::onSpeeddialSettings, view));
	popup->appendItem(PAT_(PH_KPD_OPT_CALL_SETTINGS_ABB),
			std::bind(&PhDialerView::onCallSettings, view));

	view->attachPopup(popup);
}

void PhDialerView::onSendMessage()
{
	int err = launchMessageComposer("sms:", m_Entry->getNumber().c_str());
	WPWARN(err != APP_CONTROL_ERROR_NONE, "launchMessageComposer() failed(0x%x)", err);
}

void PhDialerView::onAddWait()
{
	m_Entry->insert(';');
}

void PhDialerView::onAddPause()
{
	m_Entry->insert(',');
}

void PhDialerView::onSpeeddialSettings()
{
	getNaviframe()->push(new PhSpeedDialView(), NULL, NULL);
}

void PhDialerView::onCallSettings()
{
	app_control_h request;
	app_control_create(&request);
	app_control_set_app_id(request, UG_CALL_SETTING);
	app_control_set_launch_mode(request, APP_CONTROL_LAUNCH_MODE_GROUP);
	int err = app_control_send_launch_request(request, NULL, NULL);
	WPWARN(err != APP_CONTROL_ERROR_NONE, "app_control_send_launch_request() failed(0x%x)", err);
	app_control_destroy(request);
}

void PhDialerView::launchCall(const std::string &number)
{
	int err = ::launchCall(number.c_str());
	WPWARN(err != APP_CONTROL_ERROR_NONE, "launchCall() failed(0x%x)", err);
}

void PhDialerView::launchSpeeddial(int digit)
{
	std::string number = getSpeeddialNumber(digit);
	if (!number.empty()) {
		launchCall(number);
	} else {
		attachPopup(new PhDialerSpeeddialPopup(digit));
	}
}

std::string PhDialerView::getSpeeddialNumber(int digit)
{
	std::string number;
	contacts_filter_h filter = NULL;
	contacts_query_h query = NULL;
	contacts_list_h list = NULL;

	contacts_filter_create(_contacts_speeddial._uri, &filter);
	contacts_filter_add_int(filter, _contacts_speeddial.speeddial_number, CONTACTS_MATCH_EQUAL, digit);

	contacts_query_create(_contacts_speeddial._uri, &query);
	contacts_query_set_filter(query, filter);

	int err = contacts_db_get_records_with_query(query, 0, 1, &list);
	WPWARN(err != CONTACTS_ERROR_NONE, "contacts_db_get_records_with_query() failed(0x%x)", err);
	if (list) {
		contacts_record_h record = NULL;
		contacts_list_get_current_record_p(list, &record);
		if (record) {
			char *str = NULL;
			contacts_record_get_str_p(record, _contacts_speeddial.number, &str);
			if (str) {
				number = str;
			}
		}

		contacts_list_destroy(list, true);
	}

	contacts_query_destroy(query);
	contacts_filter_destroy(filter);

	return number;
}

std::string PhDialerView::getLastNumber()
{
	std::string number;
	contacts_list_h list = NULL;
	contacts_query_h query = NULL;
	contacts_filter_h filter = NULL;

	contacts_filter_create(_contacts_person_phone_log._uri, &filter);
	contacts_filter_add_int(filter, _contacts_person_phone_log.log_type,
			CONTACTS_MATCH_GREATER_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VOICE_INCOMMING);
	contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
	contacts_filter_add_int(filter, _contacts_person_phone_log.log_type,
			CONTACTS_MATCH_LESS_THAN_OR_EQUAL, CONTACTS_PLOG_TYPE_VIDEO_BLOCKED);

	contacts_query_create(_contacts_person_phone_log._uri, &query);
	contacts_query_set_filter(query, filter);
	contacts_query_set_sort(query, _contacts_person_phone_log.log_time, false);

	int err = contacts_db_get_records_with_query(query, 0, 1, &list);
	WPWARN(err != CONTACTS_ERROR_NONE, "contacts_db_get_records_with_query() failed(0x%x)", err);
	if (list) {
		contacts_record_h record = NULL;
		contacts_list_get_current_record_p(list, &record);
		if (record) {
			char *str = NULL;
			contacts_record_get_str_p(record, _contacts_person_phone_log.address, &str);
			if (str) {
				number = str;
			}
		}

		contacts_list_destroy(list, true);
	}

	contacts_query_destroy(query);
	contacts_filter_destroy(filter);

	return number;
}