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:
Christian Basler 2017-08-19 08:17:52 +02:00
parent 858651e808
commit 852e38b97d
14 changed files with 211 additions and 93 deletions

View File

@ -174,11 +174,20 @@
android:exported="false"/>
<!-- Receive Wi-Fi connection state changes -->
<receiver android:name=".listener.WifiReceiver">
<receiver android:name=".listener.WifiReceiver" android:enabled="@bool/is_pre_api_21">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<receiver android:name=".service.StartServiceReceiver" android:enabled="@bool/is_post_api_21">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service
android:name=".service.StartupNodeOnWifiService"
android:permission="android.permission.BIND_JOB_SERVICE"
android:exported="true"/>
<activity
android:name=".StatusActivity"

View File

@ -53,7 +53,6 @@ import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity;
import ch.dissem.apps.abit.drawer.ProfileImageListener;
import ch.dissem.apps.abit.drawer.ProfileSelectionListener;
import ch.dissem.apps.abit.listener.ListSelectionListener;
@ -61,6 +60,7 @@ import ch.dissem.apps.abit.service.BitmessageService;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.synchronization.SyncAdapter;
import ch.dissem.apps.abit.util.Labels;
import ch.dissem.apps.abit.util.NetworkUtils;
import ch.dissem.apps.abit.util.Preferences;
import ch.dissem.bitmessage.BitmessageContext;
import ch.dissem.bitmessage.entity.BitmessageAddress;
@ -271,11 +271,10 @@ public class MainActivity extends AppCompatActivity
@Override
public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView,
boolean isChecked) {
Preferences.setFullNodeActive(MainActivity.this, isChecked);
if (isChecked) {
checkAndStartNode();
NetworkUtils.enableNode(MainActivity.this);
} else {
stopService(new Intent(MainActivity.this, BitmessageService.class));
NetworkUtils.disableNode(MainActivity.this);
}
}
});
@ -417,7 +416,7 @@ public class MainActivity extends AppCompatActivity
protected void onResume() {
updateUnread();
if (Preferences.isFullNodeActive(this) && Preferences.isConnectionAllowed(MainActivity.this)) {
startService(new Intent(this, BitmessageService.class));
NetworkUtils.enableNode(this, false);
}
updateNodeSwitch();
Singleton.getMessageListener(this).resetNotification();
@ -483,14 +482,6 @@ public class MainActivity extends AppCompatActivity
}
}
private void checkAndStartNode() {
if (Preferences.isConnectionAllowed(MainActivity.this)) {
startService(new Intent(this, BitmessageService.class));
} else {
startActivity(new Intent(this, FullNodeDialogActivity.class));
}
}
public void updateUnread() {
for (IDrawerItem item : drawer.getDrawerItems()) {
if (item.getTag() instanceof Label) {

View File

@ -17,10 +17,12 @@
package ch.dissem.apps.abit.dialog;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.util.NetworkUtils;
import ch.dissem.apps.abit.util.Preferences;
/**
@ -36,12 +38,16 @@ public class FullNodeDialogActivity extends Activity {
@Override
public void onClick(View view) {
Preferences.setWifiOnly(FullNodeDialogActivity.this, false);
NetworkUtils.enableNode(getApplicationContext());
finish();
}
});
findViewById(R.id.dismiss).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
NetworkUtils.scheduleNodeStart(getApplicationContext());
}
finish();
}
});

View File

@ -37,7 +37,7 @@ import ch.dissem.bitmessage.entity.Plaintext
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import ch.dissem.apps.abit.MainActivity.EXTRA_REPLY_TO_MESSAGE
import ch.dissem.apps.abit.MainActivity.EXTRA_SHOW_MESSAGE
import ch.dissem.apps.abit.service.BitmessageIntentService.EXTRA_DELETE_MESSAGE
import ch.dissem.apps.abit.service.BitmessageIntentService.Companion.EXTRA_DELETE_MESSAGE
import ch.dissem.apps.abit.util.Drawables.toBitmap
class NewMessageNotification(ctx: Context) : AbstractNotification(ctx) {

View File

@ -91,7 +91,7 @@ class ProofOfWorkNotification(ctx: Context) : AbstractNotification(ctx) {
}
}
fun finished(item: ProofOfWorkService.PowItem) {
fun finished() {
timer?.cancel()
progress = 0
progressMax = 0

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
}
}
}

View File

@ -0,0 +1,65 @@
package ch.dissem.apps.abit.util
import android.app.Activity
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.support.annotation.RequiresApi
import ch.dissem.apps.abit.MainActivity.updateNodeSwitch
import ch.dissem.apps.abit.dialog.FullNodeDialogActivity
import ch.dissem.apps.abit.service.BitmessageService
import ch.dissem.apps.abit.service.StartupNodeOnWifiService
/**
* Created by chrigu on 18.08.17.
*/
object NetworkUtils {
@JvmStatic
@JvmOverloads
fun enableNode(ctx: Context, ask: Boolean = true) {
Preferences.setFullNodeActive(ctx, true)
if (Preferences.isWifiOnly(ctx)) {
if (Preferences.isConnectionAllowed(ctx)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
scheduleNodeStart(ctx)
} else {
ctx.startService(Intent(ctx, BitmessageService::class.java))
updateNodeSwitch()
}
} else if (ask) {
val dialogIntent = Intent(ctx, FullNodeDialogActivity::class.java)
if (ctx !is Activity) {
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
ctx.sendBroadcast(Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS))
}
ctx.startActivity(dialogIntent)
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
scheduleNodeStart(ctx)
}
} else {
ctx.startService(Intent(ctx, BitmessageService::class.java))
updateNodeSwitch()
}
}
@JvmStatic
fun disableNode(ctx: Context) {
Preferences.setFullNodeActive(ctx, false)
ctx.stopService(Intent(ctx, BitmessageService::class.java))
}
@JvmStatic
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
fun scheduleNodeStart(ctx: Context) {
val serviceComponent = ComponentName(ctx, StartupNodeOnWifiService::class.java)
val builder = JobInfo.Builder(0, serviceComponent)
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
val jobScheduler = ctx.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
jobScheduler.schedule(builder.build());
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_pre_api_21">false</bool>
<bool name="is_post_api_21">true</bool>
</resources>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_pre_api_21">true</bool>
<bool name="is_post_api_21">false</bool>
</resources>

View File

@ -5,7 +5,7 @@ configurations.all {
}
buildscript {
ext.kotlin_version = '1.1.3-2'
ext.kotlin_version = '1.1.4-2'
ext.anko_version = '0.10.1'
repositories {
jcenter()