SHARE
TWEET

Android Emulator ADB

lyogadi May 2nd, 2019 (edited) 326 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;*****************************************
  2. ;Emulator ADB.au3 by LtnhanSt94
  3. ;Created with ISN AutoIt Studio v. 1.08
  4. ;*****************************************
  5. #include-once
  6. #include <Array.au3>
  7. #include <GDIPlus.au3>
  8. #include <WinAPI.au3>
  9. #include <Color.au3>
  10.  
  11. OnAutoItExitRegister("_Emulator_Exit")
  12.  
  13. Global $__Emulator_Emulator
  14. Global $__Emulator_ADBDRIVE = ["A","B","C", "D", "E", "F", "G", "H"]
  15. GLobal $__Emulator_ADBPATH
  16.  
  17. If ProcessExists("Nox.exe") Then
  18.     $__Emulator_Emulator = "Nox"
  19.  
  20.     For $iDrive in $__Emulator_ADBDRIVE
  21.         If FileExists(StringFormat("%s:\Program Files%s\Nox\bin", $iDrive, "")) Then
  22.             $__Emulator_ADBPATH = StringFormat("%s:\Program Files%s\Nox\bin", $iDrive, "")
  23.             ExitLoop
  24.         EndIf
  25.         If FileExists(StringFormat("%s:\Program Files%s\Nox\bin", $iDrive, " (x86)")) Then
  26.             $__Emulator_ADBPATH = StringFormat("%s:\Program Files%s\Nox\bin", $iDrive, " (x86)")
  27.             ExitLoop
  28.         EndIf
  29.     Next
  30.     If $__Emulator_ADBPATH = "" or not FileExists($__Emulator_ADBPATH) Then
  31.         While 1
  32.             $__Emulator_ADBPATH = FileSelectFolder("Đường dẫn đến thư mục Nox đã cài đặt", "C:\Program Files")
  33.             If @error Then Exit
  34.             If FileExists($__Emulator_ADBPATH & "\bin") Then
  35.                 $__Emulator_ADBPATH &= "\bin"
  36.                 ExitLoop
  37.             EndIf
  38.         WEnd
  39.     EndIf
  40. ElseIf ProcessExists("MEmu.exe") Then
  41.     $__Emulator_Emulator = "MEmu"
  42.  
  43.     Global $__Emulator_MEmuHypervPATH = ""
  44.  
  45.     For $iDrive in $__Emulator_ADBDRIVE
  46.         If FileExists(StringFormat("%s:\Program Files%s\Microvirt\MEmuHyperv", $iDrive, "")) Then
  47.             $__Emulator_MEmuHypervPATH = StringFormat("%s:\Program Files%s\Microvirt\MEmuHyperv", $iDrive, "")
  48.             ExitLoop
  49.         EndIf
  50.         If FileExists(StringFormat("%s:\Program Files%s\Microvirt\MEmuHyperv", $iDrive, " (x86)")) Then
  51.             $__Emulator_MEmuHypervPATH = StringFormat("%s:\Program Files%s\Microvirt\MEmuHyperv", $iDrive, " (x86)")
  52.             ExitLoop
  53.         EndIf
  54.     Next
  55.     If $__Emulator_MEmuHypervPATH = "" or not FileExists($__Emulator_MEmuHypervPATH) Then
  56.         While 1
  57.             $__Emulator_MEmuHypervPATH = FileSelectFolder("Đường dẫn đến thư mục Microvirt đã cài đặt", "C:\Program Files")
  58.             If @error Then Exit
  59.             If FileExists($__Emulator_MEmuHypervPATH & "\MEmuHyperv") Then
  60.                 $__Emulator_MEmuHypervPATH &= "\MEmuHyperv"
  61.                 ExitLoop
  62.             EndIf
  63.         WEnd
  64.     EndIf
  65.     $__Emulator_ADBPATH = StringReplace($__Emulator_MEmuHypervPATH, "\MEmuHyperv", "\MEmu")
  66. Else
  67.     MsgBox(262192,"Thông báo","Không có giả lập đang chạy." & @CRLF & "Hãy mở giả lập trước khi mở App!",0)
  68.     Exit
  69. EndIf
  70.  
  71. _GDIPlus_Startup()
  72.  
  73. ; Example
  74. Global $ADB_LISTDEVICES = _Android_ListDevices()
  75. _ArrayDisplay($ADB_LISTDEVICES)
  76. _Android_ScreenCapture($ADB_LISTDEVICES[0], @ScriptDir & "\test.png", True)
  77. ConsoleWrite(_Android_ScreenPixel($ADB_LISTDEVICES[0], 1052, 226))
  78. _Android_Tap($ADB_LISTDEVICES[0], 1052, 226)
  79.  
  80. Func _Android_ListDevices()
  81.     Local $hDevices = __Run("adb devices")
  82.     $hDevices = StringSplit($hDevices, @LF, 2)
  83.     If @error or UBound($hDevices) <= 2 Then Return SetError(1 , 0, "")
  84.  
  85.     Local $hList[0]
  86.     For $i = 1 to UBound($hDevices) - 2
  87.         _ArrayAdd($hList, StringTrimRight($hDevices[$i], 7))
  88.     Next
  89.     Return $hList
  90. EndFunc
  91.  
  92. Func _Android_Tap($device, $x, $y)
  93.     _RunADB($device, "shell input tap " &$x& " " &$y)
  94. EndFunc
  95.  
  96. Func _Android_Swipe($device, $x1, $y1, $x2, $y2, $Delay = 1500)
  97.     _RunADB($device, "shell input swipe " & $x1 & " "& $y1 & " "& $x2 & " "& $y2 & " "& $Delay)
  98. EndFunc
  99.  
  100. Func _Android_ScreenCapture($device, $hDir = "", $SaveImg = False)
  101.     If $hDir == "" Then $hDir = @TempDir & "\" & StringReplace($device, ":", "_") & '.png'
  102.     _RunADB($device, "shell screencap /sdcard/screen.png")
  103.     _RunADB($device, 'pull /sdcard/screen.png "' & $hDir & '"')
  104.     Local $Bitmap  = _GDIPlus_BitmapCreateFromFile($hDir)
  105.     _RunADB($device, "rm /sdcard/screen.png")
  106.     If not $SaveImg Then FileDelete($hDir)
  107.     Return $Bitmap
  108. EndFunc
  109.  
  110. Func _Android_ImgHandlePixel($ImgHandle, $x, $y)
  111.     Local $hPixel = _GDIPlus_BitmapGetPixel($ImgHandle, $x, $y)
  112.     $hPixel = "0x" & Hex($hPixel, 6)
  113.     Return $hPixel
  114. EndFunc
  115.  
  116. Func _Android_ScreenPixel($device, $x, $y)
  117.     Local $Bitmap = _Android_ScreenCapture($device)
  118.  
  119.     Local $hColor =_Android_ImgHandlePixel($Bitmap, $x, $y)
  120.     _GDIPlus_BitmapDispose($Bitmap)
  121.     Return $hColor
  122. EndFunc
  123.  
  124. Func _RunADB($device, $command)
  125.     Return __Run("adb -s " & $device & " " & $command)
  126. EndFunc
  127.  
  128. Func __Run($sCommand, $PATH = $__Emulator_ADBPATH)
  129.     Local $iPID, $sLine, $sOutput = ""
  130.     $iPID = Run(@ComSpec & " /c " & $sCommand, $PATH, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
  131.     While 1
  132.         $sLine = StdoutRead($iPID)
  133.         If @error Then ExitLoop
  134.         $sOutput &= $sLine
  135.         Sleep(1)
  136.     WEnd
  137.     ProcessWaitClose($iPID)
  138.     Return StringStripCR(StringTrimRight($sOutput, StringLen(@CRLF)))
  139. EndFunc   ;==>__Run
  140.  
  141. Func _Emulator_Exit()
  142.     _GDIPlus_Shutdown()
  143. EndFunc
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top