Institute of Computer Science
  1. Courses
  2. 2020/21 fall
  3. Mobile Computing and Internet of Things (LTAT.06.009)
ET
Log in

Mobile Computing and Internet of Things 2020/21 fall

  • Main
  • Lectures
  • Labs
  • Homeworks & Home Assignments
  • Quizes
  • Task submission
  • Extra Materials
  • Projects
    • Teams & Topics
    • Presentations & Report
    • Grading & Submission
    package ee.ut.cs.lab9wprelive

    import android.Manifest
    import android.annotation.SuppressLint
    import android.content.Context
    import android.content.Context.LOCATION_SERVICE
    import android.content.pm.PackageManager
    import android.location.Location
    import android.location.LocationListener
    import android.location.LocationManager
    import android.os.Bundle
    import android.util.Log
    import androidx.core.app.ActivityCompat

    class LocationHelper(private val mContext: Context) : LocationListener {

        companion object {
            const val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 10 // meters
            // The minimum time between updates in milliseconds:
            const val MIN_TIME_BW_UPDATES = (1000 * 60 * 1).toLong() // 1 minute
        }
        // Flags
        var isGPSEnabled = false
        var isNetworkEnabled = false

        var locationManager: LocationManager

        init {
            locationManager = mContext.getSystemService(LOCATION_SERVICE) as LocationManager
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)

        }

        @SuppressLint("MissingPermission")
        fun getCurrenLocationUsingNetwork() : Location?{
            if (!isNetworkEnabled) {
                Log.v("TAG ","Network not enabled!")
                return null
            } else {
                locationManager.requestLocationUpdates(
                    LocationManager.NETWORK_PROVIDER,
                    MIN_TIME_BW_UPDATES,
                    MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
                )
                return locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER)
            }
        }

        @SuppressLint("MissingPermission")
        fun getCurrentLocationUsingGPS(): Location? {
            var location: Location? = null
            if (checkPermissions()) {
                Log.v("TAG ","No permission Granted")
            } else {
                if (!isGPSEnabled) {
                    Log.v("TAG ","GPS not enabled!")
                } else {
                    // If GPS enabled, get latitude/longitude using GPS Services
                    locationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES.toFloat(), this
                    )
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER)
                }
            }
            return location
        }

        private fun checkPermissions(): Boolean {
            return (ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_FINE_LOCATION ) != PackageManager.PERMISSION_GRANTED &&
                    ActivityCompat.checkSelfPermission( mContext, Manifest.permission.ACCESS_COARSE_LOCATION ) != PackageManager.PERMISSION_GRANTED)
        }



        override fun onLocationChanged(location: Location) {}
        override fun onProviderDisabled(provider: String) {}
        override fun onProviderEnabled(provider: String) {}
        override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}

    }
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment