2021-02-18 15:12:35 +00:00
|
|
|
#!/bin/bash
|
2020-12-09 17:15:51 +00:00
|
|
|
#############################################################################
|
|
|
|
##
|
2021-05-27 10:58:26 +00:00
|
|
|
## Copyright (C) 2021 The Qt Company Ltd.
|
2020-12-09 17:15:51 +00:00
|
|
|
## Contact: https://www.qt.io/licensing/
|
|
|
|
##
|
|
|
|
## This file is part of the plugins of the Qt Toolkit.
|
|
|
|
##
|
|
|
|
## $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
|
|
|
## Commercial License Usage
|
|
|
|
## Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
## accordance with the commercial license agreement provided with the
|
|
|
|
## Software or, alternatively, in accordance with the terms contained in
|
|
|
|
## a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
## and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
## information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
##
|
|
|
|
## GNU General Public License Usage
|
|
|
|
## Alternatively, this file may be used under the terms of the GNU
|
|
|
|
## General Public License version 3 as published by the Free Software
|
|
|
|
## Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
## included in the packaging of this file. Please review the following
|
|
|
|
## information to ensure the GNU General Public License requirements will
|
|
|
|
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
##
|
|
|
|
## $QT_END_LICENSE$
|
|
|
|
##
|
|
|
|
#############################################################################
|
|
|
|
# This util launches the Android emulator and ensures it doesn't stuck/freeze
|
|
|
|
# by detecting that and restarting it
|
|
|
|
|
|
|
|
set -ex
|
|
|
|
|
|
|
|
EMULATOR_MAX_RETRIES=5
|
2021-10-18 09:51:06 +00:00
|
|
|
EMULATOR_EXEC="$ANDROID_SDK_ROOT/emulator/emulator"
|
|
|
|
ADB_EXEC="$ANDROID_SDK_ROOT/platform-tools/adb"
|
2021-05-27 10:58:26 +00:00
|
|
|
if [[ -z "${ANDROID_EMULATOR}" ]]; then
|
|
|
|
EMULATOR_NAME="@x86emulator"
|
|
|
|
else
|
|
|
|
EMULATOR_NAME="$ANDROID_EMULATOR"
|
|
|
|
fi
|
2022-01-04 16:41:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
function check_for_android_device
|
|
|
|
{
|
|
|
|
$ADB_EXEC devices \
|
|
|
|
| awk 'NR==2{print $2}' | grep -qE '^(online|device)$'
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_if_fully_booted
|
|
|
|
{
|
|
|
|
# The "getprop" command separates lines with \r\n so we trim them
|
|
|
|
bootanim=` $ADB_EXEC shell getprop init.svc.bootanim | tr -d '\r\n'`
|
|
|
|
boot_completed=`$ADB_EXEC shell getprop sys.boot_completed | tr -d '\r\n'`
|
|
|
|
[ "$bootanim" = stopped ] && [ "$boot_completed" = 1 ]
|
|
|
|
}
|
|
|
|
|
2020-12-09 17:15:51 +00:00
|
|
|
|
|
|
|
for counter in `seq 1 ${EMULATOR_MAX_RETRIES}`; do
|
|
|
|
$ADB_EXEC start-server
|
2022-01-04 16:41:47 +00:00
|
|
|
|
|
|
|
if check_for_android_device
|
|
|
|
then
|
|
|
|
echo "Emulator is already running but it shouldn't be. Aborting\!"
|
|
|
|
exit 3
|
|
|
|
fi
|
|
|
|
|
2020-12-09 17:15:51 +00:00
|
|
|
echo "Starting emulator, try ${counter}/${EMULATOR_MAX_RETRIES}"
|
2021-05-28 08:44:53 +00:00
|
|
|
$EMULATOR_EXEC $EMULATOR_NAME -gpu swiftshader_indirect -no-audio -partition-size 4096 -cores 4 -memory 3500 -no-snapshot-load -no-snapshot-save &>/dev/null &
|
2020-12-09 17:15:51 +00:00
|
|
|
emulator_pid=$!
|
|
|
|
|
2022-01-04 16:41:47 +00:00
|
|
|
$ADB_EXEC wait-for-device
|
2020-12-09 17:15:51 +00:00
|
|
|
|
2022-01-04 16:41:47 +00:00
|
|
|
# Wait about one minute for the emulator to come up
|
|
|
|
emulator_status=down
|
|
|
|
for i in `seq 60`
|
|
|
|
do
|
|
|
|
sleep 1
|
|
|
|
if check_for_android_device && check_if_fully_booted
|
|
|
|
then
|
|
|
|
emulator_status=up
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
2020-12-09 17:15:51 +00:00
|
|
|
|
|
|
|
# If emulator status is still offline after timeout period,
|
|
|
|
# we can assume it's stuck, and we must restart it
|
2022-01-04 16:41:47 +00:00
|
|
|
if [ $emulator_status = up ]
|
|
|
|
then
|
2020-12-09 17:15:51 +00:00
|
|
|
echo "Emulator started successfully"
|
|
|
|
break
|
|
|
|
else
|
|
|
|
if [ $counter -lt $EMULATOR_MAX_RETRIES ]; then
|
2022-01-04 16:41:47 +00:00
|
|
|
echo "Emulator failed to start, forcefully killing current instance and re-starting emulator"
|
2020-12-09 17:15:51 +00:00
|
|
|
kill $emulator_pid || true
|
|
|
|
sleep 5
|
|
|
|
elif [ $counter -eq $EMULATOR_MAX_RETRIES ]; then
|
2022-01-04 16:41:47 +00:00
|
|
|
echo "Emulator failed to start, reached maximum number of retries. Aborting\!"
|
|
|
|
exit 2
|
2020-12-09 17:15:51 +00:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
|
2022-01-04 16:41:47 +00:00
|
|
|
exit 0
|