Monday, 11 August 2014

Smart Charger: automatically backup your photos

[This is the second post about Arietta: the first is here]

The project
The idea behind this little project is to have a system that charge my phone and automatically backup the photo of my android phone while it is connected.

First of all, you need to attach an usb female adapter to Arietta and connect an usb (powered) hub to it: Follow this page

In order to get the photos on the android phone from Arietta you need to use MTP protocol and there are 2 ways:
a - install mtp-tools packages from repository
b - activate mtp filesystem

For my project the first solution is easier:  launch apt-get install mtp-tools

The solution b was better, because it allows you to mount the phone filesystem and copy files from it with normal linux commands like cp,rsync,mv.. but it's harder because you need to rebuild the arietta image.

External Usb Storage
The Arietta will copy my photos on an external storage instead of the internal sdcard of arietta because this helps to improve the life of the sdcard.

To automatically mount an external storage in a predefined directory:
- use blkid command to obtain an id that identify you external usb storage for the system
- change /etc/fstab and insert a line to specify where the storage mount point

My external usb storage is a simple pendrive formatted with fat32 with UUID FC4D-37AA.
At the end of /etc/fstab I've added this line to mount it on /mnt everytime is plugged in:

UUID=FC4D-37AA /mnt vfat defaults,umask=007,gid=46 0 0

The Backup Script ...
I've created a script called that use some commands from mtp-tools package to copy and delete jpg photos from the phone:


#check if external storage is present or abort
checkmount=`mount | grep /mnt | wc -l`
echo $checkmount;
if [ $checkmount -ne "1" ]; then
        echo "Usb External Storage not mounted...exiting";

echo "Usb External Storage Found";
mtp-files | grep .jpg | awk '{ print $2 }' > lista.txt
while read F ; do
    #there is enough space on device
    spacecheck=`df -h | grep /mnt | awk {'print $5'} | tr -d '%'`
    if [ $spacecheck -gt "95" ]; then
        echo "Not enough space left on device..exiting";
    mtp-connect --getfile $F /mnt/$F # copy the file
    mtp-connect --delete $F          # delete the file from the phone


- it checks if Usb External Storage is mounted or it aborts the operation
- it creates a list of jpg files (supposing these are all photos) lista.txt

... and for every photo:
- it checks if there is enough space left on the Usb External Storage
- it reads the list of photos from lista.txt and it uses mtp-connect to copy file to the /mnt/ directory and it deletes the photo from the phone

..and the UDEV Rule!
In order to call everytime you plug the phone to the hub you need a udev rule.

Create a script 98-my.rules with this content:
SUBSYSTEM=="usb", ATTR{idVendor}=="22b8", MODE="0666", RUN+="/root/"

and copy it on /etc/udev/rules.d

Now everytime you attach the android phone to the hub, while the phone is charging, arietta copy your photos in the external pendrive, if present.

In the next article we will see how to add a led to monitor the status of the operation.

Friday, 8 August 2014

How to hack Clash Of Clans: Unlimited Shield [UPDATE]

*updates on the bottom
I've played for over a year with Clash of Clans: a very nice mobile games.
Day by day, the game become more and more difficult to play without spending money.
It's the classical in-app purchase free game mechanism: pay to survive.

The hack
During the game you have a limited shield, that can save your village from attacks for others player for a max of 16 hours.
Of course you can extends the shield with money.
When you have no shield, the others people can't attack you if you are online and connected.
My first attempt was to put the display of the phone in a "never sleep" mode and leave the app open but this was not a working solution, clash of clans see that you aren't do nothing and it disconnects.

Later I was thinking about an hardware solution with arduino, that could simulate at least the swipes on the touch screen, something like this hack for another game: but there was an easier solution.

UiTesting with UiAutomator
One of the android's tools is called uiautomator it is used to test UI on apps, so it is possible to write test/programs that simulate click, swipe and other actions on apps running on the phone.
What I have done, it has been write a simple uiautomator test that:

1 - it presses home
2 - it searches clash of clans in the list of apps
3 - it launches clash of clans
4 - it waits 15 seconds for the app to start
5 - it does swipes movement
6 - it waits some seconds
7 - it repeats from 5

I tested the app on my phone (no root required) and my village was opened for over 10 hours(see update #) without any disconnection and of course, none attack! :)

Update #1: 18 Aug 2014
Yesterday I posted on reddit this post, to understand the reaction of some players and what they are thinking about it.
On reddit I've discovered that after 6 hours of game, Clash Of Clans automatically disconnect the client for 5-10 minutes... but, my hack (I'm lucky) keep on try to reconnect during this period and at the end of this period it is correctly reconnected.

I have also discovered there are some hacks out there that works with rooted phone and works in similar way.