CellInfo.java :  » UnTagged » smartphone-networks » com » mobed » ssn » Android Open Source

Android Open Source » UnTagged » smartphone networks 
smartphone networks » com » mobed » ssn » CellInfo.java
package com.mobed.ssn;

import android.database.Cursor;
import android.telephony.NeighboringCellInfo;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;

/**
 * CDMA Cell Location is supported from 2.1
 */
public class CellInfo implements Cloneable, Comparable {
  public static final int CELL_TYPE_UNKNOWN = -1;
  public static final int CELL_TYPE_ACTIVE = 1;
  public static final int CELL_TYPE_NEIGHBOR = 2;
  
  private int cid;  // Cell id in GSM, base station id in CDMA
  private int cellType;  //Cell condition, ACTIVE, NEIGHBOR or UNKNOWN
  private long nodeId;
  private int networkType;
  
  // GSM
  private int lac;
  
  // UMTS
  private int psc;
  
  // CDMA
  private int baseStationLat;
  private int baseStationLng;
  private int networkId;
  private int systemId;

  
  private int sampleCount;
  private float signal;
  private float signalDeviation;
  private String time;
  
  public CellInfo() {
    this.clear();
  }
  public CellInfo(NeighboringCellInfo nci) {
    this.clear();
    this.set(nci);
  }
  /**
   * Clear the contents.
   */
  public void clear() {
    cid = NeighboringCellInfo.UNKNOWN_CID;
    cellType = CELL_TYPE_UNKNOWN;
    networkType = TelephonyManager.NETWORK_TYPE_UNKNOWN;
    lac = NeighboringCellInfo.UNKNOWN_CID;
    psc = NeighboringCellInfo.UNKNOWN_CID;
    nodeId = 0;
    signal = 0;
    signalDeviation = 0;
    sampleCount = 0;
    time = null;
  }
  
  public void set(CellInfo ci)
  {
    this.setCellType(ci.getCellType());
    this.setCid(ci.getCid());
    
    this.setNetworkType(ci.getNetworkType());
    this.setTime(ci.getTime());
    if(ci.getNetworkType()==TelephonyManager.NETWORK_TYPE_GPRS)
    {
      this.setLac(ci.getLac());
    }
    else if(ci.getNetworkType()==TelephonyManager.NETWORK_TYPE_CDMA)
    {
      this.setPsc(ci.getPsc());
      this.setNetworkId(ci.getNetworkId());
      this.setBaseStationLat(ci.getBaseStationLat());
      this.setBaseStationLng(ci.getBaseStationLng());
    }
  }
  
  public void set(NeighboringCellInfo nci) {
    this.cid = nci.getCid();
    /**
     * Supported from 2.1
     */
    this.lac = nci.getLac();
    this.psc = nci.getPsc();
    this.networkType = nci.getNetworkType();
    this.signal = nci.getRssi();
    this.signalDeviation = 0;
    this.sampleCount = 1;
    this.time = Common.longToTimeString(System.currentTimeMillis());
  }
  public void set(GsmCellLocation gcl) {
    this.cid = gcl.getCid();
    this.lac = gcl.getLac();
    this.networkType = TelephonyManager.NETWORK_TYPE_GPRS; // Specific case in Korea
    this.time = Common.longToTimeString(System.currentTimeMillis());
  }
  
  public void set(Cursor cursor)
  {
    if(cursor.moveToFirst())
    {
      this.setCellType(cursor.getInt(ssnDatabase.CELL_COLUMN_CELL_TYPE));
      this.setCid(cursor.getInt(ssnDatabase.CELL_COLUMN_CID));
      
      this.setNetworkType(cursor.getInt(ssnDatabase.CELL_COLUMN_NETWORK_TYPE));
      this.setTime(cursor.getString(ssnDatabase.CELL_COLUMN_TIME));
      if(this.getNetworkType()==TelephonyManager.NETWORK_TYPE_GPRS)
      {
        this.setLac(cursor.getInt(ssnDatabase.CELL_COLUMN_LAC));
      }
      else if(this.getNetworkType()==TelephonyManager.NETWORK_TYPE_CDMA)
      {
        this.setPsc(cursor.getInt(ssnDatabase.CELL_COLUMN_PSC));
        this.setNetworkId(cursor.getInt(ssnDatabase.CELL_COLUMN_NETWORK_ID));
        this.setBaseStationLat(cursor.getInt(ssnDatabase.CELL_COLUMN_BASE_STATION_LATITUDE));
        this.setBaseStationLng(cursor.getInt(ssnDatabase.CELL_COLUMN_BASE_STATION_LONGITUDE));
      }
    }
  }
  /**
   * Supported from 2.1
   * @param ccl
   */
  
  public void set(CdmaCellLocation ccl) 
  {
    this.cid = ccl.getBaseStationId();
    this.baseStationLat = ccl.getBaseStationLatitude();
    this.baseStationLng = ccl.getBaseStationLongitude();
    this.networkId = ccl.getNetworkId();
    this.systemId = ccl.getSystemId();
    this.networkType = TelephonyManager.NETWORK_TYPE_CDMA;
    this.time = Common.longToTimeString(System.currentTimeMillis());
  }
   
  public int getBaseStationLat() {
    return baseStationLat;
  }
  public void setBaseStationLat(int baseStationLat) {
    this.baseStationLat = baseStationLat;
  }
  public int getBaseStationLng() {
    return baseStationLng;
  }
  public void setBaseStationLng(int baseStationLng) {
    this.baseStationLng = baseStationLng;
  }
  public int getNetworkId() {
    return networkId;
  }
  public void setNetworkId(int networkId) {
    this.networkId = networkId;
  }
  public int getSystemId() {
    return systemId;
  }
  public void setSystemId(int systemId) {
    this.systemId = systemId;
  }
  /**
   * Returns sampling count of this access point.
   */
  public int getSampleCount() {
    return sampleCount;
  }

  /**
   * Sets sampling count of this access point.
   */
  public void setSampleCount(int sampleCount) {
    this.sampleCount = sampleCount;
  }

  public int getCid() {
    return cid;
  }

  
  public int getCellType() {
    return cellType;
  }
  public void setCellType(int cellType) {
    this.cellType = cellType;
  }
  public int getNetworkType() {
    return networkType;
  }

  public void setNetworkType(int networkType) {
    this.networkType = networkType;
  }

  public int getLac() {
    return lac;
  }

  public void setLac(int lac) {
    this.lac = lac;
  }

  public int getPsc() {
    return psc;
  }

  public void setPsc(int psc) {
    this.psc = psc;
  }

  /**
   * Sets the basic service set identifier (BSSID) of this access point.
   * 
   * @param bssid
   *            the BSSID, in the form of a six-byte MAC address:
   *            XX:XX:XX:XX:XX:XX
   */
  public void setCid(int cid) {
    this.cid = cid;
  }

  public void setNodeId(long nodeId)
  {  
    this.nodeId = nodeId; 
  }
  
  public long getNodeId()
  {
    return this.nodeId;
  }
  
  /**
   * Returns the received signal strength indicator of this access point.
   */
  public float getRssi() {
    return signal;
  }

  /**
   * Sets the received signal strength indicator of this access point.
   */
  public void setRssi(float signal) {
    this.signal = signal;
  }

  public float getRssiDeviation() {
    return signalDeviation;
  }

  public void setRssiDeviation(float signalDeviation) {
    this.signalDeviation = signalDeviation;
  }

  public String getTime() {
    return time;
  }

  public void setTime(String time) {
    this.time = time;
  }

  public Object clone() {
    CellInfo o = null;
    try {
      o = (CellInfo)super.clone();
      o.cid = cid;
      o.cellType = cellType;
      o.networkType = networkType;
      o.lac = lac;
      o.psc = psc;
      o.sampleCount = sampleCount;
      o.signal = signal;
      o.signalDeviation = signalDeviation;
      o.time = time;
    } catch (CloneNotSupportedException e) {
      e.printStackTrace();
    }

    return o;
  }
  public String toString() {
    String s = "";
    switch(cellType) {
    case CELL_TYPE_UNKNOWN:
      s += "Unknown\t"; break;
    case CELL_TYPE_ACTIVE:
      s += "Active\t"; break;
    case CELL_TYPE_NEIGHBOR:
      s += "Neighbor\t"; break;
    }
    switch(networkType) {
    case TelephonyManager.NETWORK_TYPE_GPRS:
      s += "GSM\t" + this.cid + "\t" + this.lac + "\n";
      break;
    case TelephonyManager.NETWORK_TYPE_UMTS:
      s += "UMTS\t" + this.psc + "\n"; break;
    /**
     * Supported from 2.1
     */
    //case TelephonyManager.NETWORK_TYPE_CDMA:
    //  s += "CDMA\t" + this.cid + " (" + this.baseStationLat + "," + this.baseStationLng + ")\n";
    //  s += this.networkId + "\t" + this.systemId + "\n";
    //  break;
    }
    if(sampleCount > 0) {
      s += "Signal " + String.format("%.1f", signal) + "/" + String.format("%.1f", signalDeviation) + " of " + sampleCount;
    }
    else {
      s += "No Signal";
    }
    s += "\n";
    
    return s;
  }
  @Override
  public boolean equals(Object obj) {
    switch(this.networkType) {
    case TelephonyManager.NETWORK_TYPE_UMTS:
      return this.psc == ((CellInfo)obj).psc;
    case TelephonyManager.NETWORK_TYPE_GPRS:  // GSM case in Korea
      return this.cid == ((CellInfo)obj).cid;
    }
    return this.cid == ((CellInfo)obj).cid;
  }
  
  public int compareTo(Object another) {
    if(this.cid < ((CellInfo)another).cid)
      return -1;
    else if(this.cid > ((CellInfo)another).cid)
      return 1;
    return 0;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.