Remember connection state (so you don't have to start a full node whenever you lost your WiFi connection)
This commit is contained in:
		| @@ -65,14 +65,6 @@ public class AddressDetailFragment extends Fragment { | |||||||
|      */ |      */ | ||||||
|     private BitmessageAddress item; |     private BitmessageAddress item; | ||||||
|  |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * Mandatory empty constructor for the fragment manager to instantiate the |  | ||||||
|      * fragment (e.g. upon screen orientation changes). |  | ||||||
|      */ |  | ||||||
|     public AddressDetailFragment() { |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public void onCreate(Bundle savedInstanceState) { |     public void onCreate(Bundle savedInstanceState) { | ||||||
|         super.onCreate(savedInstanceState); |         super.onCreate(savedInstanceState); | ||||||
|   | |||||||
| @@ -259,6 +259,7 @@ public class MainActivity extends AppCompatActivity | |||||||
|                 @Override |                 @Override | ||||||
|                 public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView, |                 public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView, | ||||||
|                                              boolean isChecked) { |                                              boolean isChecked) { | ||||||
|  |                     Preferences.setFullNodeActive(MainActivity.this, isChecked); | ||||||
|                     if (isChecked) { |                     if (isChecked) { | ||||||
|                         checkAndStartNode(); |                         checkAndStartNode(); | ||||||
|                     } else { |                     } else { | ||||||
| @@ -386,6 +387,9 @@ public class MainActivity extends AppCompatActivity | |||||||
|     @Override |     @Override | ||||||
|     protected void onResume() { |     protected void onResume() { | ||||||
|         updateUnread(); |         updateUnread(); | ||||||
|  |         if (Preferences.isFullNodeActive(this)){ | ||||||
|  |             checkAndStartNode(); | ||||||
|  |         } | ||||||
|         updateNodeSwitch(); |         updateNodeSwitch(); | ||||||
|         Singleton.getMessageListener(this).resetNotification(); |         Singleton.getMessageListener(this).resetNotification(); | ||||||
|         super.onResume(); |         super.onResume(); | ||||||
|   | |||||||
| @@ -17,14 +17,11 @@ | |||||||
| package ch.dissem.apps.abit.dialog; | package ch.dissem.apps.abit.dialog; | ||||||
|  |  | ||||||
| import android.app.Activity; | import android.app.Activity; | ||||||
| import android.content.Intent; |  | ||||||
| import android.os.Bundle; | import android.os.Bundle; | ||||||
| import android.view.View; | import android.view.View; | ||||||
|  |  | ||||||
| import ch.dissem.apps.abit.R; | import ch.dissem.apps.abit.R; | ||||||
| import ch.dissem.apps.abit.service.BitmessageService; | import ch.dissem.apps.abit.util.Preferences; | ||||||
|  |  | ||||||
| import static ch.dissem.apps.abit.MainActivity.updateNodeSwitch; |  | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * @author Christian Basler |  * @author Christian Basler | ||||||
| @@ -38,8 +35,7 @@ public class FullNodeDialogActivity extends Activity { | |||||||
|         findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() { |         findViewById(R.id.ok).setOnClickListener(new View.OnClickListener() { | ||||||
|             @Override |             @Override | ||||||
|             public void onClick(View view) { |             public void onClick(View view) { | ||||||
|                 startService(new Intent(FullNodeDialogActivity.this, BitmessageService.class)); |                 Preferences.setWifiOnly(FullNodeDialogActivity.this, true); | ||||||
|                 updateNodeSwitch(); |  | ||||||
|                 finish(); |                 finish(); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|   | |||||||
| @@ -22,6 +22,7 @@ import android.content.Intent; | |||||||
| import android.net.ConnectivityManager; | import android.net.ConnectivityManager; | ||||||
| import android.net.NetworkInfo; | import android.net.NetworkInfo; | ||||||
|  |  | ||||||
|  | import ch.dissem.apps.abit.service.BitmessageService; | ||||||
| import ch.dissem.apps.abit.service.Singleton; | import ch.dissem.apps.abit.service.Singleton; | ||||||
| import ch.dissem.apps.abit.util.Preferences; | import ch.dissem.apps.abit.util.Preferences; | ||||||
| import ch.dissem.bitmessage.BitmessageContext; | import ch.dissem.bitmessage.BitmessageContext; | ||||||
| @@ -30,12 +31,12 @@ public class WifiReceiver extends BroadcastReceiver { | |||||||
|     @Override |     @Override | ||||||
|     public void onReceive(Context ctx, Intent intent) { |     public void onReceive(Context ctx, Intent intent) { | ||||||
|         if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { |         if ("android.net.conn.CONNECTIVITY_CHANGE".equals(intent.getAction())) { | ||||||
|             if (Preferences.isWifiOnly(ctx)) { |  | ||||||
|             BitmessageContext bmc = Singleton.getBitmessageContext(ctx); |             BitmessageContext bmc = Singleton.getBitmessageContext(ctx); | ||||||
|  |             if (Preferences.isWifiOnly(ctx) && isConnectedToMeteredNetwork(ctx) && bmc.isRunning()) { | ||||||
|                 if (isConnectedToMeteredNetwork(ctx) && bmc.isRunning()) { |  | ||||||
|                 bmc.shutdown(); |                 bmc.shutdown(); | ||||||
|             } |             } | ||||||
|  |             if (!bmc.isRunning() && !(Preferences.isWifiOnly(ctx) && isConnectedToMeteredNetwork(ctx))) { | ||||||
|  |                 ctx.startService(new Intent(ctx, BitmessageService.class)); | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -32,7 +32,6 @@ import ch.dissem.apps.abit.service.BitmessageService; | |||||||
| import ch.dissem.bitmessage.utils.Property; | import ch.dissem.bitmessage.utils.Property; | ||||||
|  |  | ||||||
| import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; | 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) |  * 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"); |         Property connections = BitmessageService.getStatus().getProperty("network", "connections"); | ||||||
|         if (!running) { |         if (!running) { | ||||||
|             builder.setContentText(ctx.getString(R.string.connection_info_disconnected)); |             builder.setContentText(ctx.getString(R.string.connection_info_disconnected)); | ||||||
|             updateNodeSwitch(); |  | ||||||
|         } else if (connections.getProperties().length == 0) { |         } else if (connections.getProperties().length == 0) { | ||||||
|             builder.setContentText(ctx.getString(R.string.connection_info_pending)); |             builder.setContentText(ctx.getString(R.string.connection_info_pending)); | ||||||
|         } else { |         } else { | ||||||
|   | |||||||
| @@ -57,6 +57,7 @@ public class BitmessageIntentService extends IntentService { | |||||||
|         } |         } | ||||||
|         if (intent.hasExtra(EXTRA_STARTUP_NODE)) { |         if (intent.hasExtra(EXTRA_STARTUP_NODE)) { | ||||||
|             if (Preferences.isConnectionAllowed(this)) { |             if (Preferences.isConnectionAllowed(this)) { | ||||||
|  |                 Preferences.setFullNodeActive(this, true); | ||||||
|                 startService(new Intent(this, BitmessageService.class)); |                 startService(new Intent(this, BitmessageService.class)); | ||||||
|                 updateNodeSwitch(); |                 updateNodeSwitch(); | ||||||
|             } else { |             } else { | ||||||
| @@ -67,6 +68,7 @@ public class BitmessageIntentService extends IntentService { | |||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         if (intent.hasExtra(EXTRA_SHUTDOWN_NODE)) { |         if (intent.hasExtra(EXTRA_SHUTDOWN_NODE)) { | ||||||
|  |             Preferences.setFullNodeActive(this, false); | ||||||
|             stopService(new Intent(this, BitmessageService.class)); |             stopService(new Intent(this, BitmessageService.class)); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -71,11 +71,11 @@ public class SyncAdapter extends AbstractThreadedSyncAdapter { | |||||||
|     public void onPerformSync(Account account, Bundle extras, String authority, |     public void onPerformSync(Account account, Bundle extras, String authority, | ||||||
|                               ContentProviderClient provider, SyncResult syncResult) { |                               ContentProviderClient provider, SyncResult syncResult) { | ||||||
|         try { |         try { | ||||||
|             if (account.equals(Authenticator.ACCOUNT_SYNC)) { |             if (account.equals(ACCOUNT_SYNC)) { | ||||||
|                 if (Preferences.isConnectionAllowed(getContext())) { |                 if (Preferences.isConnectionAllowed(getContext())) { | ||||||
|                     syncData(); |                     syncData(); | ||||||
|                 } |                 } | ||||||
|             } else if (account.equals(Authenticator.ACCOUNT_POW)) { |             } else if (account.equals(ACCOUNT_POW)) { | ||||||
|                 syncPOW(); |                 syncPOW(); | ||||||
|             } else { |             } else { | ||||||
|                 syncResult.stats.numAuthExceptions++; |                 syncResult.stats.numAuthExceptions++; | ||||||
|   | |||||||
| @@ -26,6 +26,7 @@ public class Constants { | |||||||
|     public static final String PREFERENCE_TRUSTED_NODE = "trusted_node"; |     public static final String PREFERENCE_TRUSTED_NODE = "trusted_node"; | ||||||
|     public static final String PREFERENCE_SYNC_TIMEOUT = "sync_timeout"; |     public static final String PREFERENCE_SYNC_TIMEOUT = "sync_timeout"; | ||||||
|     public static final String PREFERENCE_SERVER_POW = "server_pow"; |     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 String BITMESSAGE_URL_SCHEMA = "bitmessage:"; | ||||||
|     public static final Pattern BITMESSAGE_ADDRESS_PATTERN = Pattern.compile("\\bBM-[a-zA-Z0-9]+\\b"); |     public static final Pattern BITMESSAGE_ADDRESS_PATTERN = Pattern.compile("\\bBM-[a-zA-Z0-9]+\\b"); | ||||||
|   | |||||||
| @@ -27,6 +27,7 @@ import ch.dissem.apps.abit.R; | |||||||
| import ch.dissem.apps.abit.listener.WifiReceiver; | import ch.dissem.apps.abit.listener.WifiReceiver; | ||||||
| import ch.dissem.apps.abit.notification.ErrorNotification; | 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_SYNC_TIMEOUT; | ||||||
| import static ch.dissem.apps.abit.util.Constants.PREFERENCE_TRUSTED_NODE; | import static ch.dissem.apps.abit.util.Constants.PREFERENCE_TRUSTED_NODE; | ||||||
| import static ch.dissem.apps.abit.util.Constants.PREFERENCE_WIFI_ONLY; | import static ch.dissem.apps.abit.util.Constants.PREFERENCE_WIFI_ONLY; | ||||||
| @@ -95,4 +96,19 @@ public class Preferences { | |||||||
|         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx); |         SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(ctx); | ||||||
|         return preferences.getBoolean(PREFERENCE_WIFI_ONLY, true); |         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(); | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user