1414import android .content .res .Configuration ;
1515import android .hardware .usb .UsbDevice ;
1616import android .hardware .usb .UsbManager ;
17- import android .net .ConnectivityManager ;
18- import android .net .Network ;
19- import android .net .NetworkCapabilities ;
20- import android .net .NetworkRequest ;
2117import android .net .Uri ;
2218import android .os .Bundle ;
2319import android .os .Handler ;
@@ -62,47 +58,6 @@ public class MainActivity extends AppCompatActivity {
6258
6359 private BitBoxWebChromeClient webChrome ;
6460
65- private ConnectivityManager connectivityManager ;
66- private ConnectivityManager .NetworkCallback networkCallback ;
67-
68- private boolean hasInternetConnectivity (NetworkCapabilities capabilities ) {
69- // To avoid false positives, if we can't obtain connectivity info,
70- // we return true.
71- // Note: this should never happen per Android documentation, as:
72- // - these can not be null it come from the onCapabilitiesChanged callback.
73- // - when obtained with getNetworkCapabilities(network), they can only be null if the
74- // network is null or unknown, but we guard against both in the caller.
75- if (capabilities == null ) {
76- Util .log ("Got null capabilities when we shouldn't have. Assuming we are online." );
77- return true ;
78- }
79-
80-
81- boolean hasInternet = capabilities .hasCapability (NetworkCapabilities .NET_CAPABILITY_INTERNET );
82-
83- // We need to check for both internet and validated, since validated reports that the system
84- // found connectivity the last time it checked. But if this callback triggers when going offline
85- // (e.g. airplane mode), this bit would still be true when we execute this method.
86- boolean isValidated = capabilities .hasCapability (NetworkCapabilities .NET_CAPABILITY_VALIDATED );
87- return hasInternet && isValidated ;
88-
89- // Fallback for older devices
90- }
91-
92- private void checkConnectivity () {
93- Network activeNetwork = connectivityManager .getActiveNetwork ();
94-
95- // If there is no active network (e.g. airplane mode), there is no check to perform.
96- if (activeNetwork == null ) {
97- Mobileserver .setOnline (false );
98- return ;
99- }
100-
101- NetworkCapabilities capabilities = connectivityManager .getNetworkCapabilities (activeNetwork );
102-
103- Mobileserver .setOnline (hasInternetConnectivity (capabilities ));
104- }
105-
10661 // Connection to bind with GoService
10762 private final ServiceConnection connection = new ServiceConnection () {
10863
@@ -129,14 +84,6 @@ public void onReceive(Context context, Intent intent) {
12984 }
13085 };
13186
132- private final BroadcastReceiver networkStateReceiver = new BroadcastReceiver () {
133- @ Override
134- public void onReceive (Context context , Intent intent ) {
135- Mobileserver .usingMobileDataChanged ();
136- }
137- };
138-
139-
14087 @ Override
14188 public void onConfigurationChanged (Configuration newConfig ) {
14289 int currentNightMode = newConfig .uiMode & Configuration .UI_MODE_NIGHT_MASK ;
@@ -248,21 +195,6 @@ protected void onCreate(Bundle savedInstanceState) {
248195 // In that case, handleIntent() is not called with ACTION_USB_DEVICE_ATTACHED.
249196 this .updateDevice ();
250197
251- connectivityManager = (ConnectivityManager ) getSystemService (Context .CONNECTIVITY_SERVICE );
252- networkCallback = new ConnectivityManager .NetworkCallback () {
253- @ Override
254- public void onCapabilitiesChanged (@ NonNull android .net .Network network , @ NonNull android .net .NetworkCapabilities capabilities ) {
255- super .onCapabilitiesChanged (network , capabilities );
256- Mobileserver .setOnline (hasInternetConnectivity (capabilities ));
257- }
258- // When we lose the network, onCapabilitiesChanged does not trigger, so we need to override onLost.
259- @ Override
260- public void onLost (@ NonNull Network network ) {
261- super .onLost (network );
262- Mobileserver .setOnline (false );
263- }
264- };
265-
266198 getOnBackPressedDispatcher ().addCallback (this , new OnBackPressedCallback (true ) {
267199 @ Override
268200 public void handleOnBackPressed () {
@@ -336,8 +268,8 @@ private void startServer() {
336268 final GoViewModel gVM = ViewModelProviders .of (this ).get (GoViewModel .class );
337269 goService .startServer (getApplicationContext ().getFilesDir ().getAbsolutePath (), gVM .getGoEnvironment (), gVM .getGoAPI ());
338270
339- // Trigger connectivity check (as the network may already be unavailable when the app starts).
340- checkConnectivity ();
271+ // Trigger connectivity and mobile connection check (as the network may already be unavailable when the app starts).
272+ gVM . getNetworkHelper (). checkConnectivity ();
341273 }
342274
343275 @ Override
@@ -355,15 +287,7 @@ protected void onStart() {
355287 Util .log ("lifecycle: onStart" );
356288 final GoViewModel goViewModel = ViewModelProviders .of (this ).get (GoViewModel .class );
357289 goViewModel .getIsDarkTheme ().observe (this , this ::setDarkTheme );
358-
359-
360- NetworkRequest request = new NetworkRequest .Builder ()
361- .addCapability (NetworkCapabilities .NET_CAPABILITY_INTERNET )
362- .addTransportType (NetworkCapabilities .TRANSPORT_WIFI )
363- .addTransportType (NetworkCapabilities .TRANSPORT_CELLULAR )
364- .build ();
365- // Register the network callback to listen for changes in network capabilities.
366- connectivityManager .registerNetworkCallback (request , networkCallback );
290+ goViewModel .getNetworkHelper ().registerNetworkCallback ();
367291 }
368292
369293 @ Override
@@ -389,11 +313,9 @@ protected void onResume() {
389313 ContextCompat .RECEIVER_NOT_EXPORTED
390314 );
391315
392- // Listen on changes in the network connection. We are interested in if the user is connected to a mobile data connection.
393- registerReceiver (this .networkStateReceiver , new IntentFilter (ConnectivityManager .CONNECTIVITY_ACTION ));
394-
395316 // Trigger connectivity check (as the network may already be unavailable when the app starts).
396- checkConnectivity ();
317+ final GoViewModel goViewModel = ViewModelProviders .of (this ).get (GoViewModel .class );
318+ goViewModel .getNetworkHelper ().checkConnectivity ();
397319
398320 Intent intent = getIntent ();
399321 handleIntent (intent );
@@ -404,7 +326,6 @@ protected void onPause() {
404326 super .onPause ();
405327 Util .log ("lifecycle: onPause" );
406328 unregisterReceiver (this .usbStateReceiver );
407- unregisterReceiver (this .networkStateReceiver );
408329 }
409330
410331 private void handleIntent (Intent intent ) {
@@ -472,9 +393,8 @@ private void updateDevice() {
472393 @ Override
473394 protected void onStop () {
474395 super .onStop ();
475- if (connectivityManager != null && networkCallback != null ) {
476- connectivityManager .unregisterNetworkCallback (networkCallback );
477- }
396+ final GoViewModel goViewModel = ViewModelProviders .of (this ).get (GoViewModel .class );
397+ goViewModel .getNetworkHelper ().unregisterNetworkCallback ();
478398 Util .log ("lifecycle: onStop" );
479399 }
480400
0 commit comments