Skip to content

Commit 959eee5

Browse files
committed
* Added a message when network is not available before scanning
* Using the latest androidx preferences * SettingsActivity design changes * Refactoring
1 parent d2f6f6a commit 959eee5

File tree

17 files changed

+226
-470
lines changed

17 files changed

+226
-470
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,18 @@
1212
<application
1313
android:name=".domain.object.Runner"
1414
android:allowBackup="true"
15+
android:fullBackupContent="@xml/backup_descriptor"
1516
android:hardwareAccelerated="true"
1617
android:icon="@mipmap/ic_launcher"
1718
android:label="@string/app_name"
1819
android:largeHeap="true"
1920
android:roundIcon="@mipmap/ic_launcher_round"
2021
android:supportsRtl="true"
2122
android:theme="@style/AppTheme"
22-
tools:ignore="GoogleAppIndexingWarning"
23-
android:fullBackupContent="@xml/backup_descriptor">
23+
tools:ignore="GoogleAppIndexingWarning">
24+
<activity
25+
android:name=".gui.activity.SettingsActivity"
26+
android:label="@string/title_activity_settings"></activity>
2427
<activity
2528
android:name=".gui.activity.LoadingActivity"
2629
android:configChanges="orientation|keyboardHidden|screenSize"
@@ -34,14 +37,11 @@
3437
</activity>
3538
<activity
3639
android:name=".gui.activity.ScanActivity"
37-
android:launchMode="singleInstance"
38-
android:label="@string/app_name" />
40+
android:label="@string/app_name"
41+
android:launchMode="singleInstance" />
3942
<activity
4043
android:name=".gui.activity.AboutActivity"
4144
android:label="@string/title_activity_about" />
42-
<activity
43-
android:name=".gui.activity.SettingsActivity"
44-
android:label="@string/title_activity_settings" />
4545
</application>
4646

4747
</manifest>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.codedead.advancedportchecker.domain.object;
2+
3+
import android.content.Context;
4+
import android.net.ConnectivityManager;
5+
import android.net.NetworkCapabilities;
6+
import android.net.NetworkInfo;
7+
import android.net.wifi.WifiManager;
8+
import android.os.Build;
9+
10+
public final class NetworkUtils {
11+
12+
private WifiManager wifiManager;
13+
private Context context;
14+
15+
/**
16+
* Initialize a new NetworkUtils
17+
* @param context The context that can be used to access device information
18+
*/
19+
public NetworkUtils(Context context) {
20+
if (context == null) throw new NullPointerException("Context cannot be null!");
21+
22+
this.context = context;
23+
wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
24+
}
25+
26+
/**
27+
* Check whether a network connection is available
28+
* @return True if a network connection is available, otherwise false
29+
*/
30+
public boolean hasNetworkConnection() {
31+
final ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
32+
33+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
34+
if (cm != null) {
35+
final NetworkCapabilities capabilities = cm.getNetworkCapabilities(cm.getActiveNetwork());
36+
if (capabilities != null) {
37+
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) || capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR);
38+
}
39+
}
40+
} else {
41+
if (cm != null) {
42+
final NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
43+
if (activeNetwork != null) {
44+
return activeNetwork.getType() == ConnectivityManager.TYPE_WIFI || activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE;
45+
}
46+
}
47+
}
48+
return false;
49+
}
50+
51+
/**
52+
* Check whether Wifi is enabled or not
53+
* @return True if Wifi is enabled, otherwise false
54+
*/
55+
public boolean isWifiEnabled() {
56+
return wifiManager.isWifiEnabled();
57+
}
58+
}

app/src/main/java/com/codedead/advancedportchecker/gui/activity/AppCompatPreferenceActivity.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

app/src/main/java/com/codedead/advancedportchecker/gui/activity/LoadingActivity.java

Lines changed: 10 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@
77
import android.content.SharedPreferences;
88
import android.content.pm.PackageManager;
99
import android.content.res.Configuration;
10-
import android.net.ConnectivityManager;
11-
import android.net.NetworkCapabilities;
12-
import android.net.NetworkInfo;
1310
import android.net.Uri;
14-
import android.net.wifi.WifiManager;
15-
import android.os.Build;
1611
import android.os.CountDownTimer;
1712

1813
import androidx.preference.PreferenceManager;
@@ -32,6 +27,7 @@
3227
import com.codedead.advancedportchecker.R;
3328
import com.codedead.advancedportchecker.domain.controller.LocaleHelper;
3429
import com.codedead.advancedportchecker.domain.controller.UtilController;
30+
import com.codedead.advancedportchecker.domain.object.NetworkUtils;
3531

3632
import 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

Comments
 (0)