• TetheringService 启动流程


    SystemServer.java

    private static final String TETHERING_CONNECTOR_CLASS = "android.net.ITetheringConnector";
    ...
                t.traceBegin("StartTethering");
                try {
                    // TODO: hide implementation details, b/146312721.
                    ConnectivityModuleConnector.getInstance().startModuleService(
                            TETHERING_CONNECTOR_CLASS,
                            PERMISSION_MAINLINE_NETWORK_STACK, service -> {
                                ServiceManager.addService(Context.TETHERING_SERVICE, service,
                                        false /* allowIsolated */,
                                        DUMP_FLAG_PRIORITY_HIGH | DUMP_FLAG_PRIORITY_NORMAL);
                            });
                } catch (Throwable e) {
                    reportWtf("starting Tethering", e);
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    开机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();
    
            // Try to bind in-process if the device was shipped with an in-process version
            1.获取tetherservice name,传入参数inSystemProcess :true
            Intent intent = mDeps.getModuleServiceIntent(pm, serviceIntentBaseAction,
                    servicePermissionName, true /* inSystemProcess */);
    
            // Otherwise use the updatable module version
            if (intent == null) {
                intent = mDeps.getModuleServiceIntent(pm, serviceIntentBaseAction,
                        servicePermissionName, false /* inSystemProcess */);
                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();
    
            // Start the network stack. The service will be added to the service manager by the
            // corresponding client in ModuleServiceCallback.onModuleServiceConnected().
            //启动TetherService 服务
            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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    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
                        		//2.如果参数inSystemProcess:true serviceIntentBaseAction 字串后面+“.InProcess”
                                ? 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;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    上面判断inSystemProcess添加不同的AndroidManifest.xml
    在这里插入图片描述
    区别在对于的报名不同
    com.android.networkstack.tethering
    com.android.networkstack.tethering.inprocess
    但是TetheringService.java是同一个。

    接下来,我们看看TetheringService 启动

        @Override
        public void onCreate() {
        	//初始化tether依赖关系
            final TetheringDependencies deps = makeTetheringDependencies();
            // The Tethering object needs a fully functional context to start, so this can't be done
            // in the constructor.
            //初始化tether 连接器
            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 /* onlyAllowPrivileged */,
                        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) { }
            }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    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);
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    通过TetheringManager 调用TetherService–Tether

    对于Tether管理代码目录
    在这里插入图片描述

  • 相关阅读:
    深入理解Java类加载机制,再也不用死记硬背了
    没域名也可以frp实现内网穿透 SSH,个人搭建内网穿透 7月6日
    选择排序.
    Feign远程调用
    30行代码做一个简易的抽奖系统(一)
    Android 8.0注册广播无效
    基于Material Design风格开源、易用、强大的WPF UI控件库
    [二叉树&单调栈/递归] LeetCode 654. 最大二叉树(笛卡尔树)
    java继承简介说明
    Java基础知识—数组
  • 原文地址:https://blog.csdn.net/wangjicong_215/article/details/126638534