Remember connection state (so you don't have to start a full node whenever you lost your WiFi connection)

This commit is contained in:
Christian Basler 2017-05-07 13:39:30 +02:00
parent 5bc1bc2a47
commit 422c7ac803
9 changed files with 37 additions and 27 deletions

View File

@ -65,14 +65,6 @@ public class AddressDetailFragment extends Fragment {
*/
private BitmessageAddress item;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public AddressDetailFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

View File

@ -259,6 +259,7 @@ public class MainActivity extends AppCompatActivity
@Override
public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView,
boolean isChecked) {
Preferences.setFullNodeActive(MainActivity.this, isChecked);
if (isChecked) {
checkAndStartNode();
} else {
@ -386,6 +387,9 @@ public class MainActivity extends AppCompatActivity
@Override
protected void onResume() {
updateUnread();
if (Preferences.isFullNodeActive(this)){
checkAndStartNode();
}
updateNodeSwitch();
Singleton.getMessageListener(this).resetNotification();
super.onResume();

View File

@ -17,14 +17,11 @@
package ch.dissem.apps.abit.dialog;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.service.BitmessageService;
import static ch.dissem.apps.abit.MainActivity.updateNodeSwitch;
import ch.dissem.apps.abit.util.Preferences;
/**
* @author Christian Basler
@ -38,8 +35,7 @@ public class FullNodeDialogActivity extends Activity {
findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startService(new Intent(FullNodeDialogActivity.this, BitmessageService.class));
updateNodeSwitch();
Preferences.setWifiOnly(FullNodeDialogActivity.this, true);
finish();
}
});

View File

@ -22,6 +22,7 @@ import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import ch.dissem.apps.abit.service.BitmessageService;
import ch.dissem.apps.abit.service.Singleton;
import ch.dissem.apps.abit.util.Preferences;
import ch.dissem.bitmessage.BitmessageContext;
@ -30,12 +31,12 @@ public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context ctx, Intent intent) {
if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) {
if (Preferences.isWifiOnly(ctx)) {
BitmessageContext bmc = Singleton.getBitmessageContext(ctx);
if (isConnectedToMeteredNetwork(ctx) && bmc.isRunning()) {
bmc.shutdown();
}
BitmessageContext bmc = Singleton.getBitmessageContext(ctx);
if (Preferences.isWifiOnly(ctx) && isConnectedToMeteredNetwork(ctx) && bmc.isRunning()) {
bmc.shutdown();
}
if (!bmc.isRunning() && !(Preferences.isWifiOnly(ctx) && isConnectedToMeteredNetwork(ctx))) {
ctx.startService(new Intent(ctx, BitmessageService.class));
}
}
}

View File

@ -32,7 +32,6 @@ import ch.dissem.apps.abit.service.BitmessageService;
import ch.dissem.bitmessage.utils.Property;
import static android.app.PendingIntent.FLAG_UPDATE_CURRENT;
import static ch.dissem.apps.abit.MainActivity.updateNodeSwitch;
/**
* Shows the network status (as long as the client is connected as a full node)
@ -63,7 +62,6 @@ public class NetworkNotification extends AbstractNotification {
Property connections = BitmessageService.getStatus().getProperty("network", "connections");
if (!running) {
builder.setContentText(ctx.getString(R.string.connection_info_disconnected));
updateNodeSwitch();
} else if (connections.getProperties().length == 0) {
builder.setContentText(ctx.getString(R.string.connection_info_pending));
} else {

View File

@ -57,6 +57,7 @@ public class BitmessageIntentService extends IntentService {
}
if (intent.hasExtra(EXTRA_STARTUP_NODE)) {
if (Preferences.isConnectionAllowed(this)) {
Preferences.setFullNodeActive(this, true);
startService(new Intent(this, BitmessageService.class));
updateNodeSwitch();
} else {
@ -67,6 +68,7 @@ public class BitmessageIntentService extends IntentService {
}
}
if (intent.hasExtra(EXTRA_SHUTDOWN_NODE)) {
Preferences.setFullNodeActive(this, false);
stopService(new Intent(this, BitmessageService.class));
}
}

View File

@ -71,11 +71,11 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter {
public void onPerformSync(Account account, Bundle extras, String authority,
ContentProviderClient provider, SyncResult syncResult) {
try {
if (account.equals(Authenticator.ACCOUNT_SYNC)) {
if (account.equals(ACCOUNT_SYNC)) {
if (Preferences.isConnectionAllowed(getContext())) {
syncData();
}
} else if (account.equals(Authenticator.ACCOUNT_POW)) {
} else if (account.equals(ACCOUNT_POW)) {
syncPOW();
} else {
syncResult.stats.numAuthExceptions++;

View File

@ -26,6 +26,7 @@ public class Constants {
public static final String PREFERENCE_TRUSTED_NODE = "trusted_node";
public static final String PREFERENCE_SYNC_TIMEOUT = "sync_timeout";
public static final String PREFERENCE_SERVER_POW = "server_pow";
public static final String PREFERENCE_FULL_NODE = "full_node";
public static final String BITMESSAGE_URL_SCHEMA = "bitmessage:";
public static final Pattern BITMESSAGE_ADDRESS_PATTERN = Pattern.compile("\\bBM-[a-zA-Z0-9]+\\b");

View File

@ -27,6 +27,7 @@ import ch.dissem.apps.abit.R;
import ch.dissem.apps.abit.listener.WifiReceiver;
import ch.dissem.apps.abit.notification.ErrorNotification;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_FULL_NODE;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_SYNC_TIMEOUT;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_TRUSTED_NODE;
import static ch.dissem.apps.abit.util.Constants.PREFERENCE_WIFI_ONLY;
@ -54,7 +55,7 @@ public class Preferences {
int index = trustedNode.lastIndexOf(':');
trustedNode = trustedNode.substring(0, index);
}
return InetAddress.getByName(trustedNode);
return InetAddress.getByName(trustedNode);
}
public static int getTrustedNodePort(Context ctx) {
@ -69,8 +70,8 @@ public class Preferences {
return Integer.parseInt(portString);
} catch (NumberFormatException e) {
new ErrorNotification(ctx)
.setError(R.string.error_invalid_sync_port, portString)
.show();
.setError(R.string.error_invalid_sync_port, portString)
.show();
}
}
return 8444;
@ -95,4 +96,19 @@ public class Preferences {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
return preferences.getBoolean(PREFERENCE_WIFI_ONLY, true);
}
public static void setWifiOnly(Context ctx, boolean status) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
preferences.edit().putBoolean(PREFERENCE_WIFI_ONLY, status).apply();
}
public static boolean isFullNodeActive(Context ctx) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
return preferences.getBoolean(PREFERENCE_FULL_NODE, false);
}
public static void setFullNodeActive(Context ctx, boolean status) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx);
preferences.edit().putBoolean(PREFERENCE_FULL_NODE, status).apply();
}
}