Admin. Mobile Software Development Framework: Android Activity, View/ViewGroup, External Resources Homework 2 questions 10/9/2012 Y. Richard Yang 1 2 Recap: TinyOS Hardware components motivated design Each component/module specifies the interfaces it provides the interfaces/modules it uses implements the functions in the declared provided interfaces event handlers in the declared used interfaces Configuration specifies the linkage among components/ modules Recap: J2ME Framework Java adaptation for mobile devices A major focus of J2ME is device heterogeneity Solution: versioning Configurations Profiles Execution model driven (triggered) handlers, who may post tasks to a FIFO task queue monitored by a task thread 3 4 Recap: J2ME MIDP Structure Recap: IOS Lifecycle callbacks - destroy d=.get(this) d.setcurrent(disp) MIDP Current able A set of commands disp.addcommand() le s adaptation for mobile devices E.g., Cocoa => Cocoa Touch KIT => UI Kit Command listener disp.setcommandlistener() 5 http://developer.apple.com/library/ios/documentation/miscellaneous/conceptual/ iphoneostechoverview/iphoneostechoverview.pdf 6 1
Recap: IOS View Controller (MVC) Structure Discussion No-fica-on and KVO will Controller Data at did count Target Outlet Delegate Ac-on What are some major differences between desktop GUI app design and mobile (phone/ tablet) GUI app design? Data source View View 7 8 Some Key Features of Mobile UI APp Limited screen real estate one thing at a time Mobile GUI Screen real-estate is limited => Focus on one thing at a time Limited resources: more dynamic system management on app life cycle give app chances to adapt, better mem management More heterogeneous display devices decoupling between display and logic 9 10 Mobile GUI Mobile GUI Workflow Screen real-estate is limited => Focus on one thing at a time lifecycle 11 2
Mobile GUI Workflow: Do One Thing Mobile GUI Workflow: Content Based on Underlining Data lifecycle lifecycle Mobile GUI Workflow: Handle s Mobile GUI Workflow: Switch to Another GUI lifecycle lifecycle Discussion Typical Design: Framework reacts to app events and invokes app lifecycle event handlers Key design points for mobile GUI app Specify app life cycle customization Specify display Specify event scheduling How to link event, display, handler, data lifecycle 17 3
How to Provide LifeCycle s? Typical Design: UI System captures UI events; and puts them in a msg queue of the app. A UI thread processes the queue class implements it Inheritance lifecycle class does not implement it Delegate Command listener 19 Example: IOS How to Provide Component s? Component class implements it Inheritance Typically a bad idea Component class does not implement it Makes reusable Delegate Command listener 10/ 4/1 2 22 Outline Admin and recap Mobile/wireless development framework GNURadio TinyOS J2ME IOS Android http://developer.android.com/index.html Android A mobile OS based on Linux Customized Linux kernel 2.6 and 3.x (Android 4.0 onwards) E.g., default no X Windows, not full set of GNU libs ly OS concepts for mobile contexts e.g., each app is considered one Linux user New key components, e.g., Binder for IPC Power management wakelock lication development framework based on Java Dalvik Virtual Machine 23 24 4
Android Architecture Android Tools Android SDK Manager (android) Android emulator Android debug bridge (adb) can connect to an Android device and start a shell on the device See http://developer.android.com/tools/workflow/index.html 25 Mapping to Android Allows external XML resource files to specify views lication Framework (Android): Key Concepts lifecycle -How to specify the customized callbacks: extend Activity class Activity View/ViewGroup (Layout) External resources -How to link the callbacks defined in view to listener/controller: View.set Listener() 28 Activity Activity: Manifest File A single, focused thing that the user can do. Creating a window to place UI views Full-screen windows, floating windows, embedded inside of another activity Typically organized as a Stack Top Activity is visible Other activities are stopped Back button to traverse the Activity Stack Long Home shows the content of the Stack To facility launching and managing Activities, each activity is announced in a manifest file Manifest the activity Instead of a hardcode string in code, defines in res/strings 5
Android Project Resources Activity: Example // MainActivity.java public class MainActivity extends Activity { public void oncreate(bundle savedinstancestate) { // savedinstancestate holds any data that may have been saved // for the activity before it got killed by the system (e.g. // to save memory) the last time super.oncreate(savedinstancestate); setcontentview( ); // set a View 31 View A view component is a building block for user interface components. Two types of views Leaf: TextView, EditText, Button, Form, TimePicker ListView (ViewGroup): LinearLayout, Relativelayout, Programmatic Usage of Views // MainActivity.java public class MainActivity extends Activity { public void oncreate(bundle savedinstancestate) { // savedinstancestate holds any data that may have been saved // for the activity before it got killed by the system (e.g. // to save memory) the last time super.oncreate(savedinstancestate); http://developer.android.com/guide/tutorials/views/index.htm TextView tv new TextView(this); tv.settext("hello! ); setcontentview(tv); Define View by XML Access View Defined by XML public void oncreate(bundle icicle) { super.oncreate(icicle); setcontentview(r.layout.main); TextView mytextview = (TextView)findViewById(R.id.myTextView); main.xml <?xml version= 1.0 encoding= utf-8?> <LinearLayout xmlns:android= http:// schemas.android.com/apk/res/android android:orientation= vertical android:layout_width= fill_parent android:layout_height= fill_parent > <TextView android:id= @+id/mytextview android:layout_width= fill_parent android:layout_height= wrap_content android:text= Hello World, HelloWorld /> </LinearLayout> 6
External Resources Compiled to numbers and included in R.java file Hello Example See HelloStart 37 38 Android Activity Life Cycle Lifecycle Example See ActivityifeCycle 39 40 Linking Views and s/controllers Example: TipCalc onkeydown. onkeyup ontrackball ontouch registerbutton.setonclicklistener(new View.OnClickListener() { public void onclick(view arg0) {. myedittext.setonkeylistener(new OnKeyListener() { public boolean onkey(view v, int keycode, Key event) { if (event.getaction() == Key.ACTION_DOWN) if (keycode == Key.KEYCODE_DPAD_CENTER) { return true; return false; ); Set listener: public class TipCalcActivity extends Activity implements OnClickListener { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_tip_calc); Button calc = (Button)findViewById(R.id.calculate); calc.setonclicklistener(this); 42 7
Example: TipCalc : public void onclick(view arg0) { EditText amounttext = (EditText)findViewById(R.id.amount_value); // get input double amt=double.parsedouble(amounttext.gettext().tostring()); // compute output double tipd = amount * 0.15; // set UI String tipt = String.format("%.2f", tipd); TextView tiptext = (TextView)findViewById(R.id.tip_value); tiptext.settext(tipt); 43 8