Skip to content

Commit

Permalink
migrate to alarm manager
Browse files Browse the repository at this point in the history
To make sure the messages keep getting sent while the app is in doze,
remove foreground service and instead use alarm manager to explicitly
schedule publishing messages. Also, request disabling of battery
optimization in Android 6+.

Due to Android policy, less than 15 minutes schedule is not allowed, so
adapted allowed schedule range.
  • Loading branch information
ostrya committed Oct 30, 2019
1 parent a38431a commit 9e71e35
Show file tree
Hide file tree
Showing 15 changed files with 325 additions and 323 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ certificate via:
* INTERNET: only necessary if your MQTT server is not running locally
* FOREGROUND_SERVICE: necessary to send notifications
* RECEIVE_BOOT_COMPLETED: necessary to start service on start-up
* REQUEST_IGNORE_BATTERY_OPTIMIZATIONS: on Android 6+, necessary to request disabling battery optimization
* WRITE_EXTERNAL_STORAGE: only necessary if you want to export log files in Android 4.0 - 4.3

## Future ideas
Expand Down
16 changes: 15 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.apache.tools.ant.taskdefs.condition.Os

plugins {
id 'com.jaredsburrows.license' version '0.8.42'
id 'pl.allegro.tech.build.axion-release' version '1.10.2'
Expand Down Expand Up @@ -63,13 +65,25 @@ android {
}

afterEvaluate {
preBuild.dependsOn(licenseDebugReport)
preBuild.dependsOn(generateLicenseFile)
}

// work around empty license file bug
// see https://github.com/jaredsburrows/gradle-license-plugin/issues/38
task generateLicenseFile(type: Exec) {
workingDir project.rootDir
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine 'gradlew.bat', 'app:licenseReleaseReport'
} else {
commandLine './gradlew', 'app:licenseReleaseReport'
}
}

dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.security:security-crypto:1.0.0-alpha02'
implementation 'androidx.work:work-runtime:2.2.0'
implementation('com.hypertrack:hyperlog:0.0.10') {
exclude group: 'com.android.support'
exclude group: 'com.android.volley'
Expand Down
5 changes: 1 addition & 4 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission-sdk-23 android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
Expand All @@ -32,10 +33,6 @@
</intent-filter>
</activity>

<service
android:name=".ForegroundService"
android:enabled="true" />

<receiver android:name=".receiver.SystemBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
Expand Down
26 changes: 26 additions & 0 deletions app/src/main/java/org/ostrya/presencepublisher/Application.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
package org.ostrya.presencepublisher;

import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Build;
import android.util.Log;
import com.hypertrack.hyperlog.HyperLog;
import org.ostrya.presencepublisher.log.CustomLogFormat;
import org.ostrya.presencepublisher.receiver.SystemBroadcastReceiver;
import org.ostrya.presencepublisher.ui.notification.NotificationFactory;

public class Application extends android.app.Application {
public static final int PERMISSION_REQUEST_CODE = 1;
public static final int LOCATION_REQUEST_CODE = 2;
public static final int BATTERY_OPTIMIZATION_REQUEST_CODE = 3;
public static final int ALARM_REQUEST_CODE = 4;
public static final int MAIN_ACTIVITY_REQUEST_CODE = 5;

@Override
public void onCreate() {
super.onCreate();
initLogger();
initNetworkReceiver();
NotificationFactory.createNotificationChannel(this);
}

private void initLogger() {
HyperLog.initialize(this, new CustomLogFormat(this));
if (BuildConfig.DEBUG) {
HyperLog.setLogLevel(Log.VERBOSE);
} else {
HyperLog.setLogLevel(Log.INFO);
}
}

@SuppressWarnings("deprecation")
private void initNetworkReceiver() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
SystemBroadcastReceiver receiver = new SystemBroadcastReceiver();
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver(receiver, filter);
}
}
}
264 changes: 0 additions & 264 deletions app/src/main/java/org/ostrya/presencepublisher/ForegroundService.java

This file was deleted.

Loading

0 comments on commit 9e71e35

Please sign in to comment.