Some more improvements for Android permissions

This commit is contained in:
Dorian Zedler 2020-10-18 15:13:08 +02:00
parent fba59b3777
commit d75f55a3a1
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
2 changed files with 78 additions and 26 deletions

View file

@ -30,29 +30,82 @@ QBluetoothLeUartClient::~QBluetoothLeUartClient(){
// - Slots for QBluetoothLeUart - // - Slots for QBluetoothLeUart -
// ------------------------------ // ------------------------------
bool QBluetoothLeUartClient::startScanningForDevices(){ bool QBluetoothLeUartClient::requestLocationPermission() {
if(this->state != Idle && this->state != AdapterTurnedOff && this->state != ScanFinished && this->state != LocationPermissionDenied)
return false; if(this->isLocationPermissionGranted())
return true;
#ifdef Q_OS_ANDROID #ifdef Q_OS_ANDROID
else if(this->state == LocationPermissionDenied) {
// try to get permission // try to get permission
QtAndroid::PermissionResultMap resultMap = QtAndroid::requestPermissionsSync({"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"}); QtAndroid::PermissionResultMap resultMap = QtAndroid::requestPermissionsSync({"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
bool resultBool = true; bool resultBool = true;
for(QtAndroid::PermissionResult result : resultMap) { for(QtAndroid::PermissionResult result : resultMap) {
if(result != QtAndroid::PermissionResult::Granted) { if(result != QtAndroid::PermissionResult::Granted) {
resultBool = false; resultBool = false;
break;
} }
} }
if(!resultBool) { if(resultBool) {
emit this->scanningErrorOccured(LocationPermissionDeniedError); if(this->state == LocationPermissionDenied)
this->setState(Idle);
return true;
}
// getting permission the traditional way failed -> open the settings app
QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); //activity is valid
if (activity.isValid())
{
// get the package name
QAndroidJniObject context = QtAndroid::androidContext();
QAndroidJniObject applicationPackageName = context.callObjectMethod<jstring>("getPackageName");
QAndroidJniObject param = QAndroidJniObject::fromString("package:" + applicationPackageName.toString());
// Equivalent to Jave code: 'Uri uri = Uri::parse("...");'
QAndroidJniObject uri = QAndroidJniObject::callStaticObjectMethod("android/net/Uri", "parse", "(Ljava/lang/String;)Landroid/net/Uri;", param.object<jstring>());
if (!uri.isValid()) {
qWarning("ERROR: Unable to create Uri object");
return false;
}
QAndroidJniObject packageName = QAndroidJniObject::fromString("android.settings.APPLICATION_DETAILS_SETTINGS");
QAndroidJniObject intent("android/content/Intent","(Ljava/lang/String;)V", packageName.object<jstring>());
if (!intent.isValid()) {
qWarning("ERROR: Unable to create Intent object");
return false;
}
intent.callObjectMethod("addCategory", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("android.intent.category.DEFAULT").object<jstring>());
intent.callObjectMethod("setData", "(Landroid/net/Uri;)Landroid/content/Intent;", uri.object<jobject>());
activity.callMethod<void>("startActivity","(Landroid/content/Intent;)V",intent.object<jobject>());
}
else {
qWarning() << "ERROR: Activity not valid!";
return false; return false;
} }
} return true;
#else
return false;
#endif #endif
}
bool QBluetoothLeUartClient::isLocationPermissionGranted() {
#ifdef Q_OS_ANDROID
QtAndroid::PermissionResult fineLocationAccess = QtAndroid::checkPermission("android.permission.ACCESS_FINE_LOCATION");
QtAndroid::PermissionResult coarseLocationAccess = QtAndroid::checkPermission("android.permission.ACCESS_COARSE_LOCATION");
return fineLocationAccess == QtAndroid::PermissionResult::Granted && coarseLocationAccess == QtAndroid::PermissionResult::Granted;
#else
return true;
#endif
}
bool QBluetoothLeUartClient::startScanningForDevices(){
if(this->state != Idle && this->state != AdapterTurnedOff && this->state != ScanFinished && this->state != LocationPermissionDenied)
return false;
this->availableDevicesModel->clear(); this->availableDevicesModel->clear();
foreach(QBluetoothLeUartDevice* oldDevice, this->availableDevices) foreach(QBluetoothLeUartDevice* oldDevice, this->availableDevices)
@ -233,20 +286,15 @@ void QBluetoothLeUartClient::handleDeviceScanError(QBluetoothDeviceDiscoveryAgen
this->setState(AdapterTurnedOff); this->setState(AdapterTurnedOff);
emit this->scanningErrorOccured(InputOutputError); emit this->scanningErrorOccured(InputOutputError);
} }
#ifdef Q_OS_ANDROID
else if (error == QBluetoothDeviceDiscoveryAgent::UnknownError) { else if (error == QBluetoothDeviceDiscoveryAgent::UnknownError) {
// check for permission error // check for permission error
QtAndroid::PermissionResult fineLocationAccess = QtAndroid::checkPermission("android.permission.ACCESS_FINE_LOCATION"); if(!this->isLocationPermissionGranted()) {
QtAndroid::PermissionResult coarseLocationAccess = QtAndroid::checkPermission("android.permission.ACCESS_COARSE_LOCATION");
if(fineLocationAccess != QtAndroid::PermissionResult::Granted || coarseLocationAccess != QtAndroid::PermissionResult::Granted) {
this->setState(LocationPermissionDenied); this->setState(LocationPermissionDenied);
emit this->scanningErrorOccured(LocationPermissionDeniedError); emit this->scanningErrorOccured(LocationPermissionDeniedError);
} }
else else
emit this->scanningErrorOccured(UnknownError); emit this->scanningErrorOccured(UnknownError);
} }
#endif
else else
emit this->scanningErrorOccured(UnknownError); emit this->scanningErrorOccured(UnknownError);

View file

@ -192,6 +192,10 @@ private:
public slots: public slots:
Q_INVOKABLE bool requestLocationPermission();
Q_INVOKABLE bool isLocationPermissionGranted();
/*! /*!
* \brief Fuction to start scanning for devices * \brief Fuction to start scanning for devices
* *