• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3# Copyright (C) 2021 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17readme() {
18  echo '''
19Change forceColdBoot for all AVD config.ini files.
20If TAG_ID of AVD is empty, it will change all AVDs. e.g.
21FORCE_COLD_BOOT="yes" AVD_ROOT_DIR="$HOME/.android/avd" TAG_ID="android-automotive" ./set_avds_force_cold_boot.sh
22'''
23}
24
25# set up for Linux or macOS
26OS="$(uname -s)"
27echo "Running on $OS"
28if [[ $OS == "Linux" ]]; then
29  SED_I_CMD="sed -i "
30elif [[ $OS == "Darwin" ]]; then
31  SED_I_CMD="sed -i ''"
32else
33  echo "ERROR: this does not work on $OS"
34  exit
35fi
36
37MY_NAME=$0
38SCRIPT_NAME=${MY_NAME##*/}
39SCRIPT_DIR=${MY_NAME%/$SCRIPT_NAME}
40echo Running from $SCRIPT_DIR
41
42# Export VAR with the value of the key in a file as:
43# getValue "VAR" "KEY" "config.ini FILE"
44# e.g. getValue "MY_AVD_ID" "AvdId" "$avd_dir/config.ini"
45getValue() {
46  VAR=$1
47  KEY=$2
48  FILE=$3
49  LINE=$(cat $FILE | grep $KEY)
50  VALUE=${LINE##*=}
51  export "${VAR}=$VALUE"
52  echo "${VAR}=$VALUE"
53}
54
55
56# Set Cold Boot or Quick/Fast Boot for an AVD as:
57# setForceColdBoot "ForceColdBoot" "ForceFastBoot" "config.ini FILE"
58# e.g. setForceColdBoot "yes" "no" "$avd_dir/config.ini"
59setForceColdBoot() {
60  forceColdBoot=$1
61  forceFastBoot=$2
62  avdConfig=$3
63
64  echo "CHANGE: fastboot.forceColdBoot=$forceColdBoot & fastboot.forceFastBoot=$forceFastBoot in $avdConfig"
65  $SED_I_CMD '/^fastboot.forceColdBoot/d' $avdConfig
66  $SED_I_CMD '/^fastboot.forceFastBoot/d' $avdConfig
67  echo "fastboot.forceColdBoot=$forceColdBoot" >> $avdConfig
68  echo "fastboot.forceFastBoot=$forceFastBoot" >> $avdConfig
69  cat $avdConfig | grep "fastboot.forceColdBoot"
70  cat $avdConfig | grep "fastboot.forceFastBoot"
71}
72
73# Set Cold Boot or Quick/Fast Boot for an AVD as:
74# setAllAVDsForceColdBoot "ForceColdBoot" "ForceFastBoot" "AVD/ROOT/DIR" "TAG.ID"
75# e.g. setAllAVDsForceColdBoot "yes" "no" "$HOME/.android/avd" "android-automotive"
76setAllAVDsForceColdBoot() {
77  forceColdBoot=$1
78  forceFastBoot=$2
79  avdRootDir=$3
80  avdTagId=$4
81
82  for file in $(ls "$avdRootDir"); do
83    avd_dir="$avdRootDir/$file"
84    if [[ $file == *.avd ]]; then
85      # Filter AVD type by TAG_ID
86      config_file="$avd_dir/config.ini"
87      getValue "myAvdId"  "AvdId" $config_file
88      if [[ -n $avdTagId  ]]; then
89        getValue "myAvdTagId" "tag.id" $config_file
90        if [[ $myAvdTagId != $avdTagId ]]; then
91          echo "SKIP: $myAvdId is $myAvdTagId rather $avdTagId"
92          echo
93          continue
94        fi
95      fi
96      setForceColdBoot $forceColdBoot $forceFastBoot $config_file
97      echo
98    fi
99  done
100}
101
102if [[ -z $AVD_ROOT_DIR ]]; then
103  AVD_ROOT_DIR="$HOME/.android/avd"
104fi
105echo "AVD_ROOT_DIR=$AVD_ROOT_DIR"
106
107if [[ -z $FORCE_COLD_BOOT ]]; then
108  FORCE_COLD_BOOT="yes"
109fi
110echo "FORCE_COLD_BOOT=$FORCE_COLD_BOOT"
111
112case "$FORCE_COLD_BOOT" in
113  yes)
114    setAllAVDsForceColdBoot "yes" "no" $AVD_ROOT_DIR $TAG_ID
115    ;;
116  no)
117    setAllAVDsForceColdBoot "no" "yes" $AVD_ROOT_DIR $TAG_ID
118    ;;
119  *)
120    readme
121    ;;
122esac
123