SystemServer.java
private static final String TETHERING_CONNECTOR_CLASS = "android.net.ITetheringConnector";
...
t.traceBegin("StartTethering");
try {
ConnectivityModuleConnector.getInstance().startModuleService(
TETHERING_CONNECTOR_CLASS,
PERMISSION_MAINLINE_NETWORK_STACK, service -> {
ServiceManager.addService(Context.TETHERING_SERVICE, service,
false ,
DUMP_FLAG_PRIORITY_HIGH | DUMP_FLAG_PRIORITY_NORMAL);
});
} catch (Throwable e) {
reportWtf("starting Tethering", e);
}
开机systemserver 启动,调用ConnectivityModuleConnector.getInstance().startModuleService 接下来,来分析startModuleService流程
public void startModuleService(
@NonNull String serviceIntentBaseAction,
@NonNull String servicePermissionName,
@NonNull ModuleServiceCallback callback) {
log("Starting networking module " + serviceIntentBaseAction);
final PackageManager pm = mContext.getPackageManager();
1.获取tetherservice name,传入参数inSystemProcess :true
Intent intent = mDeps.getModuleServiceIntent(pm, serviceIntentBaseAction,
servicePermissionName, true );
if (intent == null) {
intent = mDeps.getModuleServiceIntent(pm, serviceIntentBaseAction,
servicePermissionName, false );
log("Starting networking module in network_stack process");
} else {
log("Starting networking module in system_server process");
}
if (intent == null) {
maybeCrashWithTerribleFailure("Could not resolve the networking module", null);
return;
}
final String packageName = intent.getComponent().getPackageName();
if (!mContext.bindServiceAsUser(
intent, new ModuleServiceConnection(packageName, callback),
Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, UserHandle.SYSTEM)) {
maybeCrashWithTerribleFailure(
"Could not bind to networking module in-process, or in app with "
+ intent, packageName);
return;
}
log("Networking module service start requested");
}
1.通过private static final String TETHERING_CONNECTOR_CLASS = “android.net.ITetheringConnector”,来获取TetheringService对于的报名类名,接下来看看mDeps.getModuleServiceIntent 流程
private static final String IN_PROCESS_SUFFIX = ".InProcess";
private static class DependenciesImpl implements Dependencies {
@Nullable
@Override
public Intent getModuleServiceIntent(
@NonNull PackageManager pm, @NonNull String serviceIntentBaseAction,
@NonNull String servicePermissionName, boolean inSystemProcess) {
final Intent intent =
new Intent(inSystemProcess
? serviceIntentBaseAction + IN_PROCESS_SUFFIX
: serviceIntentBaseAction);
final ComponentName comp = intent.resolveSystemService(pm, 0);
if (comp == null) {
return null;
}
intent.setComponent(comp);
final int uid;
try {
uid = pm.getPackageUidAsUser(comp.getPackageName(), UserHandle.USER_SYSTEM);
} catch (PackageManager.NameNotFoundException e) {
throw new SecurityException(
"Could not check network stack UID; package not found.", e);
}
final int expectedUid =
inSystemProcess ? Process.SYSTEM_UID : Process.NETWORK_STACK_UID;
if (uid != expectedUid) {
throw new SecurityException("Invalid network stack UID: " + uid);
}
if (!inSystemProcess) {
checkModuleServicePermission(pm, comp, servicePermissionName);
}
return intent;
}
}
上面判断inSystemProcess添加不同的AndroidManifest.xml 区别在对于的报名不同 com.android.networkstack.tethering com.android.networkstack.tethering.inprocess 但是TetheringService.java是同一个。
接下来,我们看看TetheringService 启动
@Override
public void onCreate() {
final TetheringDependencies deps = makeTetheringDependencies();
mConnector = new TetheringConnector(makeTethering(deps), TetheringService.this);
}
@VisibleForTesting
public Tethering makeTethering(TetheringDependencies deps) {
System.loadLibrary("tetherutilsjni");
return new Tethering(deps);
}
private static class TetheringConnector extends ITetheringConnector.Stub {
private final TetheringService mService;
private final Tethering mTethering;
TetheringConnector(Tethering tether, TetheringService service) {
mTethering = tether;
mService = service;
}
@Override
public void tether(String iface, String callerPkg, IIntResultListener listener) {
if (checkAndNotifyCommonError(callerPkg, listener)) return;
try {
listener.onResult(mTethering.tether(iface));
} catch (RemoteException e) { }
}
@Override
public void untether(String iface, String callerPkg, IIntResultListener listener) {
if (checkAndNotifyCommonError(callerPkg, listener)) return;
try {
listener.onResult(mTethering.untether(iface));
} catch (RemoteException e) { }
}
@Override
public void setUsbTethering(boolean enable, String callerPkg, IIntResultListener listener) {
if (checkAndNotifyCommonError(callerPkg, listener)) return;
try {
listener.onResult(mTethering.setUsbTethering(enable));
} catch (RemoteException e) { }
}
@Override
public void startTethering(TetheringRequestParcel request, String callerPkg,
IIntResultListener listener) {
if (checkAndNotifyCommonError(callerPkg,
request.exemptFromEntitlementCheck ,
listener)) {
return;
}
mTethering.startTethering(request, listener);
}
@Override
public void stopTethering(int type, String callerPkg, IIntResultListener listener) {
if (checkAndNotifyCommonError(callerPkg, listener)) return;
try {
mTethering.stopTethering(type);
listener.onResult(TETHER_ERROR_NO_ERROR);
} catch (RemoteException e) { }
}
TetheringService 主要处理事务的其实是mTethering
如何调用TetherService
public void setHotspotEnabled(boolean enabled) {
if (mWaitingForTerminalState) {
if (DEBUG) Log.d(TAG, "Ignoring setHotspotEnabled; waiting for terminal state.");
return;
}
if (enabled) {
mWaitingForTerminalState = true;
if (DEBUG) Log.d(TAG, "Starting tethering");
mTetheringManager.startTethering(new TetheringRequest.Builder(TETHERING_WIFI).build(),
ConcurrentUtils.DIRECT_EXECUTOR,
new TetheringManager.StartTetheringCallback() {
@Override
public void onTetheringFailed(final int result) {
if (DEBUG) Log.d(TAG, "onTetheringFailed");
maybeResetSoftApState();
fireHotspotChangedCallback();
}
});
} else {
mTetheringManager.stopTethering(ConnectivityManager.TETHERING_WIFI);
}
}
通过TetheringManager 调用TetherService–Tether
对于Tether管理代码目录
|