Turn on Xperia automatically when the charger is connected - android

I'm trying to repurposed my Sony Xperia Z3 Compact into a lcd display project. In order to proceed, I would like to mod my Xperia phone so that the phone will automatically turn on when charger turn ON. I search around and found some script like https://android.stackexchange.com/questions/20021/automatically-power-on-android-when-the-charger-is-connected and some people mentioned that I should mod chargemon file. But when I replace this file, my phone cannot boot into the system.
#!/system/bin/sh
/system/bin/reboot
While checking the file, I notice that there 2 file chargemon and another is chargemon.stock. attached here chargemonfile . Actually this file have been edited during rooting process which I get it from xda forum https://forum.xda-developers.com/z3-compact/general/recovery-root-mm-575-lb-t3418714
#!/system/xbin/busybox sh
BUSYBOX=/system/xbin/busybox
VIB=/sys/class/timed_output/vibrator/enable
R_LED=/sys/class/leds/led:rgb_red/brightness
G_LED=/sys/class/leds/led:rgb_green/brightness
B_LED=/sys/class/leds/led:rgb_blue/brightness
LOG="/cache/mm_twrp_recovery.log"
WORKDIR="/cache/mm_twrp_recovery_keycheck"
MKDIR="${BUSYBOX} mkdir"
CHOWN="${BUSYBOX} chown"
CHMOD="${BUSYBOX} chmod"
TOUCH="${BUSYBOX} touch"
CAT="${BUSYBOX} cat"
SLEEP="${BUSYBOX} sleep"
KILL="${BUSYBOX} kill"
RM="${BUSYBOX} rm"
PS="${BUSYBOX} ps"
GREP="${BUSYBOX} grep"
AWK="${BUSYBOX} awk"
EXPR="${BUSYBOX} expr"
MOUNT="${BUSYBOX} mount"
LS="${BUSYBOX} ls"
HEXDUMP="${BUSYBOX} hexdump"
CP="${BUSYBOX} cp"
${RM} -f ${LOG}
echo "chargemon" >> ${LOG}
BOOTTWRP=0
if [ -e "/cache/recovery/boot" ]; then
echo " /cache/recovery/boot file found" >> ${LOG}
${RM} -f /cache/recovery/boot
echo 255 > ${R_LED}
echo 0 > ${G_LED}
echo 255 > ${B_LED}
BOOTTWRP=1
else
if [ ! -d "${WORKDIR}" ]; then
${MKDIR} ${WORKDIR}
${CHOWN} system.cache ${WORKDIR}
${CHMOD} 770 ${WORKDIR}
fi
if [ ! -e ${WORKDIR}/keycheck ]; then
${RM} ${WORKDIR}/keyevent*
${RM} ${WORKDIR}/keycheck_down
${RM} ${WORKDIR}/ps*
fi
echo 0 > ${R_LED}
echo 255 > ${G_LED}
echo 0 > ${B_LED}
echo 150 > ${VIB}
for EVENTDEV in $(${LS} /dev/input/event* ); do
SUFFIX="$(${EXPR} ${EVENTDEV} : '/dev/input/event\(.*\)')"
${CAT} ${EVENTDEV} > ${WORKDIR}/keyevent${SUFFIX} &
done
${SLEEP} 2
${PS} > ${WORKDIR}/ps.log
${CHMOD} 660 ${WORKDIR}/ps.log
for CATPROC in $(${PS} | ${GREP} /dev/input/event | ${GREP} -v grep | ${AWK} '{print $1}'); do
${KILL} -9 ${CATPROC}
done
${HEXDUMP} ${WORKDIR}/keyevent* | ${GREP} -e '^.* 0001 0072 .... ....$' > ${WORKDIR}/keycheck_down
if [ -s ${WORKDIR}/keycheck_down ]; then
echo " keycheck volume down - ok" >> ${LOG}
echo 255 > ${R_LED}
echo 0 > ${G_LED}
echo 255 > ${B_LED}
BOOTTWRP=1
fi
fi
if [ $BOOTTWRP -eq 1 ]; then
echo " remount rootfs rw" >> ${LOG}
mount -o remount,rw rootfs / 2>> ${LOG}
echo " copy busybox to /sbin" >> ${LOG}
${CP} /system/etc/mm_twrp_recovery/busybox /sbin
${CHOWN} 0.2000 /sbin/busybox
${CHMOD} 755 /sbin/busybox
BUSYBOX=/sbin/busybox
echo " copy boot_twrp_recovery.sh to /sbin" >> ${LOG}
${CP} /system/etc/mm_twrp_recovery/boot_twrp_recovery.sh /sbin
${CHOWN} 0.0 /sbin/boot_twrp_recovery.sh
${CHMOD} 755 /sbin/boot_twrp_recovery.sh
echo " copy recovery.twrp.cpio.lzma to /sbin" >> ${LOG}
${CP} /system/etc/mm_twrp_recovery/recovery.twrp.cpio.lzma /sbin
${CHOWN} 0.0 /sbin/recovery.twrp.cpio.lzma
${CHMOD} 644 /sbin/recovery.twrp.cpio.lzma
echo " unpack recovery.twrp.cpio.lzma" >> ${LOG}
${BUSYBOX} lzma -d /sbin/recovery.twrp.cpio.lzma
echo " exec boot_twrp_recovery.sh (twrp boot)" >> ${LOG}
exec /sbin/boot_twrp_recovery.sh
fi
echo 0 > ${B_LED}
echo 0 > ${R_LED}
echo 0 > ${G_LED}
echo " exec chargemon.stock (regular boot)" >> ${LOG}
exec /system/bin/chargemon.stock
exit 0
So im thinking, I shouldn't replace this file entirely but just add another line of code. Now, if anyone have experience regarding this maybe can share some input or help me modify the existing script.
My main goal is to get my xperia phone turn on when im plugin the charger.

Got it working by comment out this line exec /system/bin/chargemon.stock
Ref: https://z4-forum.com/forum/viewtopic.php?t=56746&start=90

Related

Can not run android app with valgrind

I create project Hello JNI and write state to test leak such as:
int* requieData = malloc(sizeof(int)*100);
This is my script build_valgrind.sh
I can run ./configure but does not any valgrind log can show.
My build_valgrind.sh:
#!/usr/bin/env bash
#set -x
function extract()
{
if [ -f "$1" ] ; then
case "$1" in
*.tar.bz2) tar xvjf "$1" ;;
*.tar.gz) tar xvzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xvf "$1" ;;
*.tbz2) tar xvjf "$1" ;;
*.tgz) tar xvzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.7z) 7z x "$1" ;;
*) echo "$1 cannot be extracted via >extract<" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
RUN_HELLO_JNI_THROUGH_VALGRIND=true
VALGRIND_VERSION="3.13.0"
VALGRIND_EXTENSION=".tar.bz2"
VALGRIND_DIRECTORY="valgrind-${VALGRIND_VERSION}"
VALGRIND_TARBALL="valgrind-${VALGRIND_VERSION}${VALGRIND_EXTENSION}"
# Only download Valgrind tarball again if not already downloaded
if [[ ! -f "${VALGRIND_TARBALL}" ]]; then
wget -v -nc "ftp://sourceware.org/pub/valgrind/${VALGRIND_TARBALL}"
fi
# Only extract Valgrind tarball again if not already extracted
if [[ ! -d "$VALGRIND_DIRECTORY" ]]; then
extract "$VALGRIND_TARBALL"
fi
echo "HOME: $HOME"
export ANDROID_NDK_HOME="$HOME/Android/Sdk/ndk-bundle"
# Ensure ANDROID_NDK_HOME is set
if [[ ! -z "$ANDROID_NDK_HOME" ]]; then
export ANDROID_NDK_HOME="$HOME/Android/Sdk/ndk-bundle"
fi
echo "NDK: $ANDROID_NDK_HOME"
# Ensure ANDOID_SDK_HOME is set
export ANDROID_SDK_HOME="$HOME/Android/Sdk/"
if [[ ! -z "$ANDROID_SDK_HOME" ]]; then
export ANDROID_SDK_HOME="$HOME/Android/Sdk/"
fi
echo "SDK: $ANDROID_SDK_HOME"
if [[ ! -d "$VALGRIND_DIRECTORY" ]];
then
echo "Problem with extracting Valgrind from $VALGRIND_TARBALL into $VALGRIND_DIRECTORY!!!"
exit -1
fi
echo "VALGRIND: $VALGRIND_DIRECTORY"
# Move to extracted directory
cd "$VALGRIND_DIRECTORY"
# ARM Toolchain
ARCH_ABI="arm-linux-androideabi-4.9"
export AR="$ANDROID_NDK_HOME/toolchains/${ARCH_ABI}/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ar"
export LD="$ANDROID_NDK_HOME/toolchains/${ARCH_ABI}/prebuilt/linux-x86_64/bin/arm-linux-androideabi-ld"
export CC="$ANDROID_NDK_HOME/toolchains/${ARCH_ABI}/prebuilt/linux-x86_64/bin/arm-linux-androideabi-gcc"
export CXX="$ANDROID_NDK_HOME/toolchains/${ARCH_ABI}/prebuilt/linux-x86_64/bin/arm-linux-androideabi-g++"
[[ ! -d "$ANDROID_NDK_HOME" || ! -f "$AR" || ! -f "$LD" || ! -f "$CC" || ! -f "$CXX" ]] && echo "Make sure AR, LD, CC, CXX variables are defined correctly. Ensure ANDROID_NDK_HOME is defined also" && exit -1
# Configure build
export HWKIND="lenovo"
ANDROID_PLATFORM=android-18
export CPPFLAGS="--sysroot=$ANDROID_NDK_HOME/platforms/${ANDROID_PLATFORM}/arch-arm -DANDROID_HARDWARE_$HWKIND"
export CFLAGS="--sysroot=$ANDROID_NDK_HOME/platforms/${ANDROID_PLATFORM}/arch-arm"
# BUG: For some reason file command is unable to detect if the file does not exist with ! -f , it says it doesn't exist even when it does!!!
echo "VALGRIND_DIRECTORY: $VALGRIND_DIRECTORY"
# ModeBuildTest
BUILD=true
if [[ "$BUILD" = true ]];
then
./configure --prefix="/data/local/Inst" \
--host="armv7-unknown-linux" \
--target="armv7-unknown-linux" \
--with-tmpdir="/sdcard"
[[ $? -ne 0 ]] && echo "Can't configure!" && exit -1
# Determine the number of jobs (commands) to be run simultaneously by GNU Make
NO_CPU_CORES=$(grep -c ^processor /proc/cpuinfo)
if [ $NO_CPU_CORES -le 8 ]; then
JOBS=$(($NO_CPU_CORES+1))
else
JOBS=${NO_CPU_CORES}
fi
# Compile Valgrind
make -j "${JOBS}"
[[ $? -ne 0 ]] && echo "Can't compile!" && exit -1
# Install Valgrind locally
make -j "${JOBS}" install DESTDIR="$(pwd)/Inst"
[[ $? -ne 0 ]] && echo "Can't install!" && exit -1
fi
# Push local Valgrind installtion to the phone
if [[ $(adb shell ls -ld /data/local/Inst/bin/valgrind) = *"No such file or directory"* ]];
then
adb root
adb remount
adb shell "[ ! -d /data/local/Inst ] && mkdir /data/local/Inst"
adb push Inst /
adb shell "ls -l /data/local/Inst"
# Ensure Valgrind on the phone is running
adb shell "/data/local/Inst/bin/valgrind --version"
# Add Valgrind executable to PATH (this might fail)
adb shell "export PATH=$PATH:/data/local/Inst/bin/"
echo "Push local Valgrind installtion"
fi
echo "Buil Valgrind end"
if [ $RUN_HELLO_JNI_THROUGH_VALGRIND = true ];
then
PACKAGE="com.example.hellojni"
# The location of the Hello JNI sample application
HELLO_JNI_PATH="$HOME/Documents/Project/HelloJNI"
# Workaround INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES error
adb uninstall "$PACKAGE"
cd $HELLO_JNI_PATH
./gradlew assembleArmDebug
./gradlew installArmDebug
# Build Hello JNI project in debug mode and install it on the device
echo "Finish Installation"
# Start HelloJNI app
#adb shell am start -a android.intent.action.MAIN -n $PACKAGE/.HelloJni
cd $HOME/Documents/Application/valgrindTool
# Make the script executable
chmod a+x bootstrap_valgrind.sh
# Run application through Valgrind on the phone
/usr/bin/env bash bootstrap_valgrind.sh
adb shell ls -lR "/sdcard/*grind*"
adb shell ls -lR "/storage/sdcard0/*grind*"
adb shell ls -lR "/storage/sdcard1/*grind*"
fi
echo "Call helloWord"
exit 0
And this is bootstrap_valgrind.sh
#!/usr/bin/env bash
PACKAGE="com.example.hellojni"
adb push start_valgrind.sh /data/local/
adb shell chmod 777 /data/local/start_valgrind.sh
adb root
adb shell setprop wrap.$PACKAGE "logwrapper /data/local/start_valgrind.sh"
echo "wrap.$PACKAGE: $(adb shell getprop wrap.$PACKAGE)"
adb shell am force-stop $PACKAGE
adb shell am start -a android.intent.action.MAIN -n $PACKAGE/.HelloJni
adb logcat -c
adb logcat
exit 0
This is start_valgrind.sh
# Define the package name
PACKAGE="com.example.hellojni"
# Memcheck tool: a memory error detector
VGPARAMS='-v --error-limit=no --log-file=/sdcard/hellojni.log.%p --leak-check=yes --leak-resolution=high --show-reachable=yes --undef-value-errors --read-var-info=yes'
# Massif tool: a heap profiler
# VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.log.%p --tool=massif --massif-out-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.massif.out.%p'
# Callgrind tool: a cache and branch-prediction profiler
# VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.log.%p --tool=callgrind --callgrind-out-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.callgrind.out.%p'
# Helgrind tool: a thread error detector
# VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.log.%p --tool=helgrind --helgrind-out-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.helgrind.out.%p'
# DHAT: a dynamic heap analysis tool
# VGPARAMS='-v --error-limit=no --trace-children=yes --log-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.log.%p --tool=exp-dhat --exp-dhat-out-file=/sdcard/enzo_lync_profiles/enzo_lync_profile.dhat.out.%p'
export TMPDIR=/data/local/Ints/
exec /data/local/Inst/bin/valgrind $VGPARAMS $*
My device armeabi-v7(show by android studio) but when I use this cript, device show:
Hello from JNI leak valgrind ! Compiled with ABI armeabi.
My question: Why output show armeabi? I think it is armeabi-v7.
And I can not see anylog about memory leak. What is wrong in the script?
Thank you so much.

editing android build.prop with a script

I'm trying to create a script to modify the build.prop file 'automatically'.
I want the script to check for entries I define and:
1. If they do not exist add them
2. If they do exist, check the value, and if it does not match what I've define modify it.
I have created a script.
It runs but outputs nothing, not changing the build.prop
I've attached the script. Is it correct?
#!/system/bin/sh
# Definitions
file=/system/build.prop
tmpf=/system.buildprop.bak
line_list="wifi.supplicant_scan_interval=120 ro.sf.lcd_density=480"
# Function to get args as needed for loop
getargs() {
par=$1
line=`echo $par |cut -d"=" -f1`
arg=`echo $par |cut -d"=" -f2`
}
# Loop to make all changes in line_list
for x in $line_lst; do
# Get all needed arguments
getargs $x
# Write this change to a tmp file to check on it
oldarg=`grep $line $file |cut -d"=" -f2`
sed "s/$line=.*/$line=${arg}/g" $file > $tmpf
# Check if the change was made
chknewarg=`grep $line $tmpf |cut -d"=" -f2`
if [ "$chknewarg" = "$arg" ]; then
cp $tmpf $file
if [ -f $tmpf ]; then
rm $tmpf
fi
echo "File edited"
else
if [ -f $tmpf ]; then
rm $tmpf
fi
echo "Expected $arg, got $chknewarg instead"
exit 1
fi
# If it doesn't exist at all append it to the file
chkexists=`grep -c $line $file`
if [ $chkexists -eq 0 ]; then
echo "$x" >> $file
fi
done
exit 0
I've done a bit more playing and fundamentally changes the script.
#/system/bin/sh
mount -o remount,rw /system
FILE=/system/build.prop
TMPFILE=$FILE.tmp
line1=wifi.supplicant_scan_interval
line2=ro.sf.lcd_density
line3=ro.media.enc.jpeg.quality
line1Arg=120
line2Arg=390
line3Arg=100
lineNum=
prop=$line1
arg=$line1Arg
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE
else
echo "$prop does not exist in build.prop"
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi
prop=$line2
arg=$line2Arg
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE
else
echo "$prop does not exist in build.prop"
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi
prop=$line3
arg=$line3Arg
if grep -Fq $prop $FILE ; then
lineNum=`sed -n "/${prop}/=" $FILE`
echo $lineNum
sed -i "${lineNum} c${prop}=${arg}" $FILE
else
echo "$prop does not exist in build.prop"
echo "appending to end of build.prop"
echo $prop=$arg >> $FILE
fi
mount -o remount,r /system
However I get a 'mount: invalid argument' error?
Also, how would a loop over the line and lineArg variables to compete the actions?

use file list as menu options for theme chooser

Trying to write an android/linux script to list files in specific directory as menu options then perform set of commands using that file as a variable in a script...so far I've got this:
#!/system/bin/sh
l_count=0
cd /sdcard/themes
ls *.zip|while read l_file
do
l_count=`expr $l_count + 1`
echo "${l_count}. ${l_file}"
done
echo
echo "Pick a theme:"
read $choice
but from there I can't figure out how to assign the number assigned from l_count to the l_file
Figured it out...answer was to have the script write out a second script while still inside the do loop. Here's the final script...
#!/system/bin/sh
l_count=0
cd /sdcard/themes
echo "#!/system/bin/sh" > /sdcard/themes/themer_tmp.sh
echo "echo 'Pick a theme:'" >> /sdcard/themes/themer_tmp.sh
echo "read choice" >> /sdcard/themes/themer_tmp.sh
echo case '$choice' in >> /sdcard/themes/themer_tmp.sh
ls *.zip|while read l_file
do
l_count=`expr $l_count + 1`
echo "${l_count}. ${l_file}"
echo "${l_count})" >> /sdcard/themes/themer_tmp.sh
echo echo '"boot-recovery" > /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo echo '"--update_package=/sdcard/themes/'${l_file}'"' '>> /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo echo '"--wipe_cache" >> /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo echo '"reboot"' '>> /cache/recovery/command' >> /sdcard/themes/themer_tmp.sh
echo "reboot recovery" >> /sdcard/themes/themer_tmp.sh
echo ";;" >> /sdcard/themes/themer_tmp.sh
done
echo "esac" >> /sdcard/themes/themer_tmp.sh
sh /sdcard/themes/themer_tmp.sh
Hope this helps anybody else seeking a similar solution

Shell script search & replace in text file

I have to edit text file in android shell.
so I type this shell script.
but my Galaxy Nexus does not have Sed, Awk neither.
shell#android:/ $ sed -e "s/old_pattern/new_pattern/g" file_name > modify_file
/system/bin/sh: sed: not found
it doesn't worked.
how can i modify old_pattern to new_pattern in text file.
is it possible in Shell Script?
Edited Shell Script
#!/system/bin/sh
ARGS=4
BAD=65
if [ $# -ne "$ARGS" ]
then
echo "Usage: `basename $0` TARGET_FILE,OLD_PATTERN,NEW_PATTERN,MODIFY_FILE"
exit $BAD
fi
old_pattern=$2
new_pattern=$3
modify_file=$4
if [ -f "$1" ]
then
target_file=$1
else
echo "\"$3\" Does not exist."
exit $BAD
fi
exit 0
Solution :
shell#android:/ $ while read STRING
>do
>echo "${STRING//old_pattern/new_pattern}" >> modify_file_name
>done < target_file_name
shell#android:/ $
It is possible with pure bash, but it will be very slooow on big files.
while read STRING
do
echo "${STRING//old_pattern/new_pattern}" >> modify_file
done < file_name
ps. Oh, I just mentioned that your shell isn't bash. Seems like it is just sh. That won't work with sh.

adb push/pull with progress bar

It is really annoying if you adb push/pull large files to the device that there's no way to now how far along it is. Is it possible to run adb push or adb pull and get a progress bar using the 'bar' utility?
The main issue here is I think that adb expects two file names, if the input file could be replaced by stdin you could pipe through the 'bar' utility and get a progress bar. So far I haven't succeeded in doing so, but I'm not really a shell guru which is why I'm asking here :)
Note that I'm on Linux using bash.
It looks like the latest adb has progress support.
Android Debug Bridge version 1.0.32
device commands:
adb push [-p] <local> <remote>
- copy file/dir to device
('-p' to display the transfer progress)
However, the answers above also work for 'adb install' which do not have a progress option. I modified the first answer's script to work this way:
Create "adb-install.sh" somewhere in your PATH and run "adb-install.sh " instead of "adb install -f "
#!/bin/bash
# adb install with progressbar displayed
# usage: <adb-install.sh> <file.apk>
# original code from: http://stackoverflow.com/questions/6595374/adb-push-pull-with-progress-bar
function usage()
{
echo "$0 <apk to install>"
exit 1
}
function progressbar()
{
bar="================================================================================"
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 1 ]] && usage
SRC=$1
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
export ADB_TRACE=all
adb install -r $SRC 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
export ADB_TRACE=
echo
echo 'press any key'
read n
Currently I have this little piece of bash:
function adb_push {
# NOTE: 65544 is the max size adb seems to transfer in one go
TOTALSIZE=$(ls -Rl "$1" | awk '{ sum += sprintf("%.0f\n", ($5 / 65544)+0.5) } END { print sum }')
exp=$(($TOTALSIZE * 7)) # 7 bytes for every line we print - not really accurate if there's a lot of small files :(
# start bar in the background
ADB_TRACE=adb adb push "$1" "$2" 2>&1 | unbuffer -p awk '/DATA/ { split($3,a,"="); print a[2] }' | unbuffer -p cut -d":" -s -f1 | unbuffer -p bar -of /dev/null -s $exp
echo # Add a newline after the progressbar.
}
It works somewhat, it shows a progress bar going from 0 to 100 which is nice. However, it won't be correct if you do a lot of small files, and worse, the bytes/s and total bytes shown by 'bar' aren't correct.
I challenge you to improve on my script; it shouldn't be hard! ;)
Here is my solution, it will show a simple progressbar and current numeric progress
[==================================================] 100%
Usage
./progress_adb.sh source destination
progress_adb.sh
#!/bin/bash
function usage()
{
echo "$0 source destination"
exit 1
}
function progressbar()
{
bar="=================================================="
barlength=${#bar}
n=$(($1*barlength/100))
printf "\r[%-${barlength}s] %d%%" "${bar:0:n}" "$1"
# echo -ne "\b$1"
}
export -f progressbar
[[ $# < 2 ]] && usage
SRC=$1
DST=$2
[ ! -f $SRC ] && { \
echo "source file not found"; \
exit 2; \
}
which adb >/dev/null 2>&1 || { \
echo "adb doesn't exist in your path"; \
exit 3; \
}
SIZE=$(ls -l $SRC | awk '{print $5}')
ADB_TRACE=adb adb push $SRC $DST 2>&1 \
| sed -n '/DATA/p' \
| awk -v T=$SIZE 'BEGIN{FS="[=:]"}{t+=$7;system("progressbar " sprintf("%d\n", t/T*100))}'
echo
Testing on Ubuntu 14.04
$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
TODO
directory support
progressbar size change when screen size change
Well I can give you an Idea:
ADB_TRACE=adb adb push <source> <destination>
returns logs for any command, so for example the copy command, which looks like:
writex: fd=3 len=65544: 4441544100000100000000021efd DATA....#....b..
here you can get the total bytes length before, with ls -a, then parse the output of adb with grep or awk, increment an interneral counter and send the current progress to the bar utility.
When you succeeded, please post the script here.

Categories

Resources