diff --git a/android/routerKeygen/build.gradle b/android/routerKeygen/build.gradle index 9bee9c3..97efa9a 100644 --- a/android/routerKeygen/build.gradle +++ b/android/routerKeygen/build.gradle @@ -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' } diff --git a/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListActivity.java b/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListActivity.java index 99a9667..fc402fb 100644 --- a/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListActivity.java +++ b/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListActivity.java @@ -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; @@ -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; @@ -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; @@ -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 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); @@ -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()); @@ -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; + } + } } diff --git a/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListFragment.java b/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListFragment.java index efe35de..23bd2e5 100644 --- a/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListFragment.java +++ b/android/routerKeygen/src/main/java/org/exobel/routerkeygen/ui/NetworksListFragment.java @@ -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; } @@ -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;