Slide 1

Slide 1 text

Android IPC Mechanisms And how we handled the migration issue

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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/

Slide 4

Slide 4 text

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)

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

Android IPC Mechanisms 9 Server Client

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Android IPC Mechanisms 11 Deep Dive into Android IPC/Binder Framework at Android Builders Summit 2013, Aleksandar (Saša) Gargenta.

Slide 12

Slide 12 text

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.

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

Questions 21