Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>KotlinLastKnownLocation</name>
<comment>Project KotlinLastKnownLocation created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Mon Dec 18 09:47:01 BRT 2017
connection.project.dir=
6 changes: 6 additions & 0 deletions app/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8/"/>
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
<classpathentry kind="output" path="bin"/>
</classpath>
23 changes: 23 additions & 0 deletions app/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>app</name>
<comment>Project app created by Buildship.</comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
</natures>
</projectDescription>
2 changes: 2 additions & 0 deletions app/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#Mon Dec 18 09:47:01 BRT 2017
connection.project.dir=..
50 changes: 47 additions & 3 deletions app/src/main/java/com/pranay/lastknownlocation/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ import com.google.android.gms.tasks.OnFailureListener
import com.google.android.gms.tasks.OnSuccessListener
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import android.location.LocationManager
import android.R.string.cancel
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.support.v7.app.AlertDialog


/**
* Created by Pranay on 7/16/2017.
Expand All @@ -38,15 +45,38 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT)
if (checkPermissions()) {
if (checkPermissionAccessFineLocation()) {
initLocationUpdate()
}
btnGetLocation.setOnClickListener {
if (checkPermissions()) {
if (checkPermissionAccessFineLocation()) {
initLocationUpdate()
}
}

val locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager

if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show()
} else {
showGPSDisabledAlertToUser()
}
}

private fun showGPSDisabledAlertToUser() {
val alertDialogBuilder = AlertDialog.Builder(this)
alertDialogBuilder.setMessage("GPS está desligado no seu dispositivo, gostaria de habilitá-lo?")
.setCancelable(false)
.setPositiveButton("Ir para configurações e ativar o GPS",
DialogInterface.OnClickListener { dialog, id ->
val callGPSSettingIntent = Intent(
android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS)
startActivity(callGPSSettingIntent)
})
alertDialogBuilder.setNegativeButton("Não, obrigado!",
DialogInterface.OnClickListener { dialog, id -> dialog.cancel() })
val alert = alertDialogBuilder.create()
alert.show()
}

@SuppressLint("MissingPermission")
Expand Down Expand Up @@ -120,11 +150,25 @@ class MainActivity : AppCompatActivity() {
})
}

private fun checkPermissions(): Boolean {
private fun checkPermissionAccessFineLocation(): Boolean {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Already had permission for ACCESS_FINE_LOCATION")
return true
} else {
Log.i(TAG, "Has NO permission for ACCESS_FINE_LOCATION")
requestPermissions()
return false
}
}

private fun checkPermissionAccessCoarseLocation(): Boolean {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Already had permission for ACCESS_FINE_LOCATION")
return true
} else {
Log.i(TAG, "Has NO permission for ACCESS_FINE_LOCATION")
requestPermissions()
return false
}
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0-alpha5'
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sun Jul 16 16:47:31 IST 2017
#Mon Dec 18 10:01:30 BRT 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-milestone-1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
Empty file modified gradlew
100644 → 100755
Empty file.