diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9d0b660..948689c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -3,13 +3,9 @@ - - + android:required="true" /> + + diff --git a/app/src/main/java/com/blackboxembedded/wunderlinqgopro/DeviceScanActivity.java b/app/src/main/java/com/blackboxembedded/wunderlinqgopro/DeviceScanActivity.java index d6fff08..dbe6436 100644 --- a/app/src/main/java/com/blackboxembedded/wunderlinqgopro/DeviceScanActivity.java +++ b/app/src/main/java/com/blackboxembedded/wunderlinqgopro/DeviceScanActivity.java @@ -16,6 +16,7 @@ along with this program. If not, see . */ package com.blackboxembedded.wunderlinqgopro; + import android.Manifest; import android.app.Activity; import android.bluetooth.BluetoothAdapter; @@ -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; @@ -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 || @@ -225,7 +229,7 @@ protected void onPause() { scanLeDevice(false); mLeDeviceListAdapter.clear(); } - if (cTimer != null){ + if (cTimer != null) { cancelTimer(); } } @@ -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); @@ -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(); @@ -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(); @@ -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(); @@ -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); } } @@ -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(); + } } } @@ -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(); + } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 9699d37..cae5cd4 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -11,6 +11,9 @@ Location Request Denied Location Permissions are required for Bluetooth scanning + Location Services Required + Your device appears to have location services turned off. Please re-enable them to use this app. + Location Permission Required The WunderLINQ GoPro Remote needs access to your location to scan for nearby GoPro Cameras. Bluetooth Permission Required