下面列出了android.telephony.CellSignalStrengthCdma#android.telephony.cdma.CdmaCellLocation 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Parses data from PhoneStateListener.LISTEN_CELL_LOCATION.onCellLocationChanged
* http://developer.android.com/reference/android/telephony/cdma/CdmaCellLocation.html
* @param location CdmaCellLocation
* @return JSON
*/
public static String cdmaCellLocationJSON(CdmaCellLocation location){
final Calendar calendar = Calendar.getInstance();
final JSONObject json = new JSONObject();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && location != null) {
try {
json.put("provider", CELLLOCATION_PROVIDER);
json.put("type", CDMA);
json.put("timestamp", calendar.getTimeInMillis());
json.put("baseStationId", location.getBaseStationId()); // -1 if unknown
json.put("networkId", location.getNetworkId()); // -1 if unknown
json.put("systemId", location.getSystemId()); // -1 if unknown
json.put("baseStationLatitude", CdmaCellLocation.convertQuartSecToDecDegrees(location.getBaseStationLatitude()));
json.put("baseStationLongitude", CdmaCellLocation.convertQuartSecToDecDegrees(location.getBaseStationLongitude()));
}
catch(JSONException exc) {
logJSONException(exc);
}
}
return json.toString();
}
private boolean isCellLocationEqual(CellLocation cl1, CellLocation cl2) {
boolean result;
if (cl1 instanceof GsmCellLocation && cl2 instanceof GsmCellLocation) {
GsmCellLocation gsm1 = (GsmCellLocation) cl1;
GsmCellLocation gsm2 = (GsmCellLocation) cl2;
result = (gsm1.getCid() == gsm2.getCid()
&& gsm1.getLac() == gsm2.getLac()
&& gsm1.getPsc() == gsm2.getPsc());
Timber.d("isCellLocationEqual(): GSM equals = %s", result);
} else if (cl1 instanceof CdmaCellLocation && cl2 instanceof CdmaCellLocation) {
CdmaCellLocation cdma1 = (CdmaCellLocation) cl1;
CdmaCellLocation cdma2 = (CdmaCellLocation) cl2;
result = (cdma1.getBaseStationId() == cdma2.getBaseStationId()
&& cdma1.getNetworkId() == cdma2.getNetworkId()
&& cdma1.getSystemId() == cdma2.getSystemId());
Timber.d("isCellLocationEqual(): CDMA equal = %s", result);
} else {
// different types or nulls
result = false;
Timber.d("isCellLocationEqual(): Different types or nulls");
}
return result;
}
@Nullable
static CellLocationSingleData fromCellLocation(@Nullable CellLocation location) {
int cid, lac;
if (location != null) {
if (location instanceof GsmCellLocation) {
cid = ((GsmCellLocation) location).getCid();
lac = ((GsmCellLocation) location).getLac();
}
else if (location instanceof CdmaCellLocation) {
cid = ((CdmaCellLocation) location).getBaseStationId();
lac = ((CdmaCellLocation) location).getSystemId();
} else {
//TODO: More
return null;
}
return new CellLocationSingleData(cid, lac);
}
return null;
}
ay(CellLocation celllocation)
{
a = 0x7fffffff;
b = 0x7fffffff;
c = 0x7fffffff;
d = 0x7fffffff;
e = 0x7fffffff;
if (celllocation != null)
{
if (celllocation instanceof GsmCellLocation)
{
GsmCellLocation gsmcelllocation = (GsmCellLocation)celllocation;
e = gsmcelllocation.getCid();
d = gsmcelllocation.getLac();
} else
if (celllocation instanceof CdmaCellLocation)
{
CdmaCellLocation cdmacelllocation = (CdmaCellLocation)celllocation;
c = cdmacelllocation.getBaseStationId();
b = cdmacelllocation.getNetworkId();
a = cdmacelllocation.getSystemId();
return;
}
}
}
public static GSMInfo getGSMInfo(Context context) {
try {
GSMInfo info = new GSMInfo();
TelephonyManager manager = (TelephonyManager) context.getSystemService("phone");
if (manager != null) {
CellLocation cellLocation = manager.getCellLocation();
int lac = 0;
int cellid = 0;
if (cellLocation != null) {
if (cellLocation instanceof GsmCellLocation) {
lac = ((GsmCellLocation) cellLocation).getLac();
cellid = ((GsmCellLocation) cellLocation).getCid();
} else if (cellLocation instanceof CdmaCellLocation) {
cellid = ((CdmaCellLocation) cellLocation).getNetworkId();
lac = ((CdmaCellLocation) cellLocation).getBaseStationId();
}
}
info.lac = lac;
info.cid = cellid;
}
AMapLocation location = AMapLocationTool.getInstance().location();
if (location != null) {
info.latitude = location.getLatitude();
info.longitude = location.getLongitude();
return info;
}
info.latitude = Double.parseDouble(PreferencesManager.getInstance().getLocationLongitude());
info.longitude = Double.parseDouble(PreferencesManager.getInstance().getLocationLatitude());
return info;
} catch (Exception e) {
LogInfo.log("ZSM++ ==== GSM exception e == " + e.getMessage());
e.printStackTrace();
return null;
}
}
public boolean isValid(CdmaCellLocation cell) {
boolean valid = (isBidInRange(cell.getBaseStationId()) && isNidInRange(cell.getNetworkId())
&& isSidInRange(cell.getSystemId()));
if (!valid) {
Timber.w("isValid(): Invalid CdmaCellLocation [sid=%s, nid=%s, bid=%s]", cell.getSystemId(), cell.getNetworkId(), cell.getBaseStationId());
Timber.w("isValid(): Invalid CdmaCellLocation %s", cell);
}
return valid;
}
public boolean isValid(CellLocation cellLocation, int mcc, int mnc) {
if (cellLocation instanceof GsmCellLocation) {
GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;
return getGsmValidator().isValid(gsmCellLocation, mcc, mnc);
}
if (cellLocation instanceof CdmaCellLocation) {
CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cellLocation;
return getCdmaValidator().isValid(cdmaCellLocation);
}
throw new UnsupportedOperationException("Cell location type not supported `" + cellLocation.getClass().getName() + "`");
}
public Cell convert(CellLocation cellLocation, int mcc, int mnc, NetworkGroup networkType) {
Cell cell = new Cell();
if (cellLocation instanceof GsmCellLocation) {
GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;
if (gsmCellLocation.getCid() <= 65535 && gsmCellLocation.getPsc() == NeighboringCellInfo.UNKNOWN_CID) {
cell.setGsmCellLocation(mcc, mnc, gsmCellLocation.getLac(), gsmCellLocation.getCid(), NetworkGroup.Gsm);
} else {
// fix invalid network types (unfortunately not possible to distinguish between UMTS and LTE)
if (networkType == NetworkGroup.Gsm || networkType == NetworkGroup.Cdma)
networkType = NetworkGroup.Unknown;
int psc = gsmCellLocation.getPsc();
if (psc == NeighboringCellInfo.UNKNOWN_CID || psc == Cell.UNKNOWN_CID) {
psc = Cell.UNKNOWN_CID;
} else if (psc >= 504) {
// only UMTS networks support larger PSC
networkType = NetworkGroup.Wcdma;
}
cell.setGsmCellLocation(mcc, mnc, gsmCellLocation.getLac(), gsmCellLocation.getCid(), psc, networkType);
}
} else if (cellLocation instanceof CdmaCellLocation) {
CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cellLocation;
cell.setCdmaCellLocation(cdmaCellLocation.getSystemId(), cdmaCellLocation.getNetworkId(), cdmaCellLocation.getBaseStationId());
} else {
throw new UnsupportedOperationException("Cell location type not supported `" + cellLocation.getClass().getName() + "`");
}
return cell;
}
@Override
public void onCellLocationChanged(CellLocation location) {
super.onCellLocationChanged(location);
//Log.e(Tag,"onCellLocationChanged");
if (location instanceof CdmaCellLocation) {
cdma_SID = ((CdmaCellLocation) location).getSystemId();
cdma_NID = ((CdmaCellLocation) location).getNetworkId();
cdma_BSID = ((CdmaCellLocation) location).getBaseStationId();
//Log.e(Tag,((CdmaCellLocation)location).toString());
}
((MainActivity)mcontext).mSectionsPagerAdapter.notifyDataSetChanged();
}
public static CellInfo getCellInfo(Context context) {
CellInfo cellInfo = new CellInfo();
TelephonyManager manager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
String netOperator = manager.getNetworkOperator();
// Fix crash when not connected to network (airplane mode, underground,
// etc)
if (netOperator == null || netOperator.length() < 3) {
return cellInfo;
}
/*
* FIXME: Actually check for mobile network status == connected before
* doing this stuff.
*/
if (Phone.getType(context).equals(PHONE_TYPE_CDMA)) {
CdmaCellLocation cdmaLocation = (CdmaCellLocation) manager.getCellLocation();
cellInfo.cid = cdmaLocation.getBaseStationId();
cellInfo.lac = cdmaLocation.getNetworkId();
cellInfo.mnc = cdmaLocation.getSystemId();
cellInfo.mcc = Integer.parseInt(netOperator.substring(0, 3));
cellInfo.radioType = Network.getMobileNetworkType(context);
} else if (Phone.getType(context).equals(PHONE_TYPE_GSM)) {
GsmCellLocation gsmLocation = (GsmCellLocation) manager.getCellLocation();
cellInfo.mcc = Integer.parseInt(netOperator.substring(0, 3));
cellInfo.mnc = Integer.parseInt(netOperator.substring(3));
cellInfo.lac = gsmLocation.getLac();
cellInfo.cid = gsmLocation.getCid();
cellInfo.radioType = Network.getMobileNetworkType(context);
}
return cellInfo;
}
private String getCellId() {
try {
String out = "";
if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
final GsmCellLocation gLoc = (GsmCellLocation) tm.getCellLocation();
out = Integer.toString(gLoc.getCid());
} else if(tm.getPhoneType() == TelephonyManager.PHONE_TYPE_CDMA) {
final CdmaCellLocation cLoc = (CdmaCellLocation) tm.getCellLocation();
out = Integer.toString(cLoc.getBaseStationId());
}
return out;
} catch(NullPointerException e) {
return null;
}
}
public void onCellLocationChanged(CellLocation location) {
checkForNeighbourCount(location);
compareLac(location);
refreshDevice();
mDevice.setNetID(tm);
mDevice.getNetworkTypeName();
switch (mDevice.getPhoneID()) {
case TelephonyManager.PHONE_TYPE_NONE:
case TelephonyManager.PHONE_TYPE_SIP:
case TelephonyManager.PHONE_TYPE_GSM:
GsmCellLocation gsmCellLocation = (GsmCellLocation) location;
if (gsmCellLocation != null) {
//TODO @EVA where are we sending this setCellInfo data?
//TODO
/*@EVA
Is it a good idea to dump all cells to db because if we spot a known cell
with different lac then this will also be dump to db.
*/
mDevice.setCellInfo(
gsmCellLocation.toString() + // ??
mDevice.getDataActivityTypeShort() + "|" + // No,In,Ou,IO,Do
mDevice.getDataStateShort() + "|" + // Di,Ct,Cd,Su
mDevice.getNetworkTypeName() + "|" // HSPA,LTE etc
);
mDevice.mCell.setLAC(gsmCellLocation.getLac()); // LAC
mDevice.mCell.setCID(gsmCellLocation.getCid()); // CID
if (gsmCellLocation.getPsc() != -1) {
mDevice.mCell.setPSC(gsmCellLocation.getPsc()); // PSC
}
/*
Add cell if gps is not enabled
when gps enabled lat lon will be updated
by function below
*/
}
break;
case TelephonyManager.PHONE_TYPE_CDMA:
CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) location;
if (cdmaCellLocation != null) {
mDevice.setCellInfo(
cdmaCellLocation.toString() + // ??
mDevice.getDataActivityTypeShort() + "|" + // No,In,Ou,IO,Do
mDevice.getDataStateShort() + "|" + // Di,Ct,Cd,Su
mDevice.getNetworkTypeName() + "|" // HSPA,LTE etc
);
mDevice.mCell.setLAC(cdmaCellLocation.getNetworkId()); // NID
mDevice.mCell.setCID(cdmaCellLocation.getBaseStationId()); // BID
mDevice.mCell.setSID(cdmaCellLocation.getSystemId()); // SID
mDevice.mCell.setMNC(cdmaCellLocation.getSystemId()); // MNC <== BUG!??
mDevice.setNetworkName(tm.getNetworkOperatorName()); // ??
}
}
}
/**
* Description: Add entries to the "DBi_measure" DB table
*
* Issues:
* [ ]
*
* Notes: (a)
*
*
* TODO: Remove OLD notes below, once we have new ones relevant to our new table
*
* From "locationinfo":
*
* $ sqlite3.exe -header aimsicd.db 'select * from locationinfo;'
* _id|Lac|CellID|Net|Lat|Lng|Signal|Connection|Timestamp
* 1|10401|6828xxx|10|54.67874392|25.28693531|24|[10401,6828320,126]No|Di|HSPA||2015-01-21 20:45:10
*
* From "cellinfo":
*
* $ sqlite3.exe -header aimsicd.db 'select * from cellinfo;'
* _id|Lac|CellID|Net|Lat|Lng|Signal|Mcc|Mnc|Accuracy|Speed|Direction|NetworkType|MeasurementTaken|OCID_SUBMITTED|Timestamp
* 1|10401|6828xxx|10|54.67874392|25.28693531|24|246|2|69.0|0.0|0.0|HSPA|82964|0|2015-01-21 20:45:10
*
* Issues:
*
*/
public void onLocationChanged(Location loc) {
DeviceApi18.loadCellInfo(tm, mDevice);
if (!mDevice.mCell.isValid()) {
CellLocation cellLocation = tm.getCellLocation();
if (cellLocation != null) {
switch (mDevice.getPhoneID()) {
case TelephonyManager.PHONE_TYPE_NONE:
case TelephonyManager.PHONE_TYPE_SIP:
case TelephonyManager.PHONE_TYPE_GSM:
GsmCellLocation gsmCellLocation = (GsmCellLocation) cellLocation;
mDevice.mCell.setCID(gsmCellLocation.getCid()); // CID
mDevice.mCell.setLAC(gsmCellLocation.getLac()); // LAC
mDevice.mCell.setPSC(gsmCellLocation.getPsc()); // PSC
break;
case TelephonyManager.PHONE_TYPE_CDMA:
CdmaCellLocation cdmaCellLocation = (CdmaCellLocation) cellLocation;
mDevice.mCell.setCID(cdmaCellLocation.getBaseStationId()); // BSID ??
mDevice.mCell.setLAC(cdmaCellLocation.getNetworkId()); // NID
mDevice.mCell.setSID(cdmaCellLocation.getSystemId()); // SID
mDevice.mCell.setMNC(cdmaCellLocation.getSystemId()); // MNC <== BUG!??
break;
}
}
}
if (loc != null && (Double.doubleToRawLongBits(loc.getLatitude()) != 0 && Double.doubleToRawLongBits(loc.getLongitude()) != 0)) {
mDevice.mCell.setLon(loc.getLongitude()); // gpsd_lon
mDevice.mCell.setLat(loc.getLatitude()); // gpsd_lat
mDevice.mCell.setSpeed(loc.getSpeed()); // speed // TODO: Remove, we're not using it!
mDevice.mCell.setAccuracy(loc.getAccuracy()); // gpsd_accu
mDevice.mCell.setBearing(loc.getBearing()); // -- [deg]?? // TODO: Remove, we're not using it!
mDevice.setLastLocation(loc); //
// Store last known location in preference
SharedPreferences.Editor prefsEditor;
prefsEditor = prefs.edit();
prefsEditor.putString(context.getString(R.string.data_last_lat_lon),
String.valueOf(loc.getLatitude()) + ":" + String.valueOf(loc.getLongitude()));
prefsEditor.apply();
// This only logs a BTS if we have GPS lock
// Test: ~~Is correct behaviour? We should consider logging all cells, even without GPS.~~
//if (mTrackingCell) {
// This also checks that the lac are cid are not in DB before inserting
dbHelper.insertBTS(mDevice.mCell);
//}
}
}
/**
* Converts CellInfoCdma into JSON
* @param cellInfo CellInfoCdma
* @return JSON
*/
public static String cellInfoCDMAJSON(CellInfoCdma cellInfo, boolean returnSignalStrength){
final Calendar calendar = Calendar.getInstance();
final JSONObject json = new JSONObject();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2 && cellInfo != null) {
try {
json.put("provider", CELLINFO_PROVIDER);
json.put("type", CDMA);
json.put("timestamp", calendar.getTimeInMillis());
final CellIdentityCdma identityCdma = cellInfo.getCellIdentity();
json.put("latitude", CdmaCellLocation.convertQuartSecToDecDegrees(identityCdma.getLatitude()));
json.put("longitude", CdmaCellLocation.convertQuartSecToDecDegrees(identityCdma.getLongitude()));
json.put("basestationId", identityCdma.getBasestationId());
json.put("networkId", identityCdma.getNetworkId());
json.put("systemId", identityCdma.getSystemId());
if (returnSignalStrength){
final JSONObject jsonSignalStrength = new JSONObject();
final CellSignalStrengthCdma cellSignalStrengthCdma = cellInfo.getCellSignalStrength();
jsonSignalStrength.put("asuLevel", cellSignalStrengthCdma.getAsuLevel());
jsonSignalStrength.put("cdmaDbm", cellSignalStrengthCdma.getCdmaDbm());
jsonSignalStrength.put("cdmaEcio", cellSignalStrengthCdma.getCdmaEcio());
jsonSignalStrength.put("cdmaLevel", cellSignalStrengthCdma.getCdmaLevel());
jsonSignalStrength.put("dbm", cellSignalStrengthCdma.getDbm());
jsonSignalStrength.put("evdoDbm", cellSignalStrengthCdma.getEvdoDbm());
jsonSignalStrength.put("evdoEcio", cellSignalStrengthCdma.getEvdoEcio());
jsonSignalStrength.put("evdoLevel", cellSignalStrengthCdma.getEvdoLevel());
jsonSignalStrength.put("evdoSnr", cellSignalStrengthCdma.getEvdoSnr());
jsonSignalStrength.put("level", cellSignalStrengthCdma.getLevel());
json.put("cellSignalStrengthCdma", jsonSignalStrength);
}
}
catch(JSONException exc) {
logJSONException(exc);
}
}
return json.toString();
}
private void setPhoneStateListener(){
if(_returnSignalStrength){
_signalStrengthListener = new SignalStrengthListener();
_signalStrengthListener.setListener(new StrengthChange() {
@Override
public SignalStrength onSignalStrengthChanged(SignalStrength signalStrength) {
if(!Thread.currentThread().isInterrupted()){
sendCallback(PluginResult.Status.OK,
JSONHelper.signalStrengthJSON(signalStrength));
}
return null;
}
});
_telephonyManager.listen(_signalStrengthListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
_phoneStateListener = new PhoneStateListener(){
@Override
public void onCellLocationChanged(CellLocation location){
if(!Thread.currentThread().isInterrupted()){
if(location instanceof CdmaCellLocation){
final CdmaCellLocation cellLocationCdma = (CdmaCellLocation) location;
sendCallback(PluginResult.Status.OK,
JSONHelper.cdmaCellLocationJSON(cellLocationCdma));
}
if(location instanceof GsmCellLocation){
final GsmCellLocation cellLocationGsm = (GsmCellLocation) location;
sendCallback(PluginResult.Status.OK,
JSONHelper.gsmCellLocationJSON(cellLocationGsm));
}
}
}
@Override
public void onCellInfoChanged(List<CellInfo> cellInfo){
if(!Thread.currentThread().isInterrupted()){
processCellInfos(cellInfo);
}
}
};
_telephonyManager.listen(_phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION);
}
public CdmaCellLocationAssert(CdmaCellLocation actual) {
super(actual, CdmaCellLocationAssert.class);
}
/**
* Adds or updates a cell tower.
* <p>
* If the cell tower is already in the list, it is replaced; if not, a new
* entry is created.
* <p>
* This method will set the cell's identity data. After this call,
* {@link #isServing()} will return {@code true} for this cell.
* @return The new or updated entry.
*/
public CellTowerCdma update(CdmaCellLocation location) {
CellTowerCdma result = this.get(location.getSystemId(), location.getNetworkId(), location.getBaseStationId());
if (result == null) {
result = new CellTowerCdma(location.getSystemId(), location.getNetworkId(), location.getBaseStationId());
this.put(result.getText(), result);
}
result.setCellLocation(true);
return result;
}