1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.telephony;
18
19import android.util.Log;
20import java.util.ArrayList;
21import java.util.concurrent.atomic.AtomicBoolean;
22import java.util.concurrent.atomic.AtomicInteger;
23
24/**
25 * Maintain the Apn context
26 */
27public class ApnContext {
28
29    public final String LOG_TAG;
30
31    protected static final boolean DBG = true;
32
33    private final String mApnType;
34
35    private DataConnectionTracker.State mState;
36
37    private ArrayList<ApnSetting> mWaitingApns = null;
38
39    /** A zero indicates that all waiting APNs had a permanent error */
40    private AtomicInteger mWaitingApnsPermanentFailureCountDown;
41
42    private ApnSetting mApnSetting;
43
44    DataConnection mDataConnection;
45
46    DataConnectionAc mDataConnectionAc;
47
48    String mReason;
49
50    /**
51     * user/app requested connection on this APN
52     */
53    AtomicBoolean mDataEnabled;
54
55    /**
56     * carrier requirements met
57     */
58    AtomicBoolean mDependencyMet;
59
60    public ApnContext(String apnType, String logTag) {
61        mApnType = apnType;
62        mState = DataConnectionTracker.State.IDLE;
63        setReason(Phone.REASON_DATA_ENABLED);
64        mDataEnabled = new AtomicBoolean(false);
65        mDependencyMet = new AtomicBoolean(true);
66        mWaitingApnsPermanentFailureCountDown = new AtomicInteger(0);
67        LOG_TAG = logTag;
68    }
69
70    public String getApnType() {
71        return mApnType;
72    }
73
74    public synchronized DataConnection getDataConnection() {
75        return mDataConnection;
76    }
77
78    public synchronized void setDataConnection(DataConnection dataConnection) {
79        mDataConnection = dataConnection;
80    }
81
82
83    public synchronized DataConnectionAc getDataConnectionAc() {
84        return mDataConnectionAc;
85    }
86
87    public synchronized void setDataConnectionAc(DataConnectionAc dcac) {
88        if (dcac != null) {
89            dcac.addApnContextSync(this);
90        } else {
91            if (mDataConnectionAc != null) mDataConnectionAc.removeApnContextSync(this);
92        }
93        mDataConnectionAc = dcac;
94    }
95
96    public synchronized ApnSetting getApnSetting() {
97        return mApnSetting;
98    }
99
100    public synchronized void setApnSetting(ApnSetting apnSetting) {
101        mApnSetting = apnSetting;
102    }
103
104    public synchronized void setWaitingApns(ArrayList<ApnSetting> waitingApns) {
105        mWaitingApns = waitingApns;
106        mWaitingApnsPermanentFailureCountDown.set(mWaitingApns.size());
107    }
108
109    public int getWaitingApnsPermFailCount() {
110        return mWaitingApnsPermanentFailureCountDown.get();
111    }
112
113    public void decWaitingApnsPermFailCount() {
114        mWaitingApnsPermanentFailureCountDown.decrementAndGet();
115    }
116
117    public synchronized ApnSetting getNextWaitingApn() {
118        ArrayList<ApnSetting> list = mWaitingApns;
119        ApnSetting apn = null;
120
121        if (list != null) {
122            if (!list.isEmpty()) {
123                apn = list.get(0);
124            }
125        }
126        return apn;
127    }
128
129    public synchronized void removeNextWaitingApn() {
130        if ((mWaitingApns != null) && (!mWaitingApns.isEmpty())) {
131            mWaitingApns.remove(0);
132        }
133    }
134
135    public synchronized ArrayList<ApnSetting> getWaitingApns() {
136        return mWaitingApns;
137    }
138
139    public synchronized void setState(DataConnectionTracker.State s) {
140        if (DBG) {
141            log("setState: " + s + " for type " + mApnType + ", previous state:" + mState);
142        }
143
144        mState = s;
145
146        if (mState == DataConnectionTracker.State.FAILED) {
147            if (mWaitingApns != null) {
148                mWaitingApns.clear(); // when teardown the connection and set to IDLE
149            }
150        }
151    }
152
153    public synchronized DataConnectionTracker.State getState() {
154        return mState;
155    }
156
157    public boolean isDisconnected() {
158        DataConnectionTracker.State currentState = getState();
159        return ((currentState == DataConnectionTracker.State.IDLE) ||
160                    currentState == DataConnectionTracker.State.FAILED);
161    }
162
163    public synchronized void setReason(String reason) {
164        if (DBG) {
165            log("set reason as " + reason + ", for type " + mApnType + ",current state " + mState);
166        }
167        mReason = reason;
168    }
169
170    public synchronized String getReason() {
171        return mReason;
172    }
173
174    public boolean isReady() {
175        return mDataEnabled.get() && mDependencyMet.get();
176    }
177
178    public void setEnabled(boolean enabled) {
179        if (DBG) {
180            log("set enabled as " + enabled + ", for type " +
181                    mApnType + ", current state is " + mDataEnabled.get());
182        }
183        mDataEnabled.set(enabled);
184    }
185
186    public boolean isEnabled() {
187        return mDataEnabled.get();
188    }
189
190    public void setDependencyMet(boolean met) {
191        if (DBG) {
192            log("set mDependencyMet as " + met + ", for type " + mApnType +
193                    ", current state is " + mDependencyMet.get());
194        }
195        mDependencyMet.set(met);
196    }
197
198    public boolean getDependencyMet() {
199       return mDependencyMet.get();
200    }
201
202    @Override
203    public String toString() {
204        return "state=" + getState() + " apnType=" + mApnType;
205    }
206
207    protected void log(String s) {
208        Log.d(LOG_TAG, "[ApnContext] " + s);
209    }
210}
211