Skip to content
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

Merged
merged 1 commit into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
Copy link
Contributor Author

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).

// 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())) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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, handleOnStartCommand would process the message and then give it to the handler, which would eventually start foreground and show the notification. on some devices, this could take so long, so make it the first thing we do.

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),
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
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