Advanced Android
CSCI 4448/5448: Object-Oriented Analysis & Design Lecture 19 10/30/2012
Kenneth M. Anderson, 2012
Goals of the Lecture
! Present more examples of the Android Framework ! Passing Information between Activities ! Reading and Writing Files ! 2D Graphics and Touch Events ! Application Preferences ! Working with a Database
Kenneth M. Anderson, 2012
Passing Information
! In our examples so far ! weve seen one activity launch another activity ! but each activity has been independent of the other ! Were going to look at two additional concepts ! Fragments: reusable bits of UI and behavior that live inside activities ! Passing Information: how do we pass information between activities ! Well also take a look at how an activity can store data into a le that persists between sessions of using the application
Kenneth M. Anderson, 2012
Prole Viewer
! Prole viewer will ! Use one activity/fragment to display a list of user names ! This activity can also delete existing users ! Use a second activity/fragment to add new users and edit existing users ! Our program will use Java serialization to persist user names and proles ! The data structure will be a Map<String, ProleData> ! Well discuss ProleData in a moment ! But rst, fragments!
Kenneth M. Anderson, 2012
Activities and Fragments (I)
! Activities can now contain multiple fragments ! Fragments are reusable units of UI with their own life cycle ! this life cycle is however synched with the life cycle of its activity ! onCreate(), onPause(), onResume(), etc. ! Fragments provide exibility when presenting the UI of an application on either a phone or a tablet ! Youre initial activity can detect how much screen real estate is available and then either choose to display fragments in a set of activities (for phones with small displays) or to embed multiple fragments inside of a single activity (for tablets with larger displays) ! Migrating to fragments from activities is straightforward
Kenneth M. Anderson, 2012 5
Activities and Fragments (II)
! The scenario I outlined on the previous slide is shown graphically here:
Image credit: <[Link]
Kenneth M. Anderson, 2012 6
Java Serialization (I)
! Java serialization is a technology that can both ! persist a set of objects, and ! later retrieve that set such that all objects are recreated and all connections between them are reestablished ! [Link] provides two classes to help with this ! ObjectOutputStream and ObjectInputStream ! You use the former to save and the latter to load
Kenneth M. Anderson, 2012
Java Serialization (II)
! Most Java types, including collections, can be serialized ! User-dened types can also be serialized ! You need to implement [Link] ! And, you need to implement two methods ! readObject(ObjectInputStream stream) ! writeObject(ObjectOutputStream stream)
Kenneth M. Anderson, 2012
Java Serialization (III)
! In writeObject(), you place code that writes each internal attribute of your object on to the output stream ! In readObject(), you place code that reads each attribute o" of the input stream in the same order they were written by writeObject ! Then, when it comes time for your class to be persisted, Javas serialization framework will call readObject() and writeObject() as needed passing the appropriate IO stream
Kenneth M. Anderson, 2012
ProleData (I)
! For our Prole Viewer application, our ProleData class stores a users rst name, last name, and e-mail address ! ProleData is implemented as a data holder with getter and setter methods for each attribute ! It implements [Link] as needed ! It also contains a serialVersionUID attributegenerated by Eclipsethat is used to add support for versioning. ! If we ever change the ProleData class, well need to update the UID. ! Advanced implementations would then use the UID to determine which version a le used and load it using the correct code
Kenneth M. Anderson, 2012
10
Prole Data (II)
! Our writeObject Method looks like this private void writeObject(ObjectOutputStream stream) throws IOException { [Link](firstName); [Link](lastName); [Link](email); } ! writeObject() is dened multiple times for multiple types
Kenneth M. Anderson, 2012
11
Prole Data (III)
! Our readObject Method looks like this private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException { firstName = (String)[Link](); lastName email } ! If we try to read a String and the le contains something else, then a ClassNotFoundException will be thrown by ObjectInputStream = (String)[Link](); = (String)[Link]();
Kenneth M. Anderson, 2012
12
Java Serialization (IV)
! Having congured ProleData in this way, then the code to write a Map<String, ProleData> data structure is: ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(f)); [Link](profiles);
! Two lines of code! (Ignoring exception handlers)
Kenneth M. Anderson, 2012
13
Java Serialization (V)
! The code to read a Map<String, ProleData> is:
ObjectInputStream input = new ObjectInputStream(new FileInputStream(f)); profiles = (TreeMap<String,ProfileData>) [Link]();
! Just two more lines of code!
Kenneth M. Anderson, 2012
14
Java Serialization (VI)
! Hiding in those two lines of code was a reference to a variable named f; Heres the relevant part: ! new FileInputStream(f) or new FileOutputStream(f) ! As an aside: [Link] is based on the Decorator pattern ! In both cases, we were passing an instance of [Link] to the IO streams to specify where our persistent data is stored ! So, now we need to look at how we deal with les in Android
Kenneth M. Anderson, 2012
15
Dealing With Files (I)
! Each Android application has a directory on the le system ! You can verify this by launching an emulator and then invoking the adb -e shell command ! adb is stored in $ANDROID/tools (2.x) or $ANDROID/platform_tools (3.x and 4.x) ! This command provides you with a command prompt to your device; recall that Android runs on linux ! cd to data/data to see a list of application directories ! If you encounter permission problems, run the command su and see if that helps ! If so, be careful, youre now running as root!
Kenneth M. Anderson, 2012 16
Dealing With Files (II)
! For Prole Viewer, cd into the [Link] directory (youll need to compile and install Prole Viewer onto your device rst!) ! That directory contains two subdirectories ! les and lib ! Whenever you ask for access to your applications directory and create a le, it will be stored in the les subdirectory ! Application directories are private; other apps cant access them
Kenneth M. Anderson, 2012
17
Dealing With Files (III)
! Android provides several useful methods for accessing your applications private directory ! getFilesDir() - returns a [Link] that points at the directory ! leList() - returns list of le names in apps directory ! openFileInput() - returns FileInputStream for reading ! openFileOutput() - returns FileOutputStream for writing ! deleteFile() - deletes a le that is no longer needed
Kenneth M. Anderson, 2012
18
Prole Viewers Use of Files
! In Prole Viewer, all we need to use is getFilesDir() ! We use that to create a [Link] object that points at a le called [Link] in our apps directory ! We then pass that le to our save/load methods ! That code looks like this ! [Link](new File(getFilesDir(), "[Link]"));
Kenneth M. Anderson, 2012
19
Back to Passing Information
! When we select a user and click Edit, we switch from the initial activity to an edit prole activity ! We want that second activity to display the prole of the selected user ! How do we pass that information? ! In Android, that information gets passed via the Intent that is used to launch the second activity
Kenneth M. Anderson, 2012
20
Passing Information (II)
! Each intent has a map associated with it that can store arbitrary Java objects ! The Map is updated via putExtra(key, value) ! The Map is accessed via get*Extra(key) where * can be one of several type names ! In Prole Viewer, we use getStringExtra(key) because the user name we store is a string ! An activity can get access to the intent that launched it via a call to getIntent() which is a method inherited from Activity
Kenneth M. Anderson, 2012
21
Passing Information (III)
! So, to pass information we do this in our fragment Intent intent = new Intent(this, [Link]); [Link]("name", username); getActivity().startActivity(intent); ! To retrieve it, we do this in the Edit Prole fragment
username = getActivity().getIntent().getStringExtra("name");
! Simple!
Kenneth M. Anderson, 2012
22
Other Highlights
! Prole Viewer also shows ! how to use fragments and how they interact with activities ! how to add menu items to the ActionBar ! how to enable/disable menu items based on list selections ! how to save/load data in onResume() and onPause() to ensure that data is synced between activities
Demo
Kenneth M. Anderson, 2012
23
2D Graphics and Touch Events
! The Simple Paint program takes a look at how to do simple 2D graphics in Android ! and how to handle touch events ! Whenever you want to do your own drawing, you need access to a canvas ! If you create a subclass of View and then override the onDraw(Canvas) method, you gain access to a canvas ! Essentially, a view IS-A canvas
Kenneth M. Anderson, 2012
24
Key Concepts (I)
! We draw on a canvas ! In order to draw a shape, we rst need a Paint object; it species a wide range of attributes that inuences drawing ! We then invoke one of canvass draw methods, passing in the shape info and our paint object ! In our program, we create one Paint object called background which we use to paint the canvas white ! and a second Paint object used to paint Rectangles
Kenneth M. Anderson, 2012
25
Key Concepts (II)
! Draw on Demand ! As with most frameworks, drawing in Android is done on demand when the framework determines that an update is needed ! say if our view gets exposed because a window on top of it moves ! or when our own code calls invalidate() ! onDraw is then called and we draw the current state of the view as determined by our programs data structures ! onDraw() is where all drawing occurs; it does NOT occur (for instance) when we are handling a touch event ! This is an important concept, the event handler for touch events simply updates our data structures and returns; drawing happens later
Kenneth M. Anderson, 2012 26
OnDraw (I)
! Our SimplePaint program allows rectangles to be drawn in four di"erent colors ! We have a data structure that keeps track of the rectangles that have been created and the Paint object used to draw each one ! If we are in the middle of handling a touch event, a rectangle called motionRect exists and we will draw it as well ! Our onDraw method is shown on the next slide
Kenneth M. Anderson, 2012
27
OnDraw (II)
protected void onDraw(Canvas canvas) { } } } if (motionRect != null && [Link] > 0 && [Link] > 0) { [Link](motionRect, current); [Link](0, 0, getWidth(), getHeight(), background); for (Rectangle r : rects) { [Link](r.r, [Link]);
Kenneth M. Anderson, 2012
28
Handling Touch Events (I)
! To handle a touch event on our custom view ! we override the onTouchEvent() method ! we then process the MotionEvent instance that we are passed ! and then return true to ensure that we get all of the events related to the touch event ! There are three stages: ! DOWN (the start), MOVE (updates), UP (the end)
Kenneth M. Anderson, 2012
29
Handling Touch Events (II)
! An ACTION_DOWN event means that the user has just touched the screen ! In our program, we create motionRect and set its top, left corner ! An ACTION_MOVE event means the user is moving their nger across the screen ! we update the bottom, right corner and invalidate ! An ACTION_UP event means the user has lifted their nger from the screen ! We update motionRect with the last x, y coordinate, add motionRect to our data structures and then set motionRect to null
Kenneth M. Anderson, 2012
30
Handling Touch Events (III)
! Finally, to actually receive touch events, we need to do three things ! In the constructor of our View subclass, we need to call ! setFocusable(true); ! setFocusableInTouchMode(true); ! In the constructor of our activity, we get a handle to our View subclass and call requestFocus(); ! That ensures that Android sends events to the view
Kenneth M. Anderson, 2012
31
Other Highlights
! Simple Paint also demonstrates the use of ! a radio group to keep track of the current paint color ! Androids preference mechanism to let the current paint color persist between runs of the application ! You call getSharedPreferences to gain access to a map that contains your apps preferences ! You can read and write preference values in a straightforward manner
Demo
Kenneth M. Anderson, 2012 32
Androids support for SQLite
! Android makes it straightforward to interact with SQLite databases ! SQLite is a public domain SQL library that stores a database as a text le and provides standard CRUD operations on that text le ! as if you were actually talking to a database server ! Android provides a class to make creating/opening a database a snap, a class that allows standard select, insert, update and delete statements to be executed and a Cursor class for processing result sets
Kenneth M. Anderson, 2012
33
SQL Example
! For this example, I recreated Prole Viewer and ! dropped our custom Proles / ProleData classes that made use of Java serialization ! and incorporated the use of an SQLite database ! As you will see, all of the original functionality could be recreated and the resulting program is just a tad simpler ! IF you are comfortable with database programming and SQL; if not, it will seem confusing! ! Note: this version of the program does not use Fragments ! To keep things simple, this program only uses activities to handle the UI
Kenneth M. Anderson, 2012
34
SQLiteOpenHelper
! To create a database, you make a subclass of SQLiteOpenHelper ! It takes care of creating and opening a SQLite database for you at run-time ! All you need to do is to supply the CREATE TABLE statement needed to create the table youll be using ! I created a table whose columns correspond to Prole Viewers prole name, rst name, last name, and e-mail address attributes
Kenneth M. Anderson, 2012
35
Accessing the Database
! In your activity, creating an instance of your OpenHelper subclass, automatically creates (if needed) your database and opens it ! In your onStop() method, you need to remember to close the database ! You then can acquire the database for reading or writing as needed with calls to getReadableDatabase() or getWriteableDatabase()
Kenneth M. Anderson, 2012
36
CRUD Support
! In databases, you can create, read, update or delete rows in a table ! In Androids database object these correspond to ! insert, query, update, delete ! These are methods, you supply snippets of SQL to these methods; they create the full SQL statement in the background and then execute it against the database
Kenneth M. Anderson, 2012
37
Selected Snippets (I)
! Getting a list of prole names from the database ! SQLiteDatabase db = [Link](); ! Cursor cursor = ! [Link]("proles", new String[] { "prole" }, null, null, null, null, "prole"); ! while ([Link]()) { ! [Link]([Link](0)); ! } ! [Link]();
Kenneth M. Anderson, 2012
38
Selected Snippets (II)
! Deleting a prole from the database ! ! #SQLiteDatabase db = [Link](); #[Link]("proles", "prole = ?", new String[] { name });
! The prole = ? is part of an SQL WHERE clause; ! the ? mark is a placeholder ! It gets replaced by the value of the variable name which is passed in via a String array: new String[] { name } is a string array literal in Java
Kenneth M. Anderson, 2012
39
Selected Snippets (III)
! Inserting a new prole into the database ! ! ! ! ! ! ! # # # # # # # SQLiteDatabase db = [Link](); ContentValues values = new ContentValues(); [Link]("prole", name); [Link]("rst", rst); [Link]("last", last); [Link]("email", email); [Link]("proles", null, values);
Kenneth M. Anderson, 2012
40
Wrapping Up
! Learned more about the Android framework ! Passing Information between Activities ! Reading and Writing Files ! 2D Graphics and Touch Events ! Application Preferences ! Working with a Database ! This ends our woefully incomplete review of the Android Framework; however, our three lectures should be enough to get you started!
Kenneth M. Anderson, 2012
41
Coming Up Next
! Lecture 20: Advanced iOS ! Homework 4 Due Next Week
Kenneth M. Anderson, 2012
42