Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 69da8b9d authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Location: Fix exceptions in some cases

Fixes #1953
parent d56b6a08
Loading
Loading
Loading
Loading
+15 −11
Original line number Diff line number Diff line
@@ -28,19 +28,23 @@ class CellDetailsSource(private val context: Context, private val callback: Cell
                    if (details.isNotEmpty()) callback.onCellDetailsAvailable(details)
                }
            })
            return
        } else if (SDK_INT >= 17) {
            val details = telephonyManager.allCellInfo.map(CellInfo::toCellDetails).map { it.repair(context) }.filter(CellDetails::isValid)
            if (details.isNotEmpty()) callback.onCellDetailsAvailable(details)
        } else {
            val allCellInfo = telephonyManager.allCellInfo
            if (allCellInfo != null) {
                val details = allCellInfo.map(CellInfo::toCellDetails).map { it.repair(context) }.filter(CellDetails::isValid)
                if (details.isNotEmpty()) {
                    callback.onCellDetailsAvailable(details)
                    return
                }
            }
        }
        val networkOperator = telephonyManager.networkOperator
            var mcc: Int? = null
            var mnc: Int? = null
        if (networkOperator != null && networkOperator.length > 4) {
                mcc = networkOperator.substring(0, 3).toIntOrNull()
                mnc = networkOperator.substring(3).toIntOrNull()
            }
            val detail = telephonyManager.cellLocation.toCellDetails(mcc, mnc)
            if (detail.isValid) callback.onCellDetailsAvailable(listOf(detail))
            val mcc = networkOperator.substring(0, 3).toIntOrNull()
            val mnc = networkOperator.substring(3).toIntOrNull()
            val detail = telephonyManager.cellLocation?.toCellDetails(mcc, mnc)
            if (detail?.isValid == true) callback.onCellDetailsAvailable(listOf(detail))
        }
    }

+17 −6
Original line number Diff line number Diff line
@@ -12,6 +12,7 @@ import android.net.Uri
import android.os.Build.VERSION.SDK_INT
import android.os.Bundle
import android.text.format.DateUtils
import android.util.Log
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
@@ -23,6 +24,7 @@ import org.microg.gms.location.core.R
import org.microg.gms.location.manager.LocationAppsDatabase
import org.microg.gms.ui.AppHeadingPreference
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

class LocationAppFragment : PreferenceFragmentCompat() {
@@ -86,15 +88,24 @@ class LocationAppFragment : PreferenceFragmentCompat() {
                lastLocation.title = DateUtils.getRelativeTimeSpanString(location.time)
                lastLocation.intent = Intent(Intent.ACTION_VIEW, Uri.parse("geo:${location.latitude},${location.longitude}"))
                lastLocationMap.location = location
                val address = if (SDK_INT > 33) {
                val address = try {
                    if (SDK_INT > 33) {
                        suspendCoroutine { continuation ->
                            try {
                                Geocoder(context).getFromLocation(location.latitude, location.longitude, 1) {
                                    continuation.resume(it.firstOrNull())
                                }
                            } catch (e: Exception) {
                                continuation.resumeWithException(e)
                            }
                        }
                    } else {
                        withContext(Dispatchers.IO) { Geocoder(context).getFromLocation(location.latitude, location.longitude, 1)?.firstOrNull() }
                    }
                } catch (e: Exception) {
                    Log.w(TAG, e)
                    null
                }
                if (address != null) {
                    val addressLine = StringBuilder()
                    var i = 0