Skip to content

Commit

Permalink
Move timeout receiver from service to App class
Browse files Browse the repository at this point in the history
Oreo has new limits on background services that prevent the application
timeout from cancelling notifications properly
  • Loading branch information
bpellin committed Jan 17, 2019
1 parent 21318c2 commit cadbc50
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 118 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@
android:theme="@style/NoTitleBar"></activity>
<activity android:name="com.keepassdroid.GeneratePasswordActivity"
android:theme="@style/NoTitleBar"></activity>
<service android:name="com.keepassdroid.services.TimeoutService"></service>
<meta-data android:name="com.a0soft.gphone.aTrackDog.webURL" android:value="http://keepassdroid.com" />
<meta-data android:name="com.sec.android.support.multiwindow" android:value="true" />
</application>
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/java/com/keepassdroid/EntryActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -237,15 +237,6 @@ private Notification getNotification(String intentText, int descResId) {

NotificationCompat.Builder builder = new NotificationCompat.Builder(this,
NotificationUtil.COPY_CHANNEL_ID);

// Set a timout on the notifcation of twice the app timeout, in case the TimeoutService is
// stopped prematurely.
long timeoutDuration = TimeoutHelper.getTimeoutLength(this);
if (timeoutDuration != -1) {
timeoutDuration = timeoutDuration * 2;
builder = builder.setTimeoutAfter(timeoutDuration);
}

Notification notify = builder.setContentIntent(pending).setContentText(desc).setContentTitle(getString(R.string.app_name))
.setSmallIcon(R.drawable.notify).setTicker(desc).setWhen(System.currentTimeMillis()).build();

Expand Down
40 changes: 37 additions & 3 deletions app/src/main/java/com/keepassdroid/app/App.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2013 Brian Pellin.
* Copyright 2009-2019 Brian Pellin.
*
* This file is part of KeePassDroid.
*
Expand All @@ -20,10 +20,17 @@
package com.keepassdroid.app;

import android.app.Application;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.keepassdroid.Database;
import com.keepassdroid.compat.PRNGFixes;
import com.keepassdroid.fileselect.RecentFileHistory;
import com.keepassdroid.intents.Intents;

import java.util.Calendar;

Expand All @@ -32,7 +39,10 @@ public class App extends Application {
private static boolean shutdown = false;
private static Calendar calendar = null;
private static RecentFileHistory fileHistory = null;

private static final String TAG = "KeePassDroid Timer";

private BroadcastReceiver mIntentReceiver;

public static Database getDB() {
if ( db == null ) {
db = new Database();
Expand Down Expand Up @@ -76,14 +86,38 @@ public void onCreate() {
fileHistory = new RecentFileHistory(this);

PRNGFixes.apply();

mIntentReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();

if ( action.equals(Intents.TIMEOUT) ) {
timeout(context);
}
}
};

IntentFilter filter = new IntentFilter();
filter.addAction(Intents.TIMEOUT);
registerReceiver(mIntentReceiver, filter);
}

private void timeout(Context context) {
Log.d(TAG, "Timeout");
App.setShutdown();

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancelAll();
}

@Override
public void onTerminate() {
if ( db != null ) {
db.clear(getApplicationContext());
}


unregisterReceiver(mIntentReceiver);
super.onTerminate();
}
}
99 changes: 0 additions & 99 deletions app/src/main/java/com/keepassdroid/services/TimeoutService.java

This file was deleted.

25 changes: 19 additions & 6 deletions app/src/main/java/com/keepassdroid/timers/Timeout.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
/*
* Copyright 2012-2019 Brian Pellin.
*
* This file is part of KeePassDroid.
*
* KeePassDroid is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* KeePassDroid is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KeePassDroid. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.keepassdroid.timers;

import android.app.AlarmManager;
Expand All @@ -10,7 +29,6 @@

import com.android.keepass.R;
import com.keepassdroid.intents.Intents;
import com.keepassdroid.services.TimeoutService;

public class Timeout {
private static final int REQUEST_ID = 0;
Expand Down Expand Up @@ -42,8 +60,6 @@ public static void start(Context ctx) {
return;
}

ctx.startService(new Intent(ctx, TimeoutService.class));

long triggerTime = System.currentTimeMillis() + timeout;
AlarmManager am = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);

Expand All @@ -56,9 +72,6 @@ public static void cancel(Context ctx) {

Log.d(TAG, "Timeout cancel");
am.cancel(buildIntent(ctx));

ctx.stopService(new Intent(ctx, TimeoutService.class));

}

}

0 comments on commit cadbc50

Please sign in to comment.