teaching machines

Final Project – xiongchu

December 19, 2011 by . Filed under cs491 mobile, fall 2011, postmortems.

My application working properly but its not complete. I was able to create a SQLite database with a third party program and have my application load it into its data directory on the install. I was able to create a custom button using images and a custom view that displays stats of weapons. I was able to create a similar theme; by creating a subclass of TextView using a font similar to the game and using colors similar to the game. I was unable to utilize any animations like I mention in my final project outline, this wouldn’t have been too difficult but requires some research and testing.


DESIGN DOCUMENT:
For my final project/application I plan on developing an informational application for Modern Warfare 3. I plan on releasing the final product to the Android Market as will as the Amazon Market. I plan on using a SQLite database to store the data and will most likely migrate to a remote database on some hosting service. My goal for this application is to display the basic data from the game, create a theme similar to the game’s theme, create custom views, and utilize animations.

The idea of using a pre-created database came to me mid-way through my development; I felt typing all the queries into my database class would be inefficient and hard to manage in the future. But that was my original plan. And so I realized using a pre-created database made a-lot more sense, because changing a script and executing a script is easier than using String objects and SQLiteOpenHelper subclass also users will not be allowed to insert, update, or delete data from the database. And on updates, I just need to ship the application with the new updated database.

  

How to create a SQLite database with a third party program and use it in your application:

The program I used is SQLite Database Browser, the program allows you to create your database by executing a SQL script. This was how I create my database. After this, I copied the database into the raw data folder of my project. To actually use the database in my application, I have to place it in the data directory during the install/initial steps of the applications. The idea is simple, have the SQLiteOpenHelper subclass create an empty database and then overwrite the empty with the database in the assets folder (the one in the raw data folder).

I created some methods in my SQLiteOpenHelper: one to check that the empty database exist, one to copy the database from the raw folder to the empty database, and the last one to open the database/retrieve the database (this one is optional as you can call the static SQLiteDatabase.openDatabase() method anywhere, essentially this is what my third method does).

My check database method; a SQLiteDatabase object will return if it exists:

private boolean checkDataBase() {

	SQLiteDatabase checkDB = null;
	try {
		String myPath = DB_PATH + DB_NAME;
		checkDB = SQLiteDatabase.openDatabase(myPath, null,
				SQLiteDatabase.OPEN_READONLY);

	} catch (SQLiteException e) {

		// database does't exist yet.
	}
	if (checkDB != null) {
		checkDB.close();
	}
	return checkDB != null ? true : false;
}

If the empty database exist, then I want to copy it. I how did this was copy it byte by byte:

public void copyDatabase() {
	// Open your local db as the input stream
	InputStream myInput;
	try {
		myInput = context.getAssets().open(ASSET_DB_PATH + DB_NAME);
		// Path to the just created empty db
		String outFileName = DB_PATH + DB_NAME;

		// Open the empty db as the output stream
		OutputStream myOutput = new FileOutputStream(outFileName);

		// transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}
		// Close the streams
		myOutput.flush();
		myOutput.close();
		myInput.close();
	} catch (IOException e) {
		Log.d("XIONGCHU", "ERROR: " + e.toString());
	}
}

If your interested, I found the above idea here.

Creating a custom button using xml:

The way I did this was through using xml. The first xml you’ll need defines what image you want to display given the state of the button (press, unpressed, enable, disabled, etc). This xml is actually a drawable xml, to be stored in the drawable folders of the project. The xml element you’ll be using is a <selector> and the sub-elements are <item>. The attributes of the <item> specifics the state of the item, if there states are met by the button then the drawable attribute of the <item> will be drawn. The drawable attribute is the image you want to be drawn.

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_pressed="true"
        android:drawable="@drawable/btn_pressed" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/btn_neutral" />
</selector>

The second xml is the style.xml. Here you specific the unique style for your button. To draw the correct button given its state you need to specify the background attribute of your style, you want the drawable xml we’ve created above to be the value. And this file will be stored in your res/values folder.

<resources>
    <!-- http://blog.androgames.net/40/custom-button-style-and-theme/ -->
    <style
        name="customButtonStyle"
        parent="@android:style/Widget.Button">
        <item name="android:gravity">center_vertical|center_horizontal</item>
        <item name="android:textColor">#FFFFFFFF</item>
        <item name="android:textSize">16dip</item>
        <item name="android:background">@drawable/btn_custom</item>
        <item name="android:clickable">true</item>

    </style>
</resources>

The last place, for every button you want to use the your custom images you must specify the style attribute of the button to have the value be the name of the style we’ve declared above.

<xiongchu.modernwarfare3direct.CButton
        android:text="Equipments"
        android:id="@+id/btnMainContentActivityEquipments"
        ...
        style="@style/customButtonStyle"
        ...
        ></xiongchu.modernwarfare3direct.CButton>

If your interested, I found the above here.

How to use your own font/override default typeface:
The general idea is add your desired font in the project asset folder and then create a TypeFace object setting it as the TypeFace to be used by your view.
Code showing you how to create the TypeFace object using your font (stored in project assets) and setting it to be use by your view:

//My subclass
public class MyTextView extends TextView {
	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
                //Create the typeface object from the asset
		Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/fontfile.ttf");

                //Call super call method passing your typeface
                setTypeface(tf);
	}
}

As for future features, I do plan to more add pictures, add animations, and more data. But as of now, I feel I’ve made some good progress towards the release, the basic idea and mechanics are down.