1515import android .hardware .usb .UsbDevice ;
1616import android .hardware .usb .UsbManager ;
1717import android .net .ConnectivityManager ;
18- import android .net .Network ;
19- import android .net .NetworkCapabilities ;
20- import android .net .NetworkRequest ;
2118import android .net .Uri ;
2219import android .os .Bundle ;
2320import android .os .Handler ;
@@ -62,47 +59,6 @@ public class MainActivity extends AppCompatActivity {
6259
6360 private BitBoxWebChromeClient webChrome ;
6461
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-
10662 // Connection to bind with GoService
10763 private final ServiceConnection connection = new ServiceConnection () {
10864
@@ -129,14 +85,6 @@ public void onReceive(Context context, Intent intent) {
12985 }
13086 };
13187
132- private final BroadcastReceiver networkStateReceiver = new BroadcastReceiver () {
133- @ Override
134- public void onReceive (Context context , Intent intent ) {
135- Mobileserver .usingMobileDataChanged ();
136- }
137- };
138-
139-
14088 @ Override
14189 public void onConfigurationChanged (Configuration newConfig ) {
14290 int currentNightMode = newConfig .uiMode & Configuration .UI_MODE_NIGHT_MASK ;
@@ -248,20 +196,7 @@ protected void onCreate(Bundle savedInstanceState) {
248196 // In that case, handleIntent() is not called with ACTION_USB_DEVICE_ATTACHED.
249197 this .updateDevice ();
250198
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- };
199+ goViewModel .setNetworkHelper (new NetworkHelper ((ConnectivityManager ) getSystemService (Context .CONNECTIVITY_SERVICE )));
265200
266201 getOnBackPressedDispatcher ().addCallback (this , new OnBackPressedCallback (true ) {
267202 @ Override
@@ -337,7 +272,7 @@ private void startServer() {
337272 goService .startServer (getApplicationContext ().getFilesDir ().getAbsolutePath (), gVM .getGoEnvironment (), gVM .getGoAPI ());
338273
339274 // Trigger connectivity check (as the network may already be unavailable when the app starts).
340- checkConnectivity ();
275+ gVM . getNetworkHelper (). checkConnectivity ();
341276 }
342277
343278 @ Override
@@ -355,15 +290,7 @@ protected void onStart() {
355290 Util .log ("lifecycle: onStart" );
356291 final GoViewModel goViewModel = ViewModelProviders .of (this ).get (GoViewModel .class );
357292 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 );
293+ goViewModel .getNetworkHelper ().registerNetworkCallback ();
367294 }
368295
369296 @ Override
@@ -389,11 +316,9 @@ protected void onResume() {
389316 ContextCompat .RECEIVER_NOT_EXPORTED
390317 );
391318
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-
395319 // Trigger connectivity check (as the network may already be unavailable when the app starts).
396- checkConnectivity ();
320+ final GoViewModel goViewModel = ViewModelProviders .of (this ).get (GoViewModel .class );
321+ goViewModel .getNetworkHelper ().checkConnectivity ();
397322
398323 Intent intent = getIntent ();
399324 handleIntent (intent );
@@ -404,7 +329,6 @@ protected void onPause() {
404329 super .onPause ();
405330 Util .log ("lifecycle: onPause" );
406331 unregisterReceiver (this .usbStateReceiver );
407- unregisterReceiver (this .networkStateReceiver );
408332 }
409333
410334 private void handleIntent (Intent intent ) {
@@ -472,9 +396,8 @@ private void updateDevice() {
472396 @ Override
473397 protected void onStop () {
474398 super .onStop ();
475- if (connectivityManager != null && networkCallback != null ) {
476- connectivityManager .unregisterNetworkCallback (networkCallback );
477- }
399+ final GoViewModel goViewModel = ViewModelProviders .of (this ).get (GoViewModel .class );
400+ goViewModel .getNetworkHelper ().unregisterNetworkCallback ();
478401 Util .log ("lifecycle: onStop" );
479402 }
480403
0 commit comments