Skip to content

Commit

Permalink
Check if location services are enabled on older API levels
Browse files Browse the repository at this point in the history
  • Loading branch information
kconger committed Apr 3, 2023
1 parent 896626d commit eef65b3
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 54 deletions.
10 changes: 3 additions & 7 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@

<uses-feature
android:name="android.hardware.bluetooth_le"
android:required="true" /> <!-- Request legacy Bluetooth permissions on older devices. -->
<uses-permission
android:name="android.permission.BLUETOOTH"
android:maxSdkVersion="30" />
<uses-permission
android:name="android.permission.BLUETOOTH_ADMIN"
android:maxSdkVersion="30" />
android:required="true" />
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission
android:name="android.permission.ACCESS_FINE_LOCATION"
android:maxSdkVersion="30" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.blackboxembedded.wunderlinqgopro;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
Expand All @@ -31,16 +32,20 @@
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.location.LocationManagerCompat;

import android.os.ParcelUuid;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -179,15 +184,14 @@ public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
finish();
return;
}

checkPermissions();

}

@Override
protected void onResume() {
super.onResume();

checkSystem();

// Ensures Bluetooth is enabled on the device. If Bluetooth is not currently enabled,
// fire an intent to display a dialog asking the user to grant permission to enable it.
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S ||
Expand Down Expand Up @@ -225,7 +229,7 @@ protected void onPause() {
scanLeDevice(false);
mLeDeviceListAdapter.clear();
}
if (cTimer != null){
if (cTimer != null) {
cancelTimer();
}
}
Expand All @@ -238,7 +242,7 @@ public boolean onCreateOptionsMenu(Menu menu) {

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
switch (item.getItemId()) {
case R.id.menu_settings:
//Launch Settings
Intent settingsIntent = new Intent(this, SettingsActivity.class);
Expand All @@ -263,7 +267,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
case KeyEvent.KEYCODE_DPAD_UP:
case KeyEvent.KEYCODE_PLUS:
case KeyEvent.KEYCODE_NUMPAD_ADD:
if (listView.getSelectedItemPosition() == 0 && lastPosition == 0){
if (listView.getSelectedItemPosition() == 0 && lastPosition == 0) {
listView.setSelection(listView.getCount() - 1);
}
lastPosition = listView.getSelectedItemPosition();
Expand All @@ -272,7 +276,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
case KeyEvent.KEYCODE_DPAD_DOWN:
case KeyEvent.KEYCODE_MINUS:
case KeyEvent.KEYCODE_NUMPAD_SUBTRACT:
if ((listView.getSelectedItemPosition() == (listView.getCount() - 1)) && lastPosition == (listView.getCount() - 1) ){
if ((listView.getSelectedItemPosition() == (listView.getCount() - 1)) && lastPosition == (listView.getCount() - 1)) {
listView.setSelection(0);
}
lastPosition = listView.getSelectedItemPosition();
Expand All @@ -291,7 +295,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
}

private void scanLeDevice(final boolean enable) {
Log.d(TAG,"scanLeDevice()");
Log.d(TAG, "scanLeDevice()");
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S ||
(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED)) {
bluetoothLeScanner = mBluetoothAdapter.getBluetoothLeScanner();
Expand Down Expand Up @@ -350,7 +354,7 @@ public void addDevice(Device device) {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S ||
(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_CONNECT) == PackageManager.PERMISSION_GRANTED)) {
if (!mLeDevices.contains(device)) {
Log.d(TAG,"Found Camera: " + device.getDevice().getName());
Log.d(TAG, "Found Camera: " + device.getDevice().getName());
mLeDevices.add(device);
}
}
Expand Down Expand Up @@ -518,45 +522,66 @@ public void onDismiss(DialogInterface dialog) {
}
}

//Check Permissions
void checkPermissions(){
// Check permissions
if ((android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S) &&
(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.location_request_alert_title));
builder.setMessage(getString(R.string.location_request_alert_body));
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(DeviceScanActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_FINE_LOCATION);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
} else if ((android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) &&
(ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED)){
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.bt_request_alert_title));
builder.setMessage(getString(R.string.bt_request_alert_body));
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(DeviceScanActivity.this, new String[]{Manifest.permission.BLUETOOTH_SCAN}, PERMISSION_REQUEST_BLUETOOTH_SCAN);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
// Check Required System Services and Permissions
void checkSystem() {
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.S) {
if (isLocationEnabled(this)) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.location_request_alert_title));
builder.setMessage(getString(R.string.location_request_alert_body));
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(DeviceScanActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_FINE_LOCATION);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
});
builder.show();
} else {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.location_services_alert_title));
builder.setMessage(getString(R.string.location_services_alert_body));
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
} else if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.S) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.BLUETOOTH_SCAN) != PackageManager.PERMISSION_GRANTED) {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(getString(R.string.bt_request_alert_title));
builder.setMessage(getString(R.string.bt_request_alert_body));
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(DeviceScanActivity.this, new String[]{Manifest.permission.BLUETOOTH_SCAN}, PERMISSION_REQUEST_BLUETOOTH_SCAN);
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.show();
}
}
}

Expand All @@ -582,4 +607,14 @@ void cancelTimer() {
if(cTimer!=null)
cTimer.cancel();
}

private boolean isLocationEnabled(Context context) {
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
return LocationManagerCompat.isLocationEnabled(locationManager);
}

private static boolean isBluetoothEnabled(Context context) {
BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
return bluetoothAdapter != null && bluetoothAdapter.isEnabled();
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<string name="negative_location_alert_title">Location Request Denied</string>
<string name="negative_location_alert_body">Location Permissions are required for Bluetooth scanning</string>

<string name="location_services_alert_title">Location Services Required</string>
<string name="location_services_alert_body">Your device appears to have location services turned off. Please re-enable them to use this app.</string>

<string name="location_request_alert_title">Location Permission Required</string>
<string name="location_request_alert_body">The WunderLINQ GoPro Remote needs access to your location to scan for nearby GoPro Cameras.</string>
<string name="bt_request_alert_title">Bluetooth Permission Required</string>
Expand Down

0 comments on commit eef65b3

Please sign in to comment.