android StateMachine.java

android 4.0.4: /frameworks/base/core/java/com/android/internal/util/StateMachine.java

StateMachine状态机中,会创建HandlerThread线程,和依附该线程的Handler;消息处理都是在HandlerThread中执行

framework层的好几个状态机,都是在StateMachine基础上实现的,比如:WifiStateMachine、DhcpStateMachine、WifiWatchdogStateMachine。。。

http://androidxref.com/4.0.4/xref/frameworks/base/core/java/com/android/internal/util/StateMachine.java

SmHandler:

       /**
         * Handle messages sent to the state machine by calling
         * the current state's processMessage. It also handles
         * the enter/exit calls and placing any deferred messages
         * back onto the queue when transitioning to a new state.
         */
        @Override
        public final void handleMessage(Message msg) {
            if (mDbg) Log.d(TAG, "handleMessage: E msg.what=" + msg.what);

            /** Save the current message */
            mMsg = msg;

            /**
             * Check that construction was completed
             */
            if (!mIsConstructionCompleted) {
                Log.e(TAG, "The start method not called, ignore msg: " + msg);//  在状态机没有启动之前,收到msg,只是打印error日志
                return;
            }


            /**
             * Process the message abiding by the hierarchical semantics
             * and perform any requested transitions.
             */
            processMsg(msg);
            performTransitions();

            if (mDbg) Log.d(TAG, "handleMessage: X");
        }


        /**
         * Complete the construction of the state machine.
         */
        private final void completeConstruction() {
            if (mDbg) Log.d(TAG, "completeConstruction: E");

            /**
             * Determine the maximum depth of the state hierarchy
             * so we can allocate the state stacks.
             */
            int maxDepth = 0;
            for (StateInfo si : mStateInfo.values()) {
                int depth = 0;
                for (StateInfo i = si; i != null; depth++) {
                    i = i.parentStateInfo;
                }
                if (maxDepth < depth) {
                    maxDepth = depth;
                }
            }
            if (mDbg) Log.d(TAG, "completeConstruction: maxDepth=" + maxDepth);

            mStateStack = new StateInfo[maxDepth];
            mTempStateStack = new StateInfo[maxDepth];
            setupInitialStateStack();

            /**
             * Construction is complete call all enter methods
             * starting at the first entry.
             */
            mIsConstructionCompleted = true;
            mMsg = obtainMessage(SM_INIT_CMD);
            invokeEnterMethods(0);// 初始状态的enter方法,在调用线程中执行;即如果在UI线程中,调用状态机的start()方法,则初始状态的enter方法,在UI线程中执行,如果enter方法中有耗时的操作,肯定会占用UI线程的资源

            /**
             * Perform any transitions requested by the enter methods
             */
            performTransitions();// 如果初始状态的enter方法中,有跳转,即调用了transitionTo方法,则初始状态的exit方法、新状态的enter方法,也都会在调用线程中执行,依次类推,也会进一步占用调用线程的资源

            if (mDbg) Log.d(TAG, "completeConstruction: X");
        }

StateMachine:

    /**
     * Start the state machine.
     */
    public void start() {
        // mSmHandler can be null if the state machine has quit.
        if (mSmHandler == null) return;

        /** Send the complete construction message */
        mSmHandler.completeConstruction();
    }

///////////////////////////////////////////////////////////////////////////////////////////////////////////     

android 4.1.1: /frameworks/base/core/java/com/android/internal/util/StateMachine.java

http://androidxref.com/4.1.1/xref/frameworks/base/core/java/com/android/internal/util/StateMachine.java

SmHandler:

        /**
         * Handle messages sent to the state machine by calling
         * the current state's processMessage. It also handles
         * the enter/exit calls and placing any deferred messages
         * back onto the queue when transitioning to a new state.
         */
        @Override
        public final void handleMessage(Message msg) {
            if (mDbg) Log.d(TAG, "handleMessage: E msg.what=" + msg.what);

            /** Save the current message */
            mMsg = msg;

            if (mIsConstructionCompleted) {
                /** Normal path */
                processMsg(msg);
            } else if (!mIsConstructionCompleted && (mMsg.what == SM_INIT_CMD) && (mMsg.obj == mSmHandlerObj)) {
                /** Initial one time path. */
                mIsConstructionCompleted = true;// 调用start方法,走该分支一次;后面处理消息,都走前面的分支processMsg;(在HandlerThread线程中执行)
                invokeEnterMethods(0);

            } else {
                throw new RuntimeException("StateMachine.handleMessage: " + "The start method not called, received msg: " + msg);//  这里也是修改点,状态机没有启动之前,即没有调用start()方法之前,在4.0.4上,是打印error日志;在4.1.1上,则是
            }
            performTransitions();

            if (mDbg) Log.d(TAG, "handleMessage: X");
        }


        /**
         * Complete the construction of the state machine.
         */
        private final void completeConstruction() {
            if (mDbg) Log.d(TAG, "completeConstruction: E");

            /**
             * Determine the maximum depth of the state hierarchy
             * so we can allocate the state stacks.
             */
            int maxDepth = 0;
            for (StateInfo si : mStateInfo.values()) {
                int depth = 0;
                for (StateInfo i = si; i != null; depth++) {
                    i = i.parentStateInfo;
                }
                if (maxDepth < depth) {
                    maxDepth = depth;
                }
            }
            if (mDbg) Log.d(TAG, "completeConstruction: maxDepth=" + maxDepth);

            mStateStack = new StateInfo[maxDepth];
            mTempStateStack = new StateInfo[maxDepth];
            setupInitialStateStack();

            /** Sending SM_INIT_CMD message to invoke enter methods asynchronously */
            sendMessageAtFrontOfQueue(obtainMessage(SM_INIT_CMD, mSmHandlerObj));// 不同于4.0.4,比如:在UI线程中,调用状态机的start方法,这里只是把SM_INIT_CMD消息发送给HandlerThread中的Handler处理,初始状态的enter、exit方法,都是在状态机的HandlerThread线程中执行的,不会占用UI线程的资源^-^算是一个小改进吧


            if (mDbg) Log.d(TAG, "completeConstruction: X");
        }

StateMachine:

    /**
     * Enqueue a message to the front of the queue for this state machine.
     * Protected, may only be called by instances of StateMachine.
     */
    protected final void sendMessageAtFrontOfQueue(int what, Object obj) {
        mSmHandler.sendMessageAtFrontOfQueue(obtainMessage(what, obj));
    }

03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: exception when invoking removeOnUidImportanceListener 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: java.lang.IllegalArgumentException: Listener not registered: com.android.bluetooth.le_scan.ScanManager$2@a5c7225 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at android.app.ActivityManager.removeOnUidImportanceListener(ActivityManager.java:4734) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.bluetooth.le_scan.ScanManager.cleanup(ScanManager.java:329) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.bluetooth.le_scan.TransitionalScanHelper.cleanup(TransitionalScanHelper.java:283) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.bluetooth.gatt.GattService.cleanup(GattService.java:407) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.bluetooth.btservice.AdapterService.stopGattProfileService(AdapterService.java:1284) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.bluetooth.btservice.AdapterService.bringDownBle(AdapterService.java:1173) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.bluetooth.btservice.AdapterState$TurningBleOffState.enter(AdapterState.java:430) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.internal.util.StateMachine$SmHandler.invokeEnterMethods(StateMachine.java:1042) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.internal.util.StateMachine$SmHandler.performTransitions(StateMachine.java:888) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at com.android.internal.util.StateMachine$SmHandler.handleMessage(StateMachine.java:828) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at android.os.Handler.dispatchMessage(Handler.java:112) 03-16 12:32:57.511 18324 18324 W BtGatt.ScanManager: at android.os.Looper.loopOnce(Looper.java:288) 帮我解决下这个报错
03-19
03-12 19:05:45.149904 29300 29389 W System.err: java.lang.SecurityException: Not allowed to start service Intent { act=onetrack.action.TRACK_EVENT flg=0x2 pkg=com.miui.analytics (has extras) } without permission com.miui.analytics.onetrack.TRACK_EVENT 03-12 19:05:45.149965 29300 29389 W System.err: at android.app.ContextImpl.startServiceCommon(ContextImpl.java:2041) 03-12 19:05:45.149971 29300 29389 W System.err: at android.app.ContextImpl.startService(ContextImpl.java:2002) 03-12 19:05:45.149992 29300 29389 W System.err: at android.content.ContextWrapper.startService(ContextWrapper.java:866) 03-12 19:05:45.150001 29300 29389 W System.err: at com.android.bluetooth.util.OneTrackInterfaceUtil.track(OneTrackInterfaceUtil.java:212) 03-12 19:05:45.150008 29300 29389 W System.err: at com.android.bluetooth.util.OneTrackInterfaceUtil.trackTwsHeadset(OneTrackInterfaceUtil.java:1380) 03-12 19:05:45.150013 29300 29389 W System.err: at com.android.bluetooth.hfp.HeadsetService.onConnectionStateChangedFromStateMachine(HeadsetService.java:3419) 03-12 19:05:45.150018 29300 29389 W System.err: at com.android.bluetooth.hfp.HeadsetStateMachine$HeadsetStateBase.broadcastConnectionState(HeadsetStateMachine.java:972) 03-12 19:05:45.150023 29300 29389 W System.err: at com.android.bluetooth.hfp.HeadsetStateMachine$HeadsetStateBase.broadcastStateTransitions(HeadsetStateMachine.java:954) 03-12 19:05:45.150030 29300 29389 W System.err: at com.android.bluetooth.hfp.HeadsetStateMachine$Connected.enter(HeadsetStateMachine.java:2035) 03-12 19:05:45.150035 29300 29389 W System.err: at com.android.bluetooth.x.com.android.internal.util.StateMachine$SmHandler.invokeEnterMethods(StateMachine.java:1043) 03-12 19:05:45.150040 29300 29389 W System.err: at com.android.bluetooth.x.com.android.internal.util.StateMachine$SmHandler.performTransitions(StateMachine.java:889) 03-12 19:05:45.150044 29300 29389 W System.err: at com.android.bluetooth.x.com.android.internal.util.StateMachine$SmHandler.handleMessage(StateMachine.java:829) 03-12 19:05:45.150050 29300 29389 W System.err: at android.os.Handler.dispatchMessage(Handler.java:109) 03-12 19:05:45.150056 29300 29389 W System.err: at android.os.Looper.loopOnce(Looper.java:249) 03-12 19:05:45.150061 29300 29389 W System.err: at android.os.Looper.loop(Looper.java:337) 03-12 19:05:45.150065 29300 29389 W System.err: at android.os.HandlerThread.run(HandlerThread.java:85) 03-12 19:05:45.150167 29300 29300 D SilenceDeviceManager: handleMessage: 11解释下这段日志
最新发布
04-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值