Warning, /graphics/krita/3rdparty/ext_qt/0008-Android-Add-support-for-getting-information-about-co.patch is written in an unsupported language. File is not indexed.
0001 From 486f169614b059ff42ce72428a7841ec5f329eaa Mon Sep 17 00:00:00 2001 0002 From: Andy Shaw <andy.shaw@qt.io> 0003 Date: Tue, 4 Feb 2020 11:39:29 +0100 0004 Subject: [PATCH 08/46] Android: Add support for getting information about 0005 content uris 0006 0007 This enables things like size(), exists() to work with Android content 0008 uris with the provided uri given from a filedialog. It is expected that 0009 it is always a full path due to the nature of content uris, so relative 0010 paths will not work. 0011 0012 Change-Id: I9c9ea42833677eb9d937b33e9dd42ee2a7d9c7c5 0013 Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io> 0014 Reviewed-by: BogDan Vatra <bogdan@kdab.com> 0015 --- 0016 .../org/qtproject/qt5/android/QtNative.java | 57 +++++++++++++++++++ 0017 .../android/androidcontentfileengine.cpp | 47 ++++++++++++++- 0018 .../android/androidcontentfileengine.h | 6 ++ 0019 3 files changed, 108 insertions(+), 2 deletions(-) 0020 0021 diff --git a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java 0022 index 6a4ec6687a..f53a038657 100644 0023 --- a/src/android/jar/src/org/qtproject/qt5/android/QtNative.java 0024 +++ b/src/android/jar/src/org/qtproject/qt5/android/QtNative.java 0025 @@ -44,6 +44,7 @@ import java.io.File; 0026 import java.io.FileNotFoundException; 0027 import java.util.ArrayList; 0028 import java.util.concurrent.Semaphore; 0029 +import java.io.IOException; 0030 0031 import android.app.Activity; 0032 import android.app.Service; 0033 @@ -71,6 +72,7 @@ import android.view.Menu; 0034 import android.view.MotionEvent; 0035 import android.view.View; 0036 import android.view.InputDevice; 0037 +import android.database.Cursor; 0038 0039 import java.lang.reflect.Method; 0040 import java.security.KeyStore; 0041 @@ -236,6 +238,61 @@ public class QtNative 0042 } 0043 } 0044 0045 + public static long getSize(Context context, String contentUrl) 0046 + { 0047 + Uri uri = getUriWithValidPermission(context, contentUrl, "r"); 0048 + long size = -1; 0049 + 0050 + if (uri == null) { 0051 + Log.e(QtTAG, "getSize(): No permissions to open Uri"); 0052 + return size; 0053 + } 0054 + 0055 + try { 0056 + ContentResolver resolver = context.getContentResolver(); 0057 + Cursor cur = resolver.query(uri, null, null, null, null); 0058 + if (cur != null) { 0059 + if (cur.moveToFirst()) 0060 + size = cur.getLong(5); // size column 0061 + cur.close(); 0062 + } 0063 + return size; 0064 + } catch (IllegalArgumentException e) { 0065 + Log.e(QtTAG, "getSize(): Invalid Uri"); 0066 + return size; 0067 + } catch (UnsupportedOperationException e) { 0068 + Log.e(QtTAG, "getSize(): Unsupported operation for given Uri"); 0069 + return size; 0070 + } 0071 + } 0072 + 0073 + public static boolean checkFileExists(Context context, String contentUrl) 0074 + { 0075 + Uri uri = getUriWithValidPermission(context, contentUrl, "r"); 0076 + boolean exists = false; 0077 + 0078 + if (uri == null) { 0079 + Log.e(QtTAG, "checkFileExists(): No permissions to open Uri"); 0080 + return exists; 0081 + } 0082 + 0083 + try { 0084 + ContentResolver resolver = context.getContentResolver(); 0085 + Cursor cur = resolver.query(uri, null, null, null, null); 0086 + if (cur != null) { 0087 + exists = true; 0088 + cur.close(); 0089 + } 0090 + return exists; 0091 + } catch (IllegalArgumentException e) { 0092 + Log.e(QtTAG, "checkFileExists(): Invalid Uri"); 0093 + return exists; 0094 + } catch (UnsupportedOperationException e) { 0095 + Log.e(QtTAG, "checkFileExists(): Unsupported operation for given Uri"); 0096 + return false; 0097 + } 0098 + } 0099 + 0100 // this method loads full path libs 0101 public static void loadQtLibraries(final ArrayList<String> libraries) 0102 { 0103 diff --git a/src/plugins/platforms/android/androidcontentfileengine.cpp b/src/plugins/platforms/android/androidcontentfileengine.cpp 0104 index 1444407195..3e3bdc2592 100644 0105 --- a/src/plugins/platforms/android/androidcontentfileengine.cpp 0106 +++ b/src/plugins/platforms/android/androidcontentfileengine.cpp 0107 @@ -44,9 +44,10 @@ 0108 0109 #include <QDebug> 0110 0111 -AndroidContentFileEngine::AndroidContentFileEngine(const QString &fileName) 0112 - : QFSFileEngine(fileName) 0113 +AndroidContentFileEngine::AndroidContentFileEngine(const QString &f) 0114 + : m_file(f) 0115 { 0116 + setFileName(f); 0117 } 0118 0119 bool AndroidContentFileEngine::open(QIODevice::OpenMode openMode) 0120 @@ -78,6 +79,48 @@ bool AndroidContentFileEngine::open(QIODevice::OpenMode openMode) 0121 return QFSFileEngine::open(openMode, fd, QFile::AutoCloseHandle); 0122 } 0123 0124 +qint64 AndroidContentFileEngine::size() const 0125 +{ 0126 + const jlong size = QJNIObjectPrivate::callStaticMethod<jlong>( 0127 + "org/qtproject/qt5/android/QtNative", "getSize", 0128 + "(Landroid/content/Context;Ljava/lang/String;)J", QtAndroidPrivate::context(), 0129 + QJNIObjectPrivate::fromString(fileName(DefaultName)).object()); 0130 + return (qint64)size; 0131 +} 0132 + 0133 +AndroidContentFileEngine::FileFlags AndroidContentFileEngine::fileFlags(FileFlags type) const 0134 +{ 0135 + FileFlags commonFlags(ReadOwnerPerm|ReadUserPerm|ReadGroupPerm|ReadOtherPerm|ExistsFlag); 0136 + FileFlags flags; 0137 + const bool exists = QJNIObjectPrivate::callStaticMethod<jboolean>( 0138 + "org/qtproject/qt5/android/QtNative", "checkFileExists", 0139 + "(Landroid/content/Context;Ljava/lang/String;)Z", QtAndroidPrivate::context(), 0140 + QJNIObjectPrivate::fromString(fileName(DefaultName)).object()); 0141 + if (!exists) 0142 + return flags; 0143 + flags = FileType | commonFlags; 0144 + return type & flags; 0145 +} 0146 + 0147 +QString AndroidContentFileEngine::fileName(FileName f) const 0148 +{ 0149 + switch (f) { 0150 + case PathName: 0151 + case AbsolutePathName: 0152 + case CanonicalPathName: 0153 + case DefaultName: 0154 + case AbsoluteName: 0155 + case CanonicalName: 0156 + return m_file; 0157 + case BaseName: 0158 + { 0159 + const int pos = m_file.lastIndexOf(QChar(QLatin1Char('/'))); 0160 + return m_file.mid(pos); 0161 + } 0162 + default: 0163 + return QString(); 0164 + } 0165 +} 0166 0167 AndroidContentFileEngineHandler::AndroidContentFileEngineHandler() = default; 0168 AndroidContentFileEngineHandler::~AndroidContentFileEngineHandler() = default; 0169 diff --git a/src/plugins/platforms/android/androidcontentfileengine.h b/src/plugins/platforms/android/androidcontentfileengine.h 0170 index db3def03d6..09e5d77553 100644 0171 --- a/src/plugins/platforms/android/androidcontentfileengine.h 0172 +++ b/src/plugins/platforms/android/androidcontentfileengine.h 0173 @@ -47,6 +47,12 @@ class AndroidContentFileEngine : public QFSFileEngine 0174 public: 0175 AndroidContentFileEngine(const QString &fileName); 0176 bool open(QIODevice::OpenMode openMode) override; 0177 + qint64 size() const override; 0178 + FileFlags fileFlags(FileFlags type = FileInfoAll) const override; 0179 + QString fileName(FileName file = DefaultName) const override; 0180 +private: 0181 + QString m_file; 0182 + 0183 }; 0184 0185 class AndroidContentFileEngineHandler : public QAbstractFileEngineHandler 0186 -- 0187 2.33.0 0188