From 812e2c1754156135af942ba18df288875d63a9f8 Mon Sep 17 00:00:00 2001 From: Nabin Khatiwada Date: Sun, 12 Jul 2020 10:49:19 +0545 Subject: [PATCH] null safety operator added in replacement of requireActivity --- .../androidbolts/library/LocationManager.kt | 1 + .../androidbolts/library/gps/GpsManager.kt | 43 +++++++++++++------ .../androidbolts/library/gps/GpsProvider.kt | 4 +- 3 files changed, 32 insertions(+), 16 deletions(-) diff --git a/library/src/main/java/com/androidbolts/library/LocationManager.kt b/library/src/main/java/com/androidbolts/library/LocationManager.kt index 017b444..cf2dde9 100644 --- a/library/src/main/java/com/androidbolts/library/LocationManager.kt +++ b/library/src/main/java/com/androidbolts/library/LocationManager.kt @@ -144,6 +144,7 @@ class LocationManager internal constructor( } @ExperimentalSharedPrefs + /** This is currently is experimental state so it may not work. */ fun getLastUpdatedLocation(): LocationModel? { return prefs?.getLocationModel() } diff --git a/library/src/main/java/com/androidbolts/library/gps/GpsManager.kt b/library/src/main/java/com/androidbolts/library/gps/GpsManager.kt index 36579f5..9373dba 100644 --- a/library/src/main/java/com/androidbolts/library/gps/GpsManager.kt +++ b/library/src/main/java/com/androidbolts/library/gps/GpsManager.kt @@ -17,6 +17,7 @@ import com.androidbolts.library.R import com.androidbolts.library.utils.LocationConstants import com.androidbolts.library.utils.LocationConstants.REQUEST_CHECK_SETTINGS import com.androidbolts.library.utils.LocationConstants.TIME_OUT_NONE +import com.androidbolts.library.utils.orElse import com.androidbolts.library.utils.showLoadingDialog import com.google.android.gms.common.api.ApiException import com.google.android.gms.common.api.ResolvableApiException @@ -79,17 +80,22 @@ internal class GpsManager internal constructor() : GpsProvider() { private fun initFusedAndSettingClient() { when { getFragment() != null -> { - mFusedLocationClient = - LocationServices.getFusedLocationProviderClient(getFragment()?.activity!!) - mSettingsClient = - LocationServices.getSettingsClient(getFragment()!!.requireActivity()) + val activity = getFragment()?.activity + activity?.let { + mFusedLocationClient = + LocationServices.getFusedLocationProviderClient(activity) + mSettingsClient = + LocationServices.getSettingsClient(activity) + }.orElse { + Log.i("Activity:", activity.toString()) + } } getActivity() != null -> { mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getActivity()!!) mSettingsClient = LocationServices.getSettingsClient(getActivity()!!) } - else -> Log.d("LocationManager", "Host is invalid.") + else -> Log.i("LocationManager", "Host is invalid.") } } @@ -163,15 +169,24 @@ internal class GpsManager internal constructor() : GpsProvider() { try { val rae = it as ResolvableApiException when { - getFragment() != null -> rae.startResolutionForResult( - getFragment()?.requireActivity(), - REQUEST_CHECK_SETTINGS - ) - getActivity() != null -> rae.startResolutionForResult( - getActivity(), - REQUEST_CHECK_SETTINGS - ) - else -> Log.d("Invalid host", "Host is invalid.") + getFragment() != null -> { + val activity = getFragment()?.activity + activity?.let { fragmentActivity -> + rae.startResolutionForResult( + fragmentActivity, + REQUEST_CHECK_SETTINGS + ) + } + } + getActivity() != null -> { + rae.startResolutionForResult( + getActivity(), + REQUEST_CHECK_SETTINGS + ) + } + else -> { + Log.d("Invalid host", "Host is invalid.") + } } } catch (sie: IntentSender.SendIntentException) { Log.i("LocationManager", "PendingIntent unable to execute request.") diff --git a/library/src/main/java/com/androidbolts/library/gps/GpsProvider.kt b/library/src/main/java/com/androidbolts/library/gps/GpsProvider.kt index 3491429..0ea6399 100644 --- a/library/src/main/java/com/androidbolts/library/gps/GpsProvider.kt +++ b/library/src/main/java/com/androidbolts/library/gps/GpsProvider.kt @@ -42,12 +42,12 @@ abstract class GpsProvider { @Nullable protected fun getActivity(): Activity? { - return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()!!.activity + return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()?.activity } @Nullable protected fun getFragment(): Fragment? { - return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()!!.fragment + return if (weakContextProcessor.get() == null) null else weakContextProcessor.get()?.fragment } fun getLocationListener(): LocationListener? {