1*1789df15SXin LiImplementation Details for SdkControllerApp 2*1789df15SXin Li------------------------------------------- 3*1789df15SXin Li 4*1789df15SXin Li---- 2012-03-22 5*1789df15SXin LiApp is in the namespace com.android.tools.sdkcontroller. 6*1789df15SXin Li 7*1789df15SXin LiThis is an app that has a minSdkVersion of 7 (Eclair) 8*1789df15SXin Liand a targetSdkVersion of 15 (ICS). The target version 9*1789df15SXin Limeans the app is forbidden under ICS from doing any network 10*1789df15SXin Licommunication on its main thread. 11*1789df15SXin Li 12*1789df15SXin LiThe overall design: 13*1789df15SXin Li- A background service is started by the app. It handles the connection 14*1789df15SXin Li to the emulator and provides a number of "handlers". Handlers can be 15*1789df15SXin Li though as being separate tasks that the user wants to achieve, for example 16*1789df15SXin Li sending sensor data, sending multi-touch events, receiving screen updates, 17*1789df15SXin Li sending a camera feed, etc. 18*1789df15SXin Li- All the handlers are started when the service starts and shutdown with it. 19*1789df15SXin Li They basically stay there as long as the app is running, and its up to the 20*1789df15SXin Li handler to deal with emulator connections starts/stopping. Some handlers 21*1789df15SXin Li will run in the background (e.g. sending sensor data) whereas other might 22*1789df15SXin Li need an activity to connect to them first. 23*1789df15SXin Li- The app has a number of activities which connect to existing handlers. 24*1789df15SXin Li 25*1789df15SXin LiAnother way to see it is that the app handles a number of tasks which are 26*1789df15SXin Licomposed of a background handler (that consumes data form the emulator and 27*1789df15SXin Lican send data to the emulator) and an optional activity for UI (that displays 28*1789df15SXin Lior controls the handler's state.) 29*1789df15SXin Li 30*1789df15SXin Li 31*1789df15SXin LiHere's a quick overview of the classes in the application: 32*1789df15SXin Li 33*1789df15SXin Li 34*1789df15SXin LiThe main UI is in activities.MainActivity. 35*1789df15SXin LiThere are 2 tasks activities: SensorActivity and MultiTouchActivity. 36*1789df15SXin Li 37*1789df15SXin LiThese all derive from BaseBindingActivity which provides a few convenient common features 38*1789df15SXin Li- in onResume this will bind to the service, creating and starting it if necessary. 39*1789df15SXin Li- in onPause, this will unbind from the service, but does not stop it. 40*1789df15SXin Li 41*1789df15SXin LiNote however that due to the asynchronous nature of the bind operation, the activity 42*1789df15SXin Limust not attempt to use the service from onResume. Instead there are 2 callbacks to use: 43*1789df15SXin Li- onServiceConnected when the bind succeeded. 44*1789df15SXin Li- onServiceDisconnected as the reverse operation. 45*1789df15SXin Li 46*1789df15SXin LiWhen the activity is connected to the service, it can then use getServiceBinder() 47*1789df15SXin Lito get an interface to talk to the service. 48*1789df15SXin Li 49*1789df15SXin LiIn the other direction, the activity provides a listener for the service to notify 50*1789df15SXin Lithe application: ControllerListener createControllerListener(). 51*1789df15SXin Li 52*1789df15SXin LiThe activity can then access the handler: 53*1789df15SXin Li handler = getServiceBinder().getHandler(HandlerType....) 54*1789df15SXin Li 55*1789df15SXin Liand then the activity wants to provide a listener to get notified by the handler: 56*1789df15SXin Li handler.addUiHandler(new android.os.Handler(this)); 57*1789df15SXin Li 58*1789df15SXin LiThe emulator connection is separated in the "lib" subpackage: 59*1789df15SXin Li- EmulatorConnection abstracts a connection to the emulator. 60*1789df15SXin Li - Object is first created by giving a non-null EmulatorListener. 61*1789df15SXin Li - then connect(port) is called to initiate the connection. 62*1789df15SXin Li - The EmulatorConnection is always created in SYNC mode. 63*1789df15SXin Li- EmulatorListener is a callback: the emulator connection uses it to indicate 64*1789df15SXin Li when the connection is actually connected or disconnected. 65*1789df15SXin Li 66*1789df15SXin LiIn the end we have the following workflow describing who controls what (-->): 67*1789df15SXin Li 68*1789df15SXin Li 69*1789df15SXin Li Emulator 70*1789df15SXin Li ^ ^ 71*1789df15SXin Li | | EmuCnxHandler 72*1789df15SXin Li sendEventToEmulator| | (EmulatorListener) 73*1789df15SXin Li | +-------------+ 74*1789df15SXin Li | | 75*1789df15SXin Li handlers.BaseHandler | v 76*1789df15SXin Li Activity ------------------------> Handler <---- ControllerService 77*1789df15SXin Li UI <------------------------ | ^ 78*1789df15SXin Li android.os.Handler | | 79*1789df15SXin Li | ^ | | 80*1789df15SXin Li | | ControllerListener | | 81*1789df15SXin Li | +--------------------------------------------------+ | 82*1789df15SXin Li +-----------------------------------------------------------+ 83*1789df15SXin Li ControllerBinder 84*1789df15SXin Li 85*1789df15SXin Li---- 86