A ContentProvider is a high-level object like anActivity that needs to be declared to the system. So, the first step when making one is to add it to yourAndroidManifest.xmlfile before the <activity>tag (as a child of
<application>):
Download Eventsv3/AndroidManifest.xml
<provider android:name=".EventsProvider"
android:authorities="org.example.events" />
android:name is the class name (appended to the manifest’s package name), andandroid:authoritiesis the string used in the content URI.
Next we create the EventsProvider class, which must extend Content-Provider. Here’s the basic outline:
Download Eventsv3/src/org/example/events/EventsProvider.java
public class EventsProvider extends ContentProvider { private static final int EVENTS = 1;
private static final int EVENTS_ID = 2;
/** The MIME type of a directory of events */
private static final String CONTENT_TYPE
= "vnd.android.cursor.dir/vnd.example.event";
/** The MIME type of a single event */
private static final String CONTENT_ITEM_TYPE
= "vnd.android.cursor.item/vnd.example.event";
private EventsData events;
private UriMatcher uriMatcher;
// ...
}
By convention we usevnd.example instead of org.example in the MIME type.4 EventsProviderhandles two types of data:
• EVENTS(MIME type CONTENT_TYPE): A directory or list of events
• EVENTS_ID(MIME typeCONTENT_ITEM_TYPE): A single event
In terms of the URI, the difference is that the first type does not spec-ify an ID, but the second type does. We use Android’sUriMatcher class to parse the URI and tell us which one the client specified. And we reuse the EventsData class from earlier in the chapter to manage the real database inside the provider.
In the interest of space, I’m not going to show the rest of the class here, but you can download the whole thing from the book website. All three versions of the Events example can be found in the source code.zipfile.
The final version of the Events sample looks exactly like the previous version on the outside (see Figure 9.3, on page 192). On the inside, however, you now have the framework for an event store that can be used by other applications in the system, even ones written by other developers.
9.7 Fast-Forward >>
In this chapter, we learned how to store data in an Android SQL data-base. If you want to do more with SQL, you’ll need to learn about more statements and expressions than the ones we covered here. A book such as SQL Pocket Guide [Gen06] by Jonathan Gennick or The Defini-tive Guide to SQLite [Owe06] by Mike Owens would be a good invest-ment, but keep in mind that the SQL syntax and functions vary slightly from database to database.
4. Multipurpose Internet Mail Extensions (MIME) is an Internet standard for describing the type of any kind of content.
FAST-FORWARD>> 197
Another option for data storage on Android is db4o.5 This library is larger than SQLite and uses a different license (GNU Public License), but it’s free and may be easier for you to use, especially if you don’t know SQL.
TheSimpleCursorAdapterintroduced in this chapter can be customized to show more than just text. For example, you could display rating stars or sparklines or other views based on data in theCursor. Look forViewBinder in theSimpleCursorAdapterdocumentation for more information.6 And now for something completely different...the next chapter will cover 3D graphics with OpenGL.
5. http://www.db4o.com/android
6. http://d.android.com/reference/android/widget/SimpleCursorAdapter.html
3D Graphics in OpenGL
Two-dimensional graphics are great for most programs, but sometimes you need an extra level of depth, interactivity, or realism that isn’t possible in 2D. For these times, Android provides a three-dimensional graphics library based on the OpenGL ES standard. In this chapter, we’ll explore 3D concepts and build up a sample program that uses OpenGL.
10.1 Understanding 3D Graphics
The world is three-dimensional, yet we routinely view it in two dimen-sions. When you watch television or look at a picture in a book, the 3D images are flattened out, or projected, onto a 2D surface (the TV panel or book page).
Try this simple experiment: cover one eye and look out the window.
What do you see? Light from the sun bounces off objects outside, passes through the window, and travels to your eye so you can per-ceive it. In graphics terms, the scene outside is projected onto the win-dow (or viewport). If someone replaced your winwin-dow with a high-quality photograph, it would look the same until you moved.
Based on how close your eye is to the window and how big the window is, you can see a limited amount of the world outside. This is called your field of view. If you draw a line from your eye to the four corners of the window and beyond, you would get the pyramid in Figure 10.1, on the next page. This is called the view frustum (Latin for a “piece broken off”). For performance reasons, the frustum is usually bounded by near and far clipping planes as well. You can see everything inside the frustum but nothing outside of it.
INTRODUCING OPENGL 199
Figure 10.1: Viewing a three-dimensional scene
In 3D computer graphics, your computer screen acts as the viewport.
Your job is to fool the user into thinking it’s a window into another world just on the other side of the glass. The OpenGL graphics library is the API you use to accomplish that.