Commit bd039c16 authored by Matthieu Gallien's avatar Matthieu Gallien 🎵
Browse files

use QtAndroid::requestPermissionsSync instead of custom code

parent 85b4f44d
......@@ -18,8 +18,6 @@ import android.provider.MediaStore;
public class ElisaActivity extends QtActivity
{
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
private static String[] tracksRequestedColumns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
......@@ -93,46 +91,6 @@ public class ElisaActivity extends QtActivity
androidMusicScanAlbumsFinishing();
}
public static void checkPermissions(Activity activity)
{
if (ContextCompat.checkSelfPermission(activity,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(activity,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
} else {
readExternalStoragePermissionIsOk();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
readExternalStoragePermissionIsOk();
} else {
readExternalStoragePermissionIsKo();
}
return;
}
}
}
private static native void androidMusicScanTracksStarting();
private static native void sendMusicFile(String musicFile);
......@@ -144,8 +102,4 @@ public class ElisaActivity extends QtActivity
private static native void sendMusicAlbum(String musicFile);
private static native void androidMusicScanAlbumsFinishing();
private static native void readExternalStoragePermissionIsOk();
private static native void readExternalStoragePermissionIsKo();
}
......@@ -120,8 +120,6 @@ void AndroidFileListing::registerNativeMethods()
{"androidMusicScanAlbumsStarting", "()V", reinterpret_cast<void *>(albumsAndroidScanStarted)},
{"sendMusicAlbum", "(Ljava/lang/String;)V", reinterpret_cast<void *>(sentMusicAlbum)},
{"androidMusicScanAlbumsFinishing", "()V", reinterpret_cast<void *>(albumsAndroidScanFinished)},
{"readExternalStoragePermissionIsOk", "()V", reinterpret_cast<void *>(::readExternalStoragePermissionIsOk)},
{"readExternalStoragePermissionIsKo", "()V", reinterpret_cast<void *>(::readExternalStoragePermissionIsKo)},
};
QAndroidJniObject javaClass("org/kde/elisa/ElisaActivity");
......@@ -135,10 +133,6 @@ void AndroidFileListing::registerNativeMethods()
void AndroidFileListing::readExternalStoragePermissionIsOk()
{
QAndroidJniObject::callStaticMethod<void>("org/kde/elisa/ElisaActivity",
"listAudioFiles",
"(Landroid/content/Context;)V",
QtAndroid::androidContext().object());
}
void AndroidFileListing::readExternalStoragePermissionIsKo()
......@@ -232,12 +226,21 @@ void AndroidFileListing::triggerRefreshOfContent()
qCInfo(orgKdeElisaAndroid()) << "AndroidFileListing::triggerRefreshOfContent";
Q_EMIT indexingStarted();
if (QtAndroid::checkPermission(QStringLiteral("android.permission.READ_EXTERNAL_STORAGE")) == QtAndroid::PermissionResult::Denied) {
QtAndroid::requestPermissionsSync({QStringLiteral("android.permission.READ_EXTERNAL_STORAGE")});
}
if (QtAndroid::checkPermission(QStringLiteral("android.permission.READ_EXTERNAL_STORAGE")) == QtAndroid::PermissionResult::Denied) {
qCInfo(orgKdeElisaAndroid()) << "AndroidFileListing::triggerRefreshOfContent" << "not scanning files due to missing permission";
return;
}
AbstractFileListing::triggerRefreshOfContent();
QAndroidJniObject::callStaticMethod<void>("org/kde/elisa/ElisaActivity",
"checkPermissions",
"(Landroid/app/Activity;)V",
QtAndroid::androidActivity().object());
"listAudioFiles",
"(Landroid/content/Context;)V",
QtAndroid::androidContext().object());
}
DataTypes::TrackDataType AndroidFileListing::scanOneFile(const QUrl &scanFile, const QFileInfo &scanFileInfo, FileSystemWatchingModes watchForFileSystemChanges)
......@@ -247,11 +250,4 @@ DataTypes::TrackDataType AndroidFileListing::scanOneFile(const QUrl &scanFile, c
return newTrack;
}
#if 0
void AndroidMusicListener::init()
{
Q_EMIT askRestoredTracks(d->mSourceName);
}
#endif
#include "moc_androidfilelisting.cpp"
......@@ -61,8 +61,6 @@ public Q_SLOTS:
private Q_SLOTS:
private:
void registerToBaloo();
void executeInit(QHash<QUrl, QDateTime> allFiles) override;
void triggerRefreshOfContent() override;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment