Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [35]

By Root 2487 0
Figure 6-5). We are using the DELETE FROM statement. Note that deleting a row does not modify the IDs of the other items. The ID of the deleted row is no longer usable:

function deleteItem(myCountry:String):void {

var statement:SQLStatement = new SQLStatement();

statement.sqlConnection = connection;

var deleteMessage:String = "DELETE FROM geography where country = :co";

statement.text = deleteMessage;

statement.parameters[":co"] = myCountry;

try {

statement.execute();

trace("all removed");

} catch(error:Error) {

trace("item", error.message);

}

}

Figure 6-5. The geography table with row 0 deleted

As you have seen, you can do a lot while working within a structure you create.

Embedding a Database


Sometimes you may want to install your application with a database that is already prepopulated. In such a case, you should include the db file when you package your application. In Flash Professional, select File→AIR Android Settings→General→Included files, click the plus sign (+), and browse to the db file. In Flash Builder, place the db file in the src directory.

To make changes to the database, first you need to move it from applicationDirectory to applicationStorageDirectory. As discussed previously, applicationDirectory is a read-only directory.

Now check that the database is embedded:

var embedded:File = File.applicationDirectory.resolvePath("embeddedDB.db");

if (embedded.exists) {

trace("database was embedded");

// move on to the next step

} else {

trace("database was not embedded");

}

Next, check that it has not yet been copied. If it does not exist yet, copy the database over:

var local:File =

File.applicationStorageDirectory.resolvePath("myDatabase.db");

if (!local.exists) {

embedded.copyTo(local);

}

Try to keep the embedded database small. If it is substantial in size, consider copying it asynchronously using the copyToAsync method.

Once the database is copied to the editable directory, you can use it as you would any other database. Note that the original database cannot be deleted. Any attempt to change the application package would make it invalid.

Using Encrypted Local Storage


AIR has an encryption functionality which you can use to store sensitive data. The data is encrypted using AES-CBC 128-bit. Although this capability is not available for Android at the time of this writing, it is worth discussing in case it is added in the future.

The EncryptedLocalStorage static class belongs to the flash.data package and inherits from Object. The data is application-specific and can only be retrieved from within its security sandbox. It is saved as a ByteArray and requires a key.

To encrypt the data, use:

import flash.utils.ByteArray;

import flash.data.EncryptedLocalStore;

function write():void {

var myData:ByteArray = new ByteArray();

myData.writeUTFBytes("my very sensitive data");

EncryptedLocalStore.setItem("myKey", myData);

}

To decrypt the data, use:

function read():void {

var myData:ByteArray = EncryptedLocalStore.getItem("myKey");

if (myData != null) {

trace(myData.readUTFBytes(myData.bytesAvailable);

}

}

To remove the data, use:

function delete():void {

EncryptedLocalStore.removeItem("myKey");

}

Conclusion

We covered a lot of material in this chapter. Being able to save data expands your application’s functionality and allows you to customize it to your users’ needs. It is also an important feature on mobile devices where local data access is so much quicker—and cheaper, especially if your audience uses 3G—than remote data.

Chapter 7. Multitouch Technology

Touch has a memory.

—John Keats

Put a young child in front of a computer and see her reach for the screen to grab moving pixels. Touching an element to impact it is the most natural form of input.

Over the past few years, there has been an explosion of innovation utilizing the latest research in haptic technology, including everything from touch-based computers such as the iPhone and Microsoft Surface to tangible user interfaces such as the Reactable to gesture-based motion tracking

Return Main Page Previous Page Next Page

®Online Book Reader