Skip to content

Commit a3b14a4

Browse files
Merge pull request #1 from krlan2789/main
Android 14 device support . .
2 parents e52c2d8 + 4bb98e2 commit a3b14a4

File tree

9 files changed

+63
-24
lines changed

9 files changed

+63
-24
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ plugins {
55

66
android {
77
namespace 'com.singularity_code.gpstracker'
8-
compileSdk 33
8+
compileSdk 34
99

1010
defaultConfig {
1111
applicationId "com.singularity_code.gpstracker"
1212
minSdk 24
13-
targetSdk 33
13+
targetSdk 34
1414
versionCode 1
1515
versionName "1.0"
1616

gradle.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ kotlin.code.style=official
2222
# thereby reducing the size of the R class for that library
2323
android.nonTransitiveRClass=true
2424
android.experimental.cacheCompileLibResources=true
25-
android.enableJetifier =true
26-
org.gradle.caching =true
27-
kotlin.incremental =true
28-
org.gradle.parallel =true
25+
android.enableJetifier=true
26+
org.gradle.caching=true
27+
kotlin.incremental=true
28+
org.gradle.parallel=true

live-location/build.gradle

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ plugins {
55

66
android {
77
namespace 'com.singularity_code.live_location'
8-
compileSdk 33
8+
compileSdk 34
99

1010
defaultConfig {
1111
minSdk 24
12-
targetSdk 33
12+
targetSdk 34
1313

1414
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1515
consumerProguardFiles "consumer-rules.pro"
@@ -67,5 +67,4 @@ dependencies {
6767
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.1'
6868
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.1"
6969
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.1"
70-
7170
}

live-location/src/main/AndroidManifest.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33

44
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
5+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" android:minSdkVersion="29" />
6+
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
57
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
68
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
7-
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
89
<uses-permission android:name="android.permission.INTERNET" />
910

1011
<application android:usesCleartextTraffic="true">

live-location/src/main/java/com/singularity_code/live_location/LiveLocationService.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.app.Service
88
import android.content.Context
99
import android.content.Intent
1010
import android.content.pm.PackageManager
11+
import android.content.pm.ServiceInfo
1112
import android.graphics.Color
1213
import android.net.Uri
1314
import android.os.Binder
@@ -85,11 +86,15 @@ class LiveLocationService : Service() {
8586
private val liveLocationError = MutableStateFlow<ErrorMessage?>(null)
8687
private val currentLocation = MutableStateFlow<LocationData?>(null)
8788
private val liveLocationRunning = MutableStateFlow(false)
88-
private val locationRequest: LocationRequest by lazy {
89-
LocationRequest.create()
90-
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
91-
.setInterval(gpsSamplingRate)
92-
.setFastestInterval(gpsSamplingRate)
89+
// private val locationRequest: LocationRequest by lazy {
90+
// LocationRequest.create()
91+
// .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
92+
// .setInterval(gpsSamplingRate)
93+
// .setFastestInterval(gpsSamplingRate)
94+
// }
95+
96+
private val locationRequest: LocationRequest by lazy {
97+
LocationRequest.Builder(Priority.PRIORITY_HIGH_ACCURACY, gpsSamplingRate).build()
9398
}
9499

95100
private val locationProviderClient: FusedLocationProviderClient by lazy {
@@ -259,10 +264,20 @@ class LiveLocationService : Service() {
259264
}
260265
.build()
261266

262-
startForeground(
263-
foregroundServiceID ?: 1005,
264-
notification
265-
)
267+
268+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
269+
startForeground(
270+
foregroundServiceID ?: 1005,
271+
notification,
272+
ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
273+
)
274+
} else {
275+
startForeground(
276+
foregroundServiceID ?: 1005,
277+
notification
278+
)
279+
}
280+
266281
}
267282

268283
/** prepare websocket connection **/

live-location/src/main/java/com/singularity_code/live_location/data/Repository.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.util.Log
55
import arrow.core.Either
66
import com.singularity_code.live_location.util.ErrorMessage
77
import com.singularity_code.live_location.util.defaultOkhttp
8+
import com.singularity_code.live_location.util.isConnected
89
import com.singularity_code.live_location.util.websocket
910
import kotlinx.coroutines.CoroutineScope
1011
import kotlinx.coroutines.Dispatchers
@@ -39,6 +40,7 @@ class WebSocketRepository(
3940
private lateinit var webSocket: WebSocket
4041

4142
private var socketPendingJob: Job? = null
43+
// private var isConnected: Boolean = false
4244

4345
override fun openConnection() {
4446
socketPendingJob?.cancel()
@@ -60,6 +62,8 @@ class WebSocketRepository(
6062

6163
override suspend fun sendData(data: String): Either<ErrorMessage, String> {
6264
return kotlin.runCatching {
65+
if (!webSocket.isConnected) openConnection()
66+
6367
val result = webSocket.send(data)
6468
Either.Right(result.toString())
6569
}.getOrElse {

live-location/src/main/java/com/singularity_code/live_location/util/WebSocker.kt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ import kotlinx.coroutines.flow.map
1010
import kotlinx.coroutines.launch
1111
import okhttp3.*
1212

13+
var _isConnected: Boolean = false
14+
var WebSocket.isConnected: Boolean
15+
get() = _isConnected
16+
set(value) {
17+
_isConnected = value
18+
}
19+
1320
fun websocket(
1421
context: Context,
1522
apiURL: String,
@@ -36,13 +43,15 @@ fun websocket(
3643

3744
val listener = object : WebSocketListener() {
3845
override fun onOpen(webSocket: WebSocket, response: Response) {
39-
Log.d("TAG", "WebSocket: Opened")
46+
Log.d("WSS_TAG", "WebSocket: Opened ${response.message}")
47+
webSocket.isConnected = true
4048
super.onOpen(webSocket, response)
4149
}
4250

4351
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
4452
super.onFailure(webSocket, t, response)
45-
Log.d("TAG", "WebSocket: Failed $t")
53+
Log.d("WSS_TAG", "WebSocket: Failed ${t.message} | ${t.stackTraceToString()} | ${response?.message} | ${response?.body}")
54+
webSocket.isConnected = false
4655

4756
// IMPORTANT trigger recreate socket
4857
val errorLog = hashMapOf(
@@ -56,12 +65,13 @@ fun websocket(
5665
}
5766

5867
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
59-
Log.d("TAG", "WebSocket: Closed")
68+
Log.d("WSS_TAG", "WebSocket: Closed")
69+
webSocket.isConnected = false
6070
super.onClosed(webSocket, code, reason)
6171
}
6272

6373
override fun onMessage(webSocket: WebSocket, text: String) {
64-
Log.d("TAG", "WebSocket: Message $text")
74+
Log.d("WSS_TAG", "WebSocket: Message: $text")
6575
super.onMessage(webSocket, text)
6676
}
6777
}

live-location/src/main/java/com/singularity_code/live_location/util/other/AlienPortal.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package com.singularity_code.live_location.util.other;
22

3+
import android.app.Activity;
34
import android.content.Context;
45
import android.os.Build;
6+
import android.util.Log;
7+
8+
import androidx.core.app.ActivityCompat;
59
import androidx.core.app.NotificationCompat;
610
import com.google.android.gms.location.LocationResult;
711
import com.singularity_code.live_location.util.enums.NetworkMethod;
812
import com.singularity_code.live_location.util.pattern.*;
913
import org.jetbrains.annotations.NotNull;
1014

15+
import java.util.Arrays;
1116
import java.util.HashMap;
1217

1318
public class AlienPortal {
@@ -82,6 +87,11 @@ public String getUpdatedTime() {
8287

8388
private LiveLocationServiceInteractor liveLocationServiceInteractor;
8489

90+
public void requestPermissions(String[] permissions) {
91+
Log.d("AlienPortal", "requestPermissions: " + Arrays.toString(permissions));
92+
ActivityCompat.requestPermissions((Activity) context.getApplicationContext(), permissions, 201);
93+
}
94+
8595
public void setGPSSamplingRate(long samplingRate) {
8696
gpsSamplingRate = samplingRate;
8797
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ dependencyResolutionManagement {
1313
maven { url 'https://jitpack.io' }
1414
}
1515
}
16-
rootProject.name = "GPS Tracker"
16+
rootProject.name = "Android Background Location"
1717
include ':app'
1818
include ':live-location'

0 commit comments

Comments
 (0)