1 Mobile Software Development Framework: IOS 10/2/2012 Y. Richard Yang
2 Admin. r Homework 2 questions
3 Recap: TinyOS r Hardware components motivated design m 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 m Configuration specifies the linkage among components/modules r Event driven (triggered) handlers r A single task queue
4 Recap: J2ME r Java adaptation for mobile devices r A major software concept is versioning m Configurations m Profiles r For mobile phone devices, the key profile is MIDP
5 Recap: MIDP Key Concepts MIDP Lifecycle callbacks -startApp -pauseApp -destroyApp Current Displayable A set of commands Command listener d=Display.getDisplay(this) d.setCurrent(disp) disp.addCommand() disp.setCommandListener()
6 HelloWorldMIDlet.java import javax.microedition.midlet.*; import javax.microedition.lcdui.*; public class HelloWorldMIDlet extends MIDlet implements CommandListener { private Command exitCommand; private Display display; private TextBox t; public HelloWorldMIDlet() { display = Display.getDisplay(this); exitCommand = new Command("Exit", Command.EXIT, 2); t = new TextBox(“CS434", "Hello World!", 256, 0); t.addCommand(exitCommand); t.setCommandListener(this); } public void startApp() { display.setCurrent(t); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } public void commandAction(Command c, Displayable s) { if (c == exitCommand) { destroyApp(false); notifyDestroyed(); }
7 Extend MIDP GUI Key Concepts to General Setting App App lifecycle callbacks/custom -start -pause -… View Group View Group View View Group View Event listener/co ntroller View Event listener/co ntroller
8 System Support App App lifecycle callbacks/custom -start -pause -… View Group View Group View View Group View Event listener/co ntroller View Event listener/co ntroller System schedules apps and notifies app on life cycle events
9 System Support App App lifecycle callbacks/custom -start -pause -… View Group View Group View View Group View Event listener/co ntroller View Event listener/co ntroller System schedules apps and notifies app on life cycle events Each view defines events it can handle, and system propagates events among view components
10 GUI Design/Implementation Points App App lifecycle callbacks/custom -start -pause -… View Group View Group View View Group View Event listener/co ntroller View Event listener/co ntroller -How to specify the customized callbacks?
11 GUI Design/Implementation Points App App lifecycle callbacks/custom -start -pause -… View Group View Group View View Group View Event listener/co ntroller View Event listener/co ntroller -How to specify the customized callbacks? -How to specify the view structure for multiple display capabilities?
12 GUI Design/Implementation Points App App lifecycle callbacks/custom -start -pause -… View Group View Group View View Group View Event listener/co ntroller View Event listener/co ntroller -How to specify the customized callbacks? -How to specify the view structure for multiple display capabilities? -How to link the callbacks defined in view to listener/controller?
13 Outline r Admin and recap r Mobile/wireless development framework m GNURadio m TinyOS m J2ME m Android
Android r A mobile OS, application framework, and a set of applications m OS Customized Linux kernel 2.6 and 3.x (Android 4.0 onwards) –E.g., default no X Windows, not full set of GNU libs m Application development framework Based on Java (J2SE not J2ME) Dalvik Virtual Machine 14
Android Architecture 15
Seeing Android OS r See w/index.html w/index.html r Android SDK Manager (android) to start a simulator r Android debug bridge (adb) can connect to an Android device and start a shell on the device
17 Mapping to Android Activity App lifecycle callbacks/custom -start -pause -… View Group View Group View View Group View Event listener/co ntroller View Event listener/co ntroller -How to specify the customized callbacks: extend Activity class Allows external XML resource files to specify views -How to link the callbacks defined in view to listener/controller: View.set…Listener()
Application Framework (Android): Key Concepts r Activity r View/ViewGroup (Layout) r External resources 18
Activity r A single, focused thing that the user can do. r Creating a window to place UI views m Full-screen windows, floating windows, embedded inside of another activity r Typically organized as a Stack m Top Activity is visible m Other activities are stopped m Back button to traverse the Activity Stack m Long Home shows the content of the Stack
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(R.layout.main); // set a View }
Activity: Manifest File r 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 resource
Android Project Resources 22
r Android Activity Life cycle 23
Lifecycle Example r See ActivityifeCycle 24
View r A view component is a building block for user interface components. r Two types of views m Leaf: TextView, EditText, Button, Form, TimePicker… ListView m Composite (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); TextView tv new TextView(this); tv.setText("Hello!“); setContentView(tv); }
Define View by XML
Access View Defined by public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); } … TextView myTextView = (TextView)findViewById(R.id.myTextView); <LinearLayout xmlns:android=” /apk/res/android” android:orientation=”vertical” android:layout_width=”fill_parent” android:layout_height=”fill_parent”> <TextView android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:text=”Hello World, HelloWorld” /> main.xml
External Resources r Compiled to numbers and included in R.java file 29
Linking Views and Handlers/Controllers r onKeyDown. onKeyUp r onTrackBallEvent r onTouchEvent myEditText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction() == KeyEvent.ACTION_DOWN) if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { … return true; } return false; }}); } registerButton.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) {….}}
Example: TipCalc 31
Event handler and ANR 32 r ANRs (Application not responding) happen when m Main thread (“event”/UI) does not respond to input in 5 sec r 5-10 sec is absolute upper bound
ANR r Numbers (Nexus One) m ~5-25 ms – uncached flash reading a byte m ~5-200+(!) ms – uncached flash writing tiny amount m ms – human perception of slow action m 108/350/500/800 ms – ping over 3G. varies! m ~1-6+ seconds – TCP setup + HTTP fetch of 6k over 3G r Rules m Notify users m Use background processing Example: LaunchThread 33
Background Processing using a Thread r Problem: m Background thread and UI thread are running concurrently and may have race conditions if they modify simultaneously r Solution: Android Handler m Use Handler to send and process Message and Runnable objects associated with a thread's MessageQueue.MessageMessageQueue 34
Android Handler r Each Handler instance is associated with a single thread and that thread's message queue. r A handler is bound to the thread / message queue of the thread that is creating it m from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue. 35
Using Handler r There are two main uses for a Handler: m to schedule messages and runnables to be executed as some point in the future; and m to enqueue an action to be performed on a different thread than your own. 36
Handler public class MyActivity extends Activity { [... ] // Need handler for callbacks to the UI thread final Handler mHandler = new Handler(); // Create runnable for posting final Runnable mUpdateResults = new Runnable() { public void run() { updateResultsInUi(); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); [... ] } 37
Handler protected void startLongRunningOperation() { // Fire off a thread to do some work that we shouldn't do directly in the UI thread Thread t = new Thread() { public void run() { mResults = doSomethingExpensive(); mHandler.post(mUpdateResults); } }; t.start(); } private void updateResultsInUi() { // Back in the UI thread -- update our UI elements based on the data in mResults [... ] } } 38
Examples r See BackgroundTimer r See HandleMessage 39
Tools r AsyncTask r IntentService 40
Tools: AsyncTask 41 private class DownloadFilesTask extends AsyncTask { protected Long doInBackground(URL... urls) { // on some background thread int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); } return totalSize; } protected void onProgressUpdate(Integer... progress) { // on UI thread! setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { // on UI thread! showDialog("Downloaded " + result + " bytes"); } new DownloadFilesTask().execute(url1, url2, url3); // call from UI thread! See GoogleSearch
Does Background Solve All Issues? 42
Example: Accessing Data in Cloud r A typical setting is that a device accesses data in the cloud, e.g., m background sync r Challenge: How do you keep data on a device fresh? 43
Polling r Simple to implement r Device periodically asks server for new data r Appropriate for content that changes constantly m Stock Quotes, News Headlines 44
Impact of Polling on Battery r Baseline: ~5-8 mA r Network: ~ mA m Tx more expensive than Rx r Assume radio stays on for 10 sec. m Energy per poll: ~0.50 mAh m 5 min frequency: ~144 mAh / day r Droid 2 total battery: 1400 mAh 45 Source: Android development team at Google
Solution: Push r Google Contacts, Calendar, Gmail, etc., use push sync r A single persistent connection from device to Google r Android Cloud to Device Messaging (C2DM) to make it a public service 46
C2DM Overview r Uses existing connection for Google services r Your servers send lightweight “data” messages to apps r Tell app new data available r Intent broadcast wakes up app r App supplies UI, e.g., Notification, if/as necessary 47
C2DM Flow r Enabling cloud to device messaging m App (on device) registers with Google, gets registration ID m App sends registration ID to its App Server r Per message m App Server sends (authenticated) message to Google m Google sends message to device r Disabling cloud to device messaging m App can unregister ID, e.g., when user no longer wants push 48
C2DM 49
Android Code: Registration to C2DM // Use the Intent API to get a registration ID // Registration ID is compartmentalized per app/device Intent regIntent = new Intent(“com.google.android.c2dm.intent.REGISTER”); // Identify your app regIntent.putExtra(“app”, PendingIntent.getBroadcast(this, 0, new Intent(), 0); // Identify role account server will use to send regIntent.putExtra(“sender”, OfSender); // Start the registration process startService(regIntent); 50
Receiving Registration ID 51 // Registration ID received via an Intent public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (“…REGISTRATION”.equals(action)) { handleRegistration(context, intent); } private void handleRegistration(Context context, Intent intent){ String id = intent.getExtra(“registration_id”); if ((intent.getExtra(“error”) != null) { // Registration failed. Try again later, with backoff. } else if (id != null) { // Send the registration ID to the app’s server. // Be sure to do this in a separate thread. }
Receiving Registration ID r App receives the ID as an Intent m com.google.android.c2dm.intent.REGISTRATION r App should send this ID to its server r Service may issue new registration ID at any time r App will receive REGISTRATION Intent broadcast r App must update server with new ID 52
Application Framework (Android): Key Concepts r Activity and view m Visible screen for user interaction r External resources 53
External Resources 54
Application Framework (Android): Key Concepts r Activity and view m Visible screen for user interaction r External resources r Service 55
Application Framework (Android): Key Concepts r Activity r View/ViewGroup (Layout) r External resources r Service r Intercommunications 56 App Communication among apps: - Intent - broadcast - data provider
Service: Working in Background r A basic function of Android Service: m A facility for an application to tell the system about something it wants to be doing in the background (even when the user is not directly interacting with the application). m The system to schedule work for the service, to be run until the service or someone else explicitly stop it. m NO GUI, higher priority than inactive Activities r Note m A Service is not a separate process. The Service object itself does not imply it is running in its own process; unless otherwise specified, it runs in the same process as the application it is part of. m A Service is not a thread. It is not a means itself to do work off of the main thread (to avoid Application Not Responding errors).
Application and Component Glues r Intent m An intent is an abstract description of an operation to be performed. m To invoke operations from your own or others m Can pass data back and forth between app. r Intent Filter m Register Activities, Services, and Broadcast Receivers as being capable of performing an action on a particular kind of data.
Intent Description m m Action m Data m Category, e.g., LAUNCHER 59
Intent Usage r Pass to Context.startActivity() or Activity.startActivityForResult() to launch an activity or get an existing activity to do something new.Context.startActivity() Activity.startActivityForResult() r Pass to Context.startService() to initiate a service or deliver new instructions to an ongoing service.Context.startService() m Pass to Context.bindService() to establish a connection between the calling component and a target service. It can optionally initiate the service if it's not already running.Context.bindService() r Pass to any of the broadcast methods (such as Context.sendBroadcast(), Context.sendOrderedBroadcast(), or Context.sendStickyBroadcast()) are delivered to all interested broadcast receivers. Many kinds of broadcasts originate in system code. Context.sendBroadcast()Context.sendOrderedBroadcast() Context.sendStickyBroadcast() 60
Android: Broadcast Receiver r Sending a broadcast: m Context.sendBroadcast(Intent intent, String receiverPermission) m Context.sendOrderedBroadcast() r Receiving broadcast: m Intent registerReceiver (BroadcastReceiver receiver, IntentFilter filter) 61
Intent Resolution: Explicit r Explicit intents: component identified 62 r Make sure AndroidManifest.xml announces activities to be started Intent myIntent = new Intent(IntentController.this, TipCal.class); startActivity(myIntent); <activity android:name=".IntentController" android:label="Intent1">
Intent Resolution: Implicit r Implicit intents m System matches an intent object to the intent filters of others 63
Intent filter 64 action category data
Intent Example II: Implicit r AndroidManifest.xml file for com.android.browser 65 String action = "android.intent.action.VIEW"; Uri data = Uri.parse(" Intent myIntent = new Intent(action, data); startActivity(myIntent);
Intent Example II: Implicit String action = "android.intent.action.DIAL"; String phno = "tel: "; Uri data = Uri.parse(phno); Intent dialIntent = new Intent(action, data); startActivity(dialIntent);
A Design Template: Invoker 67 String action = “com.hotelapp.ACTION_BOOK"; String hotel = “hotel://name/“ + selectedHotel; Uri data = Uri.parse(hotel); Intent bookingIntent = new Intent(action, data); startActivityForResults(bookingIntent, requestCode);
A Design Template: Provider 68 r For more complex data passing, please read the tutorial
A Design Template: Provider public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = getIntent(); // why am I called String action = intent.getAction(); Uri data = intent.getdata(); String hotelName = data.getPath(); // do the booking setResult(RESULT_OK); finish(); }
Intent and Broadcast: Sender String action = "edu.yale.cs434.RUN"; Intent cs434BroadcastIntent = new Intent(action); cs434BroadcastIntent.putExtra("message", "Wake up."); sendBroadcast(cs434BroadcastIntent); 70 Example: IntentLaunch
Intent and Broadcast: Receiver 71
Intent, Broadcast, Receiver, Notification public class CS434BroadcastReceiver extends BroadcastReceiver { public static final String CUSTOM_INTENT = "edu.yale.cs434.RUN"; // Display an alert that we've received a public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(CUSTOM_INTENT)) { String message = (String)intent.getExtras().get("message"); CharSequence text = "Got intent " + CUSTOM_INTENT + " with " + message; int duration = Toast.LENGTH_SHORT; Toast mToast = Toast.makeText(context, text, duration); mToast.show(); } // end of if } // end of onReceive } 72
Android: Content Provider r Each provider can expose its data as a simple table on a database model r Each content provider exposes a public URI that uniquely identifies its data set: m android.provider.Contacts.Phones.CONTENT_URI android.provider.Contacts.Photos.CONTENT_URI android.provider.CallLog.Calls.CONTENT_URI android.provider.Calendar.CONTENT_URI 73
Intent and Content Provider private void pickContact() { // Create an intent to "pick" a contact, as defined by the content provider URI Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI); startActivityForResult(intent, PICK_CONTACT_REQUEST); protected void onActivityResult(int requestCode, int resultCode, Intent data) { // If the request went well (OK) and the request was PICK_CONTACT_REQUEST if (resultCode == Activity.RESULT_OK && requestCode == PICK_CONTACT_REQUEST) { // Perform a query to the contact's content provider for the contact's name Cursor cursor = getContentResolver().query(data.getData(), new String[] {Contacts.DISPLAY_NAME}, null, null, null); if (cursor.moveToFirst()) { // True if the cursor is not empty int columnIndex = cursor.getColumnIndex(Contacts.DISPLAY_NAME); String name = cursor.getString(columnIndex); // Do something with the selected contact's name... } } } 74
75 Windows.NET Compact Framework r Similar to J2ME r Scales down a popular programming environment to ease learning m the.NET CF is a subset of the full.NET framework with some additions m designed for resource constrained devices m 1,400 classes for.NET CF vs. 8,000 for full m 27 UI controls for.NET CF vs. 52 for full m 1.5 MB for.NET CF vs. 30 MB for full r Uses versioning to avoid using lowest common denominator m pocket PC m pocket PC phone version m smart phone version r Uses virtual machines to mask device heterogeneity m programming languages compile to MSIL MSIL is JIT compiled on the device MSIL code is smaller than native executables MSIL allows your code to be processor independent
Android r Linux kernel as foundation r Java based framework (J2SE not J2ME) m Dalvik Virtual machine r Nice features m Touch screen, accelerometer, compass, microphone, camera, GPS, m GSM, EDGE, and 3G networks, WiFi, Bluetooth, Near field communications m Media, SQLite, WebKit, SSL m Location-based service, map (Google API) 76