下面列出了android.telephony.CellInfoWcdma#android.telephony.CellInfo 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void removeDuplicatedCells(List<CellInfo> cells) {
List<CellInfo> cellsToRemove = new ArrayList<CellInfo>();
Set<String> uniqueCellKeys = new HashSet<String>();
for (CellInfo cell : cells) {
String key = cellIdentityConverter.createCellKey(cell);
if (uniqueCellKeys.contains(key)) {
Timber.d("removeDuplicatedCells(): Remove duplicated cell: %s", key);
cellsToRemove.add(cell);
} else {
uniqueCellKeys.add(key);
}
}
cells.removeAll(cellsToRemove);
}
private static boolean isApi17CellInfoAvailable(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cells;
try {
cells = telephonyManager.getAllCellInfo();
} catch (SecurityException ex) {
Timber.d(ex, "isApi17CellInfoAvailable(): Result = coarse location permission is denied");
return false;
}
if (cells == null || cells.size() == 0) {
Timber.d("isApi17CellInfoAvailable(): Result = no cell info");
return false;
}
CellIdentityValidator validator = new CellIdentityValidator();
for (CellInfo cell : cells) {
if (validator.isValid(cell)) {
Timber.d("isApi17CellInfoAvailable(): Result = true");
return true;
}
}
Timber.d("isApi17CellInfoAvailable(): Result = false");
return false;
}
@Override
protected Void doInBackground(Void... voids) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
List<CellInfo> cellInfoList = telephonyManager.getAllCellInfo();
if (cellInfoList != null) {
for (CellInfo cellInfo : cellInfoList) {
if (cellInfo.isRegistered()) {
publishProgress(CellLocationSingleData.fromCellInfo(cellInfo));
}
}
}
} else {
CellLocation cellLocation = telephonyManager.getCellLocation();
publishProgress(CellLocationSingleData.fromCellLocation(cellLocation));
}
return null;
}
private void getWcdmaSignalStrength() {
List<CellInfo> cellInfoList = mTM.getAllCellInfo();
if (cellInfoList == null) {
//Log.e(Tag,"getAllCellInfo is null");
return;
}
//Log.e(Tag,"getAllCellInfo size "+cellInfoList.size());
for (CellInfo cellInfo : cellInfoList) {
if (!cellInfo.isRegistered())
continue;
if (cellInfo instanceof CellInfoWcdma) {
CellInfoWcdma wcdmaInfo = (CellInfoWcdma) cellInfo;
wcdma_RSSI = wcdmaInfo.getCellSignalStrength().getDbm();
}
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static Set<VisibleCell> getAllVisibleCellsPostJellyBeanMr1(
TelephonyManager telephonyManager) {
Set<VisibleCell> visibleCells = new HashSet<>();
// Retrieve visible cell networks
List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();
if (cellInfos == null) {
return visibleCells;
}
long elapsedTime = sTimeProvider.getElapsedRealtime();
long currentTime = sTimeProvider.getCurrentTime();
for (int i = 0; i < cellInfos.size(); i++) {
CellInfo cellInfo = cellInfos.get(i);
VisibleCell visibleCell =
getVisibleCellPostJellyBeanMr1(cellInfo, elapsedTime, currentTime);
if (visibleCell.radioType() != VisibleCell.UNKNOWN_RADIO_TYPE) {
visibleCells.add(visibleCell);
}
}
return visibleCells;
}
/**
* Returns a CellInfo object representing the currently registered base stations, containing
* its identity fields and signal strength. Null if no base station is active.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@Nullable
private static CellInfo getActiveCellInfo(TelephonyManager telephonyManager) {
int numRegisteredCellInfo = 0;
List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();
if (cellInfos == null) {
return null;
}
CellInfo result = null;
for (int i = 0; i < cellInfos.size(); i++) {
CellInfo cellInfo = cellInfos.get(i);
if (cellInfo.isRegistered()) {
numRegisteredCellInfo++;
if (numRegisteredCellInfo > 1) {
return null;
}
result = cellInfo;
}
}
// Only found one registered cellinfo, so we know which base station was used to measure
// network quality
return result;
}
/**
* @return Signal strength (in dbM) from {@link cellInfo}. Returns {@link
* CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength is unavailable.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static int getSignalStrengthDbm(CellInfo cellInfo) {
if (cellInfo instanceof CellInfoCdma) {
return ((CellInfoCdma) cellInfo).getCellSignalStrength().getDbm();
}
if (cellInfo instanceof CellInfoGsm) {
return ((CellInfoGsm) cellInfo).getCellSignalStrength().getDbm();
}
if (cellInfo instanceof CellInfoLte) {
return ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm();
}
if (cellInfo instanceof CellInfoWcdma) {
return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm();
}
return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
}
/**
* @return the signal level from {@link cellInfo}. Returns {@link
* CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal
* level is unavailable with lower value indicating lower signal strength.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static int getSignalStrengthLevel(CellInfo cellInfo) {
if (cellInfo instanceof CellInfoCdma) {
return ((CellInfoCdma) cellInfo).getCellSignalStrength().getLevel();
}
if (cellInfo instanceof CellInfoGsm) {
return ((CellInfoGsm) cellInfo).getCellSignalStrength().getLevel();
}
if (cellInfo instanceof CellInfoLte) {
return ((CellInfoLte) cellInfo).getCellSignalStrength().getLevel();
}
if (cellInfo instanceof CellInfoWcdma) {
return ((CellInfoWcdma) cellInfo).getCellSignalStrength().getLevel();
}
return CellularSignalStrengthError.ERROR_NOT_SUPPORTED;
}
private void requestPermission() {
if (Build.VERSION.SDK_INT >= 17) {
boolean isGranted = true;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (checkSelfPermission("android.permission.ACCESS_COARSE_LOCATION") == PackageManager.PERMISSION_DENIED) {
isGranted = false;
requestPermissions(new String[]{"android.permission.ACCESS_COARSE_LOCATION"}, 10086);
}
}
if (isGranted) {
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
try {
@SuppressLint("MissingPermission") List<CellInfo> list = telephonyManager.getAllCellInfo();
if (list != null) {
LogUtil.v(list);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public void notifyCellInfoForSubscriber(int subId, List<CellInfo> cellInfo) {
if (!checkNotifyPermission("notifyCellInfo()")) {
return;
}
if (VDBG) {
log("notifyCellInfoForSubscriber: subId=" + subId
+ " cellInfo=" + cellInfo);
}
int phoneId = SubscriptionManager.getPhoneId(subId);
synchronized (mRecords) {
if (validatePhoneId(phoneId)) {
mCellInfo.set(phoneId, cellInfo);
for (Record r : mRecords) {
if (validateEventsAndUserLocked(r, PhoneStateListener.LISTEN_CELL_INFO) &&
idMatch(r.subId, subId, phoneId) &&
checkLocationAccess(r)) {
try {
if (DBG_LOC) {
log("notifyCellInfo: mCellInfo=" + cellInfo + " r=" + r);
}
r.callback.onCellInfoChanged(cellInfo);
} catch (RemoteException ex) {
mRemoveList.add(r.binder);
}
}
}
}
handleRemoveListLocked();
}
}
private boolean handleUpdateCellInfo() {
List<CellInfo> cellInfos = mTelephonyManager.getAllCellInfo();
if (cellInfos == null) {
return false;
}
boolean stopScanningAfterScan = false;
for (CellInfo cellInfo : cellInfos) {
int mcc = 0;
if (cellInfo instanceof CellInfoGsm) {
mcc = ((CellInfoGsm) cellInfo).getCellIdentity().getMcc();
} else if (cellInfo instanceof CellInfoLte) {
mcc = ((CellInfoLte) cellInfo).getCellIdentity().getMcc();
} else if (cellInfo instanceof CellInfoWcdma) {
mcc = ((CellInfoWcdma) cellInfo).getCellIdentity().getMcc();
}
if (mccRequiresEmergencyAffordance(mcc)) {
setNetworkNeedsEmergencyAffordance(true);
return true;
} else if (mcc != 0 && mcc != Integer.MAX_VALUE) {
// we found an mcc that isn't in the list, abort
stopScanningAfterScan = true;
}
}
if (stopScanningAfterScan) {
stopScanning();
} else {
onCellScanFinishedUnsuccessful();
}
setNetworkNeedsEmergencyAffordance(false);
return false;
}
@Override
public Object onInvoke(Object originObject, Object proxyObject, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException {
if (!GpsMockManager.getInstance().isMocking()) {
return method.invoke(originObject, args);
}
return new ArrayList<CellInfo>();
}
private void logCellInfo(List<CellInfo> cellInfo){
if (logger != null) {
String message = getCellInfoString(cellInfo);
logger.log(message);
Log.d(TAG, message);
logger.log(System.lineSeparator());
}
}
@Override
public void onCellInfoChanged(List<CellInfo> cellInfo) {
super.onCellInfoChanged(cellInfo);
if (cellInfo == null) return;
logCellInfo(cellInfo);
}
/**
* Returns all observed cell information from all radios on the device including the primary and
* neighboring cells. Calling this method does not trigger a call to onCellInfoChanged(), or change
* the rate at which onCellInfoChanged() is called.
*/
private void getAllCellInfos(){
if(_telephonyManager != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
final List<CellInfo> cellInfos = _telephonyManager.getAllCellInfo();
processCellInfos(cellInfos);
}
else {
Log.w(TAG, "Unable to provide cell info due to version restriction");
}
}
private static void processCellInfos(List<CellInfo> cellInfos){
if(cellInfos != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
for(CellInfo cellInfo : cellInfos){
if(cellInfo instanceof CellInfoWcdma){
final CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoWCDMAJSON(cellInfoWcdma, _returnSignalStrength));
}
if(cellInfo instanceof CellInfoGsm){
final CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoGSMJSON(cellInfoGsm, _returnSignalStrength));
}
if(cellInfo instanceof CellInfoCdma){
final CellInfoCdma cellIdentityCdma = (CellInfoCdma) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoCDMAJSON(cellIdentityCdma, _returnSignalStrength));
}
if(cellInfo instanceof CellInfoLte){
final CellInfoLte cellInfoLte = (CellInfoLte) cellInfo;
sendCallback(PluginResult.Status.OK,
JSONHelper.cellInfoLTEJSON(cellInfoLte, _returnSignalStrength));
}
Log.d(TAG,cellInfo.toString());
}
}
else {
Log.e(TAG, "CellInfoLocation returning null. Is it supported on this phone?");
// There are several reasons as to why cell location data would be null.
// * could be an older device running an unsupported version of the Android OS
// * could be a device that doesn't support this capability.
// * could be incorrect permissions: ACCESS_COARSE_LOCATION
sendCallback(PluginResult.Status.ERROR,
JSONHelper.errorJSON(CELLINFO_PROVIDER, ErrorMessages.CELL_DATA_IS_NULL()));
}
}
public boolean isValid(CellInfo cellInfo) {
if (cellInfo instanceof CellInfoGsm) {
CellInfoGsm gsmCellInfo = (CellInfoGsm) cellInfo;
// If is compatible with API 17 hack (PSC on GSM) return true
boolean wcdmaApi17Valid = getWcdmaValidator().isValid(gsmCellInfo.getCellIdentity());
if (wcdmaApi17Valid)
return true;
return getGsmValidator().isValid(gsmCellInfo.getCellIdentity());
}
if (cellInfo instanceof CellInfoWcdma) {
CellInfoWcdma wcdmaCellInfo = (CellInfoWcdma) cellInfo;
return getWcdmaValidator().isValid(wcdmaCellInfo.getCellIdentity());
}
if (cellInfo instanceof CellInfoLte) {
CellInfoLte lteCellInfo = (CellInfoLte) cellInfo;
return getLteValidator().isValid(lteCellInfo.getCellIdentity());
}
if (cellInfo instanceof CellInfoCdma) {
CellInfoCdma cdmaCellInfo = (CellInfoCdma) cellInfo;
return getCdmaValidator().isValid(cdmaCellInfo.getCellIdentity());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && cellInfo instanceof CellInfoNr) {
CellInfoNr nrCellInfo = (CellInfoNr) cellInfo;
return getNrValidator().isValid((CellIdentityNr) nrCellInfo.getCellIdentity());
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && cellInfo instanceof CellInfoTdscdma) {
CellInfoTdscdma tdscdmaCellInfo = (CellInfoTdscdma) cellInfo;
return getTdscdmaValidator().isValid(tdscdmaCellInfo.getCellIdentity());
}
throw new UnsupportedOperationException("Cell identity type not supported `" + cellInfo.getClass().getName() + "`");
}
public synchronized void setLastCellInfo(List<CellInfo> cellInfo) {
Timber.d("setLastCellInfo(): Cell info updated: %s ", cellInfo);
this.lastCellInfo = cellInfo;
this.lastNetworkType = NetworkGroup.Unknown;
this.lastOperatorName = null;
notifyIfReadyToProcess();
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private static VisibleCell getConnectedCellPostJellyBeanMr1(
Context context, TelephonyManager telephonyManager) {
if (!hasLocationPermission(context)) {
return VisibleCell.UNKNOWN_MISSING_LOCATION_PERMISSION_VISIBLE_CELL;
}
CellInfo cellInfo = getActiveCellInfo(telephonyManager);
return getVisibleCellPostJellyBeanMr1(
cellInfo, sTimeProvider.getElapsedRealtime(), sTimeProvider.getCurrentTime());
}
/**
* @return Signal strength (in dbM) for the currently registered cellular network. Returns
* {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength is
* unavailable or if there are multiple cellular radios on the device.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@CalledByNative
public static int getSignalStrengthDbm() {
List<CellInfo> cellInfos = getRegisteredCellInfo();
return cellInfos == null || cellInfos.size() != 1
? CellularSignalStrengthError.ERROR_NOT_SUPPORTED
: getSignalStrengthDbm(cellInfos.get(0));
}
/**
* @return the signal strength level (between 0 and 4, both inclusive) for the currently
* registered cellular network with lower value indicating lower signal strength. Returns
* {@link CellularSignalStrengthError#ERROR_NOT_SUPPORTED} if the signal strength level is
* unavailable or if there are multiple cellular radios on the device.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@CalledByNative
public static int getSignalStrengthLevel() {
List<CellInfo> cellInfos = getRegisteredCellInfo();
return cellInfos == null || cellInfos.size() != 1
? CellularSignalStrengthError.ERROR_NOT_SUPPORTED
: getSignalStrengthLevel(cellInfos.get(0));
}
/**
* Returns all observed cell information from all radios on the device including the primary
* and neighboring cells. Returns only the information of cells that are registered to a
* mobile network. May return {@code null}.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
private static List<CellInfo> getRegisteredCellInfo() {
if (!isAPIAvailable()) {
return null;
}
TelephonyManager telephonyManager =
(TelephonyManager) ContextUtils.getApplicationContext().getSystemService(
Context.TELEPHONY_SERVICE);
if (telephonyManager == null) {
return null;
}
List<CellInfo> cellInfos = telephonyManager.getAllCellInfo();
if (cellInfos == null) {
return null;
}
Iterator<CellInfo> iter = cellInfos.iterator();
while (iter.hasNext()) {
if (!iter.next().isRegistered()) {
iter.remove();
}
}
return cellInfos;
}
@SuppressWarnings("deprecation")
private synchronized void fallbackScan() {
if (lastScan + MIN_UPDATE_INTERVAL > System.currentTimeMillis()) return;
List<CellInfo> allCellInfo = telephonyManager.getAllCellInfo();
if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) {
allCellInfo = new ArrayList<CellInfo>();
CellLocation cellLocation = telephonyManager.getCellLocation();
CellInfo cellInfo = fromCellLocation(cellLocation);
if (cellInfo != null) allCellInfo.add(cellInfo);
}
onCellsChanged(allCellInfo);
}
@SuppressLint("MissingPermission")
private void getAllCellInfo() {
if (telephonyManager != null) {
List<CellInfo> cellInfo = null;
if (Permissions.checkLocation(context.getApplicationContext()))
cellInfo = telephonyManager.getAllCellInfo();
//PPApplication.logE("PhoneStateScanner.getAllCellInfo.2", "cellInfo="+cellInfo);
getAllCellInfo(cellInfo);
}
}
private void shareCellInfo(com.spideyapp.sqlite.model.CellInfo cellInfo) {
Intent intent = new Intent(NOTIFICATION);
intent.putExtra("cid",cellInfo.getCID());
intent.putExtra("dbm",cellInfo.getDBM());
sendBroadcast(intent);
}
public HashMap<String, Integer> getAllCellInfoSignalStrength(){
if(!(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION)==PackageManager.PERMISSION_GRANTED)){
return null;
}
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
List<CellInfo> cellInfos = (List<CellInfo>) telephonyManager.getAllCellInfo();
HashMap<String, Integer> results = new HashMap<String, Integer>();
if(cellInfos==null){
return results;
}
for(CellInfo cellInfo : cellInfos){
if(cellInfo.getClass().equals(CellInfoLte.class)){
results.put("LTE", ((CellInfoLte) cellInfo).getCellSignalStrength().getDbm());
}else if(cellInfo.getClass().equals(CellInfoGsm.class)){
results.put("GSM", ((CellInfoGsm) cellInfo).getCellSignalStrength().getDbm());
}else if(cellInfo.getClass().equals(CellInfoCdma.class)){
results.put("CDMA", ((CellInfoCdma) cellInfo).getCellSignalStrength().getDbm());
}else if(cellInfo.getClass().equals(CellInfoWcdma.class)){
results.put("WCDMA", ((CellInfoWcdma) cellInfo).getCellSignalStrength().getDbm());
}
}
return results;
}
@SuppressWarnings("deprecation")
private synchronized void fallbackScan() {
if (lastScan + MIN_UPDATE_INTERVAL > System.currentTimeMillis()) return;
List<CellInfo> allCellInfo = telephonyManager.getAllCellInfo();
if ((allCellInfo == null || allCellInfo.isEmpty()) && telephonyManager.getNetworkType() > 0) {
allCellInfo = new ArrayList<CellInfo>();
CellLocation cellLocation = telephonyManager.getCellLocation();
CellInfo cellInfo = fromCellLocation(cellLocation);
if (cellInfo != null) allCellInfo.add(cellInfo);
}
onCellsChanged(allCellInfo);
}
/**
* Adds or updates a list of cell towers.
* <p>
* This method first calls {@link #removeSource(int)} with
* {@link com.vonglasow.michael.satstat.data.CellTower#SOURCE_CELL_INFO} as
* its argument. Then it iterates through all entries in {@code cells} and
* updates each entry that is of type {@link android.telephony.CellInfoCdma}
* by calling {@link #update(CellInfoCdma)}, passing that entry as the
* argument.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public void updateAll(List<CellInfo> cells) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
return;
this.removeSource(CellTower.SOURCE_CELL_INFO);
if (cells == null)
return;
for (CellInfo cell : cells)
if (cell instanceof CellInfoCdma)
this.update((CellInfoCdma) cell);
}
/**
* Adds or updates a list of cell towers.
* <p>
* This method first calls {@link #removeSource(int)} with
* {@link com.vonglasow.michael.satstat.data.CellTower#SOURCE_CELL_INFO} as
* its argument. Then it iterates through all entries in {@code cells} and
* updates each entry that is of type {@link android.telephony.CellInfoLte}
* by calling {@link #update(CellInfoLte)}, passing that entry as the
* argument.
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public void updateAll(List<CellInfo> cells) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
return;
this.removeSource(CellTower.SOURCE_CELL_INFO);
if (cells == null)
return;
for (CellInfo cell : cells)
if (cell instanceof CellInfoLte)
this.update((CellInfoLte) cell);
}
/**
* Adds or updates a list of cell towers.
* <p>
* This method first calls {@link #removeSource(int)} with
* {@link com.vonglasow.michael.satstat.data.CellTower#SOURCE_CELL_INFO} as
* its argument. Then it iterates through all entries in {@code cells} and
* updates each entry that is of type {@link android.telephony.CellInfoGsm}
* or {@link android.telephony.CellInfoWcdma} by calling
* {@link #update(CellInfoGsm)} or {@link #update(CellInfoWcdma)}
* (depending on type), passing that entry as the argument.
*/
public void updateAll(List<CellInfo> cells) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
return;
this.removeSource(CellTower.SOURCE_CELL_INFO);
if (cells == null)
return;
for (CellInfo cell : cells)
if (cell instanceof CellInfoGsm)
this.update((CellInfoGsm) cell);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
if (cell instanceof CellInfoWcdma)
this.update((CellInfoWcdma) cell);
}