Skip to content

Commit

Permalink
Start services as foreground sooner (#1099)
Browse files Browse the repository at this point in the history
Whenever a service is started as foreground, make the startForeground
call (along with showing the notification) one of the first things that
the service does.
  • Loading branch information
ahmedre authored Mar 16, 2019
1 parent e015f9a commit 5196c14
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}

final String action = intent.getAction();
if (ACTION_PLAYBACK.equals(action)) {
// this is the only action with startForegroundService, so
// go to the foreground as quickly as possible.
setUpAsForeground();
}

if (ACTION_CONNECT.equals(action)) {
if (State.Stopped == state) {
processStopRequest(true);
Expand Down Expand Up @@ -401,12 +407,6 @@ public int onStartCommand(Intent intent, int flags, int startId) {
}
}

// this is the only action called with startForegroundService, so go into the foreground
// as quickly as possible.
if (!isSetupAsForeground) {
setUpAsForeground();
}

if (intent.getBooleanExtra(EXTRA_STOP_IF_PLAYING, false)) {
if (player != null) {
player.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import android.os.Looper;
import android.os.Message;
import android.os.StatFs;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.crashlytics.android.Crashlytics;
import com.quran.labs.androidquran.QuranApplication;
Expand All @@ -35,6 +34,7 @@

import javax.inject.Inject;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;
Expand Down Expand Up @@ -187,8 +187,7 @@ private void handleOnStartCommand(Intent intent, int startId) {
Intent currentLast = lastSentIntent;
String currentDownload = currentLast == null ? null :
currentLast.getStringExtra(ProgressIntent.DOWNLOAD_KEY);
if (download != null && currentDownload != null &&
download.equals(currentDownload)) {
if (download != null && download.equals(currentDownload)) {
Timber.d("resending last broadcast...");
broadcastManager.sendBroadcast(currentLast);

Expand All @@ -204,8 +203,7 @@ private void handleOnStartCommand(Intent intent, int startId) {
}
}

int what = intent.getIntExtra(EXTRA_DOWNLOAD_TYPE,
DOWNLOAD_TYPE_UNDEF);
int what = intent.getIntExtra(EXTRA_DOWNLOAD_TYPE, DOWNLOAD_TYPE_UNDEF);
currentOperations.incrementAndGet();
// put the message in the queue
Message msg = serviceHandler.obtainMessage();
Expand Down Expand Up @@ -233,6 +231,12 @@ private void sendNoOpMessage(int id) {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// if it's a download, it wants to be a foreground service.
// quickly start as foreground before actually enqueueing the request.
if (ACTION_DOWNLOAD_URL.equals(intent.getAction())) {
notifier.notifyDownloadStarting();
}

handleOnStartCommand(intent, startId);
return START_NOT_STICKY;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.crashlytics.android.Crashlytics;
import com.quran.labs.androidquran.QuranDataActivity;
import com.quran.labs.androidquran.R;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.content.ContextCompat;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

public class QuranDownloadNotifier {
// error messages
public static final int ERROR_DISK_SPACE = 1;
Expand Down Expand Up @@ -273,6 +274,16 @@ public Intent notifyError(int errorCode, boolean isFatal, NotificationDetails de
return progressIntent;
}

public void notifyDownloadStarting(){
String title = appContext.getString(R.string.downloading_title);
notificationManager.cancel(DOWNLOADING_ERROR_NOTIFICATION);

lastMaximum = -1;
lastProgress = -1;
showNotification(title, appContext.getString(R.string.downloading_message),
DOWNLOADING_NOTIFICATION, true, true);
}

public void stopForeground() {
if (isForeground) {
service.stopForeground(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ private void downloadRequiredFiles() {

@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent == null) {
return;
}
Expand Down

0 comments on commit 5196c14

Please sign in to comment.