Adapted service GeneratorService to be a job.
Since Android 8.0 background execution is limited, therefore Context.startService may throw an IllegalStateException if the service is not permitted to run as a background service.
This commit is contained in:
parent
265f8809b8
commit
c1fdb758ea
3 changed files with 17 additions and 9 deletions
|
@ -73,6 +73,7 @@
|
||||||
<service
|
<service
|
||||||
android:name="org.secuso.privacyfriendlysudoku.controller.GeneratorService"
|
android:name="org.secuso.privacyfriendlysudoku.controller.GeneratorService"
|
||||||
android:enabled="true"
|
android:enabled="true"
|
||||||
|
android:permission="android.permission.BIND_JOB_SERVICE"
|
||||||
android:exported="false" />
|
android:exported="false" />
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,10 @@ package org.secuso.privacyfriendlysudoku.controller;
|
||||||
|
|
||||||
import android.app.IntentService;
|
import android.app.IntentService;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.core.app.JobIntentService;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
@ -49,7 +51,7 @@ import static org.secuso.privacyfriendlysudoku.controller.NewLevelManager.PRE_SA
|
||||||
*
|
*
|
||||||
* @author Christopher Beckmann
|
* @author Christopher Beckmann
|
||||||
*/
|
*/
|
||||||
public class GeneratorService extends IntentService {
|
public class GeneratorService extends JobIntentService {
|
||||||
|
|
||||||
private static final String TAG = GeneratorService.class.getSimpleName();
|
private static final String TAG = GeneratorService.class.getSimpleName();
|
||||||
public static final String ACTION_GENERATE = TAG + " ACTION_GENERATE";
|
public static final String ACTION_GENERATE = TAG + " ACTION_GENERATE";
|
||||||
|
@ -64,11 +66,11 @@ public class GeneratorService extends IntentService {
|
||||||
//private Handler mHandler = new Handler();
|
//private Handler mHandler = new Handler();
|
||||||
|
|
||||||
|
|
||||||
public GeneratorService() {
|
//public GeneratorService() {
|
||||||
super("Generator Service");
|
// super("Generator Service");
|
||||||
}
|
//}
|
||||||
|
|
||||||
public GeneratorService(String name) { super(name); }
|
//public GeneratorService(String name) { super(name); }
|
||||||
|
|
||||||
|
|
||||||
private void buildGenerationList() {
|
private void buildGenerationList() {
|
||||||
|
@ -120,12 +122,11 @@ public class GeneratorService extends IntentService {
|
||||||
// if we start this service multiple times while we are already generating...
|
// if we start this service multiple times while we are already generating...
|
||||||
// we ignore this call and just keep generating
|
// we ignore this call and just keep generating
|
||||||
buildGenerationList();
|
buildGenerationList();
|
||||||
|
|
||||||
// generate from the list
|
// generate from the list
|
||||||
if(generationList.size() > 0) {
|
if(generationList.size() > 0) {
|
||||||
|
|
||||||
// generate 1 level and wait for it to be done.
|
// generate 1 level and wait for it to be done.
|
||||||
Pair<GameType, GameDifficulty> dataPair = generationList.get(0);
|
Pair<GameType, GameDifficulty> dataPair = generationList.remove(0);
|
||||||
GameType type = dataPair.first;
|
GameType type = dataPair.first;
|
||||||
GameDifficulty diff = dataPair.second;
|
GameDifficulty diff = dataPair.second;
|
||||||
|
|
||||||
|
@ -275,8 +276,13 @@ public class GeneratorService extends IntentService {
|
||||||
builder.setSmallIcon(R.drawable.splash_icon);
|
builder.setSmallIcon(R.drawable.splash_icon);
|
||||||
startForeground(50, builder.build());
|
startForeground(50, builder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void enqueueWork(Context context, Intent intent) {
|
||||||
|
enqueueWork(context, GeneratorService.class, 1000, intent);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onHandleIntent(@Nullable Intent intent) {
|
protected void onHandleWork(@Nullable Intent intent) {
|
||||||
if (intent != null) {
|
if (intent != null) {
|
||||||
|
|
||||||
String action = intent.getAction();
|
String action = intent.getAction();
|
||||||
|
|
|
@ -217,7 +217,8 @@ public class NewLevelManager {
|
||||||
Intent i = new Intent(context, GeneratorService.class);
|
Intent i = new Intent(context, GeneratorService.class);
|
||||||
i.setAction(GeneratorService.ACTION_GENERATE);
|
i.setAction(GeneratorService.ACTION_GENERATE);
|
||||||
//i.putExtra(ProtocolService.EXTRA_PROTOCOL, current.componentName().flattenToString());
|
//i.putExtra(ProtocolService.EXTRA_PROTOCOL, current.componentName().flattenToString());
|
||||||
context.startService(i);
|
//context.startService(i);
|
||||||
|
GeneratorService.enqueueWork(context, i);
|
||||||
|
|
||||||
//new AsyncGenerationTask().execute();
|
//new AsyncGenerationTask().execute();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue