Skip to content

Commit

Permalink
Ensure that location is always there otherwise scanning does not work.
Browse files Browse the repository at this point in the history
  • Loading branch information
ruiaraujo committed Nov 14, 2017
1 parent 6c41b2d commit d4b60e1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 14 deletions.
11 changes: 7 additions & 4 deletions android/routerKeygen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,13 @@ dependencies {
implementation project(':aFileChooser')
implementation (name: 'android-ad-sdk', ext: 'aar')

implementation 'com.android.support:support-compat:27.0.0'
implementation 'com.android.support:support-core-ui:27.0.0'
implementation 'com.android.support:support-core-utils:27.0.0'
implementation ('com.google.android.gms:play-services-analytics:11.4.2', { exclude module: 'support-v4' })
implementation 'com.android.support:support-compat:27.0.1'
implementation 'com.android.support:support-core-ui:27.0.1'
implementation 'com.android.support:support-core-utils:27.0.1'
implementation 'com.android.support:support-fragment:27.0.1'

implementation ('com.google.android.gms:play-services-location:11.6.0', { exclude module: 'support-v4' })
implementation ('com.google.android.gms:play-services-analytics:11.6.0', { exclude module: 'support-v4' })
implementation ('ch.acra:acra:4.9.0', { exclude module: 'support-v4' })
testImplementation 'junit:junit:4.12'
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentSender;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
Expand All @@ -45,6 +46,17 @@
import android.widget.Toast;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.CommonStatusCodes;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;

import org.exobel.routerkeygen.AdsUtils;
import org.exobel.routerkeygen.BuildConfig;
Expand All @@ -60,6 +72,7 @@ public class NetworksListActivity extends Activity implements
NetworksListFragment.OnItemSelectionListener, OnScanListener {
private final static String LAST_DIALOG_TIME = "last_time";
private static final int MY_PERMISSIONS_ACCESS_FINE_LOCATION = 1;
private static final int REQUEST_CHECK_SETTINGS = 1;
private boolean mTwoPane;
private NetworksListFragment networkListFragment;
private WifiManager wifi;
Expand Down Expand Up @@ -318,12 +331,52 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String permissi
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
scanPermission = true;
networkListFragment.updatePermission(this);
scan();
}
}
}
}

public static void settingsRequest(final Activity activity, OnSuccessListener cb) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(100000);
mLocationRequest.setFastestInterval(50000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);

SettingsClient client = LocationServices.getSettingsClient(activity);
Task<LocationSettingsResponse> task = client.checkLocationSettings(builder.build());

task.addOnSuccessListener(activity, cb);

task.addOnFailureListener(activity, e -> {
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode) {
case CommonStatusCodes.RESOLUTION_REQUIRED:
// Location settings are not satisfied, but this can be fixed
// by showing the user a dialog.
try {
// Show the dialog by calling startResolutionForResult(),
// and check the result in onActivityResult().
ResolvableApiException resolvable = (ResolvableApiException) e;
resolvable.startResolutionForResult(activity, REQUEST_CHECK_SETTINGS);
} catch (IntentSender.SendIntentException sendEx) {
// Ignore the error.
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
// Location settings are not satisfied. However, we have no way
// to fix the settings so we won't show the dialog.
break;
}
});
}


private void scan() {
if (!wifiState && !wifiOn) {
networkListFragment.setMessage(R.string.msg_nowifi);
Expand All @@ -340,14 +393,22 @@ private void scan() {
Toast.makeText(this, R.string.msg_wifienabling, Toast.LENGTH_SHORT)
.show();
} else {
if (wifi.startScan()) {
//setRefreshActionItemState(true);
mSwipeRefreshLayout.setRefreshing(true);
} else
networkListFragment.setMessage(R.string.msg_scanfailed);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
settingsRequest(this, o -> startScan());
} else {
startScan();
}
}
}

private void startScan() {
if (wifi.startScan()) {
//setRefreshActionItemState(true);
mSwipeRefreshLayout.setRefreshing(true);
} else
networkListFragment.setMessage(R.string.msg_scanfailed);
}

private void getPrefs() {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
Expand Down Expand Up @@ -383,4 +444,21 @@ public void onItemSelected(String mac) {
ManualInputFragment.MAC_ADDRESS_ARG, mac));
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
switch (requestCode) {
case REQUEST_CHECK_SETTINGS:
switch (resultCode) {
case Activity.RESULT_OK:
scan();
break;
case Activity.RESULT_CANCELED:
break;
default:
break;
}
break;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,7 @@ public void onAttach(Activity activity) {
throw new IllegalStateException(
"Activity must implement fragment's callbacks.");
}
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
scanPermission = false;
}
updatePermission(activity);
mCallbacks = (OnItemSelectionListener) activity;
}

Expand Down Expand Up @@ -218,6 +214,21 @@ public void setMessage(int message) {
}
}

public void updatePermission(Activity activity) {
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
scanPermission = false;
} else {
scanPermission = true;
}
if (networksFound == null && scanPermission && noNetworksMessage != null) {
noNetworksMessage.setVisibility(View.GONE);
permissionButton.setVisibility(View.GONE);
noNetworksMessage.findViewById(R.id.loading_spinner).setVisibility(View.VISIBLE);
}
}

@Override
public void onScanFinished(WiFiNetwork[] networks) {
networksFound = networks;
Expand Down

0 comments on commit d4b60e1

Please sign in to comment.