diff --git a/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.java b/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.java index 704726d..dae54bb 100644 --- a/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.java +++ b/app/src/main/java/ch/dissem/apps/abit/AddressDetailFragment.java @@ -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); diff --git a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java index 590e983..5970b60 100644 --- a/app/src/main/java/ch/dissem/apps/abit/MainActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/MainActivity.java @@ -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(); diff --git a/app/src/main/java/ch/dissem/apps/abit/dialog/FullNodeDialogActivity.java b/app/src/main/java/ch/dissem/apps/abit/dialog/FullNodeDialogActivity.java index a0f48f5..015dbfd 100644 --- a/app/src/main/java/ch/dissem/apps/abit/dialog/FullNodeDialogActivity.java +++ b/app/src/main/java/ch/dissem/apps/abit/dialog/FullNodeDialogActivity.java @@ -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(); } }); diff --git a/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java b/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java index 329d903..898be32 100644 --- a/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java +++ b/app/src/main/java/ch/dissem/apps/abit/listener/WifiReceiver.java @@ -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)); } } } diff --git a/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java b/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java index 4a11cc2..2bce79b 100644 --- a/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java +++ b/app/src/main/java/ch/dissem/apps/abit/notification/NetworkNotification.java @@ -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 { diff --git a/app/src/main/java/ch/dissem/apps/abit/service/BitmessageIntentService.java b/app/src/main/java/ch/dissem/apps/abit/service/BitmessageIntentService.java index 84a5ed8..ad17d9d 100644 --- a/app/src/main/java/ch/dissem/apps/abit/service/BitmessageIntentService.java +++ b/app/src/main/java/ch/dissem/apps/abit/service/BitmessageIntentService.java @@ -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)); } } diff --git a/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java b/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java index dd8f984..770adc0 100644 --- a/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java +++ b/app/src/main/java/ch/dissem/apps/abit/synchronization/SyncAdapter.java @@ -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++; diff --git a/app/src/main/java/ch/dissem/apps/abit/util/Constants.java b/app/src/main/java/ch/dissem/apps/abit/util/Constants.java index bd1817d..66fa847 100644 --- a/app/src/main/java/ch/dissem/apps/abit/util/Constants.java +++ b/app/src/main/java/ch/dissem/apps/abit/util/Constants.java @@ -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"); diff --git a/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java b/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java index e5d52ba..ebb2dfb 100644 --- a/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java +++ b/app/src/main/java/ch/dissem/apps/abit/util/Preferences.java @@ -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(); + } }