-
Notifications
You must be signed in to change notification settings - Fork 892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Start services as foreground sooner #1099
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is actually a lot sooner than where it was happening before - before, |
||
notifier.notifyDownloadStarting(); | ||
} | ||
|
||
handleOnStartCommand(intent, startId); | ||
return START_NOT_STICKY; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. most important here is the flag for foreground being explicitly set to true. |
||
DOWNLOADING_NOTIFICATION, true, true); | ||
} | ||
|
||
public void stopForeground() { | ||
if (isForeground) { | ||
service.stopForeground(true); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just moving it up sooner - and start foreground even if we're already foreground (multiple calls are fine, but missing a call is asking for a crash).