Skip to content

Instantly share code, notes, and snippets.

@yarons
Created August 24, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yarons/cfba55f81cb1f50a29d859a8affb822b to your computer and use it in GitHub Desktop.
Save yarons/cfba55f81cb1f50a29d859a8affb822b to your computer and use it in GitHub Desktop.
Genmon XFCE panel to show phone battery level via adb
ACTION=="add|remove", SUBSYSTEM=="usb", ATTRS{idVendor}=="XXXX", ATTRS{idProduct}=="XXXX", RUN+="/usr/bin/su YOUR_USER_NAME /path/to/refresh-genmon-udev.sh"
# replace YOUR_USER_NAME with your actual user name (hardcoded, this is ran by root, it's possible to determin your user name
# by finding out who is the owner of xfce4-panel
# You'll also have to replace XXXX with the output from lsusb
# for example:
# Bus 001 Device 094: ID 046d:c05a Logitech, Inc. M90/M100 Optical Mouse
# After the term ID this is the code we're after, 046d is the idVendor code while c05a is the idProduct code
# Better have that hardcoded as well
# This file should be in /etc/udev/rules.d (at least on RedHat based and Arch based) and requires root permissions to edit.
#!/bin/sh
$username=`whoami`
pids=`/usr/bin/pgrep -u $username xfce4-panel`
sleep 1
for pid in $pids; do
DBUS_SESSION_BUS_ADDRESS=`/usr/bin/awk -v 'RS=\0' -F= '/DBUS/ {print $2"="$3}' /proc/$pid/environ`
DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS \
/usr/bin/xfce4-panel --plugin-event=genmon-10:refresh:bool:true --display=:0
# genmon number can be determined by hovering the name of the genmon instance in XFCE Panel Preferences > Items (10 is in my case)
done
# This script should be executable, run chmod +x on it.
#!/bin/bash
# First add a genmon item to the XFCE panel.
# This script is checking for connectivity, on no connectivity a red N/A message will appear.
# If there is connectivity the script will show the battery precentage in a color varying from
# red (empty) to green (full) and a bar next to it (can be more granular but that's highly
# unnecessary, yet possible).
# The output is formated to also display a tooltip showing some more data like phone type.
# A click can also be implemented but I didn't find any use for it.
adb shell echo 1
if [ $? -eq 1 ]
then
echo '<txt><span fgcolor="#FF4E42">N/A</span></txt>'
echo '<tool>Phone is not connected</tool>'
exit 1
fi
export BATT_STATS=`adb shell dumpsys battery | awk '/level/ {printf $2}'`
export PHONE_TYPE=`adb shell getprop | awk '/ro.product.manufacturer/ {x=$2} /ro.product.model/ {y=$2} END{z=x" "y;gsub(/\[|\]/,"",z);print z}'`
colors=(FF4E42 E95F41 D37140 BD833F A7943E 91A63D 7BB83C 65C93B 4FDB3A 39ED39 24FF38)
echo "<txt><span weight='Bold' fgcolor='#`echo ${colors[$(expr $BATT_STATS / 10)]}`'>$BATT_STATS</span></txt>"
echo "<tool>Battery level of $PHONE_TYPE: "$BATT_STATS"</tool>"
echo "<bar>$BATT_STATS</bar>"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment