From 5196c14954dfd8e5943cff493323117819fa3cec Mon Sep 17 00:00:00 2001 From: Ahmed El-Helw Date: Sat, 16 Mar 2019 12:59:46 +0400 Subject: [PATCH] Start services as foreground sooner (#1099) 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. --- .../androidquran/service/AudioService.java | 12 ++++++------ .../service/QuranDownloadService.java | 14 +++++++++----- .../service/util/QuranDownloadNotifier.java | 19 +++++++++++++++---- .../labs/androidquran/ui/PagerActivity.java | 1 + 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/quran/labs/androidquran/service/AudioService.java b/app/src/main/java/com/quran/labs/androidquran/service/AudioService.java index 3c8e5bcbe5..2b80954458 100644 --- a/app/src/main/java/com/quran/labs/androidquran/service/AudioService.java +++ b/app/src/main/java/com/quran/labs/androidquran/service/AudioService.java @@ -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); @@ -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(); diff --git a/app/src/main/java/com/quran/labs/androidquran/service/QuranDownloadService.java b/app/src/main/java/com/quran/labs/androidquran/service/QuranDownloadService.java index aa8292fe3b..95e9206e16 100644 --- a/app/src/main/java/com/quran/labs/androidquran/service/QuranDownloadService.java +++ b/app/src/main/java/com/quran/labs/androidquran/service/QuranDownloadService.java @@ -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; @@ -35,6 +34,7 @@ import javax.inject.Inject; +import androidx.localbroadcastmanager.content.LocalBroadcastManager; import okhttp3.Call; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -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); @@ -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(); @@ -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; } diff --git a/app/src/main/java/com/quran/labs/androidquran/service/util/QuranDownloadNotifier.java b/app/src/main/java/com/quran/labs/androidquran/service/util/QuranDownloadNotifier.java index fe238eae81..128b6232a4 100644 --- a/app/src/main/java/com/quran/labs/androidquran/service/util/QuranDownloadNotifier.java +++ b/app/src/main/java/com/quran/labs/androidquran/service/util/QuranDownloadNotifier.java @@ -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; @@ -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); diff --git a/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.java b/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.java index deef4c7be9..f1eac20e22 100644 --- a/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.java +++ b/app/src/main/java/com/quran/labs/androidquran/ui/PagerActivity.java @@ -782,6 +782,7 @@ private void downloadRequiredFiles() { @Override public void onNewIntent(Intent intent) { + super.onNewIntent(intent); if (intent == null) { return; }