Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Android IPC Mechanisms - And how we handled the migration issue

Android IPC Mechanisms - And how we handled the migration issue

By Haley

Buzzvil

May 16, 2018
Tweet

More Decks by Buzzvil

Other Decks in Programming

Transcript

  1. Contents - The Structure of Android Platform - Android Application

    Component - Android IPC Mechanisms - About the “Bridge” module API - DataStorage using ContentProvider - EventHandler using BroadcastReceiver - RequestHandler using Messenger with Service - Use case of each Bridge API in Migration SDK 2
  2. The Structure of Android Platform - The Android OS is

    built upon multi-user Linux system - Each application has a unique Linux user id. - Each application runs in its own, separate process. - Process isolation is done between the apps. - By default, all components of the same application run in the same process. 3 https://developer.android.com/guide/platform/
  3. Android Application Components 4 The essential building blocks of an

    Android App. - Each type serves a distinct purpose. - Activity : Provides user interface and interacts with the user, representing a single screen. - Content Provider : Manages a shared set of app data that is stored in the file system. - Broadcast Receiver : Receives and handle messages sent from other component. - Service : Performs long-running operations in the background - Can also be bound to another component which can even do Inter Process Communication(IPC)
  4. Android Application Components A unique aspect of the Android system

    design is that any app can start another app’s component. - Each component can be an entry point of the app. - The system runs each app in a separate process with file permissions that restrict access to other apps. - Cannot directly activate a component from another app. - Need to do frequent Inter-Process Communication. 5
  5. Android IPC Mechanisms - In Linux : Signal, Pipe, Socket,

    Shared memory, ... - In Android : Binder framework which provides lightweight remote procedure communication (RPC) style mechanism. - Binder : Kernel driver - Uses Shared memory resulting in less memory usage during IPC 6 https://www.slideshare.net/tetsu.koba/interprocess-communication-of-android
  6. Android IPC Mechanisms - Wrapped functionalities that Android OS provides

    - Binder (with AIDL) - Messenger - Intent - The latter is the abstracted version of the former. - Binder is the lowest level API that Android provides - Messenger = Binder + Predefined AIDL + Restricted Thread (Handler) - Intent = Messenger + Connection control 7
  7. Android IPC Mechanisms - Binder - Must define AIDL (Android

    Interface Definition Language) to be used as IPC. - AIDL decomposes Objects into primitives that the OS can understand, and passes them across processes to perform IPC. - Directly invoke methods on other processes. - Need to ensure thread safety and handle multi threading while using it. 8 Deep Dive into Android IPC/Binder Framework at Android Builders Summit 2013, Aleksandar (Saša) Gargenta.
  8. Android IPC Mechanisms - Messenger - Dependent on ‘Handler’ which

    is associated with a single thread and handles that thread’s event queue. - Creates a queue of all the client requests in a single thread, receiving requests one at a time (Thread safety). - Only asynchronous communications. - How to use - The server implements a Handler that receives a callback for each call from a client and create a Messenger using the handler. - When the client is bound to the server, the Messenger reference is given to the client which also references the server’s handler. - The client uses the Messenger to send Message objects to the server. - The server receives each Message in its Handler. 10
  9. Android IPC Mechanisms 11 Deep Dive into Android IPC/Binder Framework

    at Android Builders Summit 2013, Aleksandar (Saša) Gargenta.
  10. Android IPC Mechanisms - Intent - Universal communication medium between

    the app components. - The most abstracted, easiest form to use. - Only need to set the target component or declare a general action to perform. - Can start the other components : Broadcast Receiver, Service, Activity. - High latency issue. - No callbacks after firing a intent. 12 Deep Dive into Android IPC/Binder Framework at Android Builders Summit 2013, Aleksandar (Saša) Gargenta.
  11. About The “Bridge” module - Newly developed SDK for handling

    the Migration issue. - https://github.com/Buzzvil/buzzscreen-sdk-publisher-migration/wiki/COMMUNICATION-UTILS - Utility for easy communication between two apps, L app and M app. - Bridge module’s class should be initialized with each other app’s package name to set the destination of communication. - Restrict each component with the signature-level permission - The system grants only if the requesting application is signed with the same certificate as the application that declared the permission. - 3 types of communication - DataStorage for sharing data between two apps. - EventHandler for one-way communication, designating each app as a sender and a receiver. - RequestHandler for two-way communication, similar to that of server-client relationship. 13
  12. DataStorage Provide “Mocked” shared data storage between two apps. -

    Implemented using ContentProvider - Data is stored in a key-value structure - Calling ‘put’ stores the value mapped to the key in current app’s storage. - Calling ‘get’ queries data from the partner and itself’s ContentProvider. MigrationXXX.getDataStorage().put("SHARED_CONFIG_KEY", "config_value"); String value = MigrationXXX.getDataStorage().get("SHARED_CONFIG_KEY"); 14
  13. EventHandler Handle’s the one-way communication - Implemented using BroadcastReceiver -

    Each event is distinguished by its unique name - The sender sends intents to the receiver specifying which event is currently sent. - The receiver registers listener for each event. 15
  14. EventHandler // Sender Bundle eventData = new Bundle(); eventData.putString("extra_info", "extra_value");

    MigrationXXX.getEventHandler().post("SAMPLE_EVENT", eventData); // Receiver MigrationXXX.getEventHandler().registerEventListener("SAMPLE_EVENT", new EventHandler.OnEventListener() { @Override public void onEvent(Bundle extras) { Log.d(TAG, "onReceive SAMPLE_EVENT"); String extraInfo = extras.getString("extra_info"); // "extra_value" returned ... } }); 16
  15. RequestHandler Offers a client-server interface that allows components in the

    other processes to interact with the service, send requests, receive results. - Implemented using Messenger with bound Service. - Each request is distinguished by its own request code. - Handles client-server connection and retries when request failed. - The server responds to the client’s request. - The client requests to server with callback indicating whether the request have succeeded or not. 17
  16. RequestHandler // Server MigrationXXX.getRequestHandler().registerResponder(1000, new MsgRequestHandler.Responder() { @Override public Bundle

    respond(Bundle parameters) { Bundle response = new Bundle(); response.putString("response_key", "response_sample"); return response; } }); // Client MigrationXXX.getRequestHandler().request(1000, null, new Request.OnResponseListener() { @Override public void onResponse(Bundle response) { String value = response.getString("response_key"); // "response_sample" returned } @Override public void onFail(Request.FailReason failReason) { } }); 18
  17. Use case in Migration SDK - DataStorage - To determine

    whether the lock screen of the L app is active. - The L app updates the value in the DataStorage whenever the lock screen is activated or deactivated. - the M app reads the value from the DataStorage when it wants to check the status of the L app. - EventHandler - To disable the L app lock screen from the M app. - The L app implements logic that disables the lock screen as a receiver and registers it as an event. - The M app sends the event as a Sender. - RequestHandler - To retrieve BuzzScreen settings data for M apps from L apps. - The L app requests the settings data to the M app as a client. - The M app responds with the BuzzScreen settings data when it receives this request as a server. 19
  18. Reference - https://developer.android.com/training/articles/security-tips#IPC (Official android developer site for IPC) -

    https://developer.android.com/guide/components/bound-services?hl=ko (Official android developer site for bound service) - http://androidyongyong.tistory.com/m/8 (프로세스 간 통신) - https://www.slideshare.net/jserv/android-ipc-mechanism (Android IPC Mechanism) - https://d2.naver.com/helloworld/47656 (Android 프로세스의 통신 메커니즘 : 바인더) - Deep Dive into Android IPC/Binder Framework, Android Builders Summit 2013, Aleksandar (Saša) Gargenta. 20