Implementing a Download Background Service

3,063 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,063
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
29
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • What do we do with scarce resurces *bottlenecks)? We manage them
  • So what du we understand with “smart” queue?
  • Mention pros and cons of each possibility
  • Mention pros and cons of each possibility
  • We use aidl
  • Implementing a Download Background Service

    1. 1. Implementing a Download Background Service Android Services Stefan Tramm Patrick Bönzli Android Experience Day, FHNW 2008-12-03
    2. 2. Agenda <ul><li>Part I: Business Part </li></ul><ul><ul><li>the application </li></ul></ul><ul><ul><li>some screenshots </li></ul></ul><ul><li>Part II: Technical Part </li></ul><ul><ul><li>general introduction </li></ul></ul><ul><ul><li>our approach </li></ul></ul>
    3. 3. Part I: Business Part <ul><ul><li>the mission </li></ul></ul><ul><ul><li>customers and usage scenario </li></ul></ul><ul><ul><li>technical background </li></ul></ul><ul><ul><li>screenshots </li></ul></ul>
    4. 4. Prototype “Jukebox” <ul><li>Mission : Jukebox is a mobile application that collects selected information of various types from authenticated feeds – pull and push. Jukebox interacts with other data and applications on the device and allows to perform custom actions based on the information content type. </li></ul>
    5. 5. Prototype “Jukebox” <ul><ul><li>Information content types: </li></ul></ul><ul><ul><ul><li>Invitations to phone conferences Actions: set reminder/alarm, one-click dial-in , put to calendar </li></ul></ul></ul><ul><ul><ul><li>Audio/Video podcasts </li></ul></ul></ul><ul><ul><li>Attachments: viewing accompanying documents (e.g. PDF) </li></ul></ul><ul><ul><li>add personal notes </li></ul></ul><ul><ul><li>instant feedback during event/show </li></ul></ul><ul><ul><li>uses open data format standards </li></ul></ul>
    6. 6. Usage scenario: enlarge your CRM-capabilities <ul><ul><li>push news to subscribers onto mobile phones </li></ul></ul><ul><ul><li>provide well known end users with the right information at the right time: </li></ul></ul><ul><ul><ul><li>invite to telephone conferences (or meetings) </li></ul></ul></ul><ul><ul><ul><li>allow easy joining at the right time </li></ul></ul></ul><ul><ul><ul><li>provide attachments (PDF, Word, Excel, Video) </li></ul></ul></ul><ul><ul><ul><li>link to a specific website </li></ul></ul></ul><ul><ul><ul><li>provide a podcast afterwards </li></ul></ul></ul><ul><ul><ul><li>visually indicate updates and changes </li></ul></ul></ul><ul><ul><ul><li>feedback: reading confirmation, voting </li></ul></ul></ul>
    7. 7. Technical background info <ul><ul><li>Android application </li></ul></ul><ul><ul><li>based on standard ATOM file format ( RSS-reader on steroids ) </li></ul></ul><ul><ul><li>asynchronously updating data in background </li></ul></ul><ul><ul><li>uses internal SQLite DB and aggressive caching </li></ul></ul><ul><ul><ul><li>works without network connection (e.g. airplane) </li></ul></ul></ul>
    8. 8. Jukebox: Listview
    9. 9. Jukebox: Detailview
    10. 10. Part II: Technical Part <ul><ul><li>Android Services </li></ul></ul><ul><ul><li>download manager </li></ul></ul>
    11. 11. Goals <ul><ul><li>Understand reasons for background services </li></ul></ul><ul><ul><li>Know the Android Service lifecycle </li></ul></ul><ul><ul><li>See service IPC possibilities by theory and examples </li></ul></ul><ul><ul><li>Understand our download manager approach </li></ul></ul>Quality Software Engineering
    12. 12. Starting Point <ul><ul><li>multiple Activities, triggered by Intents </li></ul></ul><ul><ul><li>each Activity needs to download many media entities </li></ul></ul><ul><ul><li>network bandwidth scarce resource  bottleneck </li></ul></ul>
    13. 13. Managing Network Resources <ul><li>More reasons to manage network resources carefully: </li></ul><ul><ul><li>optimize application performance </li></ul></ul><ul><ul><li>longer battery lifetime by minimizing network activity </li></ul></ul><ul><ul><li>conserve data volume limits </li></ul></ul><ul><li>Boils down to two problems: </li></ul><ul><ul><li>asynchronous working </li></ul></ul><ul><ul><li>classic resource scheduling problem </li></ul></ul><ul><li>Our approach: </li></ul><ul><li>Uncoupling network from Activity using a queue . </li></ul>
    14. 14. Asynchronous Working Problem <ul><li>Instruments for solving this problem with a queue: </li></ul><ul><li>Parallel processing to remove waiting times </li></ul><ul><ul><ul><li>using threads and services </li></ul></ul></ul><ul><li>Indicating finished work </li></ul><ul><ul><ul><li>using events or notifications </li></ul></ul></ul>
    15. 15. Resource Access Scheduling Problem <ul><li>Instruments for solving this problem with a queue: </li></ul><ul><li>Control access to the resource: </li></ul><ul><ul><ul><li>resource allocations only over a central entity </li></ul></ul></ul><ul><ul><ul><li>by limiting number of parallel connections </li></ul></ul></ul><ul><li>  </li></ul><ul><li>Prioritize , to determine the order of access: </li></ul><ul><ul><ul><li>using a priority queue </li></ul></ul></ul><ul><li>Optimize resource access: </li></ul><ul><ul><ul><li>active caching (memory and disk) </li></ul></ul></ul><ul><li>  </li></ul>
    16. 16. Android Application Building Blocks <ul><ul><li>AndroidManifest.xml </li></ul></ul><ul><ul><li>Activities </li></ul></ul><ul><ul><li>Views </li></ul></ul><ul><ul><li>Layouts </li></ul></ul><ul><ul><li>Intents & IntentReceivers </li></ul></ul><ul><ul><li>Services </li></ul></ul><ul><ul><li>Notifications </li></ul></ul><ul><ul><li>ContentProviders </li></ul></ul>
    17. 17. Android Toolbox <ul><li>Android offers: </li></ul><ul><ul><li>synchronous downloading: OpenHTTPConnection </li></ul></ul><ul><ul><li>parallel processing using threads: java.lang.Thread </li></ul></ul><ul><ul><li>thread pools: java.util.concurrent.ThreadPoolExecutor </li></ul></ul><ul><ul><li>communication with threads: android.os.Handler </li></ul></ul><ul><ul><li>http request queues: android.net.http.RequestQueue  </li></ul></ul><ul><ul><li>background services: android.Service  </li></ul></ul>
    18. 18. Architecture Overview
    19. 19. Android Service <ul><li>Service is an Android application component running in background: </li></ul><ul><ul><li>class extends android.Service </li></ul></ul><ul><ul><li>not directly interacting with the user </li></ul></ul><ul><ul><li>started by a hosting process </li></ul></ul><ul><ul><li>runs in the main thread of their hosting process  </li></ul></ul><ul><ul><li>uses a different address space than the hosting process </li></ul></ul><ul><ul><li>service can be made publicly available </li></ul></ul>
    20. 20. Services Lifecycle I <ul><ul><li>services are started by a „hosting“ process </li></ul></ul><ul><ul><li>two possibilities to start a service: </li></ul></ul><ul><ul><ul><li>startService() : starts a service </li></ul></ul></ul><ul><ul><ul><li>bindService() : starts and binds service to host life cycle </li></ul></ul></ul>
    21. 21. Services Lifecycle II <ul><li>Starting a service using startService() </li></ul>
    22. 22. Services Lifecycle III <ul><li>Starting a service using bindService() </li></ul>
    23. 23. Services IPC <ul><li>How to share data among multiple concurrent processes? </li></ul><ul><li>  </li></ul><ul><li>Persistent: </li></ul><ul><ul><li>files (e.g. flash drive) </li></ul></ul><ul><ul><li>databases (SQLite), Bonus: offers transactions </li></ul></ul><ul><ul><li>Android properties </li></ul></ul><ul><li>  </li></ul><ul><li>Non-persistent: </li></ul><ul><ul><li>shared memory </li></ul></ul><ul><ul><li>network sockets </li></ul></ul><ul><ul><li>IDL (interface description language) the Android way: AIDL </li></ul></ul>
    24. 24. Android Interface Description Language - AIDL <ul><ul><li>IDL used to describe interfaces in language-neutral way </li></ul></ul><ul><ul><li>AIDL is the Android way of doing IPC </li></ul></ul><ul><ul><li>command line tool available </li></ul></ul><ul><li>Important AIDL properties: </li></ul><ul><ul><li>calls are synchronous </li></ul></ul><ul><ul><li>objects are reference counted across processes </li></ul></ul><ul><ul><li>no inter process exception forwarding </li></ul></ul>
    25. 25. Coding Example: The Service <ul><li>Service definition </li></ul><ul><li>public class MyService extends Service { </li></ul><ul><li>@Override </li></ul><ul><li>protected void onCreate() { </li></ul><ul><li>super.onCreate(); </li></ul><ul><li>Log.i(getClass().getSimpleName(), “Service started.&quot;); </li></ul><ul><li>} </li></ul><ul><li>@Override </li></ul><ul><li>protected void onDestroy() { </li></ul><ul><li>super.onDestroy(); </li></ul><ul><li>Log.i(getClass().getSimpleName(), “Service stopped.&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Service start public class SomeActivity extends Activity { ... startService(new Intent(&quot;com.example.android.apis.app.MyService&quot;)); ... }
    26. 26. Coding Example: AIDL I <ul><li>Interface declaration </li></ul><ul><li>interface IRemoteDownloadService { </li></ul><ul><li>void startDownload(in Uri uri, String type, IRemoteDownloadServiceCallback cb); </li></ul><ul><li>} </li></ul>Interface implementation public class RemoteDownloadService extends Service { private final IRemoteDownloadService.Stub mBinder = new IRemoteDownloadService.Stub() { public void startDownload(Uri uri, String type, IRemoteDownloadServiceCallback cb) { ResourceHandler handler = new ResourceHandler(uri, type, cb); completionService.handleResource(handler); } }; }
    27. 27. Coding Example: AIDL II <ul><li>Communicating with Service </li></ul><ul><li>public class SomeClass { </li></ul><ul><li>private ServiceConnection connection = new ServiceConnection() { </li></ul><ul><li>public void onServiceConnected(ComponentName name, IBinder service) { </li></ul><ul><li>downloader = IRemoteDownloadService.Stub.asInterface(service); </li></ul><ul><li>} </li></ul><ul><li>public void onServiceDisconnected(ComponentName name) { </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>... </li></ul><ul><li>bindService(new Intent(&quot;ch.netcetera.REMOTE_DOWNLOAD_SERVICE&quot;), connection, BIND_AUTO_CREATE); </li></ul><ul><li>... </li></ul><ul><li>downloader.startDownload(uri, type, downloadCallback); </li></ul><ul><li>} </li></ul>
    28. 28. Downloading Manager
    29. 29. Download Manager Error Handling <ul><li>We need to handle special states separately: </li></ul><ul><ul><li>telephone going to sleep </li></ul></ul><ul><ul><ul><li>save current downloading state persistently </li></ul></ul></ul><ul><ul><ul><li>resume download on application startup </li></ul></ul></ul><ul><ul><li>low battery </li></ul></ul><ul><ul><ul><li>possibility to switch to offline mode </li></ul></ul></ul><ul><ul><li>low memory </li></ul></ul><ul><ul><ul><li>resize in memory cache </li></ul></ul></ul><ul><ul><li>network connection down </li></ul></ul><ul><ul><ul><li>working in offline mode </li></ul></ul></ul><ul><ul><ul><li>resume downloads if network comes up again </li></ul></ul></ul>
    30. 30. Q & A Any questions ?
    31. 31. Conclusions <ul><ul><li>Android allows background services </li></ul></ul><ul><ul><li>background services must follow a well defined lifecycle </li></ul></ul><ul><ul><li>the implementation of a background data loader is feasible </li></ul></ul><ul><ul><li>experience on real hardware and under day-to-day conditions is needed </li></ul></ul>

    ×