Integrate turn off hotspot feature with UI for devices>=O

Added method checkHotspotState()
It checks the state of the hostpot for devices>=Oreo
This commit is contained in:
Adeel Zafar 2019-06-01 19:13:47 +05:00
parent 7807927562
commit 5f5f6709fb
2 changed files with 35 additions and 11 deletions

View File

@ -948,17 +948,23 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
enableDisableMobileData();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
// // == PackageManager.PERMISSION_GRANTED) {
// // wifiHotspotManager.turnOnHotspot();
// //} else {
// // //Show rationale and request permission.
// // //No explanation needed; request the permission
// // ActivityCompat.requestPermissions(this,
// // new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
// // MY_PERMISSIONS_ACCESS_FINE_LOCATION);
// //}
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (wifiHotspotManager.checkHotspotState()) //If hotspot is already enabled, turn it off
{
wifiHotspotManager.turnOffHotspot();
} else //If hotspot is not already enabled, then turn it on.
{
setupLocationServices();
}
} else {
//Show rationale and request permission.
//No explanation needed; request the permission
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.ACCESS_FINE_LOCATION },
MY_PERMISSIONS_ACCESS_FINE_LOCATION);
}
} else {
if (wifiHotspotManager.isWifiApEnabled()) {
wifiHotspotManager.setWifiEnabled(null, false);
@ -2219,6 +2225,10 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
task = LocationServices.getSettingsClient(this).checkLocationSettings(builder.build());
locationSettingsResponseBuilder();
}
private void locationSettingsResponseBuilder() {
task.addOnCompleteListener(new OnCompleteListener<LocationSettingsResponse>() {
@Override
public void onComplete(Task<LocationSettingsResponse> task) {
@ -2226,6 +2236,7 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
LocationSettingsResponse response = task.getResult(ApiException.class);
// All location settings are satisfied. The client can initialize location
// requests here.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
wifiHotspotManager.turnOnHotspot();
}
@ -2267,6 +2278,9 @@ public class MainActivity extends BaseActivity implements WebViewCallback,
// case Activity.RESULT_OK:
// // All required changes were successfully made
// Toast.makeText(MainActivity.this,states.isLocationPresent()+"",Toast.LENGTH_SHORT).show();
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// wifiHotspotManager.turnOnHotspot();
// }
// break;
// case Activity.RESULT_CANCELED:
// // The user was asked to change settings, but chose not to

View File

@ -102,10 +102,20 @@ public class WifiHotspotManager {
public void turnOffHotspot() {
if (hotspotReservation != null) {
hotspotReservation.close();
hotspotReservation = null;
oreoenabled = false;
}
}
//This method checks the state of the hostpot for devices>=Oreo
public boolean checkHotspotState() {
if (hotspotReservation != null) {
return true;
} else {
return false;
}
}
// This method returns the current state of the Wifi access point
public WIFI_AP_STATE_ENUMS getWifiApState() {
try {