77import android .content .SharedPreferences ;
88import android .content .pm .PackageManager ;
99import android .content .res .Configuration ;
10- import android .net .ConnectivityManager ;
11- import android .net .NetworkCapabilities ;
12- import android .net .NetworkInfo ;
1310import android .net .Uri ;
14- import android .net .wifi .WifiManager ;
15- import android .os .Build ;
1611import android .os .CountDownTimer ;
1712
1813import androidx .preference .PreferenceManager ;
3227import com .codedead .advancedportchecker .R ;
3328import com .codedead .advancedportchecker .domain .controller .LocaleHelper ;
3429import com .codedead .advancedportchecker .domain .controller .UtilController ;
30+ import com .codedead .advancedportchecker .domain .object .NetworkUtils ;
3531
3632import static android .content .pm .PackageManager .GET_META_DATA ;
3733
@@ -40,7 +36,7 @@ public final class LoadingActivity extends AppCompatActivity {
4036 private static final int ACTIVITY_SETTINGS_CODE = 1337 ;
4137 private static final int ACTIVITY_WIFI_CODE = 443 ;
4238
43- private WifiManager wifi ;
39+ private NetworkUtils networkUtils ;
4440 private static boolean hasStopped ;
4541
4642 @ Override
@@ -56,6 +52,7 @@ protected void onCreate(Bundle savedInstanceState) {
5652 decorView .setSystemUiVisibility (uiOptions );
5753
5854 setContentView (R .layout .activity_loading );
55+ networkUtils = new NetworkUtils (this );
5956
6057 checkPermissions ();
6158 }
@@ -193,54 +190,17 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
193190 super .onActivityResult (requestCode , resultCode , data );
194191 }
195192
196- /**
197- * Check whether an internet connection is available
198- *
199- * @return True if an internet connection is available, otherwise false
200- */
201- private boolean hasInternet () {
202- final ConnectivityManager cm = (ConnectivityManager ) getApplicationContext ().getSystemService (Context .CONNECTIVITY_SERVICE );
203-
204- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
205- if (cm != null ) {
206- final NetworkCapabilities capabilities = cm .getNetworkCapabilities (cm .getActiveNetwork ());
207- if (capabilities != null ) {
208- return capabilities .hasTransport (NetworkCapabilities .TRANSPORT_WIFI ) || capabilities .hasTransport (NetworkCapabilities .TRANSPORT_CELLULAR );
209- }
210- }
211- } else {
212- if (cm != null ) {
213- final NetworkInfo activeNetwork = cm .getActiveNetworkInfo ();
214- if (activeNetwork != null ) {
215- return activeNetwork .getType () == ConnectivityManager .TYPE_WIFI || activeNetwork .getType () == ConnectivityManager .TYPE_MOBILE ;
216- }
217- }
218- }
219- return false ;
220- }
221-
222- /**
223- * Check if Wifi is enabled. If Wifi is not enabled, request the user to enable Wifi
224- */
225- private void checkWifiState () {
226- if (!wifi .isWifiEnabled ()) {
227- wifiConfirmationCheck ();
228- } else {
229- delayedWifiCheck ();
230- }
231- }
232-
233193 /**
234194 * Check whether an internet connection is available
235195 */
236196 private void checkConnectivity () {
237- // Initialize WifiManager
238- wifi = (WifiManager ) getApplicationContext ().getSystemService (Context .WIFI_SERVICE );
239-
240197 // Check if an internet connection is available
241- if (!hasInternet ()) {
242- // Check if the wifi module on the device is enabled or not
243- checkWifiState ();
198+ if (!networkUtils .hasNetworkConnection ()) {
199+ if (!networkUtils .isWifiEnabled ()) {
200+ wifiConfirmationCheck ();
201+ } else {
202+ delayedWifiCheck ();
203+ }
244204 } else {
245205 continueLoading ();
246206 }
@@ -284,7 +244,7 @@ public void onTick(long l) {
284244 public void onFinish () {
285245 // No need for this code if the app is already finishing
286246 if (isFinishing ()) return ;
287- if (hasInternet ()) {
247+ if (networkUtils . hasNetworkConnection ()) {
288248 // Load the next screen because an internet connection is available
289249 continueLoading ();
290250 } else {
0 commit comments