mirror of
https://github.com/moparisthebest/open-keychain
synced 2024-12-25 16:38:50 -05:00
Make PassphraseCacheService a foreground service
This commit is contained in:
parent
ee307cdf28
commit
97e8faa1dd
@ -18,9 +18,9 @@
|
|||||||
package org.sufficientlysecure.keychain.service;
|
package org.sufficientlysecure.keychain.service;
|
||||||
|
|
||||||
import android.app.AlarmManager;
|
import android.app.AlarmManager;
|
||||||
|
import android.app.Notification;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.app.Service;
|
import android.app.Service;
|
||||||
import android.app.NotificationManager;
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@ -70,7 +70,7 @@ public class PassphraseCacheService extends Service {
|
|||||||
public static final String EXTRA_KEY_ID = "key_id";
|
public static final String EXTRA_KEY_ID = "key_id";
|
||||||
public static final String EXTRA_PASSPHRASE = "passphrase";
|
public static final String EXTRA_PASSPHRASE = "passphrase";
|
||||||
public static final String EXTRA_MESSENGER = "messenger";
|
public static final String EXTRA_MESSENGER = "messenger";
|
||||||
public static final String EXTRA_USERID = "userid";
|
public static final String EXTRA_USER_ID = "user_id";
|
||||||
|
|
||||||
private static final int REQUEST_ID = 0;
|
private static final int REQUEST_ID = 0;
|
||||||
private static final long DEFAULT_TTL = 15;
|
private static final long DEFAULT_TTL = 15;
|
||||||
@ -102,7 +102,7 @@ public class PassphraseCacheService extends Service {
|
|||||||
intent.putExtra(EXTRA_TTL, Preferences.getPreferences(context).getPassphraseCacheTtl());
|
intent.putExtra(EXTRA_TTL, Preferences.getPreferences(context).getPassphraseCacheTtl());
|
||||||
intent.putExtra(EXTRA_PASSPHRASE, passphrase);
|
intent.putExtra(EXTRA_PASSPHRASE, passphrase);
|
||||||
intent.putExtra(EXTRA_KEY_ID, keyId);
|
intent.putExtra(EXTRA_KEY_ID, keyId);
|
||||||
intent.putExtra(EXTRA_USERID, primaryUserId);
|
intent.putExtra(EXTRA_USER_ID, primaryUserId);
|
||||||
|
|
||||||
context.startService(intent);
|
context.startService(intent);
|
||||||
}
|
}
|
||||||
@ -201,18 +201,18 @@ public class PassphraseCacheService extends Service {
|
|||||||
// get cached passphrase
|
// get cached passphrase
|
||||||
CachedPassphrase cachedPassphrase = mPassphraseCache.get(keyId);
|
CachedPassphrase cachedPassphrase = mPassphraseCache.get(keyId);
|
||||||
if (cachedPassphrase == null) {
|
if (cachedPassphrase == null) {
|
||||||
Log.d(Constants.TAG, "PassphraseCacheService Passphrase not (yet) cached, returning null");
|
Log.d(Constants.TAG, "PassphraseCacheService: Passphrase not (yet) cached, returning null");
|
||||||
// not really an error, just means the passphrase is not cached but not empty either
|
// not really an error, just means the passphrase is not cached but not empty either
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set it again to reset the cache life cycle
|
// set it again to reset the cache life cycle
|
||||||
Log.d(Constants.TAG, "PassphraseCacheService Cache passphrase again when getting it!");
|
Log.d(Constants.TAG, "PassphraseCacheService: Cache passphrase again when getting it!");
|
||||||
addCachedPassphrase(this, keyId, cachedPassphrase.getPassphrase(), cachedPassphrase.getPrimaryUserID());
|
addCachedPassphrase(this, keyId, cachedPassphrase.getPassphrase(), cachedPassphrase.getPrimaryUserID());
|
||||||
return cachedPassphrase.getPassphrase();
|
return cachedPassphrase.getPassphrase();
|
||||||
|
|
||||||
} catch (ProviderHelper.NotFoundException e) {
|
} catch (ProviderHelper.NotFoundException e) {
|
||||||
Log.e(Constants.TAG, "PassphraseCacheService Passphrase for unknown key was requested!");
|
Log.e(Constants.TAG, "PassphraseCacheService: Passphrase for unknown key was requested!");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -229,7 +229,7 @@ public class PassphraseCacheService extends Service {
|
|||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
|
|
||||||
Log.d(Constants.TAG, "PassphraseCacheService Received broadcast...");
|
Log.d(Constants.TAG, "PassphraseCacheService: Received broadcast...");
|
||||||
|
|
||||||
if (action.equals(BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE)) {
|
if (action.equals(BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE)) {
|
||||||
long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
|
long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
|
||||||
@ -254,10 +254,8 @@ public class PassphraseCacheService extends Service {
|
|||||||
private static PendingIntent buildIntent(Context context, long keyId) {
|
private static PendingIntent buildIntent(Context context, long keyId) {
|
||||||
Intent intent = new Intent(BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE);
|
Intent intent = new Intent(BROADCAST_ACTION_PASSPHRASE_CACHE_SERVICE);
|
||||||
intent.putExtra(EXTRA_KEY_ID, keyId);
|
intent.putExtra(EXTRA_KEY_ID, keyId);
|
||||||
PendingIntent sender = PendingIntent.getBroadcast(context, REQUEST_ID, intent,
|
return PendingIntent.getBroadcast(context, REQUEST_ID, intent,
|
||||||
PendingIntent.FLAG_CANCEL_CURRENT);
|
PendingIntent.FLAG_CANCEL_CURRENT);
|
||||||
|
|
||||||
return sender;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -276,11 +274,12 @@ public class PassphraseCacheService extends Service {
|
|||||||
long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
|
long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
|
||||||
|
|
||||||
String passphrase = intent.getStringExtra(EXTRA_PASSPHRASE);
|
String passphrase = intent.getStringExtra(EXTRA_PASSPHRASE);
|
||||||
String primaryUserID = intent.getStringExtra(EXTRA_USERID);
|
String primaryUserID = intent.getStringExtra(EXTRA_USER_ID);
|
||||||
|
|
||||||
Log.d(Constants.TAG,
|
Log.d(Constants.TAG,
|
||||||
"PassphraseCacheService Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: "
|
"PassphraseCacheService: Received ACTION_PASSPHRASE_CACHE_ADD intent in onStartCommand() with keyId: "
|
||||||
+ keyId + ", ttl: " + ttl + ", usrId: " + primaryUserID);
|
+ keyId + ", ttl: " + ttl + ", usrId: " + primaryUserID
|
||||||
|
);
|
||||||
|
|
||||||
// add keyId, passphrase and primary user id to memory
|
// add keyId, passphrase and primary user id to memory
|
||||||
mPassphraseCache.put(keyId, new CachedPassphrase(passphrase, primaryUserID));
|
mPassphraseCache.put(keyId, new CachedPassphrase(passphrase, primaryUserID));
|
||||||
@ -292,8 +291,7 @@ public class PassphraseCacheService extends Service {
|
|||||||
am.set(AlarmManager.RTC_WAKEUP, triggerTime, buildIntent(this, keyId));
|
am.set(AlarmManager.RTC_WAKEUP, triggerTime, buildIntent(this, keyId));
|
||||||
}
|
}
|
||||||
|
|
||||||
updateNotifications();
|
updateService();
|
||||||
|
|
||||||
} else if (ACTION_PASSPHRASE_CACHE_GET.equals(intent.getAction())) {
|
} else if (ACTION_PASSPHRASE_CACHE_GET.equals(intent.getAction())) {
|
||||||
long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
|
long keyId = intent.getLongExtra(EXTRA_KEY_ID, -1);
|
||||||
Messenger messenger = intent.getParcelableExtra(EXTRA_MESSENGER);
|
Messenger messenger = intent.getParcelableExtra(EXTRA_MESSENGER);
|
||||||
@ -307,7 +305,7 @@ public class PassphraseCacheService extends Service {
|
|||||||
try {
|
try {
|
||||||
messenger.send(msg);
|
messenger.send(msg);
|
||||||
} catch (RemoteException e) {
|
} catch (RemoteException e) {
|
||||||
Log.e(Constants.TAG, "PassphraseCacheService Sending message failed", e);
|
Log.e(Constants.TAG, "PassphraseCacheService: Sending message failed", e);
|
||||||
}
|
}
|
||||||
} else if (ACTION_PASSPHRASE_CACHE_CLEAR.equals(intent.getAction())) {
|
} else if (ACTION_PASSPHRASE_CACHE_CLEAR.equals(intent.getAction())) {
|
||||||
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
|
AlarmManager am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
|
||||||
@ -319,9 +317,9 @@ public class PassphraseCacheService extends Service {
|
|||||||
|
|
||||||
mPassphraseCache.clear();
|
mPassphraseCache.clear();
|
||||||
|
|
||||||
updateNotifications();
|
updateService();
|
||||||
} else {
|
} else {
|
||||||
Log.e(Constants.TAG, "PassphraseCacheService Intent or Intent Action not supported!");
|
Log.e(Constants.TAG, "PassphraseCacheService: Intent or Intent Action not supported!");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -340,26 +338,27 @@ public class PassphraseCacheService extends Service {
|
|||||||
|
|
||||||
Log.d(Constants.TAG, "PassphraseCacheService Timeout of keyId " + keyId + ", removed from memory!");
|
Log.d(Constants.TAG, "PassphraseCacheService Timeout of keyId " + keyId + ", removed from memory!");
|
||||||
|
|
||||||
// stop whole service if no cached passphrases remaining
|
updateService();
|
||||||
if (mPassphraseCache.size() == 0) {
|
|
||||||
Log.d(Constants.TAG, "PassphraseCacheServic No passphrases remaining in memory, stopping service!");
|
|
||||||
stopSelf();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateNotifications();
|
private void updateService() {
|
||||||
}
|
|
||||||
|
|
||||||
private void updateNotifications() {
|
|
||||||
NotificationManager notificationManager =
|
|
||||||
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
|
|
||||||
|
|
||||||
if (mPassphraseCache.size() > 0) {
|
if (mPassphraseCache.size() > 0) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
startForeground(NOTIFICATION_ID, getNotification());
|
||||||
|
} else {
|
||||||
|
// stop whole service if no cached passphrases remaining
|
||||||
|
Log.d(Constants.TAG, "PassphraseCacheService: No passphrases remaining in memory, stopping service!");
|
||||||
|
stopForeground(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Notification getNotification() {
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
|
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
|
||||||
builder.setSmallIcon(R.drawable.ic_launcher)
|
builder.setSmallIcon(R.drawable.ic_launcher)
|
||||||
.setContentTitle(getString(R.string.app_name))
|
.setContentTitle(getString(R.string.app_name))
|
||||||
.setContentText(String.format(getString(R.string.passp_cache_notif_n_keys), mPassphraseCache.size()));
|
.setContentText(String.format(getString(R.string.passp_cache_notif_n_keys),
|
||||||
|
mPassphraseCache.size()));
|
||||||
|
|
||||||
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
|
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
|
||||||
|
|
||||||
@ -386,13 +385,11 @@ public class PassphraseCacheService extends Service {
|
|||||||
PendingIntent.FLAG_UPDATE_CURRENT
|
PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
} else {
|
||||||
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
// Fallback, since expandable notifications weren't available back then
|
||||||
} else { // Fallback, since expandable notifications weren't available back then
|
|
||||||
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
|
|
||||||
|
|
||||||
builder.setSmallIcon(R.drawable.ic_launcher)
|
builder.setSmallIcon(R.drawable.ic_launcher)
|
||||||
.setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys, mPassphraseCache.size())))
|
.setContentTitle(String.format(getString(R.string.passp_cache_notif_n_keys,
|
||||||
|
mPassphraseCache.size())))
|
||||||
.setContentText(getString(R.string.passp_cache_notif_click_to_clear));
|
.setContentText(getString(R.string.passp_cache_notif_click_to_clear));
|
||||||
|
|
||||||
Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class);
|
Intent intent = new Intent(getApplicationContext(), PassphraseCacheService.class);
|
||||||
@ -406,13 +403,9 @@ public class PassphraseCacheService extends Service {
|
|||||||
PendingIntent.FLAG_UPDATE_CURRENT
|
PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
notificationManager.notify(NOTIFICATION_ID, builder.build());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
return builder.build();
|
||||||
notificationManager.cancel(NOTIFICATION_ID);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -455,6 +448,7 @@ public class PassphraseCacheService extends Service {
|
|||||||
public String getPrimaryUserID() {
|
public String getPrimaryUserID() {
|
||||||
return primaryUserID;
|
return primaryUserID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassphrase() {
|
public String getPassphrase() {
|
||||||
return passphrase;
|
return passphrase;
|
||||||
}
|
}
|
||||||
@ -462,6 +456,7 @@ public class PassphraseCacheService extends Service {
|
|||||||
public void setPrimaryUserID(String primaryUserID) {
|
public void setPrimaryUserID(String primaryUserID) {
|
||||||
this.primaryUserID = primaryUserID;
|
this.primaryUserID = primaryUserID;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPassphrase(String passphrase) {
|
public void setPassphrase(String passphrase) {
|
||||||
this.passphrase = passphrase;
|
this.passphrase = passphrase;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user