1
0

androidfiledialog.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include "androidfiledialog.h"
  2. AndroidFileDialog::ResultReceiver::ResultReceiver(AndroidFileDialog *dialog) : _dialog(dialog) {}
  3. AndroidFileDialog::ResultReceiver::~ResultReceiver() {}
  4. void AndroidFileDialog::ResultReceiver::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject &data)
  5. {
  6. jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
  7. if (receiverRequestCode == EXISTING_FILE_NAME_REQUEST && resultCode == RESULT_OK) {
  8. QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
  9. QString path = uriToPath(uri);
  10. _dialog->emitExistingFileNameReady(path);
  11. } else {
  12. _dialog->emitExistingFileNameReady(QString());
  13. }
  14. }
  15. QString AndroidFileDialog::ResultReceiver::uriToPath(QAndroidJniObject uri)
  16. {
  17. if (uri.toString().startsWith("file:", Qt::CaseInsensitive)) {
  18. return uri.callObjectMethod("getPath", "()Ljava/lang/String;").toString();
  19. } else {
  20. QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
  21. QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), 0, 0, 0, 0);
  22. QAndroidJniObject DATA = QAndroidJniObject::fromString("_data");
  23. jint columnIndex = cursor.callMethod<jint>("getColumnIndexOrThrow", "(Ljava/lang/String;)I", DATA.object<jstring>());
  24. cursor.callMethod<jboolean>("moveToFirst", "()Z");
  25. QAndroidJniObject result = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);
  26. return result.isValid() ? result.toString() : QString();
  27. }
  28. }
  29. AndroidFileDialog::AndroidFileDialog(QObject *parent) : QObject(parent)
  30. {
  31. receiver = new ResultReceiver(this);
  32. }
  33. AndroidFileDialog::~AndroidFileDialog()
  34. {
  35. delete receiver;
  36. }
  37. bool AndroidFileDialog::provideExistingFileName()
  38. {
  39. QAndroidJniObject ACTION_GET_CONTENT = QAndroidJniObject::fromString("android.intent.action.GET_CONTENT");
  40. QAndroidJniObject intent("android/content/Intent");
  41. if (ACTION_GET_CONTENT.isValid() && intent.isValid()) {
  42. intent.callObjectMethod("setAction", "(Ljava/lang/String;)Landroid/content/Intent;", ACTION_GET_CONTENT.object<jstring>());
  43. intent.callObjectMethod("setType", "(Ljava/lang/String;)Landroid/content/Intent;", QAndroidJniObject::fromString("file/*").object<jstring>());
  44. QtAndroid::startActivity(intent.object<jobject>(), EXISTING_FILE_NAME_REQUEST, receiver);
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. void AndroidFileDialog::emitExistingFileNameReady(QString result)
  51. {
  52. emit existingFileNameReady(result);
  53. }