You can use a case statement to get the density. This worked for me:
bash
ppi=$(adb wait-for-device shell getprop ro.sf.lcd_density | tr -d '\r')
density="N/A"
case $ppi in
120)
density="ldpi" ;;
160)
density="mdpi" ;;
240)
density="hdpi" ;;
480)
density="xhdpi" ;;
320)
density="xxhdpi" ;;
640)
density="xxxhdpi" ;;
esac
echo $density
Updated code using -le (less than or equal to):
bash
get_ppi() {
adb wait-for-device shell getprop ro.sf.lcd_density | tr -d '\r'
}
get_density() {
_ppi=$1
if [ $_ppi -le 120 ]; then
echo "ldpi"
elif [ $_ppi -le 160 ]; then
echo "mdpi"
elif [ $_ppi -le 240 ]; then
echo "hdpi"
elif [ $_ppi -le 480 ]; then
echo "xhdpi"
elif [ $_ppi -le 320 ]; then
echo "xxhdpi"
elif [ $_ppi -le 640 ]; then
echo "xxxhdpi"
else
echo "N/A"
fi
}
ppi=$(get_ppi)
density=$(get_density $ppi)
echo $ppi
echo $density
Output on my device:
bash
480
xhdpi