Skip to content

Commit

Permalink
Location: Fix exceptions in CellDetailsSource when the ACCESS_FINE_LO…
Browse files Browse the repository at this point in the history
…CATION permission isn't granted (microg#2220)
  • Loading branch information
ale5000-git authored Mar 10, 2024
1 parent 1bad8b1 commit df0b759
Showing 1 changed file with 30 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ import android.os.Build.VERSION.SDK_INT
import android.os.WorkSource
import android.telephony.CellInfo
import android.telephony.TelephonyManager
import android.util.Log
import androidx.core.content.getSystemService

private const val TAG = "CellDetailsSource"

class CellDetailsSource(private val context: Context, private val callback: CellDetailsCallback) {
fun enable() = Unit
fun disable() = Unit
Expand All @@ -21,15 +24,27 @@ class CellDetailsSource(private val context: Context, private val callback: Cell
fun startScan(workSource: WorkSource?) {
val telephonyManager = context.getSystemService<TelephonyManager>() ?: return
if (SDK_INT >= 29) {
telephonyManager.requestCellInfoUpdate(context.mainExecutor, object : TelephonyManager.CellInfoCallback() {
override fun onCellInfo(cells: MutableList<CellInfo>) {
val details = cells.map(CellInfo::toCellDetails).map { it.repair(context) }.filter(CellDetails::isValid)
if (details.isNotEmpty()) callback.onCellDetailsAvailable(details)
}
})
try {
telephonyManager.requestCellInfoUpdate(context.mainExecutor, object : TelephonyManager.CellInfoCallback() {
override fun onCellInfo(cells: MutableList<CellInfo>) {
val details = cells.map(CellInfo::toCellDetails).map { it.repair(context) }.filter(CellDetails::isValid)
if (details.isNotEmpty()) callback.onCellDetailsAvailable(details)
}
})
} catch (e: SecurityException) {
// It may trigger a SecurityException if the ACCESS_FINE_LOCATION permission isn't granted
Log.w(TAG, "requestCellInfoUpdate in startScan failed", e)
}

return
} else if (SDK_INT >= 17) {
val allCellInfo = telephonyManager.allCellInfo
val allCellInfo: List<CellInfo>? = try {
telephonyManager.allCellInfo
} catch (e: SecurityException) {
// It may trigger a SecurityException if the ACCESS_FINE_LOCATION permission isn't granted
Log.w(TAG, "allCellInfo in startScan failed", e)
null
}
if (allCellInfo != null) {
val details = allCellInfo.map(CellInfo::toCellDetails).map { it.repair(context) }.filter(CellDetails::isValid)
if (details.isNotEmpty()) {
Expand All @@ -42,12 +57,18 @@ class CellDetailsSource(private val context: Context, private val callback: Cell
if (networkOperator != null && networkOperator.length > 4) {
val mcc = networkOperator.substring(0, 3).toIntOrNull()
val mnc = networkOperator.substring(3).toIntOrNull()
val detail = telephonyManager.cellLocation?.toCellDetails(mcc, mnc)
val detail: CellDetails? = try {
telephonyManager.cellLocation?.toCellDetails(mcc, mnc)
} catch (e: SecurityException) {
// It may trigger a SecurityException if the ACCESS_FINE_LOCATION permission isn't granted
Log.w(TAG, "cellLocation in startScan failed", e)
null
}
if (detail?.isValid == true) callback.onCellDetailsAvailable(listOf(detail))
}
}

companion object {
fun create(context: Context, callback: CellDetailsCallback): CellDetailsSource = CellDetailsSource(context, callback)
}
}
}

0 comments on commit df0b759

Please sign in to comment.