Home | History | Annotate | Download | only in telephony
      1 /*
      2  * Copyright (C) 2012 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 android.telephony;
     18 
     19 import android.annotation.IntDef;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 import java.lang.annotation.Retention;
     23 import java.lang.annotation.RetentionPolicy;
     24 
     25 /**
     26  * Immutable cell information from a point in time.
     27  */
     28 public abstract class CellInfo implements Parcelable {
     29 
     30     // Type fields for parceling
     31     /** @hide */
     32     protected static final int TYPE_GSM = 1;
     33     /** @hide */
     34     protected static final int TYPE_CDMA = 2;
     35     /** @hide */
     36     protected static final int TYPE_LTE = 3;
     37     /** @hide */
     38     protected static final int TYPE_WCDMA = 4;
     39 
     40     // Type to distinguish where time stamp gets recorded.
     41 
     42     /** @hide */
     43     public static final int TIMESTAMP_TYPE_UNKNOWN = 0;
     44     /** @hide */
     45     public static final int TIMESTAMP_TYPE_ANTENNA = 1;
     46     /** @hide */
     47     public static final int TIMESTAMP_TYPE_MODEM = 2;
     48     /** @hide */
     49     public static final int TIMESTAMP_TYPE_OEM_RIL = 3;
     50     /** @hide */
     51     public static final int TIMESTAMP_TYPE_JAVA_RIL = 4;
     52 
     53     /** @hide */
     54     @Retention(RetentionPolicy.SOURCE)
     55     @IntDef({
     56         CONNECTION_NONE,
     57         CONNECTION_PRIMARY_SERVING,
     58         CONNECTION_SECONDARY_SERVING,
     59         CONNECTION_UNKNOWN
     60     })
     61     public @interface CellConnectionStatus {}
     62 
     63     /**
     64      * Cell is not a serving cell.
     65      *
     66      * <p>The cell has been measured but is neither a camped nor serving cell (3GPP 36.304).
     67      */
     68     public static final int CONNECTION_NONE = 0;
     69 
     70     /** UE is connected to cell for signalling and possibly data (3GPP 36.331, 25.331). */
     71     public static final int CONNECTION_PRIMARY_SERVING = 1;
     72 
     73     /** UE is connected to cell for data (3GPP 36.331, 25.331). */
     74     public static final int CONNECTION_SECONDARY_SERVING = 2;
     75 
     76     /** Connection status is unknown. */
     77     public static final int CONNECTION_UNKNOWN = Integer.MAX_VALUE;
     78 
     79     private int mCellConnectionStatus = CONNECTION_NONE;
     80 
     81     // True if device is mRegistered to the mobile network
     82     private boolean mRegistered;
     83 
     84     // Observation time stamped as type in nanoseconds since boot
     85     private long mTimeStamp;
     86 
     87     // Where time stamp gets recorded.
     88     // Value of TIMESTAMP_TYPE_XXXX
     89     private int mTimeStampType;
     90 
     91     /** @hide */
     92     protected CellInfo() {
     93         this.mRegistered = false;
     94         this.mTimeStampType = TIMESTAMP_TYPE_UNKNOWN;
     95         this.mTimeStamp = Long.MAX_VALUE;
     96     }
     97 
     98     /** @hide */
     99     protected CellInfo(CellInfo ci) {
    100         this.mRegistered = ci.mRegistered;
    101         this.mTimeStampType = ci.mTimeStampType;
    102         this.mTimeStamp = ci.mTimeStamp;
    103         this.mCellConnectionStatus = ci.mCellConnectionStatus;
    104     }
    105 
    106     /** True if this cell is registered to the mobile network */
    107     public boolean isRegistered() {
    108         return mRegistered;
    109     }
    110     /** @hide */
    111     public void setRegistered(boolean registered) {
    112         mRegistered = registered;
    113     }
    114 
    115     /** Approximate time of this cell information in nanos since boot */
    116     public long getTimeStamp() {
    117         return mTimeStamp;
    118     }
    119     /** @hide */
    120     public void setTimeStamp(long timeStamp) {
    121         mTimeStamp = timeStamp;
    122     }
    123 
    124     /**
    125      * Gets the connection status of this cell.
    126      *
    127      * @see #CONNECTION_NONE
    128      * @see #CONNECTION_PRIMARY_SERVING
    129      * @see #CONNECTION_SECONDARY_SERVING
    130      * @see #CONNECTION_UNKNOWN
    131      *
    132      * @return The connection status of the cell.
    133      */
    134     @CellConnectionStatus
    135     public int getCellConnectionStatus() {
    136         return mCellConnectionStatus;
    137     }
    138     /** @hide */
    139     public void setCellConnectionStatus(@CellConnectionStatus int cellConnectionStatus) {
    140         mCellConnectionStatus = cellConnectionStatus;
    141     }
    142 
    143     /**
    144      * Where time stamp gets recorded.
    145      * @return one of TIMESTAMP_TYPE_XXXX
    146      *
    147      * @hide
    148      */
    149     public int getTimeStampType() {
    150         return mTimeStampType;
    151     }
    152     /** @hide */
    153     public void setTimeStampType(int timeStampType) {
    154         if (timeStampType < TIMESTAMP_TYPE_UNKNOWN || timeStampType > TIMESTAMP_TYPE_JAVA_RIL) {
    155             mTimeStampType = TIMESTAMP_TYPE_UNKNOWN;
    156         } else {
    157             mTimeStampType = timeStampType;
    158         }
    159     }
    160 
    161     @Override
    162     public int hashCode() {
    163         int primeNum = 31;
    164         return ((mRegistered ? 0 : 1) * primeNum) + ((int)(mTimeStamp / 1000) * primeNum)
    165                 + (mTimeStampType * primeNum) + (mCellConnectionStatus * primeNum);
    166     }
    167 
    168     @Override
    169     public boolean equals(Object other) {
    170         if (other == null) {
    171             return false;
    172         }
    173         if (this == other) {
    174             return true;
    175         }
    176         try {
    177             CellInfo o = (CellInfo) other;
    178             return mRegistered == o.mRegistered
    179                     && mTimeStamp == o.mTimeStamp
    180                     && mTimeStampType == o.mTimeStampType
    181                     && mCellConnectionStatus == o.mCellConnectionStatus;
    182         } catch (ClassCastException e) {
    183             return false;
    184         }
    185     }
    186 
    187     private static String timeStampTypeToString(int type) {
    188         switch (type) {
    189             case 1:
    190                 return "antenna";
    191             case 2:
    192                 return "modem";
    193             case 3:
    194                 return "oem_ril";
    195             case 4:
    196                 return "java_ril";
    197             default:
    198                 return "unknown";
    199         }
    200     }
    201 
    202     @Override
    203     public String toString() {
    204         StringBuffer sb = new StringBuffer();
    205         String timeStampType;
    206 
    207         sb.append("mRegistered=").append(mRegistered ? "YES" : "NO");
    208         timeStampType = timeStampTypeToString(mTimeStampType);
    209         sb.append(" mTimeStampType=").append(timeStampType);
    210         sb.append(" mTimeStamp=").append(mTimeStamp).append("ns");
    211         sb.append(" mCellConnectionStatus=").append(mCellConnectionStatus);
    212 
    213         return sb.toString();
    214     }
    215 
    216     /**
    217      * Implement the Parcelable interface
    218      */
    219     @Override
    220     public int describeContents() {
    221         return 0;
    222     }
    223 
    224     /** Implement the Parcelable interface */
    225     @Override
    226     public abstract void writeToParcel(Parcel dest, int flags);
    227 
    228     /**
    229      * Used by child classes for parceling.
    230      *
    231      * @hide
    232      */
    233     protected void writeToParcel(Parcel dest, int flags, int type) {
    234         dest.writeInt(type);
    235         dest.writeInt(mRegistered ? 1 : 0);
    236         dest.writeInt(mTimeStampType);
    237         dest.writeLong(mTimeStamp);
    238         dest.writeInt(mCellConnectionStatus);
    239     }
    240 
    241     /**
    242      * Used by child classes for parceling
    243      *
    244      * @hide
    245      */
    246     protected CellInfo(Parcel in) {
    247         mRegistered = (in.readInt() == 1) ? true : false;
    248         mTimeStampType = in.readInt();
    249         mTimeStamp = in.readLong();
    250         mCellConnectionStatus = in.readInt();
    251     }
    252 
    253     /** Implement the Parcelable interface */
    254     public static final Creator<CellInfo> CREATOR = new Creator<CellInfo>() {
    255         @Override
    256         public CellInfo createFromParcel(Parcel in) {
    257                 int type = in.readInt();
    258                 switch (type) {
    259                     case TYPE_GSM: return CellInfoGsm.createFromParcelBody(in);
    260                     case TYPE_CDMA: return CellInfoCdma.createFromParcelBody(in);
    261                     case TYPE_LTE: return CellInfoLte.createFromParcelBody(in);
    262                     case TYPE_WCDMA: return CellInfoWcdma.createFromParcelBody(in);
    263                     default: throw new RuntimeException("Bad CellInfo Parcel");
    264                 }
    265         }
    266 
    267         @Override
    268         public CellInfo[] newArray(int size) {
    269             return new CellInfo[size];
    270         }
    271     };
    272 }
    273