autoconnect-wireless-debugging-linux.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/python
  2. # -*- coding: utf-8 -*-
  3. # This script checks your clipboard contents and connects to the wireless debugging bridge if requested
  4. #
  5. # Requirements:
  6. # pyperclip
  7. from threading import Thread
  8. import pyperclip
  9. import time
  10. import os
  11. def waitForNewPaste(timeout=None):
  12. """
  13. This function is taken from pyperclip 1.8.2 as it was removed in later releases.
  14. This function call blocks until a new text string exists on the
  15. clipboard that is different from the text that was there when the function
  16. was first called. It returns this text.
  17. This function raises PyperclipTimeoutException if timeout was set to
  18. a number of seconds that has elapsed without non-empty text being put on
  19. the clipboard.
  20. """
  21. startTime = time.time()
  22. originalText = pyperclip.paste()
  23. while True:
  24. currentText = pyperclip.paste()
  25. if currentText != originalText:
  26. return currentText
  27. time.sleep(0.01)
  28. if timeout is not None and time.time() > startTime + timeout:
  29. raise pyperclip.PyperclipTimeoutException('waitForNewPaste() timed out after ' + str(timeout) + ' seconds.')
  30. def main():
  31. if not pyperclip.is_available:
  32. return
  33. connect_action_prefix = 'connect-wireless-debugging://'
  34. previous_clipboard = pyperclip.paste()
  35. while True:
  36. new_clipboard = waitForNewPaste()
  37. print('New item copied: ' + new_clipboard)
  38. if not new_clipboard.startswith(connect_action_prefix):
  39. previous_clipboard = new_clipboard
  40. continue
  41. address = new_clipboard.removeprefix(connect_action_prefix)
  42. result = os.popen('adb connect ' + address).readline()
  43. result = result[0].upper() + result[1:]
  44. if result.startswith('Connected to '):
  45. icon = 'nm-device-wireless'
  46. pyperclip.copy(previous_clipboard)
  47. else:
  48. icon = 'error'
  49. raw_connection_data = new_clipboard.removeprefix(connect_action_prefix)
  50. pyperclip.copy(raw_connection_data)
  51. command = 'notify-send' + \
  52. ' --expire-time 3000' + \
  53. ' --icon ' + icon + \
  54. ' --app-name "Android Wireless Debugging"' + \
  55. ' "' + result + '"'
  56. # 'notify-send' blocks the thread, so it is invoked in the background
  57. Thread(target=os.system, args=[command]).start()
  58. if __name__ == '__main__':
  59. main()