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
630
ConstraintLayout, Inside and Out
devunwired
21
1.7k
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
230
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
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
ローコードSaaSのUXを向上させるためのTypeScript
taro28
1
630
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
110
タクシーアプリ『GO』のリアルタイムデータ分析基盤における機械学習サービスの活用
mot_techtalk
4
1.5k
NSOutlineView何もわからん:( 前編 / I Don't Understand About NSOutlineView :( Pt. 1
usagimaru
0
340
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
1k
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
6
1.7k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
Remix on Hono on Cloudflare Workers
yusukebe
1
300
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
カンファレンスの「アレ」Webでなんとかしませんか? / Conference “thing” Why don't you do something about it on the Web?
dero1to
1
110
Featured
See All Featured
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Statistics for Hackers
jakevdp
796
220k
Scaling GitHub
holman
458
140k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
246
1.3M
What's new in Ruby 2.0
geeforr
343
31k
4 Signs Your Business is Dying
shpigford
180
21k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Optimizing for Happiness
mojombo
376
70k
Designing the Hi-DPI Web
ddemaree
280
34k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
506
140k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
29k
Thoughts on Productivity
jonyablonski
67
4.3k
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