Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
Attempt to fix location permission issue
Browse files Browse the repository at this point in the history
  • Loading branch information
velitasali committed Mar 13, 2018
1 parent fcaafda commit a0c96da
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public ArrayList<NetworkDevice> onLoad()
{
ArrayList<NetworkDevice> list = new ArrayList<>();

if (mFragment.getWifiManager().isWifiEnabled()) {
if (mFragment.getWifiManager().isWifiEnabled()
&& mFragment.hasLocationPermission()
&& mFragment.isLocationServiceEnabled()) {
for (ScanResult resultIndex : mFragment.getWifiManager().getScanResults()) {
if (!resultIndex.SSID.startsWith(AppConfig.PREFIX_ACCESS_POINT))
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public class NetworkDeviceListFragment
private HotspotUtils mHotspotUtils;
private WifiManager mWifiManager;
private ConnectivityManager mConnectivityManager;
private LocationManager mLocationManager;
private MenuItem mMenuItemShowQR;
private boolean mShowHotspotInfo = false;
private boolean mWirelessEnableRequested = false;
Expand All @@ -98,6 +99,7 @@ public void onCreate(Bundle savedInstanceState)
mHotspotUtils = HotspotUtils.getInstance(getContext());
mWifiManager = mHotspotUtils.getWifiManager();
mConnectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
mLocationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

mIntentFilter.addAction(DeviceScannerService.ACTION_SCAN_STARTED);
mIntentFilter.addAction(CommunicationService.ACTION_HOTSPOT_STATUS);
Expand Down Expand Up @@ -323,49 +325,6 @@ public void checkRefreshing()
.isScannerAvailable());
}

public boolean checkLocationPermission()
{
if (Build.VERSION.SDK_INT < 23)
return false;

if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
createSnackbar(R.string.mesg_locationPermissionRequiredSelfHotspot)
.setAction(R.string.butn_locationSettings, new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getActivity().requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION);
}
}
})
.show();

return false;
}

LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

if (!locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
createSnackbar(R.string.mesg_locationDisabledSelfHotspot)
.setAction(R.string.butn_locationSettings, new View.OnClickListener()
{
@Override
public void onClick(View view)
{
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.show();

return false;
}

return true;
}

public String getCleanNetworkName(String networkName)
{
if (networkName == null)
Expand Down Expand Up @@ -393,6 +352,11 @@ public WifiManager getWifiManager()
return mWifiManager;
}

public boolean hasLocationPermission()
{
return ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
}

public boolean isConnectionSelfNetwork()
{
WifiInfo wifiInfo = getWifiManager().getConnectionInfo();
Expand All @@ -412,6 +376,11 @@ public boolean isConnectedToNetwork(NetworkDeviceListAdapter.HotspotNetwork hots
return hotspotNetwork.SSID.equals(getCleanNetworkName(getWifiManager().getConnectionInfo().getSSID()));
}

public boolean isLocationServiceEnabled()
{
return mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}

public boolean isMobileDataActive()
{
return mConnectivityManager.getActiveNetworkInfo() != null
Expand Down Expand Up @@ -454,7 +423,7 @@ public void onClick(View view)
}
})
.show();
else if (checkLocationPermission())
else if (validateLocationPermission())
createSnackbar(R.string.mesg_scanningSelfHotspot)
.setAction(R.string.butn_wifiSettings, new View.OnClickListener()
{
Expand Down Expand Up @@ -555,7 +524,7 @@ public boolean toggleHotspot(boolean conditional)
return false;

if (conditional) {
if (Build.VERSION.SDK_INT >= 26 && !checkLocationPermission())
if (Build.VERSION.SDK_INT >= 26 && !validateLocationPermission())
return false;

if (Build.VERSION.SDK_INT >= 23
Expand Down Expand Up @@ -697,6 +666,47 @@ public void setOnListClickListener(AbsListView.OnItemClickListener listener)
mClickListener = listener;
}

public boolean validateLocationPermission()
{
if (Build.VERSION.SDK_INT < 23)
return false;

if (!hasLocationPermission()) {
createSnackbar(R.string.mesg_locationPermissionRequiredSelfHotspot)
.setAction(R.string.butn_locationSettings, new View.OnClickListener()
{
@Override
public void onClick(View view)
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getActivity().requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION);
}
}
})
.show();

return false;
}

if (!isLocationServiceEnabled()) {
createSnackbar(R.string.mesg_locationDisabledSelfHotspot)
.setAction(R.string.butn_locationSettings, new View.OnClickListener()
{
@Override
public void onClick(View view)
{
startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.show();

return false;
}

return true;
}

private class StatusReceiver extends BroadcastReceiver
{
@Override
Expand Down

0 comments on commit a0c96da

Please sign in to comment.