Home | History | Annotate | Download | only in telephony
      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 
     17 package com.android.internal.telephony;
     18 
     19 import android.util.Log;
     20 
     21 import java.io.FileDescriptor;
     22 import java.io.PrintWriter;
     23 import java.util.ArrayList;
     24 import java.util.concurrent.atomic.AtomicBoolean;
     25 import java.util.concurrent.atomic.AtomicInteger;
     26 
     27 /**
     28  * Maintain the Apn context
     29  */
     30 public class ApnContext {
     31 
     32     public final String LOG_TAG;
     33 
     34     protected static final boolean DBG = true;
     35 
     36     private final String mApnType;
     37 
     38     private DataConnectionTracker.State mState;
     39 
     40     private ArrayList<ApnSetting> mWaitingApns = null;
     41 
     42     /** A zero indicates that all waiting APNs had a permanent error */
     43     private AtomicInteger mWaitingApnsPermanentFailureCountDown;
     44 
     45     private ApnSetting mApnSetting;
     46 
     47     DataConnection mDataConnection;
     48 
     49     DataConnectionAc mDataConnectionAc;
     50 
     51     String mReason;
     52 
     53     /**
     54      * user/app requested connection on this APN
     55      */
     56     AtomicBoolean mDataEnabled;
     57 
     58     /**
     59      * carrier requirements met
     60      */
     61     AtomicBoolean mDependencyMet;
     62 
     63     public ApnContext(String apnType, String logTag) {
     64         mApnType = apnType;
     65         mState = DataConnectionTracker.State.IDLE;
     66         setReason(Phone.REASON_DATA_ENABLED);
     67         mDataEnabled = new AtomicBoolean(false);
     68         mDependencyMet = new AtomicBoolean(true);
     69         mWaitingApnsPermanentFailureCountDown = new AtomicInteger(0);
     70         LOG_TAG = logTag;
     71     }
     72 
     73     public String getApnType() {
     74         return mApnType;
     75     }
     76 
     77     public synchronized DataConnection getDataConnection() {
     78         return mDataConnection;
     79     }
     80 
     81     public synchronized void setDataConnection(DataConnection dc) {
     82         if (DBG) {
     83             log("setDataConnection: old dc=" + mDataConnection + " new dc=" + dc + " this=" + this);
     84         }
     85         mDataConnection = dc;
     86     }
     87 
     88 
     89     public synchronized DataConnectionAc getDataConnectionAc() {
     90         return mDataConnectionAc;
     91     }
     92 
     93     public synchronized void setDataConnectionAc(DataConnectionAc dcac) {
     94         if (DBG) {
     95             log("setDataConnectionAc: old dcac=" + mDataConnectionAc + " new dcac=" + dcac);
     96         }
     97         if (dcac != null) {
     98             dcac.addApnContextSync(this);
     99         } else {
    100             if (mDataConnectionAc != null) {
    101                 mDataConnectionAc.removeApnContextSync(this);
    102             }
    103         }
    104         mDataConnectionAc = dcac;
    105     }
    106 
    107     public synchronized ApnSetting getApnSetting() {
    108         return mApnSetting;
    109     }
    110 
    111     public synchronized void setApnSetting(ApnSetting apnSetting) {
    112         mApnSetting = apnSetting;
    113     }
    114 
    115     public synchronized void setWaitingApns(ArrayList<ApnSetting> waitingApns) {
    116         mWaitingApns = waitingApns;
    117         mWaitingApnsPermanentFailureCountDown.set(mWaitingApns.size());
    118     }
    119 
    120     public int getWaitingApnsPermFailCount() {
    121         return mWaitingApnsPermanentFailureCountDown.get();
    122     }
    123 
    124     public void decWaitingApnsPermFailCount() {
    125         mWaitingApnsPermanentFailureCountDown.decrementAndGet();
    126     }
    127 
    128     public synchronized ApnSetting getNextWaitingApn() {
    129         ArrayList<ApnSetting> list = mWaitingApns;
    130         ApnSetting apn = null;
    131 
    132         if (list != null) {
    133             if (!list.isEmpty()) {
    134                 apn = list.get(0);
    135             }
    136         }
    137         return apn;
    138     }
    139 
    140     public synchronized void removeWaitingApn(ApnSetting apn) {
    141         if (mWaitingApns != null) {
    142             mWaitingApns.remove(apn);
    143         }
    144     }
    145 
    146     public synchronized ArrayList<ApnSetting> getWaitingApns() {
    147         return mWaitingApns;
    148     }
    149 
    150     public synchronized void setState(DataConnectionTracker.State s) {
    151         if (DBG) {
    152             log("setState: " + s + ", previous state:" + mState);
    153         }
    154 
    155         mState = s;
    156 
    157         if (mState == DataConnectionTracker.State.FAILED) {
    158             if (mWaitingApns != null) {
    159                 mWaitingApns.clear(); // when teardown the connection and set to IDLE
    160             }
    161         }
    162     }
    163 
    164     public synchronized DataConnectionTracker.State getState() {
    165         return mState;
    166     }
    167 
    168     public boolean isDisconnected() {
    169         DataConnectionTracker.State currentState = getState();
    170         return ((currentState == DataConnectionTracker.State.IDLE) ||
    171                     currentState == DataConnectionTracker.State.FAILED);
    172     }
    173 
    174     public synchronized void setReason(String reason) {
    175         if (DBG) {
    176             log("set reason as " + reason + ",current state " + mState);
    177         }
    178         mReason = reason;
    179     }
    180 
    181     public synchronized String getReason() {
    182         return mReason;
    183     }
    184 
    185     public boolean isReady() {
    186         return mDataEnabled.get() && mDependencyMet.get();
    187     }
    188 
    189     public void setEnabled(boolean enabled) {
    190         if (DBG) {
    191             log("set enabled as " + enabled + ", current state is " + mDataEnabled.get());
    192         }
    193         mDataEnabled.set(enabled);
    194     }
    195 
    196     public boolean isEnabled() {
    197         return mDataEnabled.get();
    198     }
    199 
    200     public void setDependencyMet(boolean met) {
    201         if (DBG) {
    202             log("set mDependencyMet as " + met + " current state is " + mDependencyMet.get());
    203         }
    204         mDependencyMet.set(met);
    205     }
    206 
    207     public boolean getDependencyMet() {
    208        return mDependencyMet.get();
    209     }
    210 
    211     @Override
    212     public String toString() {
    213         // We don't print mDataConnection because its recursive.
    214         return "{mApnType=" + mApnType + " mState=" + getState() + " mWaitingApns=" + mWaitingApns +
    215                 " mWaitingApnsPermanentFailureCountDown=" + mWaitingApnsPermanentFailureCountDown +
    216                 " mApnSetting=" + mApnSetting + " mDataConnectionAc=" + mDataConnectionAc +
    217                 " mReason=" + mReason + " mDataEnabled=" + mDataEnabled +
    218                 " mDependencyMet=" + mDependencyMet + "}";
    219     }
    220 
    221     protected void log(String s) {
    222         Log.d(LOG_TAG, "[ApnContext:" + mApnType + "] " + s);
    223     }
    224 
    225     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
    226         pw.println("ApnContext: " + this.toString());
    227     }
    228 }
    229