Skip to content

Commit

Permalink
Merge pull request #410 from adeekshith/issue/406/android12-foregroun…
Browse files Browse the repository at this point in the history
…dservice

Issue/406/android12 foregroundservice
  • Loading branch information
adeekshith authored Feb 11, 2024
2 parents e8700d2 + 93c0422 commit 998d66d
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 21 deletions.
20 changes: 19 additions & 1 deletion .github/workflows/android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,22 @@ jobs:
with:
name: PR-${{ github.event.number }}-googleplay
path: ${{ steps.google-play-apk-path-id.outputs.google-play-apk-path }}
retention-days: 2
retention-days: 2

- name: upload apk
uses: wei/curl@v1
with:
args: --upload-file ${{ steps.apk-path-id.outputs.apk-path }} https://transfer.sh/PR-${{ github.event.number }}.apk -o apkpath.txt


- name: upload apk
id: upload-apk-path-id
run: |
echo "::set-output name=apk-path::$(cat apkpath.txt)"
shell: bash

- name: commit
uses: mshick/add-pr-comment@v2
with:
message: |
**Download apk from path: ${{ steps.upload-apk-path-id.outputs.apk-path }}**
7 changes: 4 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 30
compileSdkVersion 33

defaultConfig {
applicationId "com.parishod.watomatic"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 33
versionCode 26
versionName "1.21"
versionName "1.22"

javaCompileOptions {
annotationProcessorOptions {
Expand Down Expand Up @@ -75,6 +75,7 @@ dependencies {
implementation 'com.google.android.material:material:1.5.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
implementation 'androidx.room:room-runtime:2.3.0'
implementation "androidx.work:work-runtime:2.7.1"
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
25 changes: 18 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
android:supportsRtl="true"
android:theme="@style/Theme.WaReply">

<activity android:name="com.parishod.watomatic.activity.main.MainActivity">
<activity android:name="com.parishod.watomatic.activity.main.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -34,7 +35,8 @@
<activity
android:name="com.parishod.watomatic.activity.customreplyeditor.CustomReplyEditorActivity"
android:label="@string/mainAutoReplyLabel"
android:parentActivityName="com.parishod.watomatic.activity.main.MainActivity">
android:parentActivityName="com.parishod.watomatic.activity.main.MainActivity"
android:exported="true">
<intent-filter android:label="global-auto-reply-message-intent">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
Expand All @@ -49,61 +51,70 @@
android:name="com.parishod.watomatic.activity.about.AboutActivity"
android:label="@string/about"
android:parentActivityName="com.parishod.watomatic.activity.main.MainActivity"
android:exported="false"
>
</activity>

<activity
android:name="com.parishod.watomatic.activity.settings.SettingsActivity"
android:label="@string/settings"
android:parentActivityName="com.parishod.watomatic.activity.main.MainActivity"
android:exported="false"
>
</activity>

<activity
android:name="com.parishod.watomatic.activity.advancedsettings.AdvancedSettingsActivity"
android:label="@string/advanced_settings"
android:parentActivityName="com.parishod.watomatic.activity.settings.SettingsActivity"
android:exported="false"
>
</activity>

<activity
android:name=".activity.contactselector.ContactSelectorActivity"
android:label="@string/contact_selector"
android:parentActivityName=".activity.advancedsettings.AdvancedSettingsActivity"
android:exported="false"
>
</activity>

<activity
android:name="com.parishod.watomatic.activity.notification.NotificationIntentActivity"
android:launchMode="singleTop"
android:exported="false"
>
</activity>

<activity android:name=".activity.enabledapps.EnabledAppsActivity"
android:label="@string/enabled_apps_title"
android:parentActivityName=".activity.main.MainActivity"/>
android:parentActivityName=".activity.main.MainActivity"
android:exported="false"/>

<activity android:name=".activity.donation.DonationActivity"
android:label="@string/donations_activity_title"
android:parentActivityName=".activity.main.MainActivity"/>
android:parentActivityName=".activity.main.MainActivity"
android:exported="false"/>

<service
android:name="com.parishod.watomatic.NotificationService"
android:label="@string/service_name"
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
android:exported="true">
<intent-filter>
<action android:name="android.service.notification.NotificationListenerService" />
</intent-filter>
</service>

<service android:name=".service.KeepAliveService"
android:stopWithTask="false"/>
android:stopWithTask="false"
android:exported="true"/>

<!--https://techstop.github.io/android-broadcastreceiver/-->
<receiver android:name=".receivers.NotificationServiceRestartReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true"
android:exported="false">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<category android:name="android.intent.category.DEFAULT"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ private void enableService(boolean enable) {
}

private void startNotificationService() {
if (preferencesManager.isForegroundServiceNotificationEnabled()) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || preferencesManager.isForegroundServiceNotificationEnabled()) {
ServieUtils.getInstance(mActivity).startNotificationService();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.parishod.watomatic.fragment;

import android.os.Build;
import android.os.Bundle;

import androidx.preference.Preference;
Expand Down Expand Up @@ -31,6 +32,9 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

SwitchPreference foregroundServiceNotifPref = findPreference(getString(R.string.pref_show_foreground_service_notification));
if (foregroundServiceNotifPref != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
foregroundServiceNotifPref.setVisible(false);
}
foregroundServiceNotifPref.setOnPreferenceChangeListener((preference, newValue) -> {
if (newValue.equals(true)) {
ServieUtils.getInstance(getActivity()).startNotificationService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void sendNotification(String title, String message, String packageName) {
Intent intent = new Intent(appContext, NotificationIntentActivity.class);
intent.putExtra("package", packageName);
intent.setAction(Long.toString(System.currentTimeMillis()));//This is needed to generate unique pending intents, else when we create multiple pending intents they will be overwritten by last one
PendingIntent pIntent = PendingIntent.getActivity(appContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pIntent = PendingIntent.getActivity(appContext, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_UPDATE_CURRENT);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(appContext, Constants.NOTIFICATION_CHANNEL_ID)
.setGroup("watomatic-" + packageName)
Expand Down Expand Up @@ -141,7 +141,7 @@ public NotificationCompat.Builder getForegroundServiceNotification(Service servi

Intent intent = new Intent(appContext, NotificationIntentActivity.class);
intent.setAction(Long.toString(System.currentTimeMillis()));//This is needed to generate unique pending intents, else when we create multiple pending intents they will be overwritten by last one
PendingIntent pIntent = PendingIntent.getActivity(appContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent pIntent = PendingIntent.getActivity(appContext, 0, intent, PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_CANCEL_CURRENT);

NotificationCompat.Builder notificationBuilder;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.util.Log
import com.parishod.watomatic.NotificationService
import com.parishod.watomatic.model.preferences.PreferencesManager
Expand All @@ -21,15 +22,19 @@ class NotificationServiceRestartReceiver : BroadcastReceiver() {

private fun restartService(context: Context) {
val preferencesManager = PreferencesManager.getPreferencesInstance(context)
if (preferencesManager.isForegroundServiceNotificationEnabled) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || preferencesManager.isForegroundServiceNotificationEnabled) {
val serviceIntent = Intent(context, KeepAliveService::class.java)
// ToDo: Should probably start using foreground service to prevent IllegalState exception below
try {
context.startService(serviceIntent)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(serviceIntent)
} else {
context.startService(serviceIntent)
}
} catch (e: IllegalStateException) {
Log.e("NotifServiceRestart", "Unable to restart notification service")
}
} else {
}else{
enableService(context)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

Expand Down Expand Up @@ -63,7 +64,7 @@ public void onTaskRemoved(Intent rootIntent) {

public void tryReconnectService() {
if (PreferencesManager.getPreferencesInstance(getApplicationContext()).isServiceEnabled()
&& PreferencesManager.getPreferencesInstance(getApplicationContext()).isForegroundServiceNotificationEnabled()) {
&& (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || PreferencesManager.getPreferencesInstance(getApplicationContext()).isForegroundServiceNotificationEnabled())) {
Log.d("DEBUG", "KeepAliveService tryReconnectService");
//Send broadcast to restart service
Intent broadcastIntent = new Intent(getApplicationContext(), NotificationServiceRestartReceiver.class);
Expand Down Expand Up @@ -94,7 +95,9 @@ private boolean isMyServiceRunning() {

private void startForeground(Service service) {
Log.e("DEBUG", "startForeground");
NotificationCompat.Builder notificationBuilder = NotificationHelper.getInstance(getApplicationContext()).getForegroundServiceNotification(service);
service.startForeground(FOREGROUND_NOTIFICATION_ID, notificationBuilder.build());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationCompat.Builder notificationBuilder = NotificationHelper.getInstance(getApplicationContext()).getForegroundServiceNotification(service);
service.startForeground(FOREGROUND_NOTIFICATION_ID, notificationBuilder.build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.util.Log;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -32,7 +33,7 @@ protected void onCleared() {

public void tryReconnectService() {
if (PreferencesManager.getPreferencesInstance(context).isServiceEnabled()
&& PreferencesManager.getPreferencesInstance(context).isForegroundServiceNotificationEnabled()) {
&& (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S || PreferencesManager.getPreferencesInstance(context).isForegroundServiceNotificationEnabled())) {
Log.d("DEBUG", "viewmodel tryReconnectService");
//Send broadcast to restart service
Intent broadcastIntent = new Intent(context, NotificationServiceRestartReceiver.class);
Expand Down

0 comments on commit 998d66d

Please sign in to comment.