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

View File

@ -102,10 +102,20 @@ public class WifiHotspotManager {
public void turnOffHotspot() { public void turnOffHotspot() {
if (hotspotReservation != null) { if (hotspotReservation != null) {
hotspotReservation.close(); hotspotReservation.close();
hotspotReservation = null;
oreoenabled = false; 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 // This method returns the current state of the Wifi access point
public WIFI_AP_STATE_ENUMS getWifiApState() { public WIFI_AP_STATE_ENUMS getWifiApState() {
try { try {