Skip to content

Commit

Permalink
Merge pull request #77 from keepalivedev/MigrateToSDK34
Browse files Browse the repository at this point in the history
Migrate to sdk34
  • Loading branch information
keepalivedev authored Jul 14, 2024
2 parents 98e2bcc + 684ed5d commit 98288b6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 18 deletions.
30 changes: 15 additions & 15 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ plugins {
}

android {
compileSdk 33
compileSdk 34

defaultConfig {
applicationId "io.keepalive.android"
minSdk 22
targetSdk 33
targetSdk 34
versionCode 120
versionName "1.2.0"

Expand Down Expand Up @@ -62,19 +62,19 @@ android {
dependencies {

// next versions require compile sdk 34+
implementation 'androidx.core:core-ktx:1.10.1'
implementation "androidx.activity:activity-ktx:1.7.2"
implementation 'androidx.navigation:navigation-ui-ktx:2.6.0'
implementation 'androidx.core:core-ktx:1.13.1'
implementation 'androidx.activity:activity-ktx:1.9.0'
implementation 'androidx.navigation:navigation-ui-ktx:2.7.7'

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.code.gson:gson:2.8.9'
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'androidx.preference:preference-ktx:1.2.1'

// for encrypted shared preferences, no longer used
// implementation 'androidx.security:security-crypto:1.1.0-alpha06'

// work manager stuff, but also gives us ListenableFuture...
implementation 'androidx.work:work-runtime-ktx:2.8.1'
implementation 'androidx.work:work-runtime-ktx:2.9.0'

implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'

Expand All @@ -83,18 +83,18 @@ dependencies {
//implementation 'com.google.guava:guava:31.0.1-android'

// if this is google play, and not f-droid, then use the google play location library
googlePlayImplementation 'com.google.android.gms:play-services-location:21.0.1'
googlePlayImplementation 'com.google.android.gms:play-services-location:21.3.0'

// test stuff
testImplementation 'org.mockito:mockito-core:5.7.0'
testImplementation 'org.mockito:mockito-core:5.12.0'
testImplementation 'junit:junit:4.13.2'

// android instrumented test dependencies
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.5.1'
androidTestImplementation 'org.mockito:mockito-android:5.7.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.5.1'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.6.1'
androidTestImplementation 'org.mockito:mockito-android:5.12.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.6.1'
androidTestImplementation 'tools.fastlane:screengrab:2.1.1'

}
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

<uses-permission
android:name="android.permission.PACKAGE_USAGE_STATS"
Expand Down Expand Up @@ -70,7 +71,7 @@
<service
android:name="io.keepalive.android.AlertService"
android:exported="false"
android:foregroundServiceType="location" />
android:foregroundServiceType="shortService|location" />

<receiver
android:name="io.keepalive.android.receivers.AlarmReceiver"
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/io/keepalive/android/AlertFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,15 @@ class AlertMessageSender(private val context: Context) {

DebugLogger.d("sendAlertMessage", context.getString(R.string.debug_log_sending_alert_sms))

// API 34+ requires that a package name be specified for pending and implicit intents
// it seemed to work without this though...
val smsSentIntent = Intent("SMS_SENT").apply {
setPackage(context.packageName)
}

// create a pending intent for the SMS sent intent to use when sending the SMS
val sentPI = PendingIntent.getBroadcast(
context, 0, Intent("SMS_SENT"),
context, 0, smsSentIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

Expand Down
30 changes: 29 additions & 1 deletion app/src/main/java/io/keepalive/android/AlertService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.ServiceInfo
import android.os.IBinder
import androidx.core.app.NotificationCompat
import android.os.Handler
import android.os.Looper
import android.os.PowerManager
import android.util.Log
import androidx.core.app.ServiceCompat

class AlertService : Service() {

Expand Down Expand Up @@ -55,8 +57,32 @@ class AlertService : Service() {
// create a notification to show that the service is running
val notification = createNotification()

// a lot of this seems unnecessary, the type of service declared doesn't seem to affect
// what we are actually able to do in the service...
var foregroundServiceTypes = 0

// if this is API 29+ then we should add the foreground service type(s)
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {

// if the user has enabled location then add the location type
if (prefs.getBoolean("location_enabled", false)) {
foregroundServiceTypes = foregroundServiceTypes or ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION
}

// if this is API 34+ then add the new short service type
// https://developer.android.com/about/versions/14/changes/fgs-types-required#short-service
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
foregroundServiceTypes = foregroundServiceTypes or ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE
}
}

// start the service
startForeground(AppController.ALERT_SERVICE_NOTIFICATION_ID, notification)
ServiceCompat.startForeground(
this,
AppController.ALERT_SERVICE_NOTIFICATION_ID,
notification,
foregroundServiceTypes
)

DebugLogger.d("AlertService", getString(R.string.debug_log_wake_lock_acquired))
wakeLock.acquire(wakeLockTimeout)
Expand Down Expand Up @@ -120,6 +146,8 @@ class AlertService : Service() {
.setContentTitle(getString(R.string.alert_service_notification_title))
.setContentText(getString(R.string.alert_service_notification_message))
.setSmallIcon(R.drawable.ic_notification)
// the OS may delay the visibility of the notification so force it to be visible immediately
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
.build()
}

Expand Down

0 comments on commit 98288b6

Please sign in to comment.