Use startForegroundService instead of startService on Android 8

This commit is contained in:
Moxie Marlinspike 2017-11-27 12:18:14 -08:00
parent d9ab1a93f8
commit acfc9d75e0
11 changed files with 77 additions and 40 deletions

View File

@ -1310,6 +1310,7 @@
<!-- transport_selection_list_item --> <!-- transport_selection_list_item -->
<string name="transport_selection_list_item__transport_icon">Transport icon</string> <string name="transport_selection_list_item__transport_icon">Transport icon</string>
<string name="ConversationListFragment_loading">Loading...</string> <string name="ConversationListFragment_loading">Loading...</string>
<string name="CallNotificationBuilder_connecting">Connecting...</string>
<!-- EOF --> <!-- EOF -->

View File

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.components.reminder; package org.thoughtcrime.securesms.components.reminder;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
@ -14,6 +15,7 @@ import android.view.View;
import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
@SuppressLint("BatteryLife")
public class DozeReminder extends Reminder { public class DozeReminder extends Reminder {
@RequiresApi(api = Build.VERSION_CODES.M) @RequiresApi(api = Build.VERSION_CODES.M)
@ -21,14 +23,11 @@ public class DozeReminder extends Reminder {
super(context.getString(R.string.DozeReminder_optimize_for_missing_play_services), super(context.getString(R.string.DozeReminder_optimize_for_missing_play_services),
context.getString(R.string.DozeReminder_this_device_does_not_support_play_services_tap_to_disable_system_battery)); context.getString(R.string.DozeReminder_this_device_does_not_support_play_services_tap_to_disable_system_battery));
setOkListener(new View.OnClickListener() { setOkListener(v -> {
@Override
public void onClick(View v) {
TextSecurePreferences.setPromptedOptimizeDoze(context, true); TextSecurePreferences.setPromptedOptimizeDoze(context, true);
Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS, Intent intent = new Intent(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS,
Uri.parse("package:" + context.getPackageName())); Uri.parse("package:" + context.getPackageName()));
context.startActivity(intent); context.startActivity(intent);
}
}); });
setDismissListener(new View.OnClickListener() { setDismissListener(new View.OnClickListener() {

View File

@ -2,6 +2,7 @@ package org.thoughtcrime.securesms.jobs;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.util.Log; import android.util.Log;
import android.util.Pair; import android.util.Pair;
@ -239,7 +240,9 @@ public class PushDecryptJob extends ContextJob {
intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource())); intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource()));
intent.putExtra(WebRtcCallService.EXTRA_REMOTE_DESCRIPTION, message.getDescription()); intent.putExtra(WebRtcCallService.EXTRA_REMOTE_DESCRIPTION, message.getDescription());
intent.putExtra(WebRtcCallService.EXTRA_TIMESTAMP, envelope.getTimestamp()); intent.putExtra(WebRtcCallService.EXTRA_TIMESTAMP, envelope.getTimestamp());
context.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(intent);
else context.startService(intent);
} }
} }
@ -252,7 +255,9 @@ public class PushDecryptJob extends ContextJob {
intent.putExtra(WebRtcCallService.EXTRA_CALL_ID, message.getId()); intent.putExtra(WebRtcCallService.EXTRA_CALL_ID, message.getId());
intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource())); intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource()));
intent.putExtra(WebRtcCallService.EXTRA_REMOTE_DESCRIPTION, message.getDescription()); intent.putExtra(WebRtcCallService.EXTRA_REMOTE_DESCRIPTION, message.getDescription());
context.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(intent);
else context.startService(intent);
} }
private void handleCallIceUpdateMessage(@NonNull SignalServiceEnvelope envelope, private void handleCallIceUpdateMessage(@NonNull SignalServiceEnvelope envelope,
@ -267,7 +272,9 @@ public class PushDecryptJob extends ContextJob {
intent.putExtra(WebRtcCallService.EXTRA_ICE_SDP, message.getSdp()); intent.putExtra(WebRtcCallService.EXTRA_ICE_SDP, message.getSdp());
intent.putExtra(WebRtcCallService.EXTRA_ICE_SDP_MID, message.getSdpMid()); intent.putExtra(WebRtcCallService.EXTRA_ICE_SDP_MID, message.getSdpMid());
intent.putExtra(WebRtcCallService.EXTRA_ICE_SDP_LINE_INDEX, message.getSdpMLineIndex()); intent.putExtra(WebRtcCallService.EXTRA_ICE_SDP_LINE_INDEX, message.getSdpMLineIndex());
context.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(intent);
else context.startService(intent);
} }
} }
@ -283,7 +290,9 @@ public class PushDecryptJob extends ContextJob {
intent.setAction(WebRtcCallService.ACTION_REMOTE_HANGUP); intent.setAction(WebRtcCallService.ACTION_REMOTE_HANGUP);
intent.putExtra(WebRtcCallService.EXTRA_CALL_ID, message.getId()); intent.putExtra(WebRtcCallService.EXTRA_CALL_ID, message.getId());
intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource())); intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource()));
context.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(intent);
else context.startService(intent);
} }
} }
@ -294,7 +303,9 @@ public class PushDecryptJob extends ContextJob {
intent.setAction(WebRtcCallService.ACTION_REMOTE_BUSY); intent.setAction(WebRtcCallService.ACTION_REMOTE_BUSY);
intent.putExtra(WebRtcCallService.EXTRA_CALL_ID, message.getId()); intent.putExtra(WebRtcCallService.EXTRA_CALL_ID, message.getId());
intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource())); intent.putExtra(WebRtcCallService.EXTRA_REMOTE_ADDRESS, Address.fromExternal(context, envelope.getSource()));
context.startService(intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(intent);
else context.startService(intent);
} }
private void handleEndSessionMessage(@NonNull MasterSecretUnion masterSecret, private void handleEndSessionMessage(@NonNull MasterSecretUnion masterSecret,

View File

@ -3,13 +3,18 @@ package org.thoughtcrime.securesms.service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
public class BootReceiver extends BroadcastReceiver { public class BootReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
Intent messageRetrievalService = new Intent(context, MessageRetrievalService.class); Intent messageRetrievalService = new Intent(context, MessageRetrievalService.class);
context.startService(messageRetrievalService);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(messageRetrievalService);
else context.startService(messageRetrievalService);
}
} }
} }

View File

@ -1,4 +1,4 @@
/** /*
* Copyright (C) 2011 Whisper Systems * Copyright (C) 2011 Whisper Systems
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
@ -16,6 +16,7 @@
*/ */
package org.thoughtcrime.securesms.service; package org.thoughtcrime.securesms.service;
import android.annotation.SuppressLint;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.AlarmManager; import android.app.AlarmManager;
import android.app.Notification; import android.app.Notification;
@ -44,6 +45,7 @@ import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
import org.thoughtcrime.securesms.jobs.MasterSecretDecryptJob; import org.thoughtcrime.securesms.jobs.MasterSecretDecryptJob;
import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.notifications.MessageNotifier;
import org.thoughtcrime.securesms.util.DynamicLanguage; import org.thoughtcrime.securesms.util.DynamicLanguage;
import org.thoughtcrime.securesms.util.ServiceUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -95,6 +97,7 @@ public class KeyCachingService extends Service {
return masterSecret; return masterSecret;
} }
@SuppressLint("StaticFieldLeak")
public void setMasterSecret(final MasterSecret masterSecret) { public void setMasterSecret(final MasterSecret masterSecret) {
synchronized (KeyCachingService.class) { synchronized (KeyCachingService.class) {
KeyCachingService.masterSecret = masterSecret; KeyCachingService.masterSecret = masterSecret;
@ -176,7 +179,7 @@ public class KeyCachingService extends Service {
private void handleActivityStarted() { private void handleActivityStarted() {
Log.w("KeyCachingService", "Incrementing activity count..."); Log.w("KeyCachingService", "Incrementing activity count...");
AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE); AlarmManager alarmManager = ServiceUtil.getAlarmManager(this);
alarmManager.cancel(pending); alarmManager.cancel(pending);
activitiesRunning++; activitiesRunning++;
} }
@ -188,6 +191,7 @@ public class KeyCachingService extends Service {
startTimeoutIfAppropriate(); startTimeoutIfAppropriate();
} }
@SuppressLint("StaticFieldLeak")
private void handleClearKey() { private void handleClearKey() {
Log.w("KeyCachingService", "handleClearKey()"); Log.w("KeyCachingService", "handleClearKey()");
KeyCachingService.masterSecret = null; KeyCachingService.masterSecret = null;
@ -226,7 +230,7 @@ public class KeyCachingService extends Service {
Log.w("KeyCachingService", "Starting timeout: " + timeoutMillis); Log.w("KeyCachingService", "Starting timeout: " + timeoutMillis);
AlarmManager alarmManager = (AlarmManager)this.getSystemService(ALARM_SERVICE); AlarmManager alarmManager = ServiceUtil.getAlarmManager(this);
alarmManager.cancel(pending); alarmManager.cancel(pending);
alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeoutMillis, pending); alarmManager.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + timeoutMillis, pending);
} }
@ -304,15 +308,13 @@ public class KeyCachingService extends Service {
private PendingIntent buildLockIntent() { private PendingIntent buildLockIntent() {
Intent intent = new Intent(this, KeyCachingService.class); Intent intent = new Intent(this, KeyCachingService.class);
intent.setAction(PASSPHRASE_EXPIRED_EVENT); intent.setAction(PASSPHRASE_EXPIRED_EVENT);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, intent, 0); return PendingIntent.getService(getApplicationContext(), 0, intent, 0);
return pendingIntent;
} }
private PendingIntent buildLaunchIntent() { private PendingIntent buildLaunchIntent() {
Intent intent = new Intent(this, ConversationListActivity.class); Intent intent = new Intent(this, ConversationListActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent launchIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); return PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);
return launchIntent;
} }
@Override @Override

View File

@ -20,7 +20,6 @@ import org.whispersystems.jobqueue.requirements.RequirementListener;
import org.whispersystems.libsignal.InvalidVersionException; import org.whispersystems.libsignal.InvalidVersionException;
import org.whispersystems.signalservice.api.SignalServiceMessagePipe; import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver; import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
@ -209,16 +208,13 @@ public class MessageRetrievalService extends Service implements InjectableType,
try { try {
Log.w(TAG, "Reading message..."); Log.w(TAG, "Reading message...");
localPipe.read(REQUEST_TIMEOUT_MINUTES, TimeUnit.MINUTES, localPipe.read(REQUEST_TIMEOUT_MINUTES, TimeUnit.MINUTES,
new SignalServiceMessagePipe.MessagePipeCallback() { envelope -> {
@Override
public void onMessage(SignalServiceEnvelope envelope) {
Log.w(TAG, "Retrieved envelope! " + envelope.getSource()); Log.w(TAG, "Retrieved envelope! " + envelope.getSource());
PushContentReceiveJob receiveJob = new PushContentReceiveJob(MessageRetrievalService.this); PushContentReceiveJob receiveJob = new PushContentReceiveJob(MessageRetrievalService.this);
receiveJob.handle(envelope); receiveJob.handle(envelope);
decrementPushReceived(); decrementPushReceived();
}
}); });
} catch (TimeoutException e) { } catch (TimeoutException e) {
Log.w(TAG, "Application level read timeout..."); Log.w(TAG, "Application level read timeout...");

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
@ -22,7 +23,9 @@ public class PanicResponderListener extends BroadcastReceiver {
{ {
Intent lockIntent = new Intent(context, KeyCachingService.class); Intent lockIntent = new Intent(context, KeyCachingService.class);
lockIntent.setAction(KeyCachingService.CLEAR_KEY_ACTION); lockIntent.setAction(KeyCachingService.CLEAR_KEY_ACTION);
context.startService(lockIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(lockIntent);
else context.startService(lockIntent);
} }
} }
} }

View File

@ -3,6 +3,7 @@ package org.thoughtcrime.securesms.service;
import android.content.BroadcastReceiver; import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Build;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
@ -10,10 +11,14 @@ import org.thoughtcrime.securesms.util.TextSecurePreferences;
public class PersistentConnectionBootListener extends BroadcastReceiver { public class PersistentConnectionBootListener extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (intent != null && Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
if (TextSecurePreferences.isGcmDisabled(context)) { if (TextSecurePreferences.isGcmDisabled(context)) {
Intent serviceIntent = new Intent(context, MessageRetrievalService.class); Intent serviceIntent = new Intent(context, MessageRetrievalService.class);
serviceIntent.setAction(MessageRetrievalService.ACTION_INITIALIZE); serviceIntent.setAction(MessageRetrievalService.ACTION_INITIALIZE);
context.startService(serviceIntent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) context.startForegroundService(serviceIntent);
else context.startService(serviceIntent);
}
} }
} }
} }

View File

@ -97,6 +97,7 @@ import java.util.concurrent.TimeUnit;
import javax.inject.Inject; import javax.inject.Inject;
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_ESTABLISHED; import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_ESTABLISHED;
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_INCOMING_CONNECTING;
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_INCOMING_RINGING; import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_INCOMING_RINGING;
import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_OUTGOING_RINGING; import static org.thoughtcrime.securesms.webrtc.CallNotificationBuilder.TYPE_OUTGOING_RINGING;
@ -334,6 +335,10 @@ public class WebRtcCallService extends Service implements InjectableType, PeerCo
return; return;
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
setCallInProgressNotification(TYPE_INCOMING_CONNECTING, this.recipient);
}
timeoutExecutor.schedule(new TimeoutRunnable(this.callId), 2, TimeUnit.MINUTES); timeoutExecutor.schedule(new TimeoutRunnable(this.callId), 2, TimeUnit.MINUTES);
initializeVideo(); initializeVideo();

View File

@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.util; package org.thoughtcrime.securesms.util;
import android.app.Activity; import android.app.Activity;
import android.app.AlarmManager;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.content.Context; import android.content.Context;
import android.media.AudioManager; import android.media.AudioManager;
@ -38,4 +39,8 @@ public class ServiceUtil {
public static PowerManager getPowerManager(Context context) { public static PowerManager getPowerManager(Context context) {
return (PowerManager)context.getSystemService(Context.POWER_SERVICE); return (PowerManager)context.getSystemService(Context.POWER_SERVICE);
} }
public static AlarmManager getAlarmManager(Context context) {
return (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
}
} }

View File

@ -31,6 +31,8 @@ public class CallNotificationBuilder {
public static final int TYPE_INCOMING_RINGING = 1; public static final int TYPE_INCOMING_RINGING = 1;
public static final int TYPE_OUTGOING_RINGING = 2; public static final int TYPE_OUTGOING_RINGING = 2;
public static final int TYPE_ESTABLISHED = 3; public static final int TYPE_ESTABLISHED = 3;
public static final int TYPE_INCOMING_CONNECTING = 4;
public static Notification getCallInProgressNotification(Context context, int type, Recipient recipient) { public static Notification getCallInProgressNotification(Context context, int type, Recipient recipient) {
Intent contentIntent = new Intent(context, WebRtcCallActivity.class); Intent contentIntent = new Intent(context, WebRtcCallActivity.class);
@ -44,7 +46,10 @@ public class CallNotificationBuilder {
.setOngoing(true) .setOngoing(true)
.setContentTitle(recipient.getName()); .setContentTitle(recipient.getName());
if (type == TYPE_INCOMING_RINGING) { if (type == TYPE_INCOMING_CONNECTING) {
builder.setContentText(context.getString(R.string.CallNotificationBuilder_connecting));
builder.setPriority(NotificationCompat.PRIORITY_MIN);
} else if (type == TYPE_INCOMING_RINGING) {
builder.setContentText(context.getString(R.string.NotificationBarManager__incoming_signal_call)); builder.setContentText(context.getString(R.string.NotificationBarManager__incoming_signal_call));
builder.addAction(getServiceNotificationAction(context, WebRtcCallService.ACTION_DENY_CALL, R.drawable.ic_close_grey600_32dp, R.string.NotificationBarManager__deny_call)); builder.addAction(getServiceNotificationAction(context, WebRtcCallService.ACTION_DENY_CALL, R.drawable.ic_close_grey600_32dp, R.string.NotificationBarManager__deny_call));
builder.addAction(getActivityNotificationAction(context, WebRtcCallActivity.ANSWER_ACTION, R.drawable.ic_phone_grey600_32dp, R.string.NotificationBarManager__answer_call)); builder.addAction(getActivityNotificationAction(context, WebRtcCallActivity.ANSWER_ACTION, R.drawable.ic_phone_grey600_32dp, R.string.NotificationBarManager__answer_call));