Skip to content

Commit

Permalink
Merge pull request #108 from Dimowner/foreground_service_type
Browse files Browse the repository at this point in the history
Added foregroundServiceType to foreground services which is required when target API 34
  • Loading branch information
Dimowner committed Feb 8, 2024
2 parents 29d4024 + 5437c68 commit faf8a48
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 15 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ android {

buildFeatures {
viewBinding true
buildConfig true
}

def keystorePropertiesFile = rootProject.file("keystore.properties")
Expand Down Expand Up @@ -69,8 +70,8 @@ android {
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

lintOptions {
Expand Down
35 changes: 30 additions & 5 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE"
android:minSdkVersion="33" />
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"
android:minSdkVersion="33" />
<uses-permission
android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"
android:minSdkVersion="33" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.VIBRATE" />

<uses-feature android:name="android.hardware.screen.portrait" android:required="false" />
Expand Down Expand Up @@ -81,11 +91,26 @@
android:name=".app.TransparentRecordingActivity"
android:theme="@style/Theme.Transparent" />

<service android:name=".app.RecordingService" />
<service android:name=".app.PlaybackService" />
<service android:name=".app.DownloadService" />
<service android:name=".app.DecodeService" />
<service android:name=".app.moverecords.MoveRecordsService" />
<service
android:name=".app.RecordingService"
android:exported="false"
android:foregroundServiceType="microphone" />
<service
android:name=".app.PlaybackService"
android:exported="false"
android:foregroundServiceType="mediaPlayback" />
<service
android:name=".app.DownloadService"
android:exported="false"
android:foregroundServiceType="dataSync" />
<service
android:name=".app.DecodeService"
android:exported="false"
android:foregroundServiceType="dataSync" />
<service
android:name=".app.moverecords.MoveRecordsService"
android:exported="false"
android:foregroundServiceType="dataSync" />

<receiver android:name=".WidgetReceiver" />
<receiver android:name=".app.RecordingService$StopRecordingReceiver" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.app.*
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
import android.graphics.Color
import android.os.Binder
import android.os.Build
Expand Down Expand Up @@ -205,7 +206,11 @@ class DecodeService : Service() {
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP
contentPendingIntent = PendingIntent.getActivity(applicationContext, 0, intent, PENDING_INTENT_FLAGS)
startForeground(NOTIF_ID, buildNotification())
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
startForeground(NOTIF_ID, buildNotification())
} else {
startForeground(NOTIF_ID, buildNotification(), FOREGROUND_SERVICE_TYPE_DATA_SYNC)
}
}

private fun buildNotification(): Notification {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.dimowner.audiorecorder.app;

import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
Expand Down Expand Up @@ -209,7 +210,11 @@ private void startNotification() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
contentPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, AppConstants.PENDING_INTENT_FLAGS);
startForeground(NOTIF_ID, buildNotification());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
startForeground(NOTIF_ID, buildNotification());
} else {
startForeground(NOTIF_ID, buildNotification(), FOREGROUND_SERVICE_TYPE_DATA_SYNC);
}
}

private Notification buildNotification() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.IBinder;
Expand Down Expand Up @@ -168,7 +169,11 @@ private void startForegroundService() {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
contentPendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, AppConstants.PENDING_INTENT_FLAGS);
startForeground(NOTIF_ID, buildNotification());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
startForeground(NOTIF_ID, buildNotification());
} else {
startForeground(NOTIF_ID, buildNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
}
started = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ServiceInfo;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Build;
Expand Down Expand Up @@ -243,7 +244,11 @@ private void startForegroundService() {
// remoteViewsBig.setInt(R.id.container, "setBackgroundColor", this.getResources().getColor(colorMap.getPrimaryColorRes()));

contentPendingIntent = createContentIntent();
startForeground(NOTIF_ID, buildNotification());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
startForeground(NOTIF_ID, buildNotification());
} else {
startForeground(NOTIF_ID, buildNotification(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);
}
started = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.app.Service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.pm.ServiceInfo
import android.graphics.Color
import android.os.Binder
import android.os.Build
Expand All @@ -31,7 +32,6 @@ import com.dimowner.audiorecorder.util.OnCopyListener
import com.dimowner.audiorecorder.util.copyFileToDir
import timber.log.Timber
import java.io.File
import java.util.ArrayList

/**
* Created on 14.08.2021.
Expand Down Expand Up @@ -251,7 +251,15 @@ class MoveRecordsService : Service() {
builder.setOnlyAlertOnce(true)
builder.setDefaults(0)
builder.setSound(null)
startForeground(NOTIF_ID, builder.build())
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
startForeground(NOTIF_ID, builder.build())
} else {
startForeground(
NOTIF_ID,
builder.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
)
}
}

fun stopService() {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.4.2'
classpath 'com.android.tools.build:gradle:8.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// classpath 'com.google.gms:google-services:4.3.10'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip

0 comments on commit faf8a48

Please sign in to comment.