Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Digging Into Android System Services
Search
Dave Smith
September 16, 2016
Programming
8
1.4k
Digging Into Android System Services
Walkthrough of the architecture that the Android platform uses to expose services to applications.
Dave Smith
September 16, 2016
Tweet
Share
More Decks by Dave Smith
See All by Dave Smith
Android Security Features
devunwired
4
620
ConstraintLayout, Inside and Out
devunwired
21
1.6k
Flattening Layouts with Constraints
devunwired
3
260
Hello, Brillo: ELC Edition
devunwired
0
240
Mastering CoordinatorLayout Behaviors
devunwired
16
1.3k
Hello, Brillo
devunwired
1
2k
Google Proximity Beacons Overview
devunwired
4
220
Proximity Beacons and the Nearby API
devunwired
1
1.8k
Getting Your Act Together with CoordinatorLayout
devunwired
7
460
Other Decks in Programming
See All in Programming
/←このスケジュール表に立ち向かう フロントエンド開発戦略 / A front-end development strategy to tackle a single-slash schedule.
nrslib
1
590
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
0
150
Java ジェネリクス入門 2024
nagise
0
600
Why Spring Matters to Jakarta EE - and Vice Versa
ivargrimstad
0
980
Identifying User Idenity
moro
6
7.9k
CSC509 Lecture 09
javiergs
PRO
0
110
qmuntal/stateless のススメ
sgash708
0
120
PagerDuty を軸にした On-Call 構築と運用課題の解決 / PagerDuty Japan Community Meetup 4
horimislime
1
110
現場で役立つモデリング 超入門
masuda220
PRO
13
2.9k
讓數據說話:用 Python、Prometheus 和 Grafana 講故事
eddie
0
350
Kaigi on Rails 2024 - Rails APIモードのためのシンプルで効果的なCSRF対策 / kaigionrails-2024-csrf
corocn
5
3.4k
GCCのプラグインを作る / I Made a GCC Plugin
shouth
1
150
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
504
140k
Designing Experiences People Love
moore
138
23k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
33k
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
The Power of CSS Pseudo Elements
geoffreycrofte
72
5.3k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
41
2.1k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Code Review Best Practice
trishagee
64
17k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
46
2.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Transcript
Digging Into Android System Services Dave Smith, PE @devunwired
Why Are You Here? Trace AOSP Code Learn Platform Internals
Discover App Impact
Linux Kernel Hardware Abstraction Layer Native Services Runtime Application Framework
Applications
Linux Kernel Hardware Abstraction Layer Native Services Runtime Application Framework
Applications
Application System Server Manager Service
Application System Server Manager Service Application Manager Application Manager Application
Manager
Context.getSystemService(SERVICE_NAME)
ContextImpl SystemServiceRegistry Context AlarmManager NotificationManager LocationManager PowerManager Context.getSystemService(SERVICE_NAME)
package android.app; final class SystemServiceRegistry { ... static
{ ... registerService(Context.ACCOUNT_SERVICE, AccountManager.class, new CachedServiceFetcher<AccountManager>() { @Override public AccountManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); } }); ... registerService(Context.ALARM_SERVICE, AlarmManager.class, new CachedServiceFetcher<AlarmManager>() { @Override public AlarmManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ALARM_SERVICE); IAlarmManager service = IAlarmManager.Stub.asInterface(b); return new AlarmManager(service, ctx); } }); ... } } SystemServiceRegistry.java
package android.app; final class SystemServiceRegistry { ... static
{ ... registerService(Context.ACCOUNT_SERVICE, AccountManager.class, new CachedServiceFetcher<AccountManager>() { @Override public AccountManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); } }); ... registerService(Context.ALARM_SERVICE, AlarmManager.class, new CachedServiceFetcher<AlarmManager>() { @Override public AlarmManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ALARM_SERVICE); IAlarmManager service = IAlarmManager.Stub.asInterface(b); return new AlarmManager(service, ctx); } }); ... } } SystemServiceRegistry.java
Application System Server Manager Service
Binder IPC Application System Server Service Manager
Application System Server Manager Service Binder IPC AIDL Proxy Stub
Application System Server AlarmManager AlarmManagerService IAlarmManager IAlarmManager.Stub Binder IPC
IAlarmManager.aidl AlarmManager.java package android.app; ... public class AlarmManager
{ private final IAlarmManager mService; ... public void setTime(long millis) { try { mService.setTime(millis); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } public void setTimeZone(String timeZone) { ... try { mService.setTimeZone(timeZone); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } } package android.app; interface IAlarmManager { void set(...); boolean setTime(long millis); void setTimeZone(String zone); void remove(...); long getNextWakeFromIdleTime(); ... }
IAlarmManager.aidl AlarmManager.java package android.app; ... public class AlarmManager
{ private final IAlarmManager mService; ... public void setTime(long millis) { try { mService.setTime(millis); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } public void setTimeZone(String timeZone) { ... try { mService.setTimeZone(timeZone); } catch (RemoteException ex) { throw ex.rethrowFromSystemServer(); } } } package android.app; interface IAlarmManager { void set(...); boolean setTime(long millis); void setTimeZone(String zone); void remove(...); long getNextWakeFromIdleTime(); ... }
package com.android.server; public class AlarmManagerService extends SystemService { ...
private final IBinder mService = new IAlarmManager.Stub() { @Override public boolean setTime(long millis) { getContext().enforceCallingOrSelfPermission( "android.permission.SET_TIME", "setTime"); ... } @Override public void setTimeZone(String tz) { getContext().enforceCallingOrSelfPermission( "android.permission.SET_TIME_ZONE", "setTimeZone"); ... } }; } AlarmManagerService.java IAlarmManager.aidl package android.app; interface IAlarmManager { void set(...); boolean setTime(long millis); void setTimeZone(String zone); void remove(...); long getNextWakeFromIdleTime(); ... }
Binder IPC Application Service Object
Binder IPC Application Service Object Object
public class MainActivity extends Activity implements LocationListener { @Override
protected void onResume() { super.onResume(); LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE); manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Override protected void onPause() { super.onPause(); LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE); manager.removeUpdates(this); } @Override public void onLocationChanged(Location location) { ... } ... }
public class MainActivity extends Activity implements LocationListener { @Override
protected void onResume() { super.onResume(); LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE); manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } @Override protected void onPause() { super.onPause(); LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE); manager.removeUpdates(this); } @Override public void onLocationChanged(Location location) { ... } ... }
Application System Server Manager Service
Application System Server Manager Service Service Manager Stub Stub
Application System Server Manager Service Service Manager IBinder Stub IBinder
Application System Server Manager Service Service Manager Proxy Stub IBinder
$ adb shell service list Found 116 services: 0 carrier_config:
[com.android.internal.telephony.ICarrierConfigLoader] 1 phone: [com.android.internal.telephony.ITelephony] 2 isms: [com.android.internal.telephony.ISms] 3 iphonesubinfo: [com.android.internal.telephony.IPhoneSubInfo] 4 simphonebook: [com.android.internal.telephony.IIccPhoneBook] 5 isub: [com.android.internal.telephony.ISub] 6 telecom: [com.android.internal.telecom.ITelecomService] 7 contexthub_service: [android.hardware.location.IContextHubService] 8 dns_listener: [android.net.metrics.IDnsEventListener] 9 connectivity_metrics_logger: [android.net.IConnectivityMetricsLogger] 10 imms: [com.android.internal.telephony.IMms] 11 media_projection: [android.media.projection.IMediaProjectionManager] 12 launcherapps: [android.content.pm.ILauncherApps] 13 shortcut: [android.content.pm.IShortcutService] 14 fingerprint: [android.hardware.fingerprint.IFingerprintService] 15 trust: [android.app.trust.ITrustManager] 16 media_router: [android.media.IMediaRouterService] 17 media_session: [android.media.session.ISessionManager] 18 restrictions: [android.content.IRestrictionsManager] 19 print: [android.print.IPrintManager] 20 graphicsstats: [android.view.IGraphicsStats] ...
package android.app; final class SystemServiceRegistry { ... static
{ ... registerService(Context.ACCOUNT_SERVICE, AccountManager.class, new CachedServiceFetcher<AccountManager>() { @Override public AccountManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); } }); ... registerService(Context.ALARM_SERVICE, AlarmManager.class, new CachedServiceFetcher<AlarmManager>() { @Override public AlarmManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ALARM_SERVICE); IAlarmManager service = IAlarmManager.Stub.asInterface(b); return new AlarmManager(service, ctx); } }); ... } } SystemServiceRegistry.java
package android.app; final class SystemServiceRegistry { ... static
{ ... registerService(Context.ACCOUNT_SERVICE, AccountManager.class, new CachedServiceFetcher<AccountManager>() { @Override public AccountManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ACCOUNT_SERVICE); IAccountManager service = IAccountManager.Stub.asInterface(b); return new AccountManager(ctx, service); } }); ... registerService(Context.ALARM_SERVICE, AlarmManager.class, new CachedServiceFetcher<AlarmManager>() { @Override public AlarmManager createService(ContextImpl ctx) { IBinder b = ServiceManager.getService(Context.ALARM_SERVICE); IAlarmManager service = IAlarmManager.Stub.asInterface(b); return new AlarmManager(service, ctx); } }); ... } } SystemServiceRegistry.java
package com.android.server; import android.content.Context; public final class SystemServer
{ ... ConnectivityServicer connectivity = new ConnectivityService( context, networkManagement, networkStats, networkPolicy); ServiceManager.addService(Context.CONNECTIVITY_SERVICE, connectivity); ... LocationManagerService location = new LocationManagerService(context); ServiceManager.addService(Context.LOCATION_SERVICE, location); ... } SystemServer.java
@devunwired +DaveSmithDev milehighandroid.com wiresareobsolete.com