
/*=============================================================================================
	author:ZhaoJun
	version:1.2
	email:2732810281@qq.com
=============================================================================================*/


//严格模式
/*-------------------------------------------------------------------------------------------*/	
'use strict';
/*-------------------------------------------------------------------------------------------*/
    
// 时间操作
/*-------------------------------------------------------------------------------------------*/	
class adb
{
	static async current(timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb shell dumpsys window windows | findstr "Current"`,cwd,timeout)
	}

	static async window(cur='',timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
        cur=cur.replace
		(
			/.*?Window\{(.*?)\}/,(source,target)=>
			{
				let arr=target.split(' ')
				if(arr.length==3)
				{
					return arr[2]
				}
				else
				{
					return arr[0]
				}
			}
		).trim()

		return $Sys.cmd(`adb shell am start -n ${cur}`,cwd,timeout)
	}

	static async pull(source,traget,timeout=3000) 
	{
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb pull /sdcard/${source} ${traget}`,cwd,timeout)
	}

	static async push(source,traget,timeout=3000) 
	{
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb push ${source} /sdcard/${traget}`,cwd,timeout)
	}

	static async install(source,timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb install -r ${source}`,cwd,timeout)
	}

	static async unInstall(packageName,timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb uninstall ${packageName}`,cwd,timeout)
	}

	static async text(text,timeout=3000)
	{
		//前提先将键盘设置为英文键盘
		//发送文本内容，不能发送中文
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb shell input text ${text}`,cwd,timeout)
	}

	static async keyevent(event,timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
		//KEYCODE_HOME 主键 3
		//KEYCODE_BACK 返回键 4
		//KEYCODE_MENU 菜单键 1
        
		return await $Sys.cmd(`adb shell input keyevent ${event}`,cwd,timeout)
	}

	static async tap(x,y,timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb shell input tap ${x} ${y}`,cwd,timeout)
	}

	static async swipe(x1,y1,x2,y2,tran=300,timeout=3000)
	{
		//tran持续时间，单位ms
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb shell input swipe ${x1} ${y1} ${x2} ${y2} ${tran}`,cwd,timeout)
	}

	static async screencap(source,timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
        
		return await $Sys.cmd(`adb shell screencap -p /sdcard/${source}`,cwd,timeout)
	}

	static async screenget(traget,timeout=3000)
	{
		let cwd=$Config.root+'/extend/adb'
        
		await $Sys.cmd(`adb shell screencap -p /sdcard/_private_screen.png`,cwd,timeout)
		return await adb.pull('_private_screen.png',traget,timeout)
	}
}
/*-------------------------------------------------------------------------------------------*/    
module.exports=adb;

