1.系统进程SystemServer启动
首先在SystemServer进程被创建并启动
/frameworks/base/services/java/com/android/server/SystemServer.java#main
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
在runf方法中开启服务
private void run() {
...
// Start services.
//开启服务
try {
t.traceBegin("StartServices");
startBootstrapServices(t); //boot服务
startCoreServices(t); //开启核心服务
startOtherServices(t); //其他服务
} catch (Throwable ex) {
Slog.e("System", "******************************************");
Slog.e("System", "************ Failure starting system services", ex);
throw ex;
} finally {
t.traceEnd(); // StartServices
}
...
}
其中在startOtherServices中就会去开启
/**
* Starts a miscellaneous grab bag of stuff that has yet to be refactored and organized.
*/
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
...
//启动TelecomLoaderService系统服务,用于加载Telecom
t.traceBegin("StartTelecomLoaderService");
mSystemServiceManager.startService(TelecomLoaderService.class);
t.traceEnd();
t.traceBegin("StartTelephonyRegistry");
//启动telephony注册服务,用于注册监听telephony状态的接口
telephonyRegistry = new TelephonyRegistry(
context, new TelephonyRegistry.ConfigurationProvider());
ServiceManager.addService("telephony.registry", telephonyRegistry);
...
}
在调用/frameworks/base/services/core/java/com/android/server/SystemServiceManager.java#startService方法
//
public void startService(@NonNull final SystemService service) {
// Register it.
mServices.add(service);
// Start it.
long time = SystemClock.elapsedRealtime();
try {
service.onStart();
} catch (RuntimeException ex) {
throw new RuntimeException("Failed to start service " + service.getClass().getName()
+ ": onStart threw an exception", ex);
}
warnIfTooLong(SystemClock.elapsedRealtime() - time, service, "onStart");
}
/frameworks/base/services/core/java/com/android/server/telecom/TelecomLoaderService.java#onBootPhase方法
//根据当前系统启动的不同阶段回调
@Override
public void onBootPhase(int phase) {
if (phase == PHASE_ACTIVITY_MANAGER_READY) {
registerDefaultAppNotifier();
registerCarrierConfigChangedReceiver();
// core services will have already been loaded.
setupServiceRepository();
connectToTelecom();
}
}
private static final String SERVICE_ACTION = "com.android.ITelecomService";
//绑定Telecom服务
private void connectToTelecom() {
synchronized (mLock) {
if (mServiceConnection != null) {
// TODO: Is unbinding worth doing or wait for system to rebind?
mContext.unbindService(mServiceConnection);
mServiceConnection = null;
}
TelecomServiceConnection serviceConnection = new TelecomServiceConnection();
Intent intent = new Intent(SERVICE_ACTION);
intent.setComponent(SERVICE_COMPONENT);
int flags = Context.BIND_IMPORTANT | Context.BIND_FOREGROUND_SERVICE
| Context.BIND_AUTO_CREATE;
// Bind to Telecom and register the service
if (mContext.bindServiceAsUser(intent, serviceConnection, flags, UserHandle.SYSTEM)) {
mServiceConnection = serviceConnection;
}
}
}
在TelecomLoaderService的内部类TelecomServiceConnection中的绑定服务成功回调中通过ServiceManager添加到系统服务
public void onServiceConnected(ComponentName name, IBinder service) {
// Normally, we would listen for death here, but since telecom runs in the same process
// as this loader (process="system") that's redundant here.
try {
ITelecomLoader telecomLoader = ITelecomLoader.Stub.asInterface(service);
ITelecomService telecomService = telecomLoader.createTelecomService(mServiceRepo);
SmsApplication.getDefaultMmsApplication(mContext, false);
ServiceManager.addService(Context.TELECOM_SERVICE, telecomService.asBinder());
...
} catch (RemoteException e) {
Slog.w(TAG, "Failed linking to death.");
}
}
时序图
到这里就会回调具体实现类/packages/services/Telecomm/src/com/android/server/telecom/components/TelecomService.java的onBind()方法
可以看到TelecomService是一个Service,在Telecom的AndroidManifest.xml中注册
/packages/services/Telecomm/AndroidManifest.xml
...
<service android:name=".components.TelecomService"
android:singleUser="true"
android:process="system"
android:exported="true">
<intent-filter>
<action android:name="android.telecom.ITelecomService"/>
</intent-filter>
</service>
...
该服务就是TelecomLoaderService.java#connectToTelecom中绑定的服务,查看该服务的onBind方法,在方法中返回了ITelecomLoader服务接口的Binder引用,该服务只有一个方法createTelecomService创建真正的ITelecomService服务,并返回Binder引用
@Override
public IBinder onBind(Intent intent) {
Log.d(this, "onBind");
return new ITelecomLoader.Stub() {
@Override
public ITelecomService createTelecomService(IInternalServiceRetriever retriever) {
InternalServiceRetrieverAdapter adapter =
new InternalServiceRetrieverAdapter(retriever);
initializeTelecomSystem(TelecomService.this, adapter);
synchronized (getTelecomSystem().getLock()) {
//返回ITelecomService的binder引用
return getTelecomSystem().getTelecomServiceImpl().getBinder();
}
}
};
}
static void initializeTelecomSystem(Context context,
InternalServiceRetrieverAdapter internalServiceRetriever) {
if (TelecomSystem.getInstance() == null) {
NotificationChannelManager notificationChannelManager =
new NotificationChannelManager();
notificationChannelManager.createChannels(context);
//设置一个单例对象
TelecomSystem.setInstance(
new TelecomSystem(
context...);
}
}
TelecomSystem的构造方法中
public TelecomSystem(
Context context,
/*用户未接来电通知类(不包括已接或者拒接的电话)*/
MissedCallNotifierImplFactory missedCallNotifierImplFactory,
/*查询来电信息*/
CallerInfoAsyncQueryFactory callerInfoAsyncQueryFactory,
/*耳机接入状态监听*/
HeadsetMediaButtonFactory headsetMediaButtonFactory,
/*距离传感器管理*/
ProximitySensorManagerFactory proximitySensorManagerFactory,
/*通话时电话管理*/
InCallWakeLockControllerFactory inCallWakeLockControllerFactory,
/*音频服务管理*/
AudioServiceFactory audioServiceFactory,
ConnectionServiceFocusManager.ConnectionServiceFocusManagerFactory
connectionServiceFocusManagerFactory,
/*查询所有超时信息*/
Timeouts.Adapter timeoutsAdapter,
/*响铃播放*/
AsyncRingtonePlayer asyncRingtonePlayer,
/*电话号码帮助类*/
PhoneNumberUtilsAdapter phoneNumberUtilsAdapter,
IncomingCallNotifier incomingCallNotifier,
InCallTonePlayer.ToneGeneratorFactory toneGeneratorFactory,
CallAudioRouteStateMachine.Factory callAudioRouteStateMachineFactory,
CallAudioModeStateMachine.Factory callAudioModeStateMachineFactory,
ClockProxy clockProxy,
RoleManagerAdapter roleManagerAdapter,
ContactsAsyncHelper.Factory contactsAsyncHelperFactory,
DeviceIdleControllerAdapter deviceIdleControllerAdapter) {
mContext = context.getApplicationContext();
...
//初始化CallsManager,主要负责与上层UI交互
mCallsManager = new CallsManager(
mContext,
mLock,
callerInfoLookupHelper,
mMissedCallNotifier,
disconnectedCallNotifierFactory,
mPhoneAccountRegistrar,
headsetMediaButtonFactory,
proximitySensorManagerFactory,
inCallWakeLockControllerFactory,
connectionServiceFocusManagerFactory,
audioServiceFactory,
bluetoothRouteManager,
wiredHeadsetManager,
systemStateHelper,
defaultDialerCache,
timeoutsAdapter,
asyncRingtonePlayer,
phoneNumberUtilsAdapter,
emergencyCallHelper,
toneGeneratorFactory,
clockProxy,
audioProcessingNotification,
bluetoothStateReceiver,
callAudioRouteStateMachineFactory,
callAudioModeStateMachineFactory,
inCallControllerFactory,
callDiagnosticServiceController,
roleManagerAdapter,
toastFactory);
mCallsManager.setIncomingCallNotifier(mIncomingCallNotifier);
//注册需要接收的广播
mContext.registerReceiverAsUser(mUserSwitchedReceiver, UserHandle.ALL,
USER_SWITCHED_FILTER, null, null);
mContext.registerReceiverAsUser(mUserStartingReceiver, UserHandle.ALL,
USER_STARTING_FILTER, null, null);
mContext.registerReceiverAsUser(mBootCompletedReceiver, UserHandle.ALL,
BOOT_COMPLETE_FILTER, null, null);
//所有来电与去电的中转站
mCallIntentProcessor = new CallIntentProcessor(mContext, mCallsManager,
defaultDialerCache);
mTelecomBroadcastIntentProcessor = new TelecomBroadcastIntentProcessor(
mContext, mCallsManager);
// Register the receiver for the dialer secret codes, used to enable extended logging.
mDialerCodeReceiver = new DialerCodeReceiver(mCallsManager);
mContext.registerReceiver(mDialerCodeReceiver, DIALER_SECRET_CODE_FILTER,
Manifest.permission.CONTROL_INCALL_EXPERIENCE, null);
// There is no USER_SWITCHED broadcast for user 0, handle it here explicitly.
final UserManager userManager = UserManager.get(mContext);
//创建一个TeleServiceImpl用于调用TeleService的接口
mTelecomServiceImpl = new TelecomServiceImpl(
mContext, mCallsManager, mPhoneAccountRegistrar,
new CallIntentProcessor.AdapterImpl(defaultDialerCache),
new UserCallIntentProcessorFactory() {
@Override
public UserCallIntentProcessor create(Context context,
UserHandle userHandle) {
return new UserCallIntentProcessor(context, userHandle);
}
},
defaultDialerCache,
new TelecomServiceImpl.SubscriptionManagerAdapterImpl(),
new TelecomServiceImpl.SettingsSecureAdapterImpl(),
mLock);
} finally {
Log.endSession();
}
}
TeleService服务绑定和创建时序图