Skip to content

Commit 281efe5

Browse files
Added LocationUtils class for checking Location availability
1 parent 621e461 commit 281efe5

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ protected void onCreate(Bundle savedInstanceState) {
2020
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 555);
2121
final Button button = findViewById(R.id.button);
2222
WifiUtils.enableLog(true);
23+
//TODO: CHECK IF LOCATION SERVICES ARE ON
2324
button.setOnClickListener(v -> connectWithWpa());
2425
}
2526

@@ -39,10 +40,16 @@ private void connectWithWpa() {
3940
.start();
4041
}
4142

43+
private void enableWifi() {
44+
WifiUtils.withContext(getApplicationContext()).enableWifi(this::checkResult);
45+
//or without the callback
46+
//WifiUtils.withContext(getApplicationContext()).enableWifi();
47+
}
48+
4249
private void checkResult(boolean isSuccess) {
4350
if (isSuccess)
44-
Toast.makeText(MainActivity.this, "CONNECTED YAY", Toast.LENGTH_SHORT).show();
51+
Toast.makeText(MainActivity.this, "SUCCESS!", Toast.LENGTH_SHORT).show();
4552
else
46-
Toast.makeText(MainActivity.this, "COULDN'T CONNECT", Toast.LENGTH_SHORT).show();
53+
Toast.makeText(MainActivity.this, "EPIC FAIL!", Toast.LENGTH_SHORT).show();
4754
}
4855
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.thanosfisherman.wifiutils;
2+
3+
import android.content.Context;
4+
import android.content.pm.PackageManager;
5+
import android.location.LocationManager;
6+
import android.os.Build;
7+
import android.support.annotation.NonNull;
8+
import android.util.Log;
9+
10+
import static com.thanosfisherman.elvis.Elvis.of;
11+
12+
public class LocationUtils {
13+
private static final String TAG = LocationUtils.class.getSimpleName();
14+
15+
public static final int GOOD_TO_GO = 1000;
16+
public static final int NO_LOCATION_AVAILABLE = 1111;
17+
public static final int LOCATION_DISABLED = 1112;
18+
19+
public static int checkLocationAvailability(@NonNull final Context context) {
20+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
21+
final PackageManager packMan = context.getPackageManager();
22+
if (packMan.hasSystemFeature(PackageManager.FEATURE_LOCATION)) {
23+
if (!isLocationEnabled(context)) {
24+
Log.d(TAG, "Location DISABLED");
25+
return LOCATION_DISABLED;
26+
}
27+
} else {
28+
Log.d(TAG, "NO GPS SENSOR");
29+
return NO_LOCATION_AVAILABLE;
30+
}
31+
}
32+
Log.d(TAG, "GPS GOOD TO GO");
33+
return GOOD_TO_GO;
34+
}
35+
36+
private static boolean isLocationEnabled(@NonNull Context context) {
37+
final LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
38+
return of(manager).next(locationManager -> locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)).getBoolean() ||
39+
of(manager).next(locationManager -> locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)).getBoolean();
40+
}
41+
}

0 commit comments

Comments
 (0)