all repos — caroster @ 1eff8c695a32beae4a1bd07c45c45ec05f27a0f4

[Octree] Group carpool to your event https://caroster.io

backend/e2e/bin/android-emulator.sh (view raw)

  1#!/bin/sh
  2
  3#
  4# Starts an Android virtual device with a writeable filesystem.
  5# If the -hosts option is provided, replaces /etc/hosts on the device with the
  6# given hosts file.
  7# If the -return option is given, returns to the caller when the emulator is
  8# ready, otherwise waits for the emulator process to stop.
  9# If no emulator -avd option is given, starts the first AVD in the list.
 10# If no existing AVD is available, creates a new one.
 11#
 12# Usage: ./android-emulator.sh [-hosts file] [-return] [-- emulator options]
 13#
 14# Copyright 2019, Sebastian Tschan
 15# https://blueimp.net
 16#
 17# Licensed under the MIT license:
 18# https://opensource.org/licenses/MIT
 19#
 20
 21set -e
 22
 23DEVICE_ID='pixel'
 24SYSTEM_IMAGE='system-images;android-[0-9]*;google_apis;x86'
 25SDCARD='512M'
 26
 27if [ -z "$ANDROID_HOME" ]; then
 28  echo 'Error: ANDROID_HOME is not defined.' >&2
 29  exit 1
 30fi
 31
 32adb() {
 33  "$ANDROID_HOME/platform-tools/adb" "$@"
 34}
 35
 36emulator() {
 37  "$ANDROID_HOME/emulator/emulator" "$@"
 38}
 39
 40avdmanager() {
 41  "$ANDROID_HOME/tools/bin/avdmanager" "$@"
 42}
 43
 44sdkmanager() {
 45  "$ANDROID_HOME/tools/bin/sdkmanager" "$@"
 46}
 47
 48normalize() {
 49  echo "$1" | sed 's/[^a-z A-Z 0-9._-]/-/g'
 50}
 51
 52get_avd() {
 53  emulator -list-avds | head -n 1
 54}
 55
 56get_image() {
 57  sdkmanager --list | grep -o "$SYSTEM_IMAGE" | tail -1
 58}
 59
 60download_image() {
 61  sdkmanager "$1"
 62}
 63
 64create_avd() {
 65  echo 'Downloading system image ...'
 66  download_image "$1"
 67  echo 'System image downloaded.'
 68  echo 'Creating Android Virtual Device ...'
 69  avdmanager create avd \
 70    --name "$(normalize "$DEVICE_ID-${1#*;}")" \
 71    --package "$1" \
 72    --device "$DEVICE_ID" \
 73    --sdcard "$SDCARD"
 74  echo 'Virtual Device created.'
 75}
 76
 77has_arg() {
 78  while test $# -gt 0; do
 79    test "$1" = "$ARG" && return 0
 80    shift
 81  done
 82  return 1
 83}
 84
 85has_system_prop() {
 86  test "$(adb shell getprop "$1" | tr -d '\r')" = "$2"
 87}
 88
 89wait_for_device() {
 90  echo 'Waiting for device to be ready ...'
 91  adb wait-for-device
 92  while ! has_system_prop sys.boot_completed 1; do
 93    sleep 1
 94  done
 95  echo 'Device ready.'
 96}
 97
 98update_hosts_file() {
 99  adb root
100  adb remount
101  adb push "$1" /etc/hosts
102  adb unroot
103}
104
105if [ "$1" = -hosts ]; then
106  HOSTS_FILE=$2
107  shift 2
108fi
109
110if [ "$1" = -return ]; then
111  RETURN=true
112  shift
113fi
114
115if [ "$1" = -- ]; then
116  shift
117fi
118
119if ! ARG=-avd has_arg "$@"; then
120  if [ -z "$(get_avd)" ]; then
121    create_avd "$(get_image)"
122  fi
123  set -- -avd "$(get_avd)" "$@"
124fi
125
126if [ -n "$HOSTS_FILE" ]; then
127  set -- -writable-system "$@"
128fi
129
130emulator "$@" & PID=$!
131
132wait_for_device
133
134if [ -n "$HOSTS_FILE" ]; then
135  update_hosts_file "$HOSTS_FILE"
136fi
137
138if [ "$RETURN" = true ]; then
139  exit
140fi
141
142wait "$PID"