mcu_ab568x/app/projects/AB5682C_128/functions/func_address_book.c
2025-05-30 18:03:10 +08:00

366 lines
12 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "include.h"
#include "func.h"
#include "app_variable.h"
#if TRACE_EN
#define TRACE(...) printf(__VA_ARGS__)
#else
#define TRACE(...)
#endif
#define ICS_MAX_COUNTRY_CODE_LEN 4
enum {
COMPO_ID_LISTBOX = 1,
COMPO_ID_TXT,
COMPO_ID_PIC,
};
typedef struct f_address_book_list_t_ {
compo_listbox_t *listbox;
// 临时使用pbap的数据后续有app使用contact_info_t格式
} f_address_book_list_t;
static u8 call_book_cnt;
static const compo_listbox_item_t tbl_call_list[ADDRESS_BOOK_LIST_CNT] = {0};
compo_listbox_custom_item_t tbl_call_txt_list[ADDRESS_BOOK_LIST_CNT] = {0};
const char ics_chinacode_str[][ICS_MAX_COUNTRY_CODE_LEN + 2 + 1] = {
{'0', '0', '7', '\0'}, // 俄罗斯
{'+', '7', '\0'},
{'0', '0', '8', '6', '\0'}, // 中国
{'+', '8', '6', '\0'},
{'0', '0', '8', '4', '\0'}, // 越南
{'+', '8', '4', '\0'},
{'0', '0', '4', '4', '\0'}, // 英国
{'+', '4', '4', '\0'},
{'0', '0', '3', '4', '\0'}, // 西班牙
{'+', '3', '4', '\0'},
{'0', '0', '3', '3', '\0'}, // 法国
{'+', '3', '3', '\0'},
{'0', '0', '3', '9', '\0'}, // 意大利
{'+', '3', '9', '\0'},
{'0', '0', '4', '8', '\0'}, // 波兰
{'+', '4', '8', '\0'},
{'0', '0', '3', '5', '1', '\0'}, // 葡萄牙
{'+', '3', '5', '1', '\0'},
{'0', '0', '4', '9', '\0'}, // 德国
{'+', '4', '9', '\0'},
{'0', '0', '6', '0', '\0'}, // 马来西亚
{'+', '6', '0', '\0'},
{'0', '0', '6', '6', '\0'}, // 泰国
{'+', '6', '6', '\0'},
{'0', '0', '9', '0', '\0'}, // 土耳其
{'+', '9', '0', '\0'},
{'0', '0', '9', '7', '1', '\0'}, // 阿拉伯
{'+', '9', '7', '1', '\0'},
{'0', '0', '3', '8', '7', '\0'}, // 波斯
{'+', '3', '8', '7', '\0'},
{'0', '0', '4', '2', '0', '\0'}, // 捷克
{'+', '4', '2', '0', '\0'},
};
u8 phonebook_ics_remove_prefix(u8 *number,u8 numberlen)
{
u8 len = numberlen;
int i=0;
u8 lg=sizeof(ics_chinacode_str)/sizeof(ics_chinacode_str[0]);
/*remove international code of China, such as 0086, +86 and so on*/
while(i<lg)
{
if(0 == strncmp(number,ics_chinacode_str[i],strlen(ics_chinacode_str[i])))
{
len = len-strlen(ics_chinacode_str[i]);
memcpy(number,number+strlen(ics_chinacode_str[i]),len);
number[len] = 0;
break;
}
i++;
}
return len;
}
int phonebook_get_name_by_number(u8 *number, u8 *name)
{
u8 vaild_flag = 0;
u8 number_len = 0;
char temp_num[PBAP_MAX_NUM_LEN];
number_len = phonebook_ics_remove_prefix(number, strlen(number));
printf("phonebook_get_name_by_number2_[%d] %s\n", number_len, number);
for (int i = 0; i < ADDRESS_BOOK_LIST_CNT; i++) {
memset(temp_num, 0, PBAP_MAX_NUM_LEN);
memcpy(temp_num, &SysVariable.pb_list[i].num, PBAP_MAX_NUM_LEN);
number_len = phonebook_ics_remove_prefix(temp_num, strlen(temp_num));
printf("phonebook_get_name_by_number4_[%d][%d] %s %d\n", i, number_len, temp_num, strcmp(number, temp_num));
if (strcmp(number, temp_num) == 0)
{
SysVariable.pb_list[i].name[strlen(&SysVariable.pb_list[i].name)] = '\0';
if (strlen(&SysVariable.pb_list[i].name) > 0)
{ /* hit */
memcpy(name, &SysVariable.pb_list[i].name, strlen(&SysVariable.pb_list[i].name));
name[strlen(&SysVariable.pb_list[i].name)] = '\0';
vaild_flag = 1;
}
break;
}
}
printf("%d:%s\n", vaild_flag, name);
if (vaild_flag) {
return strlen(name);
} else {
return 0;
}
}
//创建电话簿窗体
compo_form_t *func_address_book_form_create(void)
{
//新建窗体和背景
compo_form_t *frm = compo_form_create(true);
f_address_book_list_t *f_book = (f_address_book_list_t *)func_cb.f_cb;
//设置标题栏
compo_form_set_mode(frm, COMPO_FORM_MODE_SHOW_TITLE | COMPO_FORM_MODE_SHOW_TIME);
compo_form_set_title(frm, i18n[STR_CALL_LINK]);
//新建列表
compo_listbox_t *listbox = compo_listbox_create(frm, COMPO_LISTBOX_STYLE_TITLE_NORMAL);
compo_setid(listbox, COMPO_ID_LISTBOX);
if(call_book_cnt){
compo_listbox_set(listbox, tbl_call_list, call_book_cnt);
compo_listbox_set_bgimg(listbox, UI_BUF_COMMON_BG_BIN);
compo_listbox_set_text_modify(listbox, tbl_call_txt_list);
compo_listbox_set_focus_byidx(listbox, 1);
compo_listbox_update(listbox);
listbox->mcb = func_zalloc(sizeof(compo_listbox_move_cb_t)); // 建立移动控制块,退出时需要释放
compo_listbox_move_init_modify(listbox, GUI_SCREEN_HEIGHT * 3 / 8, compo_listbox_gety_byidx(listbox, call_book_cnt - (call_book_cnt >= 2 ? 2 : 1)));
} else {
// 创建 暂无通话记录 文本框
compo_textbox_t *txt = compo_textbox_create(frm, 50);
compo_setid(txt, COMPO_ID_TXT);
compo_textbox_set_location(txt, GUI_SCREEN_CENTER_X, 95+25, GUI_SCREEN_WIDTH - 20, 50);
compo_textbox_set_multiline(txt, true);
compo_textbox_set_autoroll_mode(txt, TEXT_AUTOROLL_MODE_SROLL_CIRC);
compo_textbox_set(txt, i18n[STR_NO_CONTACT]);
compo_textbox_set_visible(txt, true);
compo_picturebox_t *pic = compo_picturebox_create(frm, UI_BUF_COMMON_NOT_CONTACTS_BIN);
compo_setid(pic, COMPO_ID_PIC);
compo_picturebox_set_pos(pic, GUI_SCREEN_CENTER_X, 45+21);
compo_picturebox_set_visible(pic, true);
}
return frm;
}
static void func_address_book_sync_pb_callback(void *info, u16 count)
{
f_address_book_list_t *f_book = (f_address_book_list_t *)func_cb.f_cb;
compo_textbox_t *txt = compo_getobj_byid(COMPO_ID_TXT);
compo_picturebox_t *pic = compo_getobj_byid(COMPO_ID_PIC);
call_book_cnt = 0;
if (info == NULL || count == 0) {
printf("[%s]:error!",__func__);
return;
}
pbap_pb_buf_t *pb_buf = (pbap_pb_buf_t *)info;
for (u16 i = 0; i < count; i++) {
if(strlen(pb_buf[i].name) > PBAP_MAX_NAME_LEN)
{
pb_buf[i].name[PBAP_MAX_NAME_LEN-1] = '\0';
}
if (strlen(pb_buf[i].name) > LISTBOX_TEXT_LEN - 2) {
printf("[%s]:str len = %d exceeds the max!\n", pb_buf[i].name,strlen(pb_buf[i].name));
continue;
}
if (call_book_cnt >= ADDRESS_BOOK_LIST_CNT) {
break;
}
sprintf(tbl_call_txt_list[i].str_txt, "%s", pb_buf[i].name);
printf("func_address_book_sync_pb_callback %s %s\n",pb_buf[i].name,pb_buf[i].num);
call_book_cnt++;
}
if(call_book_cnt){
compo_listbox_t *listbox = compo_getobj_byid(COMPO_ID_LISTBOX);
compo_listbox_set(listbox, tbl_call_list, call_book_cnt);
compo_listbox_set_bgimg(listbox, UI_BUF_COMMON_BG_BIN);
compo_listbox_set_text_modify(listbox, tbl_call_txt_list);
compo_listbox_set_focus_byidx(listbox, 1);
compo_listbox_update(listbox);
if (listbox->mcb == NULL) {
listbox->mcb = func_zalloc(sizeof(compo_listbox_move_cb_t)); // 建立移动控制块,退出时需要释放
}
compo_listbox_move_init_modify(listbox, GUI_SCREEN_HEIGHT * 3 / 8, compo_listbox_gety_byidx(listbox, call_book_cnt - (call_book_cnt >= 2 ? 2 : 1)));
if (txt) {
compo_textbox_set_visible(txt,false);
}
if (pic) {
compo_picturebox_set_visible(pic,false);
}
}
}
//点进图标进入应用
void func_address_book_icon_click(void)
{
int icon_idx;
u8 i = 0;
f_address_book_list_t *f_book = (f_address_book_list_t *)func_cb.f_cb;
pbap_pb_buf_t *pb_list = SysVariable.pb_list;
compo_listbox_t *listbox = f_book->listbox;
icon_idx = compo_listbox_select(listbox, ctp_get_sxy());
if (icon_idx < 0 || icon_idx >= ADDRESS_BOOK_LIST_CNT) {
return;
}
memset(sys_cb.outgoing_number, 0, 20);
for (i = 0; i < 20; i++) {
sys_cb.outgoing_number[i] = pb_list[icon_idx].num[i];
}
if(bt_is_connected()){
bt_call_redial_number();
}
else{
func_popup_new(POPUP_ID_BT_REMIND);
}
}
//电话簿功能事件处理
static void func_address_book_process(void)
{
f_address_book_list_t *f_book = (f_address_book_list_t *)func_cb.f_cb;
compo_listbox_move(f_book->listbox);
/* 同步列表:顶部下拉列表触底后松手反弹,重新同步 */
static bool touch_flag = false;
if (ctp_get_sxy().y < 20 && f_book->listbox->ofs_y <= 10 && f_book->listbox->mcb->flag_move_auto) {
touch_flag = true;
}
if (touch_flag && f_book->listbox->ofs_y == f_book->listbox->mcb->first_y) {
touch_flag = false;
if (bt_is_connected()) {
func_popup_new(POPUP_ID_SYNCING);
if (!bt_pbap_is_syncing()) {
bt_pbap_sync_start(PBAP_OBJECT_PB, SysVariable.pb_list,
ADDRESS_BOOK_LIST_CNT, func_address_book_sync_pb_callback);
}
} else {
func_popup_new(POPUP_ID_BT_REMIND);
}
}
func_process();
}
//电话簿功能消息处理
static void func_address_book_message(size_msg_t msg)
{
f_address_book_list_t *f_book = (f_address_book_list_t *)func_cb.f_cb;
compo_listbox_t *listbox = f_book->listbox;
if (compo_listbox_message(listbox, msg)) {
return; // 处理列表框信息
}
switch (msg) {
case MSG_CTP_CLICK:
func_address_book_icon_click();
break;
case MSG_CTP_LONG:
break;
case MSG_CTP_LONG_DOWN:
case MSG_CTP_SHORT_DOWN:
if (bt_is_connected()) {
if (!bt_pbap_is_syncing()) {
func_popup_new(POPUP_ID_SYNCING);
bt_pbap_sync_start(PBAP_OBJECT_PB, SysVariable.pb_list,
ADDRESS_BOOK_LIST_CNT, func_address_book_sync_pb_callback);
}
} else {
func_popup_new(POPUP_ID_BT_REMIND);
}
break;
case MSG_CTP_SHORT_RIGHT:
bt_pbap_sync_stop();
func_message(msg);
break;
case KU_DELAY_BACK:
if (tick_check_expire(func_cb.enter_tick, TICK_IGNORE_KEY)) {
}
break;
default:
func_message(msg);
break;
}
}
//进入电话簿功能
static void func_address_book_enter(void)
{
func_cb.f_cb = func_zalloc(sizeof(f_address_book_list_t));
func_cb.frm_main = func_address_book_form_create();
f_address_book_list_t *f_book = (f_address_book_list_t *)func_cb.f_cb;
f_book->listbox = compo_getobj_byid(COMPO_ID_LISTBOX);
compo_listbox_t *listbox = f_book->listbox;
if (listbox->type != COMPO_TYPE_LISTBOX) {
halt(HALT_GUI_COMPO_LISTBOX_TYPE);
}
func_cb.enter_tick = tick_get();
/* 进入后如BT属连接状态且没有通话记录则自动同步 */
if (!bt_pbap_is_syncing() && bt_is_connected() && !call_book_cnt) {
func_popup_new(POPUP_ID_SYNCING);
bt_pbap_sync_start(PBAP_OBJECT_PB, SysVariable.pb_list,
ADDRESS_BOOK_LIST_CNT, func_address_book_sync_pb_callback);
}
}
//退出电话簿功能
static void func_address_book_exit(void)
{
f_address_book_list_t *f_book = (f_address_book_list_t *)func_cb.f_cb;
compo_listbox_t *listbox = f_book->listbox;
bt_pbap_sync_stop();
if (listbox->mcb) {
func_free(listbox->mcb); // 释放移动控制块
}
func_cb.last = FUNC_ADDRESS_BOOK;
}
//电话簿功能
void func_address_book(void)
{
func_address_book_enter();
while (func_cb.sta == FUNC_ADDRESS_BOOK) {
func_address_book_process();
func_address_book_message(msg_dequeue());
}
func_address_book_exit();
}