Skip to content

Commit

Permalink
location issue resolved (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
nawinkhatiwada authored Dec 30, 2019
1 parent b040539 commit d819068
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.location.Location
import androidx.fragment.app.Fragment
import com.androidbolts.library.LocationListener
import com.androidbolts.library.LocationManager
import com.androidbolts.library.LocationModel
import com.androidbolts.library.utils.LocationConstants

abstract class BaseFragment : Fragment(), LocationListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import com.androidbolts.library.LocationManager
import com.androidbolts.library.gps.GpsProvider
import com.androidbolts.locationmanager.R
import com.androidbolts.locationmanager.base.BaseFragment

Expand All @@ -18,6 +17,7 @@ class LocationFragment private constructor() : BaseFragment() {
private var location: Location? = null
private var locationManager: LocationManager? = null
private val tvLocation by lazy { activity?.findViewById<TextView>(R.id.tv_current_location) }
private var isObserverAdded = false

companion object {
fun getInstance() = LocationFragment()
Expand All @@ -31,20 +31,22 @@ class LocationFragment private constructor() : BaseFragment() {
val view = inflater.inflate(R.layout.fragment_location, container, false)
val btnGetLocation = view.findViewById<Button>(R.id.btn_get_loc)
locationManager = initLocationManager()
lifecycle.addObserver(locationManager!!)

btnGetLocation.setOnClickListener {
getLocation()
}
return view
}


private fun getLocation() {
locationManager?.let {
if (!it.isLoadingSet()) {
locationManager?.setShowLoading(true)
}
if (!isObserverAdded) {
lifecycle.addObserver(it)
isObserverAdded = true
}
locationManager?.getLocation()
}
}
Expand Down
22 changes: 19 additions & 3 deletions library/src/main/java/com/androidbolts/library/LocationManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.androidbolts.library

import android.app.Activity
import android.content.Context
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
Expand Down Expand Up @@ -96,17 +97,32 @@ class LocationManager private constructor(
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
fun onCreate() {
locationListener?.let {
gpsProvider.onCreate()
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
fun onResume() {
locationListener?.let {
if (permissionManager.hasPermission()) {
gpsProvider.onResume()
val hasPermission = permissionManager.hasPermission()
Log.i("Has Permission", hasPermission.toString())
val isProviderEnabled = permissionManager.isProviderEnabled()
Log.i("isProviderEnabled", isProviderEnabled.toString())
if (hasPermission) {
if (isProviderEnabled) {
gpsProvider.onResume()
}else {
gpsProvider.enableGps()
}
}
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
internal fun onPause() {
fun onPause() {
locationListener?.let {
gpsProvider.onPause()
}
Expand Down
80 changes: 40 additions & 40 deletions library/src/main/java/com/androidbolts/library/gps/GpsManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.LocationSettingsRequest
import com.google.android.gms.location.LocationSettingsStatusCodes
import com.google.android.gms.location.SettingsClient
import java.lang.ref.WeakReference

internal class GpsManager private constructor() : GpsProvider() {
private var mFusedLocationClient: FusedLocationProviderClient? = null
Expand All @@ -40,7 +39,6 @@ internal class GpsManager private constructor() : GpsProvider() {
private var dialog: AlertDialog? = null
private var mRequestingLocationUpdates: Boolean = false


companion object {
private var gpsManager: GpsManager? = null
fun getGpsManager(): GpsManager {
Expand All @@ -51,35 +49,36 @@ internal class GpsManager private constructor() : GpsProvider() {
}
}

override fun onResume() {
override fun onCreate() {
initFusedAndSettingClient()
createLocationCallback()
createLocationRequest()
buildLocationSettingsRequest()
}

override fun get() {
if (!mRequestingLocationUpdates) {
startLocationUpdates()
mRequestingLocationUpdates = true
Log.d("Tag", "onResume called")
}
}

override fun onResume() {
get()
Log.d("Tag", "onResume called")
}

override fun onPause() {
stopLocationUpdates()
Log.d("Tag", "onPause called")
}

override fun onDestroy() {
stopLocationUpdates()
mCurrentLocation = null
Log.d("Tag", "onDestroy called")
}

override fun get() {
getLocation()
}

private fun getLocation() {
if (mSettingsClient == null && mFusedLocationClient == null) {
initFusedAndSettingClient()
}
setupLocationBasic()
}

private fun initFusedAndSettingClient() {
when {
getFragment() != null -> {
Expand All @@ -97,16 +96,7 @@ internal class GpsManager private constructor() : GpsProvider() {
}
}

private fun setupLocationBasic() {
createLocationRequest()
createLocationCallback()
buildLocationSettingsRequest()
}

private fun createLocationRequest() {
if (mCurrentLocation == null || getFragment() != null || getActivity() != null) {
showDialog()
}
mLocationRequest.interval = LocationConstants.UPDATE_INTERVAL_IN_MILLISECONDS
mLocationRequest.fastestInterval =
LocationConstants.FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS
Expand Down Expand Up @@ -149,20 +139,21 @@ internal class GpsManager private constructor() : GpsProvider() {

@SuppressLint("MissingPermission")
private fun startLocationUpdates() {
if (mLocationSettingsRequest == null) {
buildLocationSettingsRequest()
}

if (mSettingsClient == null || mFusedLocationClient == null) {
initFusedAndSettingClient()
mSettingsClient?.checkLocationSettings(mLocationSettingsRequest)?.addOnCompleteListener {
if (it.isSuccessful) {
if (mCurrentLocation == null && (getFragment() != null || getActivity() != null)) {
showDialog()
}
mFusedLocationClient?.requestLocationUpdates(
mLocationRequest,
mLocationCallback, Looper.myLooper()
)
}
}
mSettingsClient?.checkLocationSettings(mLocationSettingsRequest)?.addOnSuccessListener {
mFusedLocationClient?.requestLocationUpdates(
mLocationRequest,
mLocationCallback, Looper.myLooper()
)
}

}?.addOnFailureListener {
override fun enableGps() {
mSettingsClient?.checkLocationSettings(mLocationSettingsRequest)?.addOnFailureListener {
when ((it as ApiException).statusCode) {
LocationSettingsStatusCodes.RESOLUTION_REQUIRED -> {
Log.i(
Expand Down Expand Up @@ -191,7 +182,6 @@ internal class GpsManager private constructor() : GpsProvider() {
"Location settings are inadequate, and cannot be " + "fixed here. Fix in Settings."
Log.e("LocationManager", errorMessage)
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show()
mRequestingLocationUpdates = false
}
}
}
Expand All @@ -207,7 +197,7 @@ internal class GpsManager private constructor() : GpsProvider() {
getActivity()?.let { context ->
dialog = showLoadingDialog(context, "Fetching Location", "Please wait...",
false, onPositiveButtonClicked = {
setupLocationBasic()
startLocationUpdates()
}, onNegativeButtonClicked = {
stopLocationUpdates()
dismissDialog()
Expand All @@ -226,7 +216,13 @@ internal class GpsManager private constructor() : GpsProvider() {
}, getTimeOut())
}
}
dialog?.show()
dialog?.let { loadingDialog ->
if (!loadingDialog.isShowing) {
dialog?.show()
}else {
dialog?.dismiss()
}
}
}
}
}
Expand All @@ -239,7 +235,11 @@ internal class GpsManager private constructor() : GpsProvider() {
}

private fun dismissDialog() {
dialog?.dismiss()
dialog?.let {
if (it.isShowing) {
dialog?.dismiss()
}
}
}

private fun isLocationAvailable(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ abstract class GpsProvider {
private var showDialog: Boolean = false
private var timeOut: Long = TIME_OUT_NONE
private var prefs: PreferenceManager? = null
abstract fun onCreate()
abstract fun onResume()
abstract fun onPause()
abstract fun onDestroy()
abstract fun get()
abstract fun enableGps()

fun setContextProcessor(contextProcessor: ContextProcessor) {
this.weakContextProcessor = WeakReference(contextProcessor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.androidbolts.library.permissions
import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.Context.LOCATION_SERVICE
import android.content.Intent
import android.content.pm.PackageManager
import android.location.LocationManager
import android.location.LocationManager.GPS_PROVIDER
import android.net.Uri
import android.provider.Settings
import android.util.Log
Expand Down Expand Up @@ -114,4 +117,14 @@ abstract class PermissionManager {
}
}
}

fun isProviderEnabled(): Boolean {
val ctx = if (getFragment() != null)
getFragment()?.requireActivity()
else
getActivity()
val service = ctx?.getSystemService(LOCATION_SERVICE) as LocationManager?
val isEnabled = service?.isProviderEnabled(GPS_PROVIDER)
return isEnabled ?: false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object LocationConstants {
const val LOCATION_PERMISSIONS_REQUEST_CODE = 1
const val UPDATE_INTERVAL_IN_MILLISECONDS: Long = 10000
const val FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS =
UPDATE_INTERVAL_IN_MILLISECONDS / 2
UPDATE_INTERVAL_IN_MILLISECONDS / 3
const val REQUEST_CHECK_SETTINGS = 0x1
const val PREF_FILE = "LocationManager"
const val KEY_LOCATION_MODEL = "location_model"
Expand Down

0 comments on commit d819068

Please sign in to comment.