summaryrefslogtreecommitdiff
path: root/lib/common/ph-detail-log.c
blob: fb81596622db5c7df3e4f0f97228d7d3cce1a37a (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* Copyright 2012 Samsung Electronics Co., Ltd
*
* Licensed under the Flora License, Version 1.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://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 "phone.h"
#include <unicode/udat.h>
#include <unicode/udatpg.h>
#include <unicode/ucal.h>
#include <unicode/ustring.h>
#include <vconf.h>
#include <vconf-keys.h>
#include <runtime_info.h>

#include "phone-common.h"

enum{
	LOG_FORMAT_TIME_12 = 0,
	LOG_FORMAT_TIME_24,
	LOG_FORMAT_DATE,
	LOG_FORMAT_MAX,
};

static const char *date_time_format[] = {
	"hm",
	"Hm",
	"yMMMdd",
	"yMMMddhm",
	"yMMMddHm",
};

static char *m_img_logtype_tbl[LOG_TYPE_MAX] = {
	IMGDIR"/C01-4_list_icon_call.png",
	IMGDIR"/C01-4_list_icon_vt-call.png",
	IMGDIR"/C01-4_list_icon_sms.png",
	IMGDIR"/C01-4_list_icon_mms.png"
};

static char *m_img_logaction_tbl[LOG_ACTION_MAX] = {
	IMGDIR"/C01-4_icon_dialed.png",
	IMGDIR"/C01-4_icon_missed-call.png",
	IMGDIR"/C01-4_icon_received.png",
	IMGDIR"/C01-4_icon_rejected.png",
	IMGDIR"/C01-4_icon_Blocked(Auto-rejected).png"
};

static bool is_24hour_enabled = false;

int phone_detail_log_is_call_ing_type(int type)
{
	switch (type) {
	case CONTACTS_PLOG_TYPE_VOICE_INCOMMING:
	case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
	case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING:
	case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
		return TRUE;
	default:
		return FALSE;
	}
}

static int detail_log_is_voice_call_type(int type)
{
	switch (type) {
	case CONTACTS_PLOG_TYPE_VOICE_INCOMMING:
	case CONTACTS_PLOG_TYPE_VOICE_OUTGOING:
	case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN:
	case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN:
	case CONTACTS_PLOG_TYPE_VOICE_REJECT:
	case CONTACTS_PLOG_TYPE_VOICE_BLOCKED:
		return TRUE;
	default:
		return FALSE;
	}
}

int phone_detail_log_is_mms_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_MMS_INCOMMING
			|| type == CONTACTS_PLOG_TYPE_MMS_OUTGOING
			|| type == CONTACTS_PLOG_TYPE_MMS_BLOCKED)
		return TRUE;
	else
		return FALSE;
}

int phone_detail_log_is_sms_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_SMS_INCOMMING
			|| type == CONTACTS_PLOG_TYPE_SMS_OUTGOING
			|| type == CONTACTS_PLOG_TYPE_SMS_BLOCKED)
		return TRUE;
	else
		return FALSE;
}

int phone_detail_log_is_email_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_EMAIL_RECEIVED
			|| type == CONTACTS_PLOG_TYPE_EMAIL_SENT)
		return TRUE;
	else
		return FALSE;
}


static int detail_log_is_video_call_type(int type)
{
	switch (type) {
	case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING:
	case CONTACTS_PLOG_TYPE_VIDEO_OUTGOING:
	case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN:
	case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN:
	case CONTACTS_PLOG_TYPE_VIDEO_REJECT:
	case CONTACTS_PLOG_TYPE_VIDEO_BLOCKED:
		return TRUE;
	default:
		return FALSE;
	}
}

static int phone_detail_log_is_call_dial_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_VOICE_OUTGOING
			|| type == CONTACTS_PLOG_TYPE_VIDEO_OUTGOING)
		return TRUE;
	else
		return FALSE;
}

static int detail_log_is_msg_out_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_MMS_OUTGOING
			|| type == CONTACTS_PLOG_TYPE_SMS_OUTGOING
			|| type == CONTACTS_PLOG_TYPE_EMAIL_SENT)
		return TRUE;
	else
		return FALSE;

}

static int detail_log_is_call_receive_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_VOICE_INCOMMING
			|| type == CONTACTS_PLOG_TYPE_VIDEO_INCOMMING)
		return TRUE;
	else
		return FALSE;
}

static int detail_log_is_msg_income_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_MMS_INCOMMING
			|| type == CONTACTS_PLOG_TYPE_SMS_INCOMMING
			|| type == CONTACTS_PLOG_TYPE_EMAIL_RECEIVED)
		return TRUE;
	else
		return FALSE;
}

static int detail_log_is_call_miss_type(int type)
{
	switch (type) {
	case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_UNSEEN:
	case CONTACTS_PLOG_TYPE_VOICE_INCOMMING_SEEN:
	case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_UNSEEN:
	case CONTACTS_PLOG_TYPE_VIDEO_INCOMMING_SEEN:
		return TRUE;
	default:
		return FALSE;
	}
}

static int detail_log_is_call_rejected_type(int type)
{
	if (type == CONTACTS_PLOG_TYPE_VOICE_REJECT
			|| type == CONTACTS_PLOG_TYPE_VIDEO_REJECT)
		return TRUE;
	else
		return FALSE;
}

static int detail_log_is_blocked_type(int type)
{
	switch (type) {
	case CONTACTS_PLOG_TYPE_VOICE_BLOCKED:
	case CONTACTS_PLOG_TYPE_VIDEO_BLOCKED:
	case CONTACTS_PLOG_TYPE_SMS_BLOCKED:
	case CONTACTS_PLOG_TYPE_MMS_BLOCKED:
		return TRUE;
	default:
		return FALSE;
	}
}

char* phone_detail_log_get_icon_type(int log_type)
{
	Log_Type type = -1;
	char buf[PATH_MAX]={0};

	if (TRUE == detail_log_is_voice_call_type(log_type))
		type = LOG_TYPE_CALL;
	else if (TRUE == detail_log_is_video_call_type(log_type))
		type = LOG_TYPE_VIDEO_CALL;
	else if (TRUE == phone_detail_log_is_sms_type(log_type))
		type = LOG_TYPE_SMS_MESSAGE;
	else if (TRUE == phone_detail_log_is_mms_type(log_type))
		type = LOG_TYPE_MMS_MESSAGE;
	else if (TRUE == phone_detail_log_is_email_type(log_type))
		type = LOG_TYPE_MMS_MESSAGE;
	else {
		PH_DBG("error invalid type");
		return NULL;
	}
	snprintf(buf, sizeof(buf), "%s", m_img_logtype_tbl[type]);
	return strdup(buf);
}

char* phone_detail_log_get_duration_from_number(int number)
{
	int sec, min, hour, day;
	struct tm tmp={0};
	char buf[PATH_MAX]={0};
	char buf_t[PATH_MAX]={0};

	sec = number;
	day = sec/(60*60*24);
	sec %= (60*60*24);
	hour = sec/(60*60);
	sec %= (60*60);
	min = sec/(60);
	sec %= (60);
	tmp.tm_mday = day;
	tmp.tm_hour = hour;
	tmp.tm_min = min;
	tmp.tm_sec = sec;
	strftime(buf_t, sizeof(buf_t), "%H:%M:%S", &tmp);
	snprintf(buf, sizeof(buf), "%s", buf_t);

	return strdup(buf_t);
}

void phone_detail_log_init_time_format(void)
{
	PH_FN_CALL;
	static int already_init = FALSE;

	if (!already_init) {
		UErrorCode status = U_ZERO_ERROR;

		uloc_setDefault(getenv("LC_TIME"), &status);
		PH_DBG("phone_detail_log_init_time_format : %s", getenv("LC_TIME") );
		already_init = TRUE;
	}
}

void phone_detail_log_delete_date_format(UDateFormat **formatter)
{
	PH_FN_CALL;
	int i;
	for (i=LOG_FORMAT_TIME_12;i<LOG_FORMAT_MAX;i++) {
		if (formatter[i]) {
			udat_close(formatter[i]);
			formatter[i] = NULL;
		}
	}
}

static void _ph_set_default_timezone_id()
{
	PH_FN_CALL;
	UErrorCode error = U_ZERO_ERROR;
	UChar utimezone_id [PH_TEXT_SHORT_LEN] = {0};
	char  timezone_buffer[PH_TEXT_SHORT_LEN] = {0};
	char timezone_id[PH_TEXT_SHORT_LEN] = {0};

	int timezone_str_size;

	strncpy(timezone_id, vconf_get_str(VCONFKEY_SETAPPL_TIMEZONE_ID), sizeof(timezone_id));
	timezone_str_size = readlink("/opt/etc/localtime", timezone_buffer, sizeof(timezone_buffer));


	if (timezone_str_size > 0)
	{
		char *ptr, *sp,  *zone= NULL, *city= NULL;
		ptr = strtok_r(timezone_buffer, "/", &sp);

		while((ptr = strtok_r(NULL, "/", &sp)))
		{
			zone = city;
			city = ptr;
		}

		if(zone != NULL && city != NULL) {
			if(strcmp("zoneinfo", zone) == 0)
				sprintf(timezone_id, "%s", city);
			else
				sprintf(timezone_id, "%s/%s", zone, city);
		}
	}

	else
	{
		PH_DBG("%s", "readlink fail");
	}

	if (*timezone_id) {
		u_uastrncpy(utimezone_id, timezone_id, sizeof(timezone_buffer)/2);
		ucal_setDefaultTimeZone(utimezone_id , &error);
	}
	else {
		p_retm_if(NULL == timezone_id, "get timezone_id is failed(%d)", error);
	}
}

void phone_detail_log_generate_date_format(UDateFormat **formatter)
{
	PH_FN_CALL;
	UDateTimePatternGenerator *generator;
	UErrorCode error = U_ZERO_ERROR;

	int i;
	int ret;
	const char *locale = NULL;

	_ph_set_default_timezone_id();

	ret = runtime_info_get_value_bool(RUNTIME_INFO_KEY_24HOUR_CLOCK_FORMAT_ENABLED, &is_24hour_enabled);
	if (RUNTIME_INFO_ERROR_NONE != ret || false == is_24hour_enabled)
		is_24hour_enabled = false;

	locale = uloc_getDefault();
	phone_detail_log_delete_date_format(formatter);

	generator = udatpg_open(locale, &error);
	p_retm_if(NULL == generator, "udatpg_open is failed(%d)", error);

	for (i=LOG_FORMAT_TIME_12;i<LOG_FORMAT_MAX;i++) {
		UChar skeleton[64]={'\0'};
		UChar best_pattern[64] = {0};

		u_uastrncpy(skeleton, date_time_format[i], strlen(date_time_format[i]));
		udatpg_getBestPattern(generator, skeleton, u_strlen(skeleton), best_pattern,
				(int32_t)(sizeof(best_pattern)/sizeof(UChar)), &error);
		formatter[i] = udat_open(UDAT_IGNORE, UDAT_IGNORE, locale, NULL, -1, best_pattern, -1, &error);
	}
	udatpg_close(generator);
}

char* phone_detail_log_get_icon_path_with_action(int log_type)
{
	char buf[PATH_MAX]={0};
	Log_Action action = -1;

	if (TRUE == phone_detail_log_is_call_dial_type(log_type) || TRUE == detail_log_is_msg_out_type(log_type))
		action = LOG_ACTION_DIALLED;
	else if (TRUE == detail_log_is_call_miss_type(log_type))
		action = LOG_ACTION_MISSED;
	else if (TRUE == detail_log_is_call_receive_type(log_type) || TRUE == detail_log_is_msg_income_type(log_type))
		action = LOG_ACTION_RECEIVED;
	else if (TRUE == detail_log_is_call_rejected_type(log_type))
		action = LOG_ACTION_REJECTED;
	else if (TRUE == detail_log_is_blocked_type(log_type))
		action = LOG_ACTION_BLOCKED;
	else {
		PH_DBG("error invalid type %d", log_type);
		return NULL;
	}
	snprintf(buf, sizeof(buf), "%s", m_img_logaction_tbl[action]);
	return strdup(buf);
}

char *ph_log_util_get_formatted_date(UDateFormat **formatter, int log_time,
		date_style style)
{
	time_t stime;
	UErrorCode status = U_ZERO_ERROR;
	UDate date;
	UChar formatted[64] = {0};
	int32_t formatted_size;
	int32_t formatted_len;
	char formatted_str[PH_TEXT_SHORT_LEN] = {0};
	int type = LOG_FORMAT_DATE;

	stime = log_time;
	date = (UDate)stime * 1000;

	formatted_size = (int32_t)(sizeof(formatted)/sizeof(UChar));
	if (PH_LOG_TIME_ONLY == style) {
		if (true == is_24hour_enabled)
			type = LOG_FORMAT_TIME_24;
		else
			type = LOG_FORMAT_TIME_12;
	}
	else if (PH_LOG_DATE_ONLY == style)
		type = LOG_FORMAT_DATE;
	formatted_len = udat_format(formatter[type], date, formatted, formatted_size, NULL, &status);
	u_austrncpy(formatted_str, formatted, PH_TEXT_SHORT_LEN);
	return strdup(formatted_str);
}

char *phone_detail_log_get_date(UDateFormat ** formatter, int log_time)
{
	return ph_log_util_get_formatted_date(formatter, log_time, PH_LOG_DATE_ONLY);
}

char *phone_detail_log_get_time(UDateFormat ** formatter, int log_time)
{
	return ph_log_util_get_formatted_date(formatter, log_time, PH_LOG_TIME_ONLY);;
}


// 0: today, 1: yesterday, 2: previous
int phone_detail_log_get_date_type(int log_time, int log_duration)
{
	time_t stime;
	struct timeval timeval;
	struct tm *tmp;
	int yday, cur_yday;

	gettimeofday(&timeval, 0);
	tmp = localtime(&timeval.tv_sec);
	p_retvm_if(NULL == tmp, 2, "localtime is null");
	cur_yday = tmp->tm_yday;

	stime = log_time - log_duration;
	tmp = localtime(&stime);
	p_retvm_if(NULL == tmp, 2, "localtime is null");

	yday = tmp->tm_yday;

	if (yday == cur_yday)
		return 0;
	else if (yday == cur_yday-1)
		return 1;
	else
		return 2;
}

int phone_detail_log_get_phonenumber_type(int number_type)
{
	if (number_type & CONTACTS_NUMBER_TYPE_CUSTOM) {
		return (CTTEXT_TYPE_OTHER);
	}
	else if (number_type & CONTACTS_NUMBER_TYPE_CELL) {
		if (number_type & CONTACTS_NUMBER_TYPE_HOME)
			return (CTTEXT_TYPE_HOME2);
		else if (number_type & CONTACTS_NUMBER_TYPE_WORK)
			return (CTTEXT_TYPE_WORK2);
		else
			return (CTTEXT_TYPE_MOBILE);
	}
	else if (number_type & CONTACTS_NUMBER_TYPE_VOICE) {
		if (number_type & CONTACTS_NUMBER_TYPE_HOME)
			return (CTTEXT_TYPE_HOME);
		else if (number_type & CONTACTS_NUMBER_TYPE_WORK)
			return (CTTEXT_TYPE_WORK);
		else
			return (CTTEXT_TYPE_TELEPHONE);
	}
	else if (number_type & CONTACTS_NUMBER_TYPE_FAX) {
		if (number_type & CONTACTS_NUMBER_TYPE_HOME)
			return (CTTEXT_TYPE_FAX_HOME);
		else if (number_type & CONTACTS_NUMBER_TYPE_WORK)
			return (CTTEXT_TYPE_FAX_WORK);
		else
			return (CTTEXT_TYPE_FAX);
	}
	else if (number_type & CONTACTS_NUMBER_TYPE_PAGER)
		return (CTTEXT_TYPE_PAGER);
	else if (number_type & CONTACTS_NUMBER_TYPE_CAR)
		return (CTTEXT_TYPE_CAR);
	else if (number_type & CONTACTS_NUMBER_TYPE_ASSISTANT)
		return (CTTEXT_TYPE_ASSISTANT);
	else {
		if (number_type == CONTACTS_NUMBER_TYPE_HOME)
			return (CTTEXT_TYPE_HOME);
		else if (number_type == CONTACTS_NUMBER_TYPE_WORK)
			return (CTTEXT_TYPE_WORK);
		else
			return (CTTEXT_TYPE_OTHER);
	}
}