forked from mozilla/MozStumbler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58c2d23
commit 9cbfaad
Showing
4 changed files
with
232 additions
and
239 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,217 @@ | ||
package org.mozilla.mozstumbler.cellscanner; | ||
|
||
import android.os.Build; | ||
import android.telephony.CellLocation; | ||
import android.telephony.NeighboringCellInfo; | ||
import android.telephony.TelephonyManager; | ||
import android.telephony.cdma.CdmaCellLocation; | ||
import android.telephony.gsm.GsmCellLocation; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
public class CellInfo { | ||
public static final String RADIO_GSM = "gsm"; | ||
public static final String RADIO_CDMA = "cdma"; | ||
public static final String RADIO_WCDMA = "wcdma"; | ||
|
||
public static final String CELL_RADIO_GSM = "gsm"; | ||
public static final String CELL_RADIO_UMTS = "umts"; | ||
public static final String CELL_RADIO_CDMA = "cdma"; | ||
public static final String CELL_RADIO_LTE = "lte"; | ||
|
||
static final int UNKNOWN_CID = -1; | ||
static final int UNKNOWN_SIGNAL = -1000; | ||
|
||
private String mRadio = RADIO_GSM; | ||
private String mCellRadio; | ||
|
||
private int mMcc; | ||
private int mMnc; | ||
private int mCid; | ||
private int mLac; | ||
private int mSignal; | ||
private int mAsu; | ||
private int mTa; | ||
private int mPsc; | ||
|
||
CellInfo(int phoneType) { | ||
reset(); | ||
setRadio(phoneType); | ||
} | ||
|
||
public String getRadio() { return mRadio; } | ||
|
||
public String getCellRadio() { | ||
return mCellRadio; | ||
} | ||
|
||
public JSONObject toJSONObject() { | ||
final JSONObject obj = new JSONObject(); | ||
|
||
try { | ||
obj.put("radio", getCellRadio()); | ||
obj.put("mcc", mMcc); | ||
obj.put("mnc", mMnc); | ||
if (mLac != UNKNOWN_CID) obj.put("lac", mLac); | ||
if (mCid != UNKNOWN_CID) obj.put("cid", mCid); | ||
if (mSignal != UNKNOWN_SIGNAL) obj.put("signal", mSignal); | ||
if (mAsu != UNKNOWN_SIGNAL) obj.put("asu", mAsu); | ||
if (mTa != UNKNOWN_CID) obj.put("ta", mTa); | ||
if (mPsc != UNKNOWN_CID) obj.put("psc", mPsc); | ||
} catch (JSONException jsonE) { | ||
throw new IllegalStateException(jsonE); | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
void reset() { | ||
mRadio = RADIO_GSM; | ||
mCellRadio = CELL_RADIO_GSM; | ||
mMcc = UNKNOWN_CID; | ||
mMnc = UNKNOWN_CID; | ||
mLac = UNKNOWN_CID; | ||
mCid = UNKNOWN_CID; | ||
mSignal = UNKNOWN_SIGNAL; | ||
mAsu = UNKNOWN_SIGNAL; | ||
mTa = UNKNOWN_CID; | ||
mPsc = UNKNOWN_CID; | ||
} | ||
|
||
void setRadio(int phoneType) { | ||
String radio = getRadioTypeName(phoneType); | ||
if (radio == null) { | ||
throw new IllegalArgumentException("Unexpected Phone Type: " + phoneType); | ||
} | ||
mRadio = radio; | ||
} | ||
|
||
void setCellLocation(CellLocation cl, | ||
int networkType, | ||
String networkOperator, | ||
Integer gsmSignalStrength, | ||
Integer cdmaRssi) { | ||
if (cl instanceof GsmCellLocation) { | ||
final int lac, cid; | ||
final GsmCellLocation gcl = (GsmCellLocation) cl; | ||
|
||
reset(); | ||
mCellRadio = getCellRadioTypeName(networkType); | ||
setNetworkOperator(networkOperator); | ||
|
||
lac = gcl.getLac(); | ||
cid = gcl.getCid(); | ||
if (lac >= 0) mLac = lac; | ||
if (cid >= 0) mCid = cid; | ||
|
||
if (Build.VERSION.SDK_INT >= 9) { | ||
final int psc = gcl.getPsc(); | ||
if (psc >= 0) mPsc = psc; | ||
} | ||
|
||
if (gsmSignalStrength != null) { | ||
mAsu = gsmSignalStrength; | ||
} | ||
} else if (cl instanceof CdmaCellLocation) { | ||
final CdmaCellLocation cdl = (CdmaCellLocation) cl; | ||
|
||
reset(); | ||
mCellRadio = getCellRadioTypeName(networkType); | ||
|
||
setNetworkOperator(networkOperator); | ||
|
||
mMnc = cdl.getSystemId(); | ||
|
||
mLac = cdl.getNetworkId(); | ||
mCid = cdl.getBaseStationId(); | ||
|
||
if (cdmaRssi != null) { | ||
mSignal = cdmaRssi; | ||
} | ||
} else { | ||
throw new IllegalArgumentException("Unexpected CellLocation type: " + cl.getClass().getName()); | ||
} | ||
} | ||
|
||
void setNeighboringCellInfo(NeighboringCellInfo nci, String networkOperator) { | ||
final int lac, cid, psc, rssi; | ||
|
||
reset(); | ||
mCellRadio = getCellRadioTypeName(nci.getNetworkType()); | ||
setNetworkOperator(networkOperator); | ||
|
||
lac = nci.getLac(); | ||
cid = nci.getCid(); | ||
psc = nci.getPsc(); | ||
rssi = nci.getRssi(); | ||
|
||
if (lac >= 0) mLac = lac; | ||
if (cid >= 0) mCid = cid; | ||
if (psc >= 0) mPsc = psc; | ||
if (rssi != NeighboringCellInfo.UNKNOWN_RSSI) mAsu = rssi; | ||
} | ||
|
||
void setNetworkOperator(String mccMnc) { | ||
if (mccMnc == null || mccMnc.length() < 5 || mccMnc.length() > 8) { | ||
throw new IllegalArgumentException("Bad mccMnc: " + mccMnc); | ||
} | ||
mMcc = Integer.parseInt(mccMnc.substring(0, 3)); | ||
mMnc = Integer.parseInt(mccMnc.substring(3)); | ||
} | ||
|
||
static String getCellRadioTypeName(int networkType) { | ||
String name; | ||
switch (networkType) { | ||
case TelephonyManager.NETWORK_TYPE_GPRS: | ||
case TelephonyManager.NETWORK_TYPE_EDGE: | ||
return CELL_RADIO_GSM; | ||
case TelephonyManager.NETWORK_TYPE_UMTS: | ||
case TelephonyManager.NETWORK_TYPE_HSDPA: | ||
case TelephonyManager.NETWORK_TYPE_HSUPA: | ||
case TelephonyManager.NETWORK_TYPE_HSPA: | ||
case TelephonyManager.NETWORK_TYPE_HSPAP: | ||
return CELL_RADIO_UMTS; | ||
case TelephonyManager.NETWORK_TYPE_LTE: | ||
return CELL_RADIO_LTE; | ||
case TelephonyManager.NETWORK_TYPE_EVDO_0: | ||
name = "CDMA - EvDo rev. 0"; | ||
break; | ||
case TelephonyManager.NETWORK_TYPE_EVDO_A: | ||
name = "CDMA - EvDo rev. A"; | ||
break; | ||
case TelephonyManager.NETWORK_TYPE_EVDO_B: | ||
name = "CDMA - EvDo rev. B"; | ||
break; | ||
case TelephonyManager.NETWORK_TYPE_1xRTT: | ||
name = "CDMA - 1xRTT"; | ||
break; | ||
case TelephonyManager.NETWORK_TYPE_EHRPD: | ||
name = "CDMA - eHRPD"; | ||
break; | ||
case TelephonyManager.NETWORK_TYPE_IDEN: | ||
name = "iDEN"; | ||
break; | ||
default: | ||
name = "UNKNOWN (" + String.valueOf(networkType) + ")"; | ||
break; | ||
} | ||
throw new IllegalArgumentException("Unexpected Network Type: " + name); | ||
} | ||
|
||
private static String getRadioTypeName(int phoneType) { | ||
switch (phoneType) { | ||
case TelephonyManager.PHONE_TYPE_CDMA: | ||
return RADIO_CDMA; | ||
|
||
case TelephonyManager.PHONE_TYPE_GSM: | ||
return RADIO_GSM; | ||
|
||
case TelephonyManager.PHONE_TYPE_NONE: | ||
case TelephonyManager.PHONE_TYPE_SIP: | ||
// These devices have no radio. | ||
default: | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.