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

Digging Into Android System Services

Dave Smith
September 16, 2016

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

More Decks by Dave Smith

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. 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();
 ...
 }
  4. 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();
 ...
 }
  5. 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();
 ...
 }
  6. 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) {
 ...
 }
 
 ...
 }
  7. 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) {
 ...
 }
 
 ...
 }
  8. $ 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] ...
  9. 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
  10. 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
  11. 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