NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

package com.android.geofencedemo

import android.Manifest
import android.app.Activity
import android.app.PendingIntent
import android.content.Intent
import android.content.pm.PackageManager
import android.location.Location
import android.os.Build
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.android.geofencedemo.receivers.GeoFenceBroadcastReceiver
import com.android.geofencedemo.utils.GoogleLocationManager
import com.google.android.gms.location.Geofence
import com.google.android.gms.location.GeofencingClient
import com.google.android.gms.location.GeofencingRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.tasks.OnCompleteListener


class MainActivity : AppCompatActivity() {

lateinit var locationManagerGoogle: GoogleLocationManager

private lateinit var geofencingClient: GeofencingClient
private val geofenceList = ArrayList<Geofence>()
private lateinit var pendingIntent: PendingIntent
private val TAG = "geod Geofence"

val PERMISSIONS = arrayOf(
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION
)

var myLocation: Location? = null

companion object {
const val HOME_GEOFENCE_ID = "homeGeoFenceId"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// TODO: 1.5 attach locationManager, geofencingClient, pendingIntent
locationManagerGoogle = GoogleLocationManager(applicationContext)

geofencingClient = LocationServices.getGeofencingClient(this)
pendingIntent = getPendingIntent()


if (!hasLocationPermission()) {
requestPermission()
}
handleGoogleLocation()
createGeoFenceForHome(24.6624, 46.7288, 10000f)
}

// TODO: 1.6 get device location update
fun handleGoogleLocation() {

locationManagerGoogle.checkLocationSettingsAndShowPopup(
this,
onSuccess =
{
locationManagerGoogle.registerLocationUpdates(onSuccess = {
val location: Location? = it
myLocation = location
printLocation(location)
})
}
)
}

fun printLocation(location: Location?) {
Log.i(
TAG,
"Fetched Location: latitude: ${location?.latitude} | longitude: ${location?.longitude}"
)
}

// TODO: 1.7 get PendingIntent and attached Broadcast Receiver
private fun getPendingIntent(): PendingIntent {
// The GeoFenceBroadcastReceiver class is a customized static broadcast class.
val intent = Intent(this, GeoFenceBroadcastReceiver::class.java)
intent.action = GeoFenceBroadcastReceiver.ACTION_PROCESS_LOCATION
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}

// TODO: 1.8 get GeofencingRequest, define initial triggers
private fun getGeofencingRequest(): GeofencingRequest? {
return GeofencingRequest.Builder().apply {
setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_ENTER or GeofencingRequest.INITIAL_TRIGGER_DWELL)
addGeofences(geofenceList)
}.build()
}

// TODO: 1.9 Create GeoFence, make list of geofence and geofences from geofencingClient
private fun createGeoFenceForHome(lat: Double, lon: Double, radius: Float) {
geofenceList.add(
Geofence.Builder()
.setRequestId(HOME_GEOFENCE_ID)
.setCircularRegion(lat, lon, radius)
.setExpirationDuration(Geofence.NEVER_EXPIRE)
.setTransitionTypes(
Geofence.GEOFENCE_TRANSITION_ENTER or
Geofence.GEOFENCE_TRANSITION_DWELL or
Geofence.GEOFENCE_TRANSITION_EXIT
)
.setLoiteringDelay(10000)
.build()
)

if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return
}
geofencingClient.addGeofences(getGeofencingRequest(), pendingIntent)?.run {
addOnSuccessListener {
Log.i(TAG, "geod Geofence Created: nLat: $latnLon: $lon")
}
addOnFailureListener {
Log.i(TAG, "failure $it")
}
}
}

fun removeGeofence() {
geofencingClient.removeGeofences(listOf(HOME_GEOFENCE_ID)).addOnCompleteListener {
if (it.isSuccessful) {
Log.i(TAG, "delete geofence with ID success!")
} else {
Log.w(TAG, "delete geofence with ID failed ")
}
}
}

/**
* Determine if you have the location permission
*/
private fun hasLocationPermission(): Boolean {
for (permission in PERMISSIONS) {
if (ActivityCompat.checkSelfPermission(
this,
permission
) != PackageManager.PERMISSION_GRANTED
) {
return false
}
}
return true
}

fun requestPermission() {
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
Log.i(TAG, "sdk < 28 Q")
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
||
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
val strings = arrayOf<String>(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION
)
ActivityCompat.requestPermissions(this, strings, 1)
}
} else {
if (ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
||
ActivityCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_COARSE_LOCATION
) != PackageManager.PERMISSION_GRANTED
||
ActivityCompat.checkSelfPermission(
this,
"android.permission.ACCESS_BACKGROUND_LOCATION"
) != PackageManager.PERMISSION_GRANTED
) {
val strings = arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
"android.permission.ACCESS_BACKGROUND_LOCATION"
)
ActivityCompat.requestPermissions(this, strings, 1)
}
}
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == GoogleLocationManager.REQUEST_CHECK_SETTINGS) {
when (resultCode) {
Activity.RESULT_OK -> {
Log.d(TAG, "User confirm to access location")

handleGoogleLocation()
}
Activity.RESULT_CANCELED -> {
Log.d(TAG, "User denied to access location")
}
}
}
}

override fun onRequestPermissionsResult(
requestCode: Int, permissions: Array<String>, grantResults: IntArray
) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == 1) {
if (grantResults.size > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED
) {
Log.i(
TAG, "onRequestPermissionsResult: apply LOCATION PERMISSION successful"
)
handleGoogleLocation()
} else {
Log.i(
TAG, "onRequestPermissionsResult: apply LOCATION PERMISSSION failed"
)
}
}
if (requestCode == 2) {
if (grantResults.size > 2 && grantResults[2] == PackageManager.PERMISSION_GRANTED && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED
) {
Log.i(
TAG, "onRequestPermissionsResult: apply ACCESS_BACKGROUND_LOCATION successful"
)
handleGoogleLocation()
} else {
Log.i(
TAG, "onRequestPermissionsResult: apply ACCESS_BACKGROUND_LOCATION failed"
)
}
}
}

}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.