Use JobScheduler to make the "WiFi-only" feature work properly newer Android versions.

I'm considering dropping support for KitKat, as we have now double the code for the same feature.
This commit is contained in:
2017-08-19 08:17:52 +02:00
parent 858651e808
commit 852e38b97d
14 changed files with 211 additions and 93 deletions

View File

@ -1,75 +0,0 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.apps.abit.service;
import android.app.IntentService;
import android.content.Intent;
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity;
import ch.dissem.apps.abit.util.Preferences;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.Plaintext;
import static ch.dissem.apps.abit.MainActivity.updateNodeSwitch;
/**
* @author Christian Basler
*/
public class BitmessageIntentService extends IntentService {
public static final String EXTRA_DELETE_MESSAGE = "ch.dissem.abit.DeleteMessage";
public static final String EXTRA_STARTUP_NODE = "ch.dissem.abit.StartFullNode";
public static final String EXTRA_SHUTDOWN_NODE = "ch.dissem.abit.StopFullNode";
private BitmessageContext bmc;
public BitmessageIntentService() {
super("BitmessageIntentService");
}
@Override
public void onCreate() {
super.onCreate();
bmc = Singleton.getBitmessageContext(this);
}
@Override
protected void onHandleIntent(Intent intent) {
if (intent.hasExtra(EXTRA_DELETE_MESSAGE)) {
Plaintext item = (Plaintext) intent.getSerializableExtra(EXTRA_DELETE_MESSAGE);
bmc.labeler().delete(item);
bmc.messages().save(item);
Singleton.getMessageListener(this).resetNotification();
}
if (intent.hasExtra(EXTRA_STARTUP_NODE)) {
if (Preferences.isConnectionAllowed(this)) {
Preferences.setFullNodeActive(this, true);
startService(new Intent(this, BitmessageService.class));
updateNodeSwitch();
} else {
Intent dialogIntent = new Intent(this, FullNodeDialogActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
}
if (intent.hasExtra(EXTRA_SHUTDOWN_NODE)) {
Preferences.setFullNodeActive(this, false);
stopService(new Intent(this, BitmessageService.class));
}
}
}

View File

@ -0,0 +1,63 @@
/*
* Copyright 2016 Christian Basler
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ch.dissem.apps.abit.service
import android.app.IntentService
import android.content.Intent
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity
import ch.dissem.apps.abit.util.NetworkUtils
import ch.dissem.apps.abit.util.Preferences
import ch.dissem.bitmessage.BitmessageContext
import ch.dissem.bitmessage.entity.Plaintext
import ch.dissem.apps.abit.MainActivity.updateNodeSwitch
/**
* @author Christian Basler
*/
class BitmessageIntentService : IntentService("BitmessageIntentService") {
private lateinit var bmc: BitmessageContext
override fun onCreate() {
super.onCreate()
bmc = Singleton.getBitmessageContext(this)
}
override fun onHandleIntent(intent: Intent?) {
if (intent!!.hasExtra(EXTRA_DELETE_MESSAGE)) {
val item = intent.getSerializableExtra(EXTRA_DELETE_MESSAGE) as Plaintext
bmc.labeler.delete(item)
bmc.messages.save(item)
Singleton.getMessageListener(this).resetNotification()
}
if (intent.hasExtra(EXTRA_STARTUP_NODE)) {
NetworkUtils.enableNode(this)
}
if (intent.hasExtra(EXTRA_SHUTDOWN_NODE)) {
NetworkUtils.disableNode(this)
}
}
companion object {
const val EXTRA_DELETE_MESSAGE = "ch.dissem.abit.DeleteMessage"
const val EXTRA_STARTUP_NODE = "ch.dissem.abit.StartFullNode"
const val EXTRA_SHUTDOWN_NODE = "ch.dissem.abit.StopFullNode"
}
}

View File

@ -73,7 +73,7 @@ class ProofOfWorkService : Service() {
val startTime = System.currentTimeMillis()
engine.calculateNonce(item.initialHash, item.targetValue, object : ProofOfWorkEngine.Callback {
override fun onNonceCalculated(initialHash: ByteArray, nonce: ByteArray) {
notification.finished(item)
notification.finished()
val time = System.currentTimeMillis() - startTime
PowStats.addPow(this@ProofOfWorkService, time, item.targetValue)
try {

View File

@ -0,0 +1,18 @@
package ch.dissem.apps.abit.service
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import ch.dissem.apps.abit.util.NetworkUtils
import ch.dissem.apps.abit.util.Preferences
/**
* Created by chrigu on 18.08.17.
*/
class StartServiceReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
if (Preferences.isFullNodeActive(context)) {
NetworkUtils.enableNode(context, false)
}
}
}

View File

@ -0,0 +1,31 @@
package ch.dissem.apps.abit.service
import android.app.job.JobParameters
import android.app.job.JobService
import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import ch.dissem.apps.abit.util.Preferences
/**
* Created by chrigu on 18.08.17.
*/
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class StartupNodeOnWifiService : JobService() {
override fun onStartJob(params: JobParameters?): Boolean {
val bmc = Singleton.getBitmessageContext(this)
if (Preferences.isFullNodeActive(this) && !bmc.isRunning()) {
applicationContext.startService(Intent(this, BitmessageService::class.java))
}
return true
}
override fun onStopJob(params: JobParameters?): Boolean {
if (Preferences.isWifiOnly(this)) {
Singleton.getBitmessageContext(this).shutdown()
return Preferences.isFullNodeActive(this)
} else {
return false
}
}
}