Skip to content

Commit

Permalink
all: foreground location service (fixes #1988) (#1989)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
Okuro3499 and dogi committed May 9, 2024
1 parent 7974b28 commit 7e41121
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 72 deletions.
20 changes: 10 additions & 10 deletions .github/workflows/android-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ jobs:
sha256sum output/remote.aab > output/remote.aab.sha256
ls -alR output
- name: publish AAB to playstore
if: github.ref == 'refs/heads/master'
uses: dogi/[email protected]
with:
serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
packageName: io.treehouses.remote
releaseFiles: output/remote.aab
track: internal
releaseName: "${{ env.VERSION }}"
status: completed
# - name: publish AAB to playstore
# if: github.ref == 'refs/heads/master'
# uses: dogi/[email protected]
# with:
# serviceAccountJsonPlainText: ${{ secrets.SERVICE_ACCOUNT_JSON }}
# packageName: io.treehouses.remote
# releaseFiles: output/remote.aab
# track: internal
# releaseName: "${{ env.VERSION }}"
# status: completed

# - name: mobile security framework
# run: |
Expand Down
6 changes: 5 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

<application
Expand Down Expand Up @@ -54,7 +55,10 @@
android:launchMode="singleTop"
android:theme="@style/Theme.AppCompat"
android:windowSoftInputMode="stateAlwaysVisible|adjustResize" />
<service android:name=".utils.GPSService" />

<service android:name=".utils.GPSService"
android:foregroundServiceType="location"
/>

<activity
android:name=".InitialActivity"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/io/treehouses/remote/InitialActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class InitialActivity : BaseInitialActivity() {
currentTitle = "Home"
setUpDrawer()
title = "Home"
GPSService(this)
GPSService()
val a = (application as MainApplication).getCurrentBluetoothService()
if (a != null) {
mChatService = a
Expand Down Expand Up @@ -208,4 +208,4 @@ class InitialActivity : BaseInitialActivity() {
mActionBarDrawerToggle.isDrawerIndicatorEnabled = true
bind.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)
}
}
}
125 changes: 66 additions & 59 deletions app/src/main/kotlin/io/treehouses/remote/utils/GPSService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ package io.treehouses.remote.utils

import android.Manifest
import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
Expand All @@ -13,85 +17,88 @@ import android.location.LocationManager
import android.os.Bundle
import android.os.IBinder
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.preference.PreferenceManager
import io.treehouses.remote.InitialActivity
import io.treehouses.remote.R

/**
* Created by rowsun on 9/28/16.
*/
open class GPSService(mContext: Context) : Service(), LocationListener {
open class GPSService : Service(), LocationListener {
private var locationManager: LocationManager? = null
private var isGPSEnabled = false
private var canGetLocation = false
@JvmField
var location // location
: Location? = null
@JvmField
var latitude:Double = 0.0 // latitude = 0.0
@JvmField
var longitude:Double = 0.0 // longitude = 0.0
private var pref: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext)
private lateinit var pref: SharedPreferences
private lateinit var notificationManager: NotificationManager

private val NOTIFICATION_ID = 101
private val CHANNEL_ID = "ForegroundServiceChannel"

override fun onCreate() {
super.onCreate()
pref = PreferenceManager.getDefaultSharedPreferences(this)
notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}

@SuppressLint("MissingPermission")
private fun getLocation(): Location? {
try {
if (!isGPSEnabled) {
// showSettingsAlert();
} else {
canGetLocation = true
if (location == null) {
lastKnownLocation
}
}
} catch (e: Exception) {
e.printStackTrace()
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
) {
locationManager = getSystemService(Context.LOCATION_SERVICE) as LocationManager
isGPSEnabled = locationManager?.isProviderEnabled(LocationManager.GPS_PROVIDER) == true
getLocation()
}
return location

return START_STICKY
}

override fun onBind(intent: Intent?): IBinder? {
return null
}

@get:SuppressLint("MissingPermission")
private val lastKnownLocation: Unit
get() {
locationManager!!.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_DISTANCE_CHANGE_FOR_UPDATES, MIN_TIME_BW_UPDATES.toFloat(), this)
if (locationManager != null) {
location = locationManager!!
.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (location != null) {
latitude = location!!.latitude
longitude = location!!.longitude
pref.edit().putString("last_lat", latitude.toString()).apply()
pref.edit().putString("last_lng", longitude.toString()).apply()
}
@SuppressLint("MissingPermission")
private fun getLocation() {
locationManager?.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_DISTANCE_CHANGE_FOR_UPDATES, MIN_TIME_BW_UPDATES.toFloat(), this)
if (locationManager != null) {
val location = locationManager?.getLastKnownLocation(LocationManager.GPS_PROVIDER)
if (location != null) {
onLocationChanged(location)
}
}
}

override fun onLocationChanged(location: Location) {
this.location = location
pref.edit().putString("last_lat", location.latitude.toString()).apply()
pref.edit().putString("last_lng", location.longitude.toString()).apply()
val latitude = location.latitude
val longitude = location.longitude
pref.edit().putString("last_lat", latitude.toString()).apply()
pref.edit().putString("last_lng", longitude.toString()).apply()
}

override fun onProviderDisabled(provider: String) {}
override fun onProviderEnabled(provider: String) {}
override fun onStatusChanged(provider: String, status: Int, extras: Bundle) {}
override fun onBind(arg0: Intent): IBinder? {
return null
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) {}

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_TIME_BW_UPDATES = 1000 * 60 * 5 // 1 minute
.toLong()
}

init {
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager = mContext.getSystemService(Context.LOCATION_SERVICE) as LocationManager

isGPSEnabled = locationManager!!
.isProviderEnabled(LocationManager.GPS_PROVIDER)
getLocation()
}
private const val MIN_TIME_BW_UPDATES = 1000 * 60 * 5.toLong()
}
}

0 comments on commit 7e41121

Please sign in to comment.