Followers

Saturday 4 September 2010

File Shredder


Flattr this


Today I'll tell you about "FileShredder", a data wiping tool coded by be.

My motivation for developing this one, was just to learn how to make an application from start to finish and publish it. I did all of the development on the emulator. I had dabbled at Android for a bit, but none of the work was aimed at creating something which I would try to publish.

I tried to think of something so simple I could code in an evening or to, because I tend to have somewhat short attention span. :)
I was actually code complete in one 6 hour session, testing and debugging another 6 hours and publishing took perhaps two hours to figure out.

I'll explain the parts of the program.

Browser



This is the file system browser. It is very rudimentary and ugly, but it serves its purpose just fine. You can browse the whoe file system and select a file. That's it.
Basically, when you start it, it scans the root directory:



private void ScanDir(File f) {
if(!f.getAbsolutePath().equals("/"))
a.add(f.getParentFile().getAbsolutePath());
if(f.isDirectory()) {
File[] files = f.listFiles(new AllFileFilter());
if(files == null) return;
for(int i=0;i<files.length;i++) {
a.add(files[i].getAbsolutePath());
}
}
}

"a" is an ArrayAdapter which server as the adapter for the listview.


Info view

When you click on a file instead of a directory in the previous view, it will launch this activity. It shows information about the file and determines if it can be shred. The application does not not have root permissions to the file system, so there's no way to accidentally the whole system.

If you click Shred, the application will show up a progress dialog and start a background thread to do the actual work. If you mis-click, you pretty much fucked. The file is corrupted pretty much instantaneously.

The worker thread gets a handle to the selected file, determines its size and overwrites it with pseudo-random data. It will try to determine good sized buffer sizes to process the file as fast as possible.

Progress dialog


Now, here I stumbled on some problems. I never did quite read the Android Activity/Intent lifetime documentation properly. Well, I tried but I never understood why they bothered to explain all the boring details so well with pretty diagrams and all :)

I'll explain the causes and fixes in a later blog post, but basically you need to carefully maintain you applications state and response to events such as change in orientation. Many times you do not need to bother, because the default Android behaviour is fine. But with background threads, message handlers and dialogs you might get, ahem, unexpected behaviour :)

Also, I'll promise to tell more about inter thread communication via message handling later. Showing that progress bar is a bit more involved than you might think.

Finally

After the file is shredder, the Activitys buttons are modified acordingly and you may now close the activity. You could also just use the Back button of your device, but I added the button for clarity. I actually might remove it in a later version, because it does not sit well with the Android UI style.

TODO:
Obviously, the file browser could be a lot neater.
It would be nice to be able to shred multiple files at once.
It would be nice to process entire directory trees at once.
However, in my first version I just wanted something I could develop in a day, so I designed accordingly.

QR



Here's a QR tag which you can read with an Android barcode reader. I'm not sure if it works. It would be great if you could report in the comments whether it works for you!

Also, polishing your package for Android Market is a topic of its own. I'll explain the export, key generation and icons later.

Testing code highlighting


/*
* This file is part of Libgdx by Mario Zechner (badlogicgames@gmail.com)
*
* Libgdx is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Libgdx is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libgdx. If not, see .
*/
package com.badlogic.gdx.math;

/**
* Taken from libgdx
* Modified to remove final declaration
*
* Encapsulates a 2D vector. Allows chaining methods by returning a
* reference to itself
* @author badlogicgames@gmail.com
*
*/
public class Vector2
{
/** static temporary vector **/
private final static Vector2 tmp = new Vector2();

/** the x-component of this vector **/
public float x;
/** the y-component of this vector **/
public float y;

/**
* Constructs a new vector at (0,0)
*/
public Vector2( )
{

}

/**
* Constructs a vector with the given components
* @param x The x-component
* @param y The y-component
*/
public Vector2( float x, float y )
{
this.x = x;
this.y = y;
}

/**
* Constructs a vector from the given vector
* @param v The vector
*/
public Vector2( Vector2 v )
{
set( v );
}

/**
* @return a copy of this vector
*/
public Vector2 cpy( )
{
return new Vector2( this );
}

/**
* @return The euclidian length
*/
public float len( )
{
return (float)Math.sqrt( x * x + y * y );
}

/**
* @return The squared euclidian length
*/
public float len2( )
{
return x * x + y * y;
}

/**
* Sets this vector from the given vector
* @param v The vector
* @return This vector for chaining
*/
public Vector2 set( Vector2 v )
{
x = v.x;
y = v.y;
return this;
}

/**
* Sets the components of this vector
* @param x The x-component
* @param y The y-component
* @return This vector for chaining
*/
public Vector2 set( float x, float y )
{
this.x = x;
this.y = y;
return this;
}

/**
* Substracts the given vector from this vector.
* @param v The vector
* @return This vector for chaining
*/
public Vector2 sub( Vector2 v )
{
x -= v.x;
y -= v.y;
return this;
}

/**
* Normalizes this vector
* @return This vector for chaining
*/
public Vector2 nor( )
{
float len = len( );
if( len != 0 )
{
x /= len;
y /= len;
}
return this;
}

/**
* Adds the given vector to this vector
* @param v The vector
* @return This vector for chaining
*/
public Vector2 add( Vector2 v )
{
x += v.x;
y += v.y;
return this;
}

/**
* Adds the given components to this vector
* @param x The x-component
* @param y The y-component
* @return This vector for chaining
*/
public Vector2 add( float x, float y )
{
this.x += x;
this.y += y;
return this;
}

/**
* @param v The other vector
* @return The dot product between this and the other vector
*/
public float dot( Vector2 v )
{
return x * v.x + y * v.y;
}

/**
* Multiplies this vector by a scalar
* @param scalar The scalar
* @return This vector for chaining
*/
public Vector2 mul( float scalar )
{
x *= scalar;
y *= scalar;
return this;
}

/**
* @param v The other vector
* @return the distance between this and the other vector
*/
public float dst(Vector2 v)
{
float x_d = v.x - x;
float y_d = v.y - y;
return (float)Math.sqrt( x_d * x_d + y_d * y_d );
}

/**
* @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return the distance between this and the other vector
*/
public float dst( float x, float y )
{
float x_d = x - this.x;
float y_d = y - this.y;
return (float)Math.sqrt( x_d * x_d + y_d * y_d );
}

/**
* @param v The other vector
* @return the squared distance between this and the other vector
*/
public float dst2(Vector2 v)
{
float x_d = v.x - x;
float y_d = v.y - y;
return x_d * x_d + y_d * y_d;
}

public String toString( )
{
return "[" + x + ":" + y + "]";
}

/**
* Substracts the other vector from this vector.
* @param x The x-component of the other vector
* @param y The y-component of the other vector
* @return This vector for chaining
*/
public Vector2 sub(float x, float y)
{
this.x -= x;
this.y -= y;
return this;
}

/**
* @return a temporary copy of this vector. Use with care as this is backed by a single static Vector2 instance. v1.tmp().add( v2.tmp() ) will not work!
*/
public Vector2 tmp( )
{
return tmp.set(this);
}
}

Wednesday 1 September 2010

Testpoast


Well, finally got an android phone to dev on. Testing posting from it now.

It is a Lg gt540.

First obvious grievance is that it only comes with amdrood 1.6. There is some indication that it will be eventually updated to froyo or eclair.