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,28 +30,81 @@ QBluetoothLeUartClient::~QBluetoothLeUartClient(){
// - Slots for QBluetoothLeUart - // - Slots for QBluetoothLeUart -
// ------------------------------ // ------------------------------
bool QBluetoothLeUartClient::requestLocationPermission() {
if(this->isLocationPermissionGranted())
return true;
#ifdef Q_OS_ANDROID
// try to get permission
QtAndroid::PermissionResultMap resultMap = QtAndroid::requestPermissionsSync({"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
bool resultBool = true;
for(QtAndroid::PermissionResult result : resultMap) {
if(result != QtAndroid::PermissionResult::Granted) {
resultBool = false;
}
}
if(resultBool) {
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 true;
#else
return false;
#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(){ bool QBluetoothLeUartClient::startScanningForDevices(){
if(this->state != Idle && this->state != AdapterTurnedOff && this->state != ScanFinished && this->state != LocationPermissionDenied) if(this->state != Idle && this->state != AdapterTurnedOff && this->state != ScanFinished && this->state != LocationPermissionDenied)
return false; return false;
#ifdef Q_OS_ANDROID
else if(this->state == LocationPermissionDenied) {
// try to get permission
QtAndroid::PermissionResultMap resultMap = QtAndroid::requestPermissionsSync({"android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"});
bool resultBool = true;
for(QtAndroid::PermissionResult result : resultMap) {
if(result != QtAndroid::PermissionResult::Granted) {
resultBool = false;
break;
}
}
if(!resultBool) {
emit this->scanningErrorOccured(LocationPermissionDeniedError);
return false;
}
}
#endif
this->availableDevicesModel->clear(); this->availableDevicesModel->clear();
@ -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);
@ -372,7 +420,7 @@ void QBluetoothLeUartClient::handleServiceCharacteristicChange(const QLowEnergyC
} }
void QBluetoothLeUartClient::handleServiceDescriptorWritten(const QLowEnergyDescriptor &d, void QBluetoothLeUartClient::handleServiceDescriptorWritten(const QLowEnergyDescriptor &d,
const QByteArray &value) const QByteArray &value)
{ {
if (d.isValid() && d == bluetoothTransmissionDescriptor && value == QByteArray("0000")) { if (d.isValid() && d == bluetoothTransmissionDescriptor && value == QByteArray("0000")) {
//disabled notifications -> assume disconnect intent //disabled notifications -> assume disconnect intent

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
* *