1bda761320929f714951c328bfec6a51a1978db97Wink Saville/*
2bda761320929f714951c328bfec6a51a1978db97Wink Saville * Copyright (C) 2014 The Android Open Source Project
3bda761320929f714951c328bfec6a51a1978db97Wink Saville *
4bda761320929f714951c328bfec6a51a1978db97Wink Saville * Licensed under the Apache License, Version 2.0 (the "License");
5bda761320929f714951c328bfec6a51a1978db97Wink Saville * you may not use this file except in compliance with the License.
6bda761320929f714951c328bfec6a51a1978db97Wink Saville * You may obtain a copy of the License at
7bda761320929f714951c328bfec6a51a1978db97Wink Saville *
8bda761320929f714951c328bfec6a51a1978db97Wink Saville *      http://www.apache.org/licenses/LICENSE-2.0
9bda761320929f714951c328bfec6a51a1978db97Wink Saville *
10bda761320929f714951c328bfec6a51a1978db97Wink Saville * Unless required by applicable law or agreed to in writing, software
11bda761320929f714951c328bfec6a51a1978db97Wink Saville * distributed under the License is distributed on an "AS IS" BASIS,
12bda761320929f714951c328bfec6a51a1978db97Wink Saville * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bda761320929f714951c328bfec6a51a1978db97Wink Saville * See the License for the specific language governing permissions and
14bda761320929f714951c328bfec6a51a1978db97Wink Saville * limitations under the License.
15bda761320929f714951c328bfec6a51a1978db97Wink Saville */
16bda761320929f714951c328bfec6a51a1978db97Wink Saville
17bda761320929f714951c328bfec6a51a1978db97Wink Savillepackage com.android.internal.telephony.dataconnection;
18bda761320929f714951c328bfec6a51a1978db97Wink Saville
19bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.util.AsyncChannel;
20bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.util.Protocol;
21bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.util.State;
22bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.util.StateMachine;
23bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.telephony.Phone;
24bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.telephony.PhoneConstants;
25bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.telephony.PhoneBase;
26bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.telephony.PhoneProxy;
27bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport com.android.internal.telephony.dataconnection.DcSwitchAsyncChannel.RequestInfo;
28bda761320929f714951c328bfec6a51a1978db97Wink Saville
29bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport android.os.Message;
30bda761320929f714951c328bfec6a51a1978db97Wink Savilleimport android.telephony.Rlog;
31bda761320929f714951c328bfec6a51a1978db97Wink Saville
32bda761320929f714951c328bfec6a51a1978db97Wink Savillepublic class DcSwitchStateMachine extends StateMachine {
33bda761320929f714951c328bfec6a51a1978db97Wink Saville    private static final boolean DBG = true;
34bda761320929f714951c328bfec6a51a1978db97Wink Saville    private static final boolean VDBG = false;
35bda761320929f714951c328bfec6a51a1978db97Wink Saville    private static final String LOG_TAG = "DcSwitchSM";
36bda761320929f714951c328bfec6a51a1978db97Wink Saville
37bda761320929f714951c328bfec6a51a1978db97Wink Saville    // ***** Event codes for driving the state machine
38bda761320929f714951c328bfec6a51a1978db97Wink Saville    private static final int BASE = Protocol.BASE_DATA_CONNECTION_TRACKER + 0x00001000;
39bda761320929f714951c328bfec6a51a1978db97Wink Saville    private static final int EVENT_CONNECTED = BASE + 0;
40bda761320929f714951c328bfec6a51a1978db97Wink Saville
41bda761320929f714951c328bfec6a51a1978db97Wink Saville    private int mId;
42bda761320929f714951c328bfec6a51a1978db97Wink Saville    private Phone mPhone;
43bda761320929f714951c328bfec6a51a1978db97Wink Saville    private AsyncChannel mAc;
44bda761320929f714951c328bfec6a51a1978db97Wink Saville
45bda761320929f714951c328bfec6a51a1978db97Wink Saville    private IdleState mIdleState = new IdleState();
46bda761320929f714951c328bfec6a51a1978db97Wink Saville    private AttachingState mAttachingState = new AttachingState();
47bda761320929f714951c328bfec6a51a1978db97Wink Saville    private AttachedState mAttachedState = new AttachedState();
48bda761320929f714951c328bfec6a51a1978db97Wink Saville    private DetachingState mDetachingState = new DetachingState();
49bda761320929f714951c328bfec6a51a1978db97Wink Saville    private DefaultState mDefaultState = new DefaultState();
50bda761320929f714951c328bfec6a51a1978db97Wink Saville
51bda761320929f714951c328bfec6a51a1978db97Wink Saville    protected DcSwitchStateMachine(Phone phone, String name, int id) {
52bda761320929f714951c328bfec6a51a1978db97Wink Saville        super(name);
53bda761320929f714951c328bfec6a51a1978db97Wink Saville        if (DBG) log("DcSwitchState constructor E");
54bda761320929f714951c328bfec6a51a1978db97Wink Saville        mPhone = phone;
55bda761320929f714951c328bfec6a51a1978db97Wink Saville        mId = id;
56bda761320929f714951c328bfec6a51a1978db97Wink Saville
57bda761320929f714951c328bfec6a51a1978db97Wink Saville        addState(mDefaultState);
58bda761320929f714951c328bfec6a51a1978db97Wink Saville        addState(mIdleState, mDefaultState);
59bda761320929f714951c328bfec6a51a1978db97Wink Saville        addState(mAttachingState, mDefaultState);
60bda761320929f714951c328bfec6a51a1978db97Wink Saville        addState(mAttachedState, mDefaultState);
61bda761320929f714951c328bfec6a51a1978db97Wink Saville        addState(mDetachingState, mDefaultState);
62bda761320929f714951c328bfec6a51a1978db97Wink Saville        setInitialState(mIdleState);
63bda761320929f714951c328bfec6a51a1978db97Wink Saville        if (DBG) log("DcSwitchState constructor X");
64bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
65bda761320929f714951c328bfec6a51a1978db97Wink Saville
66bda761320929f714951c328bfec6a51a1978db97Wink Saville//    public void notifyDataConnection(int phoneId, String state, String reason,
67bda761320929f714951c328bfec6a51a1978db97Wink Saville//            String apnName, String apnType, boolean unavailable) {
68bda761320929f714951c328bfec6a51a1978db97Wink Saville//        if (phoneId == mId &&
69bda761320929f714951c328bfec6a51a1978db97Wink Saville//                TextUtils.equals(state, PhoneConstants.DataState.CONNECTED.toString())) {
70bda761320929f714951c328bfec6a51a1978db97Wink Saville//            sendMessage(obtainMessage(EVENT_CONNECTED));
71bda761320929f714951c328bfec6a51a1978db97Wink Saville//        }
72bda761320929f714951c328bfec6a51a1978db97Wink Saville//    }
73bda761320929f714951c328bfec6a51a1978db97Wink Saville
74bda761320929f714951c328bfec6a51a1978db97Wink Saville    private class IdleState extends State {
75bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
76bda761320929f714951c328bfec6a51a1978db97Wink Saville        public void enter() {
77bda761320929f714951c328bfec6a51a1978db97Wink Saville            if (DBG) log("IdleState: enter");
78bda761320929f714951c328bfec6a51a1978db97Wink Saville
79bda761320929f714951c328bfec6a51a1978db97Wink Saville            try {
80bda761320929f714951c328bfec6a51a1978db97Wink Saville                DctController.getInstance().processRequests();
81bda761320929f714951c328bfec6a51a1978db97Wink Saville            } catch (RuntimeException e) {
82bda761320929f714951c328bfec6a51a1978db97Wink Saville                if (DBG) loge("DctController is not ready");
83bda761320929f714951c328bfec6a51a1978db97Wink Saville            }
84bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
85bda761320929f714951c328bfec6a51a1978db97Wink Saville
86bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
87bda761320929f714951c328bfec6a51a1978db97Wink Saville        public boolean processMessage(Message msg) {
88bda761320929f714951c328bfec6a51a1978db97Wink Saville            boolean retVal;
89bda761320929f714951c328bfec6a51a1978db97Wink Saville
90bda761320929f714951c328bfec6a51a1978db97Wink Saville            switch (msg.what) {
91bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_CONNECT: {
92bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
93bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("IdleState: REQ_CONNECT");
94bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
95bda761320929f714951c328bfec6a51a1978db97Wink Saville
96bda761320929f714951c328bfec6a51a1978db97Wink Saville                    PhoneBase pb = (PhoneBase)((PhoneProxy)mPhone).getActivePhone();
97bda761320929f714951c328bfec6a51a1978db97Wink Saville                    pb.mCi.setDataAllowed(true, null);
98bda761320929f714951c328bfec6a51a1978db97Wink Saville
99bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_CONNECT,
100bda761320929f714951c328bfec6a51a1978db97Wink Saville                            PhoneConstants.APN_REQUEST_STARTED);
101bda761320929f714951c328bfec6a51a1978db97Wink Saville
102bda761320929f714951c328bfec6a51a1978db97Wink Saville                    transitionTo(mAttachingState);
103bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
104bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
105bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
106bda761320929f714951c328bfec6a51a1978db97Wink Saville
107bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.EVENT_DATA_ATTACHED:
108bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
109bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachingState: EVENT_DATA_ATTACHED");
110bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
111bda761320929f714951c328bfec6a51a1978db97Wink Saville                    transitionTo(mAttachedState);
112bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
113bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
114bda761320929f714951c328bfec6a51a1978db97Wink Saville
115bda761320929f714951c328bfec6a51a1978db97Wink Saville                case EVENT_CONNECTED: {
116bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
117bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("IdleState: Receive invalid event EVENT_CONNECTED!");
118bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
119bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
120bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
121bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
122bda761320929f714951c328bfec6a51a1978db97Wink Saville                default:
123bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) {
124bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("IdleState: nothandled msg.what=0x" +
125bda761320929f714951c328bfec6a51a1978db97Wink Saville                                Integer.toHexString(msg.what));
126bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
127bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = NOT_HANDLED;
128bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
129bda761320929f714951c328bfec6a51a1978db97Wink Saville            }
130bda761320929f714951c328bfec6a51a1978db97Wink Saville            return retVal;
131bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
132bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
133bda761320929f714951c328bfec6a51a1978db97Wink Saville
134bda761320929f714951c328bfec6a51a1978db97Wink Saville    private class AttachingState extends State {
135bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
136bda761320929f714951c328bfec6a51a1978db97Wink Saville        public void enter() {
137bda761320929f714951c328bfec6a51a1978db97Wink Saville            log("AttachingState: enter");
138bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
139bda761320929f714951c328bfec6a51a1978db97Wink Saville
140bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
141bda761320929f714951c328bfec6a51a1978db97Wink Saville        public boolean processMessage(Message msg) {
142bda761320929f714951c328bfec6a51a1978db97Wink Saville            boolean retVal;
143bda761320929f714951c328bfec6a51a1978db97Wink Saville
144bda761320929f714951c328bfec6a51a1978db97Wink Saville            switch (msg.what) {
145bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_CONNECT: {
146bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
147bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachingState: REQ_CONNECT");
148bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
149bda761320929f714951c328bfec6a51a1978db97Wink Saville
1501392c6c61aaff0f52f8d21423956aef6fa80f46bLegler Wu                    PhoneBase pb = (PhoneBase) ((PhoneProxy) mPhone).getActivePhone();
1511392c6c61aaff0f52f8d21423956aef6fa80f46bLegler Wu                    pb.mCi.setDataAllowed(true, null);
1521392c6c61aaff0f52f8d21423956aef6fa80f46bLegler Wu
153bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_CONNECT,
154bda761320929f714951c328bfec6a51a1978db97Wink Saville                            PhoneConstants.APN_REQUEST_STARTED);
155bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
156bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
157bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
158bda761320929f714951c328bfec6a51a1978db97Wink Saville
159bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.EVENT_DATA_ATTACHED:
160bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
161bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachingState: EVENT_DATA_ATTACHED");
162bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
163bda761320929f714951c328bfec6a51a1978db97Wink Saville                    transitionTo(mAttachedState);
164bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
165bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
166bda761320929f714951c328bfec6a51a1978db97Wink Saville
167bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_DISCONNECT_ALL: {
168bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
169bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachingState: REQ_DISCONNECT_ALL" );
170bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
171bda761320929f714951c328bfec6a51a1978db97Wink Saville                    DctController.getInstance().releaseAllRequests(mId);
172bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_DISCONNECT_ALL,
173bda761320929f714951c328bfec6a51a1978db97Wink Saville                            PhoneConstants.APN_REQUEST_STARTED);
174bda761320929f714951c328bfec6a51a1978db97Wink Saville
175bda761320929f714951c328bfec6a51a1978db97Wink Saville                    transitionTo(mDetachingState);
176bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
177bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
178bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
179bda761320929f714951c328bfec6a51a1978db97Wink Saville
180bda761320929f714951c328bfec6a51a1978db97Wink Saville                default:
181bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) {
182bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachingState: nothandled msg.what=0x" +
183bda761320929f714951c328bfec6a51a1978db97Wink Saville                                Integer.toHexString(msg.what));
184bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
185bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = NOT_HANDLED;
186bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
187bda761320929f714951c328bfec6a51a1978db97Wink Saville            }
188bda761320929f714951c328bfec6a51a1978db97Wink Saville            return retVal;
189bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
190bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
191bda761320929f714951c328bfec6a51a1978db97Wink Saville
192bda761320929f714951c328bfec6a51a1978db97Wink Saville    private class AttachedState extends State {
193bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
194bda761320929f714951c328bfec6a51a1978db97Wink Saville        public void enter() {
195bda761320929f714951c328bfec6a51a1978db97Wink Saville            if (DBG) log("AttachedState: enter");
196bda761320929f714951c328bfec6a51a1978db97Wink Saville            //When enter attached state, we need exeute all requests.
197bda761320929f714951c328bfec6a51a1978db97Wink Saville            DctController.getInstance().executeAllRequests(mId);
198bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
199bda761320929f714951c328bfec6a51a1978db97Wink Saville
200bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
201bda761320929f714951c328bfec6a51a1978db97Wink Saville        public boolean processMessage(Message msg) {
202bda761320929f714951c328bfec6a51a1978db97Wink Saville            boolean retVal;
203bda761320929f714951c328bfec6a51a1978db97Wink Saville
204bda761320929f714951c328bfec6a51a1978db97Wink Saville            switch (msg.what) {
205bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_CONNECT: {
206bda761320929f714951c328bfec6a51a1978db97Wink Saville                    RequestInfo apnRequest = (RequestInfo)msg.obj;
207bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
208bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachedState: REQ_CONNECT, apnRequest=" + apnRequest);
209bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
210bda761320929f714951c328bfec6a51a1978db97Wink Saville
211bda761320929f714951c328bfec6a51a1978db97Wink Saville                    DctController.getInstance().executeRequest(apnRequest);
212bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_CONNECT,
213bda761320929f714951c328bfec6a51a1978db97Wink Saville                            PhoneConstants.APN_REQUEST_STARTED);
214bda761320929f714951c328bfec6a51a1978db97Wink Saville
215bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
216bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
217bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
218bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_DISCONNECT: {
219bda761320929f714951c328bfec6a51a1978db97Wink Saville                    RequestInfo apnRequest = (RequestInfo)msg.obj;
220bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
221bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachedState: REQ_DISCONNECT apnRequest=" + apnRequest);
222bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
223bda761320929f714951c328bfec6a51a1978db97Wink Saville
224bda761320929f714951c328bfec6a51a1978db97Wink Saville                    DctController.getInstance().releaseRequest(apnRequest);
225bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_CONNECT,
226bda761320929f714951c328bfec6a51a1978db97Wink Saville                            PhoneConstants.APN_REQUEST_STARTED);
227bda761320929f714951c328bfec6a51a1978db97Wink Saville
228bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
229bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
230bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
231bda761320929f714951c328bfec6a51a1978db97Wink Saville
232bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_DISCONNECT_ALL: {
233bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
234bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachedState: REQ_DISCONNECT_ALL" );
235bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
236bda761320929f714951c328bfec6a51a1978db97Wink Saville                    DctController.getInstance().releaseAllRequests(mId);
237bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_DISCONNECT_ALL,
238bda761320929f714951c328bfec6a51a1978db97Wink Saville                            PhoneConstants.APN_REQUEST_STARTED);
239bda761320929f714951c328bfec6a51a1978db97Wink Saville
240bda761320929f714951c328bfec6a51a1978db97Wink Saville                    transitionTo(mDetachingState);
241bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
242bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
243bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
244bda761320929f714951c328bfec6a51a1978db97Wink Saville
245bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.EVENT_DATA_DETACHED: {
246bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
247bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachedState: EVENT_DATA_DETACHED");
248bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
249bda761320929f714951c328bfec6a51a1978db97Wink Saville                    transitionTo(mAttachingState);
250bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
251bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
252bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
253bda761320929f714951c328bfec6a51a1978db97Wink Saville
254bda761320929f714951c328bfec6a51a1978db97Wink Saville                default:
255bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) {
256bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("AttachedState: nothandled msg.what=0x" +
257bda761320929f714951c328bfec6a51a1978db97Wink Saville                                Integer.toHexString(msg.what));
258bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
259bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = NOT_HANDLED;
260bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
261bda761320929f714951c328bfec6a51a1978db97Wink Saville            }
262bda761320929f714951c328bfec6a51a1978db97Wink Saville            return retVal;
263bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
264bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
265bda761320929f714951c328bfec6a51a1978db97Wink Saville
266bda761320929f714951c328bfec6a51a1978db97Wink Saville    private class DetachingState extends State {
267bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
268bda761320929f714951c328bfec6a51a1978db97Wink Saville        public void enter() {
269bda761320929f714951c328bfec6a51a1978db97Wink Saville            if (DBG) log("DetachingState: enter");
270bda761320929f714951c328bfec6a51a1978db97Wink Saville            PhoneBase pb = (PhoneBase)((PhoneProxy)mPhone).getActivePhone();
271bda761320929f714951c328bfec6a51a1978db97Wink Saville            pb.mCi.setDataAllowed(false, obtainMessage(
272bda761320929f714951c328bfec6a51a1978db97Wink Saville                    DcSwitchAsyncChannel.EVENT_DATA_DETACHED));
273bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
274bda761320929f714951c328bfec6a51a1978db97Wink Saville
275bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
276bda761320929f714951c328bfec6a51a1978db97Wink Saville        public boolean processMessage(Message msg) {
277bda761320929f714951c328bfec6a51a1978db97Wink Saville            boolean retVal;
278bda761320929f714951c328bfec6a51a1978db97Wink Saville
279bda761320929f714951c328bfec6a51a1978db97Wink Saville            switch (msg.what) {
280bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.EVENT_DATA_DETACHED: {
281bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
282bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("DetachingState: EVENT_DATA_DETACHED");
283bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
284bda761320929f714951c328bfec6a51a1978db97Wink Saville                    transitionTo(mIdleState);
285bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
286bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
287bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
288bda761320929f714951c328bfec6a51a1978db97Wink Saville
289bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_DISCONNECT_ALL: {
290bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
291bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("DetachingState: REQ_DISCONNECT_ALL, already detaching" );
292bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
293bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_DISCONNECT_ALL,
294bda761320929f714951c328bfec6a51a1978db97Wink Saville                            PhoneConstants.APN_REQUEST_STARTED);
295bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = HANDLED;
296bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
297bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
298bda761320929f714951c328bfec6a51a1978db97Wink Saville
299bda761320929f714951c328bfec6a51a1978db97Wink Saville                default:
300bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) {
301bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("DetachingState: nothandled msg.what=0x" +
302bda761320929f714951c328bfec6a51a1978db97Wink Saville                                Integer.toHexString(msg.what));
303bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
304bda761320929f714951c328bfec6a51a1978db97Wink Saville                    retVal = NOT_HANDLED;
305bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
306bda761320929f714951c328bfec6a51a1978db97Wink Saville            }
307bda761320929f714951c328bfec6a51a1978db97Wink Saville            return retVal;
308bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
309bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
310bda761320929f714951c328bfec6a51a1978db97Wink Saville
311bda761320929f714951c328bfec6a51a1978db97Wink Saville    private class DefaultState extends State {
312bda761320929f714951c328bfec6a51a1978db97Wink Saville        @Override
313bda761320929f714951c328bfec6a51a1978db97Wink Saville        public boolean processMessage(Message msg) {
314bda761320929f714951c328bfec6a51a1978db97Wink Saville            switch (msg.what) {
315bda761320929f714951c328bfec6a51a1978db97Wink Saville                case AsyncChannel.CMD_CHANNEL_FULL_CONNECTION: {
316bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (mAc != null) {
317bda761320929f714951c328bfec6a51a1978db97Wink Saville                        if (VDBG) log("Disconnecting to previous connection mAc=" + mAc);
318bda761320929f714951c328bfec6a51a1978db97Wink Saville                        mAc.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
319bda761320929f714951c328bfec6a51a1978db97Wink Saville                                AsyncChannel.STATUS_FULL_CONNECTION_REFUSED_ALREADY_CONNECTED);
320bda761320929f714951c328bfec6a51a1978db97Wink Saville                    } else {
321bda761320929f714951c328bfec6a51a1978db97Wink Saville                        mAc = new AsyncChannel();
322bda761320929f714951c328bfec6a51a1978db97Wink Saville                        mAc.connected(null, getHandler(), msg.replyTo);
323bda761320929f714951c328bfec6a51a1978db97Wink Saville                        if (VDBG) log("DcDefaultState: FULL_CONNECTION reply connected");
324bda761320929f714951c328bfec6a51a1978db97Wink Saville                        mAc.replyToMessage(msg, AsyncChannel.CMD_CHANNEL_FULLY_CONNECTED,
325bda761320929f714951c328bfec6a51a1978db97Wink Saville                                AsyncChannel.STATUS_SUCCESSFUL, mId, "hi");
326bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
327bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
328bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
329bda761320929f714951c328bfec6a51a1978db97Wink Saville                case AsyncChannel.CMD_CHANNEL_DISCONNECT: {
330bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) log("CMD_CHANNEL_DISCONNECT");
331bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.disconnect();
332bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
333bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
334bda761320929f714951c328bfec6a51a1978db97Wink Saville                case AsyncChannel.CMD_CHANNEL_DISCONNECTED: {
335bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) log("CMD_CHANNEL_DISCONNECTED");
336bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc = null;
337bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
338bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
339bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_IS_IDLE_STATE: {
340bda761320929f714951c328bfec6a51a1978db97Wink Saville                    boolean val = getCurrentState() == mIdleState;
341bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) log("REQ_IS_IDLE_STATE  isIdle=" + val);
342bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg, DcSwitchAsyncChannel.RSP_IS_IDLE_STATE, val ? 1 : 0);
343bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
344bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
345bda761320929f714951c328bfec6a51a1978db97Wink Saville                case DcSwitchAsyncChannel.REQ_IS_IDLE_OR_DETACHING_STATE: {
346bda761320929f714951c328bfec6a51a1978db97Wink Saville                    boolean val = (getCurrentState() == mIdleState ||
347bda761320929f714951c328bfec6a51a1978db97Wink Saville                            getCurrentState() == mDetachingState);
348bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (VDBG) log("REQ_IS_IDLE_OR_DETACHING_STATE  isIdleDetaching=" + val);
349bda761320929f714951c328bfec6a51a1978db97Wink Saville                    mAc.replyToMessage(msg,
350bda761320929f714951c328bfec6a51a1978db97Wink Saville                            DcSwitchAsyncChannel.RSP_IS_IDLE_OR_DETACHING_STATE, val ? 1 : 0);
351bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
352bda761320929f714951c328bfec6a51a1978db97Wink Saville                }
353bda761320929f714951c328bfec6a51a1978db97Wink Saville                default:
354bda761320929f714951c328bfec6a51a1978db97Wink Saville                    if (DBG) {
355bda761320929f714951c328bfec6a51a1978db97Wink Saville                        log("DefaultState: shouldn't happen but ignore msg.what=0x" +
356bda761320929f714951c328bfec6a51a1978db97Wink Saville                                Integer.toHexString(msg.what));
357bda761320929f714951c328bfec6a51a1978db97Wink Saville                    }
358bda761320929f714951c328bfec6a51a1978db97Wink Saville                    break;
359bda761320929f714951c328bfec6a51a1978db97Wink Saville            }
360bda761320929f714951c328bfec6a51a1978db97Wink Saville            return HANDLED;
361bda761320929f714951c328bfec6a51a1978db97Wink Saville        }
362bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
363bda761320929f714951c328bfec6a51a1978db97Wink Saville
364bda761320929f714951c328bfec6a51a1978db97Wink Saville    @Override
365bda761320929f714951c328bfec6a51a1978db97Wink Saville    protected void log(String s) {
366bda761320929f714951c328bfec6a51a1978db97Wink Saville        Rlog.d(LOG_TAG, "[" + getName() + "] " + s);
367bda761320929f714951c328bfec6a51a1978db97Wink Saville    }
368bda761320929f714951c328bfec6a51a1978db97Wink Saville}
369