Wednesday, December 16, 2009

Sunday, November 22, 2009

Droid Driver

Verizon Droid

Motorola's Droid wizard loads a Composite ADB device driver...

How to Install Apk file on device

Export the apk file from Eclipse...
use the debug key and password for now to sign your app...

***************************************************
# Connect the G1 device to the computer through the USB port. (If connecting for the first time, User is prompted to install the drivers in which case, point to $ANDROID_SDK_PATH/usb_driver, the drivers will get installed.)
# You might have to change your settings to permit apps that don’t come from the Google Market by going to the G1 home screen and choosing MENU > Settings > Applications > Unknown Sources.
# Turn USB debugging ON on your phone, On your G1 go to the home screen, press MENU > Settings > Applications > Development, then enable USB debugging.
# From Command line on your computer enter
‘adb install
(e.g. adb install C:/AndroidApps/NightStand.apk)
# Check the MENU from the home screen on the G1 for the installed applications icon.
*************************************
Install apk on G1

Tuesday, November 17, 2009

Default Sound Recorder Implementation

Default Sound Recorder Implementation

Content Providers: Android's Data Sharing Mechanisms

Android Content Provider

By Carl-Gustaf Harroch

Date: Aug 6, 2008

Return to the article
Content providers, which are part of the building blocks of the Android platform, share data between applications. In this article, Carl-Gustaf Harroch focuses on the data-sharing mechanism offered by Android (in reference to Android release v0.5).

Content providers, which are part of the building blocks of the Android platform, are the only way to share data between applications. This article will focus on the data-sharing mechanism offered by Android. I will explain the benefits and drawbacks of content providers and then demonstrate the use of content providers to handle a one-to-many relationship within a database. I will then take this example further to show some standard URIs to further narrow data via the interface.

This article presumes some previous experience with Android platform concepts such as intents and resolving of mime types. This article was written in reference to Android release v0.5.

NOTE

I recommend Google's notepad tutorial for interested beginners before continuing reading.
Permission and Application Space

As is the norm with Linux, when you install a new application, the process will run within its own user and group id. Through the Android sdk's packaged adb emulator, if you list the files (ls -l command) within the path '/data/data', different user ids will be shown for each application.

Contrary to the standard JVM, the Dalvik JVM uses one process per application. You can view your own application and its corresponding user id using the 'ps' command. Inbuilt Linux security will deny your application access to any data located within the folder of another application. There is no shared folder that can be used by several applications

NOTE

It is possible to use the same user between applications using android:sharedUserId within your AndroidManifest file.

In order to communicate data between processes you need to define an interface for data sharing. This interface is called ContentProvider. Any data you want to be shared needs to be exposed by a class that implements the ContentProvider interface.

After creating a class that implements the five abstract methods of the ContentProvider interface, you need to register it with the system. This is done within your application's AndroidManifest file.

The Android platform scans all manifest files for content providers that will then be registered. This system-wide data is then accessed via the Android ContentResolver interface.
Benefits of Content Providers

The above data-sharing mechanism presents the benefit of a platform-wide pattern for data sharing. If you want to access the data of an installed application, or some other system data, you have a single interface against which you can perform queries and manipulations.

This standard means that Android developers can approach inter-application data exchange with some predefined expectations because you cannot deviate from the methods provided by the ContentProvider interface.

Data is provided by the ContentProvider interface, but exposed by the ContentResolver. You could potentially access the implemented ContentProvider via Activity.getContentResolver().getProvider(). However, doing so would couple the requesting class to the implemented ContentProvider

Further to this standardized pattern, the ContentResolver provides several interesting functionalities. For instance, it will notify any content listener that the data has changed. For example, you could listen to any changes occurring within a user's contacts and act upon it.

The resolver can also enable data syncing—with a website or any other client/server applications. The syncing and notifying will be up to the content provider to implement. If you update a row into the database being exposed, you will need to notify the ContentResolver of that update.
And the Drawbacks

Although using content providers is a very good idea for standardizing data sharing, it involves some drawbacks. In their current state, content providers can only use SQLite as a storage mechanism. This may change in the future, though, because there are no hard-coded SQLite constraints.

public abstract Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)

Looking at the above query method, this would suit any SQL type of query. Or even other types of data storage such as RDF, XML, ODBMS, and so on. There are two object-oriented embedded databases known to work on the Android platform: db4o and McObjects's Perst.

Because these two databases are object-oriented databases, you cannot use them directly with the abstract methods. A proxy could be used between the content provider and the database interface. Or a temporary SQLite database could be a solution, but resources are crucial on a mobile environment and you might be better off using SQLite.

Furthermore, the arguments selection, selection arguments, and sort order are typical to relational databases. More complicated queries, with joins and distinct, are not possible.
So What to Choose?

The first question to ask is this: "Does my data need to be shareable?" Not all data needs to be shared and you might prefer ODBMS. If you are in doubt, I suggest sticking to the bundled SQLite.

Using content providers is very handy because you can define the activities working on your data via the AndroidManifest file. It enables a clear view, within a single file, of the action for each activity on a set of data. Querying and manipulating data gets trivial as well.

The Android platform also provides a set of helper methods and classes that use cursors; they can help the rapid development of your application and stay consistent with the rest of the system. Developing with the future in mind, content providers allow others to create applications that take the advantage of your application's data.
Applied Example: MyWalks

In order to illustrate the usage of content providers, we will use an application that simulates a walk. We define a walk by one database table that contains descriptive rows. The walk table will then have a one-to-many relationship with location points: latitude and longitude.

The application can be downloaded here as an extractable zip file that contains the .apk file. It also contains the source, which is importable into eclipse. Follow these instructions to install and configure the application (assuming that Android is set up correctly:

1. Navigate to the Android SDK's tools folder.
2. Open a shell and start the emulator by typing adb emulator
3. Open another shell in the same folder and type adb install MyWalks.apk
4. Start the My Walk application by navigating to it via the Android UI; this will also set up the databases when first started.

Alternatively you can import and run the project from eclipse.

The database consists of a table called walk that has name/description/created/modified as columns. Another table will be used for the location points. A point consists of a latitude, a longitude, and the foreign key walk_id. You can view this data easily using the handy application 'SQLite database browser'.

NOTE

SQLite does not enforce foreign key relationship. You will need to ensure the consistency of the database manually.
Accessing Data Using URIs

In order to access the data, we need URIs. (URIs follow RFC 2396 syntax.) The idea behind the data access is similar to REST services, with the difference of using intents instead of http methods. For instance, you would use android.intent.action.VIEW instead of http method GET, android.intent.action.INSERT instead of POST or PUT, and so forth.

For our MyWalks application, we will use the following URIs:

content://com.informit.mywalks.Walks - provide a list for all saved walks
content://com.informit.mywalks.Walks/1 - provide the data for a single walk
content://com.informit.mywalks.Walks/1/points - provide the list of all points for a walk
content://com.informit.mywalks.Walks/1/points/1 - provide the information for a single point of a walk (this will not be used in this application).

We begin by creating a class defining the database columns as follows (this is not mandatory but recommended for clarity):

public final class Walks {
public static final class MyWalks implements BaseColumns {
public static final URI CONTENT_URI = URI
.parse("content://com.informit.mywalks.Walks/walk");
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
public static final String CREATED_DATE = "created";
public static final String MODIFIED_DATE = "modified";
}
public static final class MyPoints implements BaseColumns {
public static final String _WALK_ID = "_walk_id";
public static final String LATITUDE = "latitude";
public static final String LONGITUDE = "longitude";
public static final String CREATED_DATE = "created";
}
}

This class is used by the system as authority for the provider and defined in the AndroidManifest file (this value must be unique, thus defining the fully-qualified class name as authority is recommended):



Second, we create the implemented ContentProvider. The class is called WalkProvider and it extends the ContentProvider interface. The following methods have to be overridden:

query(Uri, String[], String, String[], String) which returns data to the caller
insert(Uri, ContentValues) which inserts new data into the content provider
update(Uri, ContentValues, String, String[]) which updates existing data in the content provider
delete(Uri, String, String[]) which deletes data from the content provider
getType(Uri) which returns the MIME type of data in the content provider

Know Where to Look Using a UriMatcher

In our application, we created 2 tables and we have 4 different URIs which resolve to the same content provider. In order for the provider to know which database to query, we will use a helper class called UriMatcher. The UriMatcher is straightforward. You declare different URIs and map them to a final static integer in order to stay consistent across the class. For instance, we have the following values:

private static final int WALK = 1;
private static final int WALK_ID = 2;
private static final int WALK_POINTS = 3;
private static final int WALK_POINTS_ID = 4;

We then map the variables using the UriMatcher, as follows:

URI_MATCHER.addURI("com.informit.mywalks.Walks", "walk", WALK);
URI_MATCHER.addURI("com.informit.mywalks.Walks", "walk/#", WALK_ID);
URI_MATCHER.addURI("com.informit.mywalks.Walks", "walk/#/points", WALK_POINTS);
URI_MATCHER.addURI("com.informit.mywalks.Walks", "walk/#/points/#", WALK_POINTS_ID);

URI_MATCHER is our instantiated UriMatcher. We used the wildcard # to represent the id; you could also use the wildcard * to match against the name of a place.

All overridden methods will have a URI passed as argument. Thus you can use the UriMatcher to ensure that the correct logic is used. The following code snippet, from the query method, illustrates its usage:

SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

switch (URI_MATCHER.match(uri))
{
case WALK:
qb.setTables("walks");
qb.setProjectionMap(WALK_LIST_PROJECTION_MAP);
break;
case WALK_ID:
qb.setTables("walks");
qb.appendWhere("_id=" + uri.getPathSegments().get(1));
break;
case WALK_POINTS:
qb.setTables("points");
qb.appendWhere("_walk_id=" + uri.getPathSegments().get(1));
qb.setProjectionMap(POINT_LIST_PROJECTION_MAP);
break;
case WALK_POINTS_ID:
qb.setTables("points");
qb.appendWhere("_walk_id=" + uri.getPathSegments().get(1));
qb.appendWhere("_id=" + uri.getPathSegments().get(3));
break;
default:
throw new IllegalArgumentException("Unknown URL " + uri);
}

If you query the content provider with content://com.informit/walks/1/points, the third case will be used—thus set the table and its corresponding where clause for the walk_id. uri.getPathSegments().get(1) will give us "1" in the previous example. The URI and UriBuilder class contains several methods to help you create specific URIs.

The getType method is interesting mostly from a system-wide perspective. You would likely use the URI in your code whereas you would use types in the manifest. For instance, when we want to create a class that creates a new walk, we would create an activity and define it in the AndroidManifest, as follows:

android:name=".MyWalksEdit"
android:label="@string/app_name">







We could have declared an URI instead of the type here. However, it is logical to use a type because you can declare a type for a single walk (with an URI, it would be specific to a single id).
Generating Some Data

Now that we created the WalkProvider, we can add some data via our activities. Let looks at the MyWalksEdit.java file. As soon as the class is created, we create a new entry in the database:

mURI = getContentResolver().insert(intent.getData(), null);

As you can see, we use the ContentResolver given by the Activity interface. There is only one system-wide ContentResolver, which is responsible for mapping the correct content provider to the correct URI. The intent.getData() returns the URI that is being worked on. In this case, we are working on content://com.informit/walks.

We could have sent initial values using a ContentValues class instead of null. However, we will set the values later in our example. The insert function will return a new URI—in this case, something along the lines of content://com.informit/walks/3, depending where the new value is created in the database.

In order to hook into the newly created row, we will use a cursor that is very similar to a database cursor, as discussed previously. The cursor is returned while querying against the newly created row using the returned URI. We can then use Cursor's update methods to update columns of the URI and the commit function in order to save the new values into the database:

mCursor = managedQuery(mURI, null, null, null);
...
mCursor.updateString(1, name.getText().toString());
mCursor.updateString(2, description.getText().toString());
mCursor.commitUpdates();

NOTE

Wwhen you work on a cursor, you do not use the ContentProvider interface any more. For instance, if you call the commitUpdate on the cursor, it does not use the update functionality of the content provider. In order to use the update functionality of the ContentProvider interface, you should use the update method from the ContentResolver.
Adding Location Points

Now that we created the new walk, we use the following to add location points (defined by a latitude and a longitude):

getContentResolver().insert(mURI.buildUpon().appendPath("points").build(), values);

As you can see, we create a new URI based on the currently working base URI (for the walk with id 3). On top of that, we add the "point" path which would result in a URI similar to the following:

cdntent://com.informit.mywalks.Walks/walks/3/points

The insert function will return a new URI (not useful in our case as we sent the lat/long via the ContentValues values variable).
Narrowing the Data Returned by a Query

Following the RFC standard, we can use specific URIs to narrow down the data we are querying for. In our application, we might want to select a walk depending on its creation date. A URI of the following form, would be used (the from and to fields are in "zero epoch" value):

content://com.informit.mywalks.Walks/walks?from=1174497980534&to=1174497980536

To enable such a query, we will set the where clause in the query method of the instantiated content provider, as follows:

String from = uri.getQueryParameter("from");
tring to = uri.getQueryParameter("to");
if (from != null) {
qb.appendWhere("created>" + from);
}
if (to != null) {
// There seems to be a bug here. We need to manually ensure the AND
// is part of the where clause
qb.appendWhere(" AND created<" + to);
}

The code is straightforward, and you can extend it to any key/value pair. You can build up more complicated URIs in order to access the data in different ways.
Conclusion

We have discussed the usage of content providers. On the Android platform they are an indispensable way to enable data sharing between applications. If your application becomes the first big hit on Android, the data generated could be easily shared.

There are a lot of exciting possibilities being generated from the Android platform, so to best take full advantage of these possibilities now and in future, share your data via content providers!
Additional Resources

http://code.google.com/android/devel/data/contentproviders.html

http://code.google.com/android/reference/android/content/ContentProvider.html

http://www.mcobject.com/pressroom.php?step=3&article=93

http://groups.google.com/group/android-developers/browse_thread/thread/0d92afb667e5a4b7/ef3a6edatabase843c65dc?#ef3a6edatabase843c65dc

© 2009 Pearson Education, Inc. All rights reserved.

800 East 96th Street Indianapolis, Indiana 46240

Monday, November 9, 2009

a tutorial on how to use sdcard on android

Alternatively (especially for windows users) one can create the sdcard
image using the described way, loading the sdcard image into the
emulator and push some data onto it using adb.

1) mksdcard 2048M mysdcard
2) emulator -sdcard mysdcard
3) adb push local_file sdcard/remote_file

Files will remain on the image and will be ready to be used next time
you load mysdcard into the emulator.

BTW: Don't make the sdcard image too small. This will crash the
emulator :)
*****************************************************
Android Developer-mksdcard

Wednesday, September 30, 2009

Understanding User Interface Part 1


http://mobiforge.com/designing/story/understanding-user-interface-android-part-1-layouts

HelloWorld Tutorial:Baby Step


http://developer.android.com/guide/tutorials/hello-world.html

Android - How To Set Up an API Key for Google Maps

API Key for Google Maps

Passing Bundles Around Activities

Passing Bundles Around Activities

Handling User Interaction with Android App Widgets

A Visual Guide to Android GUI Widgets

Intent


An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:

•action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.

•data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.

Starting Activities and Getting Results



Use this to develop Add POC button
-- onClick will startActivityForResult(Intent, int)
---add phone number to contactlist
--should be able to edit prior entries by selecting them <- need to test this

Activity Class

User Interface

Building Custom Components

Monday, September 14, 2009