switch-device 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # DEFINES
  3. LUNCH_TYPE=generic-eng
  4. # GET SCRIPT LOCATION
  5. DIR=`pwd`
  6. BRANCH=(`cd $(dirname ${BASH_SOURCE[0]})/../../.. && pwd`)
  7. cd $DIR
  8. # Usage info
  9. show_help() {
  10. echo "
  11. Usage: ${0##*/} [HELP] [DEVICE]
  12. Quickly switch to a specified device
  13. -h, -?, --help display this help message
  14. <blank> list currently attached devices
  15. DEVICE system switches to first device that
  16. matches this term
  17. Example:
  18. ./sdv prints all connected devices
  19. ./sdv angler switches to first angler
  20. ./sdv ang switches to first angler device
  21. ./sdv vol switches to volantis
  22. ./sdv 6P switches to Nexus 6P
  23. ./sdv 8X switches to first matching device
  24. (eg. 8XV5T15725000936)
  25. "
  26. echo
  27. }
  28. # help message
  29. if [[ ( $1 == "--help" ) || ( $1 == "-h" ) || ( $1 == "-?" ) ]]; then
  30. show_help
  31. return
  32. fi
  33. # if adb is not available, try to set it up
  34. if [ ! `which adb` ]; then
  35. echo "\"adb\" not setup. Using branch \"$BRANCH\" and lunch type \"$LUNCH_TYPE\""
  36. DIR=`pwd`
  37. cd $BRANCH
  38. . build/envsetup.sh > /dev/null
  39. lunch $LUNCH_TYPE > /dev/null
  40. cd $DIR
  41. fi
  42. # get devices...
  43. if [ $# -eq 0 ]; then
  44. adb devices -l
  45. echo "Currently set to \"$ANDROID_SERIAL\""
  46. # ...or switch to specified device
  47. else
  48. STR=(`adb devices -l | grep "$1"`)
  49. if [ ${#STR[@]} -gt 0 ]; then
  50. export ANDROID_SERIAL="$STR"
  51. echo "Switched to device \"$ANDROID_SERIAL\""
  52. else
  53. echo "Device \"$1\" not found"
  54. fi
  55. fi