ais_io = {}

local timeTick={} 
function ais_io.timerInit(id)

	timeTick[id] = os.time()
end 

function ais_io.checkTimer(id,diff)
	local ret = false;
	local t1 =os.time()
	if t1-timeTick[id] >= diff then
		
		timeTick[id] = os.time()
		ret = true
	end 
	
	return ret;
end 

function ais_io.existApp(pkgName)
	
	local iRet, sRet = pcall(function()
		local ret = execute("pm list packages")
		if ret:find(pkgName) then
			return true
		else
			return false
		end
	end)
	
	return iRet;
end


function ais_io.setTime(d,t)
	
	os.execute("date -s "..d.."."..t)
end 


function ais_io.disableApp(pkgName)
	os.execute("pm disable "..pkgName)
end 

--显示app
function ais_io.enableApp(pkgname)
	
	os.execute("pm enable  "..pkgname)
end

function TempPath()
	local iRet, sRet = pcall(function()
		return getrcpath():match("(.+)/[^/]").."/"
	end)
	
	if iRet == true then
		return sRet
	else
		return ""
	end
end



--获取用户点击屏幕坐标
--参数为横向分辨率,纵向分辨率,扫描周期
--返回值为一个数组，第一个是x坐标，第二个是y坐标  [作者：小玮]
function ais_io.getUserClickPos(sw,sh,Time)

	local localpath = temppath().."userClickPos"
	os.execute("getevent -pl>"..localpath)
	file=io.open(localpath, "r+")
	data=file:read("*l")
	while data~=nil do
		data=file:read("*l")
		if data~=nil then
			find=nil
			_,_,find=data:find("ABS_MT_POSITION_X.*max%s+(%d*)")
				if find~=nil then
					maxx=find
				end
			find=nil
			_,_,find=data:find("ABS_MT_POSITION_Y.*max%s+(%d*)")
			if find~=nil then
				maxy=find
			end
		end
	end
	
	times=tostring(times)
	os.execute("getevent -l -c "..Time..">"..localpath)
	file=io.open(localpath, "r+");
	data=file:read("*l")
	while data~=nil do
		data=file:read("*l")
		if data~=nil then
			if data:find("ABS_MT_POSITION_X")~=nil then
				x= math.floor(tonumber(data:sub(59,62),16)*sw/maxx)
			elseif data:find("ABS_MT_POSITION_Y")~=nil then
				y= math.floor(tonumber(data:sub(59,62),16)*sh/maxy)
			end
		end
	end
	os.remove(localpath)
	local C = {}
	C[1] = x
	C[2] = y
	
	return C
	
end


--获取屏幕分辨率 
function ais_io.getScreen()
	sh_init()
	local iRet, sRet = pcall(function()
		local ret = execute("dumpsys window")
		local info = {}
		_,_,info[1],info[2],info[3] = ret:find("init=(%d+)x(%d+) (%d+)dpi")
		if info[1] then
			return info
		else
			return nil
		end
	end)
	if iRet == true then
		return sRet
	else
		print(sRet)
		return nil
	end
end 

return ais_io;