var mhc_status=null;
function connect(){
try{
var ret=ebus.app_upd.connect();
if(ret<0)reconnect();}catch(e){};};
function reconnect(){
var timer={};
append_timer(timer,1000,function(obj){
var ret=ebus.app_upd.connect();
if(ret==0)
delete_timer(obj);});};
var enable_appupd_log=false;
add_default_stage_event_hook(ebus.app_upd,function (stg,obj,ev,data){
if(enable_appupd_log){
try{
for(var name in ebus.app_upd)
if(name.match("^(EV|REQ)_")&&ebus.app_upd[name]==ev)
console.log("ebus.app_upd."+name);}catch(e){}
}
switch(ev){
case ebus.app_upd.EV_DISCONNECTED:
connect();
break;
case ebus.app_upd.REQ_SCREEN_RESERVE:
if(!isString(data)){
ebus.app_upd.send(ebus.app_upd.EV_SCREEN_RESERVE_NG,data,"INVALID_ID");
break;}
prepare(data);
break;
case ebus.app_upd.EV_SEND_NG:
console.log("[ebus.app_upd] send error.");
break;
case ebus.app_upd.EV_STATUS_UPDATE:
if(enable_appupd_log){
try{
for(var name in ebus.app_upd)
if(name.match("^(STATUS)_")&&ebus.app_upd[name]==data)
console.log(">> ebus.app_upd."+name);}catch(e){}
}
switch(data){
case ebus.app_upd.STATUS_SCREEN_INSTALLED:
install_in(SCREEN_INST_AUTO_SUFFIX);
complete();
break;
case ebus.app_upd.STATUS_SYNC_OK:
if(!HOME.operator)break;
var active_screen=HOME.operator.getActiveScreen();
for(var i=0;i<active_screen.length;i++){
if(active_screen[i]&&active_screen[i] instanceof Screen){
active_screen[i].updateGadget();}
}
syncScreenNoticeMessage();
break;
case ebus.app_upd.STATUS_SYNC_NG:
syncScreenNoticeMessage();
break;
case ebus.app_upd.STATUS_MARKET_INFO_UPDATED:
isDisclaimerNeeded();
break;
default:
break;}
break;
case ebus.app_upd.REQ_FDP_COLOR:
var data=JSON.stringify(fdp_color_p);
ebus.app_upd.send(ebus.app_upd.EV_FDP_COLOR,data);
break;
case ebus.app_upd.EV_USER_PROFILE_UPDATE:
if(runUpdateUsrProfHook.apply(this,arguments))
return true;
break;
case ebus.app_upd.REQ_LAUNCH_SETUPUSR:
if(!isString(data))break;
var req;
try{
req=JSON.parse(data);}catch(e){
console.log("[ebus.app_upd.REQ_LAUNCH_SETUPUSR] format error. "+e.message);
break;}
var arg="";
if(req.create===true){
arg="-C";
apputil.launch_apl(SETUPUSER_APPS,arg);}else if((isNumber(req.uprofid)&&req.uprofid>=0)&&
(isNumber(req.focus)&&req.focus>=0)){
arg="-U %s -f %s".replace("%s",req.uprofid).replace("%s",req.focus);
apputil.launch_apl(SETUPUSER_APPS,arg);}
return true;
break;
default:
}
return true;})
add_default_stage_event_hook(system.fdp_color,function (stg,obj,ev){
switch(ev){
case system.fdp_color.EV_UPDATE:
var data=JSON.stringify(fdp_color_p);
ebus.app_upd.send(ebus.app_upd.EV_FDP_COLOR,data);
break;
default:
break;}
return true;});
function prepare(screen_id){
var me=this;
if(getUserScreenNum()>=HOME.SCREEN_USER_MAX_NUM){
console.log("More than this, you can not add Home Screen. (check1)");
ebus.app_upd.send(ebus.app_upd.EV_SCREEN_RESERVE_NG,screen_id,"MAX_SCREEN_NUM");
return;}
var url=getScreenAPI("PREPARE")+"?screen_id="+screen_id;
var prepare_func=function(result){
if(result==null||
result.error!="0"||
!isString(result.screen_id)){
console.log("<<< cannot get screen_install_id >>> "+screen_id);
var code=(result)?result.error:"NULL";
ebus.app_upd.send(ebus.app_upd.EV_SCREEN_RESERVE_NG,screen_id,"PREPARE_ERROR:"+code);
return;}
if(getUserScreenNum()>=HOME.SCREEN_USER_MAX_NUM){
console.log("More than this, you can not add Home Screen. (check2)");
ebus.app_upd.send(ebus.app_upd.EV_SCREEN_RESERVE_NG,screen_id,"MAX_SCREEN_NUM");
return;}
var order=HOME.getScreenOrder();
var vacant_key=HOME.searchSpaceNumForOrder(order);
if(vacant_key){if(HOME.operator===HOME.switchscreen_op&&
HOME.operator.getMenuType()=="add2"){
var current_key=HOME.operator.getLastScreenKey();
var index=0;
if(current_key=="first"){
index=0;}else{
index=order.indexOf(current_key);
if(index==-1)index=order.length-1;
index+=1;}
if(index>=order.length){
order.push(vacant_key+":"+result.screen_id);}else{
order.splice(index,0,vacant_key+":"+result.screen_id);}
}else{
order.push(vacant_key+":"+result.screen_id);}
HOME.storeScreenOrder(order);HOME.SCREEN_ORDER=HOME.getScreenOrder();}else{
console.log("More than this, you can not add Home Screen. (regist)");
ebus.app_upd.send(ebus.app_upd.EV_SCREEN_RESERVE_NG,screen_id,"MAX_SCREEN_NUM");
return;}
var screen_install_id=result.screen_id;
var url=getScreenAPI("INSTALL")+"?screen_id="+screen_install_id;
var install_func=function(result){
if(result==null||result.error!="0"){
var code=(result)?result.error:"NULL";
ebus.app_upd.send(ebus.app_upd.EV_SCREEN_RESERVE_NG,screen_id,"INSTALL_ERROR:"+code);
return;}
if(isString(startup_screen.mhc_status)&&
startup_screen.mhc_status.match("^(LAUNCH|ANIMATION|INSTALLING:)"))
{
startup_screen.mhc_status="INSTALLING:"+screen_install_id;}else{
startup_screen.mhc_status=null;}
ebus.app_upd.send(ebus.app_upd.EV_SCREEN_RESERVE_OK,screen_id,screen_install_id);
ebus.app_upd.sync();};
requestAPI(url,install_func);};
requestAPI(url,prepare_func);}
function check_unknown_screen(order,screen_install_id){
var len=order.length;
for(var i=0;i<len;i++){
if(isDLScreen(order[i])){
if(getScreenInstallId(order[i])==screen_install_id)
return order[i];}
}
return null;}
function uninstall(screen_install_id,hook_func){
var screen_id=screen_install_id;
if(isDLScreen(screen_id)){
var ret=screen_id.match("^(.+)\\."+SCREEN_DL_SUFFIX+"$");
if(ret&&ret.length>1){
screen_id=ret[1];}
}
var url=getScreenAPI("DELETE")+"?screen_id="+screen_id;
var delete_func=function(result){
if(!result||result.error!="0"){
console.log("<<< Uninstall fail >>> "+screen_id);
if(hook_func){
try{
hook_func(null);}catch(e){
console.log(e.message);}
}
return;}
console.log("<<< Uninstalled >>> "+screen_id);
if(hook_func){
try{
hook_func(result);}catch(e){
console.log(e.message);}
}
}
requestAPI(url,delete_func);}
function uninstall_auto(){
var screen_list=null;
var ret=false;
var suffix=new RegExp("^(.+)\\."+SCREEN_DELETE_AUTO_SUFFIX+"$");
var screen_dir_list=null;

try{
screen_dir_list=screen_nvram_file.screen_dir_list();
screen_list=doFilter(screen_dir_list,suffix);}catch(e){
return true;};
try{
var order=HOME.getScreenOrder();
var len=screen_list.length;
for(var i=0;i<len;i++){
var screen_id=screen_list[i].match(suffix)[1];
if(!isDLScreen(screen_id))
break;
var max=order.length;
for(var index=0;index<order.length;index++){
if(getScreenInstallId(order[index])==screen_id){
order.splice(index,1);
try{
screen_nvram_file.remove_screen_dir({screen_id:screen_id});
HOME.storeScreenOrder(order);console.log("<<< Uninstalled >>> "+screen_id);}catch(e){};
break;}
}
var pattern=SCREEN_DL_SUFFIX+"|"+SCREEN_INST_SUFFIX+"|"+SCREEN_INST_AUTO_SUFFIX;
var pattern=new RegExp("^"+screen_id+"\\.("+pattern+")$");
var delete_list=doFilter(screen_dir_list,pattern);
for(var index=0;index<delete_list.length;index++){
try{
screen_nvram_file.remove_screen_dir({screen_id:delete_list[index]});}catch(e){
console.log("cannot remove "+delete_list[index]+" : "+e.message);}
}
try{
screen_nvram_file.remove_screen_dir({screen_id:screen_list[i]});}catch(e){
console.log("<<< Uninstall fail >>> "+screen_list[i]);}
}
}catch(e){
console.log(e.message);
return false;}
return true;}
uninstall_auto();
function make_screen_info(order_id,screen_path,screen_id){
try{
var info=screen_nvram_file.read({screen_id:screen_path,filename:"screen"});
info=eval("("+sysutil.tconv.binary_to_string(info)+")");
info.screen_name=decodeURIComponent(info.screen_name);
info.wallpaper=SCREEN_PATH+screen_id+"/wallpaper.jpg";
info.template=screen_id;
if(!info.product_id_list)info.product_id_list=[TV_GADGET];
info.uniq_id_list=HOME.getUniqIDs(info.product_id_list);
if(info.screen_name_lang!==undefined){
var tmp=info.screen_name_lang;
info.screen_name_lang={};
for(var name in tmp)
info.screen_name_lang[name]=encodeURIComponent(tmp[name]);}
return HOME.storeScreenInfo(info,order_id+"."+SCREEN_DL_SUFFIX);}catch(e){
console.log("make_screen_info: "+e.message);
return false;}
}
function make_gadget_data(order_id,screen_path){
var info=screen_nvram_file.read({screen_id:screen_path,filename:"screen"});
info=eval("("+sysutil.tconv.binary_to_string(info)+")");
try{
data=screen_nvram_file.read({screen_id:screen_path,filename:"gadget_data"});
data=eval("("+sysutil.tconv.binary_to_string(data)+")");}catch(e){
console.log(e.message);
return true;}
if(!data.product_data_list){
return true;}
var d_len=data.product_data_list.length;
var i_len=info.product_id_list.length;
var id=0;
for(var i=0;i<d_len;i++){
if(!isString(data.product_data_list[i]))
continue;
for(var j=i;j<i_len;j++){
if(info.product_id_list[j]!=data.product_id_list[i])
continue;
try{
if(data.product_data_list[i].toString()!=""){
screen_nvram_file.write({screen_id:screen_path,filename:"data/"+info.uniq_id_list[j],
data:data.product_data_list[i].toString()});}
}catch(e){}
break;}
}
try{
screen_nvram_file.remove({screen_id:screen_path,filename:"gadget_data"});}catch(e){}
return true;}
function complete(){
var screen_list=null;
try{
var tmp=screen_nvram_file.screen_dir_list();
screen_list=doFilter(tmp,new RegExp("\."+SCREEN_DL_SUFFIX+"$"));}catch(e){
screen_list=null;};
if(!screen_list||screen_list.length==0)return;
console.log("<<< Installing >>> "+screen_list);
try{
var order=HOME.getScreenOrder();
var len=screen_list.length;
var complete_installing=[];
for(var i=0;i<len;i++){
try{
var screen_path=screen_list[i];
var screen_id=screen_list[i].match("^(.+)\\."+SCREEN_DL_SUFFIX+"$")[1];
var order_id=check_unknown_screen(order,screen_id);
if(order_id===null){
var make_uninstall_func=function(){
var screen_install_id=screen_list[i];return function(result){
if(!result||result.error!="0")return;
try{
screen_nvram_file.remove_screen_dir({screen_id:screen_install_id});}catch(e){
console.log(e.message);}
};};
uninstall(screen_list[i],make_uninstall_func());
continue;}
if(!make_screen_info(order_id,screen_path,screen_id)){
console.log("<<< Install fail >>> "+screen_list[i]);
continue;}
make_gadget_data(order_id,screen_path);
try{
screen_nvram_file.read({filename:"screen",screen_id:screen_id});
screen_nvram_file.remove_screen_dir({screen_id:screen_id});}catch(e){}
screen_nvram_file.rename({from:screen_list[i],to:screen_id});
complete_installing.push(screen_id);
console.log("<<< Installed >>> "+screen_id);}catch(e){
console.log(e.message);};}
}catch(e){};
HOME.SCREEN_ORDER=HOME.getScreenOrder();
var order=HOME.SCREEN_ORDER;
if(isActiveStatus()){
if(HOME.operator&&HOME.operator.updateScreen())
return;}
try{
if(!order||order.length==0||isActiveStatus())return;
if(startup_screen.mhc_status){
var tmp=startup_screen.mhc_status.match("^INSTALLING:(.+)");
if(!tmp||tmp.length==0)return;
if(complete_installing.indexOf(tmp[1])!=-1){
var screen_key=null;
for(var i=0;i<order.length;i++){
if(isDLScreen(order[i])&&order[i].match(":"+tmp[1]+"$")){
screen_key=order[i];
break;}
}
if(screen_key){
startup_screen.mhc_status="ANIMATION:"+screen_key;}
}
}
}catch(e){};
return;}
function reinstall(){
var screen_list=null;
try{
screen_list=screen_nvram_file.screen_dir_list();}catch(e){
return true;};
var isNeedtoSync=false
try{
var order=HOME.getScreenOrder();
var len=order.length;
for(var i=0;i<len;i++){
if(!isDLScreen(order[i]))continue;
var screen_install_id=getScreenInstallId(order[i]);
if(screen_list.indexOf(screen_install_id)==-1)
{
var url=getScreenAPI("INSTALL")+"?screen_id="+screen_install_id;
requestAPI(url,null);
isNeedtoSync=true;}
}
}catch(e){
return false;}
if(isNeedtoSync)
ebus.app_upd.sync();
return true;}
function garbageCollection(){
var screen_list=null;
try{
var tmp=screen_nvram_file.screen_dir_list();
screen_list=doFilter(tmp,new RegExp("^[^\.]+$"));}catch(e){
return true;};
try{
var order=HOME.getScreenOrder();
var len=order.length;
for(var i=0;i<len;i++){
if(screen_list.length<1)break;
if(!isDLScreen(order[i]))continue;
var screen_install_id=getScreenInstallId(order[i]);
var id=screen_list.indexOf(screen_install_id);
screen_list.splice(id,1);}
}catch(e){}
try{
var len=screen_list.length;
for(var i=0;i<len;i++){
var make_uninstall_func=function(){
var screen_install_id=screen_list[i];return function(result){
if(!result||result.error!="0")return;
try{
screen_nvram_file.remove_screen_dir({screen_id:screen_install_id});}catch(e){
console.log(e.message);}
};};
uninstall(screen_list[i],make_uninstall_func());}
}catch(e){
return false;}
return true;}
function install(){
var timer={};
append_timer(timer,10,function(obj){
delete_timer(obj);
install_in("("+SCREEN_INST_SUFFIX+"|"+SCREEN_INST_AUTO_SUFFIX+")");});}
function install_in(pattern){
var screen_list=null;
var ret=false;
var suffix=new RegExp("^(.+)\\.("+pattern+")$");
try{
var tmp=screen_nvram_file.screen_dir_list();
screen_list=doFilter(tmp,suffix);}catch(e){
return true;};
var reserve_partner_slot=0;
var reserve_user_slot=0;
try{
var order=HOME.getScreenOrder();
var len=screen_list.length;
var userScreenNum=getUserScreenNum();
for(var i=0;i<len;i++){
var nv_slot=order.length+reserve_partner_slot+reserve_user_slot;
var max_slot=HOME.SCREEN_FIXED_NUM+HOME.SCREEN_USER_MAX_NUM+HOME.SCREEN_PARTNER_ADD_NUM;
if(nv_slot>=max_slot)
continue;
var tmp=screen_list[i].match(suffix);
var screen_id=tmp[1];
var extension=tmp[2];
if(extension==SCREEN_INST_AUTO_SUFFIX){
if(getPartnerScreenNum()+reserve_partner_slot>=HOME.SCREEN_PARTNER_ADD_NUM)
continue;}
if(!isDLScreen(screen_id))
continue;
var info=null;
try{
info=screen_nvram_file.read({screen_id:screen_list[i],filename:"screen"});
info=eval("("+sysutil.tconv.binary_to_string(info)+")");
info.install_type=extension;
var data=JSON.stringify(info);
screen_nvram_file.write({screen_id:screen_list[i],filename:"screen",data:data});}catch(e){
console.log(e.message);
continue;}
var vacant_key=HOME.searchSpaceNumForOrder(order);
if(vacant_key){var id=null;
try{
var info=screen_nvram_file.read({screen_id:screen_list[i],filename:"screen"});
info=eval("("+sysutil.tconv.binary_to_string(info)+")");
id=parsePosition(info);}catch(e){
console.log(e.message);}
if(!isNumber(id)){
order.push(vacant_key+":"+screen_id);}else{
order.splice(id,0,vacant_key+":"+screen_id);}
HOME.storeScreenOrder(order);HOME.SCREEN_ORDER=HOME.getScreenOrder();}else{
break;}
try{
screen_nvram_file.read({filename:"screen",screen_id:screen_id});
screen_nvram_file.remove_screen_dir({screen_id:screen_id});}catch(e){}
screen_nvram_file.rename({from:screen_list[i],to:screen_id+"."+SCREEN_DL_SUFFIX});
if(extension==SCREEN_INST_AUTO_SUFFIX){
reserve_partner_slot++;}else{
reserve_user_slot++;}
ret=true;}
}catch(e){}
if(ret)complete();}
function parsePosition(str){
return parseInt(str.position);}
var screenNoticeMessageData=[];
function initScreenNoticeMessage(){
var data=null;
try{
var tmp=apps_nvram_file.read({product_id:"home",filename:"screen_notice_message.json"});
data=eval("("+sysutil.tconv.binary_to_string(tmp)+")");}catch(e){}
screenNoticeMessageData=[];
for(var org_id in data)
screenNoticeMessageData.push(org_id);}
initScreenNoticeMessage();
function isScreenNoticeMessage(screen_key){
if(!screen_key||!isDLScreen(screen_key))
return false;
var id=-1;
try{
var info=HOME.getScreenInfo(screen_key);
id=screenNoticeMessageData.indexOf(info.org_id);}catch(e){}
return (id!=-1);}
function getScreenNoticeMessage(screen_key){
var data;
if(!screen_key||!isDLScreen(screen_key))
return false;
var tmp=apps_nvram_file.read({product_id:"home",filename:"screen_notice_message.json"});
data=eval("("+sysutil.tconv.binary_to_string(tmp)+")");
var info=HOME.getScreenInfo(screen_key);
if(!info.org_id||!data[info.org_id]||!data[info.org_id].message)
return;
var lang_list=data[info.org_id].message;
try{
var tmplang=lang_list[system.locale.language.substr(0,5)];
if(isString(tmplang))
return tmplang;
var tmplang=lang_list[system.locale.language.substr(0,2)];
if(isString(tmplang))
return tmplang;
var tmplang=lang_list["default"];
if(isString(tmplang))
return tmplang;}catch(e){
console.log("cannot change screen_name."+e.message);};
return;}
function syncScreenNoticeMessage(){
var list=[];
var order=HOME.getScreenOrder();
for(var i=0;i<order.length;i++){
if(!isDLScreen(order[i]))continue;
var info=HOME.getScreenInfo(order[i]);
if(isDLScreenKey(info.org_id)){
list.push(info.org_id);}
}
if(list.length==0)
return;
var url=getScreenAPI("REPORT")+"?screen_id="+list.join(",");
var finished_func=function (body){
var data=null;
try{
data=apps_nvram_file.read({product_id:"home",filename:"screen_notice_message.json"});}catch(e){}
var newdata=JSON.stringify(body);
try{
if(data!=newdata){
apps_nvram_file.write({product_id:"home",filename:"screen_notice_message.json",data:newdata});}
}catch(e){
console.log("cannot store screen_notice_message.json file. "+e.message);}
screenNoticeMessageData=[];
for(var org_id in body)
screenNoticeMessageData.push(org_id);};
requestAPI(url,finished_func);}
function requestAPI(url,hook_func){
if(!system.is_ip_available){
if(hook_func)hook_func(null);
return
}
http_request({url:url,
onload:function(status,header,body){
try{
if(status!=200)
throw "status error.";
var ret=eval("("+body+")");
if(hook_func)hook_func(ret);}catch(e){
if(hook_func)hook_func(null);}
}});}
provide("startup_screen");
