Skip to content

Commit

Permalink
all: foreground when location allowed (fixes #1992) (#2004)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi authored May 23, 2024
1 parent e007b02 commit bb7a6cb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 73 deletions.
66 changes: 16 additions & 50 deletions app/src/main/kotlin/io/treehouses/remote/InitialActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
Expand Down Expand Up @@ -37,15 +38,18 @@ class InitialActivity : BaseInitialActivity() {
instance = this
setContentView(bind.root)
requestPermission()
// g
//PreferenceManager.getDefaultSharedPreferences(this).getInt("font_size", 1)
// adjustFontScale(resources.configuration, PreferenceManager.getDefaultSharedPreferences(this).getInt("font_size", 1))
val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
currentTitle = "Home"
setUpDrawer()
title = "Home"
GPSService()

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(this, Manifest.permission.FOREGROUND_SERVICE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
startGPSService()
}

val a = (application as MainApplication).getCurrentBluetoothService()
if (a != null) {
mChatService = a
Expand All @@ -56,52 +60,14 @@ class InitialActivity : BaseInitialActivity() {
openCallFragment(HomeFragment())
}



// override fun onStart() {
// super.onStart()
// Bind to LocalService
// if (!isBluetoothServiceRunning(BluetoothChatService::class.java)) {
// Log.e("InitialActivity", "STARTING SERVICE")
// Intent(this, BluetoothChatService::class.java).also { intent ->
// bindService(intent, connection, Context.BIND_AUTO_CREATE)
// }
// }
// }

// override fun onDestroy() {
// super.onDestroy()
// try {
// unbindService(connection)
// } catch (e: IllegalArgumentException) {
// e.printStackTrace()
// }

// }

// private val receiver: BroadcastReceiver = object : BroadcastReceiver() {
// override fun onReceive(context: Context, intent: Intent) {
// Log.e("RECEIVED", "RECEIVE")
// Toast.makeText(applicationContext, "received", Toast.LENGTH_SHORT).show()
// val a = (application as MainApplication).getCurrentBluetoothService()
// if (a != null ) {
// setChatService(a)
// openCallFragment(HomeFragment())
// }
// }
// }

// override fun onResume() {
// val filter = IntentFilter()
// filter.addAction(MainApplication.BLUETOOTH_SERVICE_CONNECTED)
// applicationContext.registerReceiver(receiver, filter)
// super.onResume()
// }
//
// override fun onPause() {
// applicationContext.unregisterReceiver(receiver)
// super.onPause()
// }
private fun startGPSService() {
val intent = Intent(this, GPSService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(intent)
} else {
startService(intent)
}
}

override fun onResume() {
super.onResume()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.treehouses.remote.fragments

import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand All @@ -9,6 +11,7 @@ import android.webkit.WebViewClient
import io.treehouses.remote.bases.BaseFragment
import io.treehouses.remote.callback.BackPressReceiver
import io.treehouses.remote.databinding.FragmentCommunityBinding
import io.treehouses.remote.utils.GPSService

class CommunityFragment : BaseFragment(), BackPressReceiver {
private lateinit var bind: FragmentCommunityBinding
Expand All @@ -29,8 +32,19 @@ class CommunityFragment : BaseFragment(), BackPressReceiver {
bind.map.settings.javaScriptEnabled = true
bind.map.loadUrl("https://www.google.com/maps/d/u/0/viewer?ll=11.88717970130264%2C10.93123241891862&z=4&mid=1rO3RmHQnrSNsBwB9skqHos970zI-ZVAA")

startGPSService()
}

private fun startGPSService() {
val intent = Intent(requireContext(), GPSService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
requireContext().startForegroundService(intent)
} else {
requireContext().startService(intent)
}
}


override fun onBackPressed() {
if (bind.map.canGoBack()) {
bind.map.goBack()
Expand Down
49 changes: 26 additions & 23 deletions app/src/main/kotlin/io/treehouses/remote/utils/GPSService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import android.content.pm.PackageManager
import android.location.Location
import android.location.LocationListener
import android.location.LocationManager
import android.os.Build
import android.os.Bundle
import android.os.IBinder
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.preference.PreferenceManager
Expand All @@ -37,30 +39,30 @@ open class GPSService : Service(), LocationListener {
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}

@SuppressLint("MissingPermission")
@RequiresApi(Build.VERSION_CODES.O)
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
createNotificationChannel()

val notificationIntent = Intent(this, InitialActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0)

val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID)
val notification: Notification = notificationBuilder.setOngoing(true)
.setContentTitle("GPS Service")
.setContentText("Treehouses Remote is currently estimating your location")
.setSmallIcon(R.drawable.treehouses2)
.setContentIntent(pendingIntent)
.build()

startForeground(NOTIFICATION_ID, notification)
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, Manifest.permission.FOREGROUND_SERVICE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

createNotificationChannel()
val notificationIntent = Intent(this, InitialActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_IMMUTABLE)
val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, CHANNEL_ID)
val notification: Notification = notificationBuilder.setOngoing(true)
.setContentTitle("GPS Service")
.setContentText("Treehouses Remote is currently estimating your location")
.setSmallIcon(R.drawable.treehouses2)
.setContentIntent(pendingIntent)
.build()

startForeground(NOTIFICATION_ID, notification)

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION
) == PackageManager.PERMISSION_GRANTED
) {
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
isGPSEnabled = locationManager?.isProviderEnabled(LocationManager.GPS_PROVIDER) == true
getLocation()
} else {
stopSelf()
}

return START_STICKY
Expand All @@ -84,21 +86,22 @@ open class GPSService : Service(), LocationListener {
override fun onLocationChanged(location: Location) {
val latitude = location.latitude
val longitude = location.longitude
pref.edit().putString("last_lat", latitude.toString()).apply()
pref.edit().putString("last_lng", longitude.toString()).apply()
pref.edit().putString("last_lat", "$latitude").apply()
pref.edit().putString("last_lng", "$longitude").apply()
}

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

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel() {
val channel = NotificationChannel(CHANNEL_ID, "Foreground Service Channel", NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(channel)
}

companion object {
private const val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 10 // 10 meters
private const val MIN_DISTANCE_CHANGE_FOR_UPDATES: Long = 10
private const val MIN_TIME_BW_UPDATES = 1000 * 60 * 5.toLong()
}
}
}

0 comments on commit bb7a6cb

Please sign in to comment.