Refactor Android open file method
Fix some bugs with mimetype filters when the Qt mimetype database does not match the Android database
This commit is contained in:
parent
7437d47d92
commit
e8cc80f046
5 changed files with 89 additions and 14 deletions
|
@ -2,6 +2,7 @@ package org.amnezia.vpn
|
|||
|
||||
import android.content.ComponentName
|
||||
import android.content.Intent
|
||||
import android.content.Intent.EXTRA_MIME_TYPES
|
||||
import android.content.Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY
|
||||
import android.content.ServiceConnection
|
||||
import android.net.Uri
|
||||
|
@ -12,11 +13,13 @@ import android.os.IBinder
|
|||
import android.os.Looper
|
||||
import android.os.Message
|
||||
import android.os.Messenger
|
||||
import android.webkit.MimeTypeMap
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.MainThread
|
||||
import androidx.core.content.ContextCompat
|
||||
import java.io.IOException
|
||||
import kotlin.LazyThreadSafetyMode.NONE
|
||||
import kotlin.text.RegexOption.IGNORE_CASE
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
@ -35,6 +38,7 @@ private const val TAG = "AmneziaActivity"
|
|||
|
||||
private const val CHECK_VPN_PERMISSION_ACTION_CODE = 1
|
||||
private const val CREATE_FILE_ACTION_CODE = 2
|
||||
private const val OPEN_FILE_ACTION_CODE = 3
|
||||
private const val BIND_SERVICE_TIMEOUT = 1000L
|
||||
|
||||
class AmneziaActivity : QtActivity() {
|
||||
|
@ -201,6 +205,15 @@ class AmneziaActivity : QtActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
OPEN_FILE_ACTION_CODE -> {
|
||||
when (resultCode) {
|
||||
RESULT_OK -> data?.data?.toString() ?: ""
|
||||
else -> ""
|
||||
}.let { uri ->
|
||||
QtAndroidController.onFileOpened(uri)
|
||||
}
|
||||
}
|
||||
|
||||
CHECK_VPN_PERMISSION_ACTION_CODE -> {
|
||||
when (resultCode) {
|
||||
RESULT_OK -> {
|
||||
|
@ -370,6 +383,36 @@ class AmneziaActivity : QtActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
fun openFile(filter: String?) {
|
||||
Log.v(TAG, "Open file with filter: $filter")
|
||||
|
||||
val mimeTypes = if (!filter.isNullOrEmpty()) {
|
||||
val extensionRegex = "\\*\\.[a-z .]+".toRegex(IGNORE_CASE)
|
||||
val mime = MimeTypeMap.getSingleton()
|
||||
extensionRegex.findAll(filter).map {
|
||||
mime.getMimeTypeFromExtension(it.value.drop(2))
|
||||
}.filterNotNull().toSet()
|
||||
} else emptySet()
|
||||
|
||||
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
Log.d(TAG, "File mimyType filter: $mimeTypes")
|
||||
when (mimeTypes.size) {
|
||||
1 -> type = mimeTypes.first()
|
||||
|
||||
in 2..Int.MAX_VALUE -> {
|
||||
type = "*/*"
|
||||
putExtra(EXTRA_MIME_TYPES, mimeTypes.toTypedArray())
|
||||
}
|
||||
|
||||
else -> type = "*/*"
|
||||
}
|
||||
}.also {
|
||||
startActivityForResult(it, OPEN_FILE_ACTION_CODE)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("unused")
|
||||
fun setNotificationText(title: String, message: String, timerSec: Int) {
|
||||
Log.v(TAG, "Set notification text")
|
||||
|
|
|
@ -15,6 +15,8 @@ object QtAndroidController {
|
|||
external fun onVpnReconnecting()
|
||||
external fun onStatisticsUpdate(rxBytes: Long, txBytes: Long)
|
||||
|
||||
external fun onFileOpened(uri: String)
|
||||
|
||||
external fun onConfigImported(data: String)
|
||||
|
||||
external fun decodeQrCode(data: String): Boolean
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <QCoreApplication>
|
||||
#include <QJniEnvironment>
|
||||
#include <QJsonDocument>
|
||||
#include <QQmlFile>
|
||||
|
||||
#include "android_controller.h"
|
||||
#include "ui/controllers/importController.h"
|
||||
|
@ -106,6 +107,7 @@ bool AndroidController::initialize()
|
|||
{"onVpnDisconnected", "()V", reinterpret_cast<void *>(onVpnDisconnected)},
|
||||
{"onVpnReconnecting", "()V", reinterpret_cast<void *>(onVpnReconnecting)},
|
||||
{"onStatisticsUpdate", "(JJ)V", reinterpret_cast<void *>(onStatisticsUpdate)},
|
||||
{"onFileOpened", "(Ljava/lang/String;)V", reinterpret_cast<void *>(onFileOpened)},
|
||||
{"onConfigImported", "(Ljava/lang/String;)V", reinterpret_cast<void *>(onConfigImported)},
|
||||
{"decodeQrCode", "(Ljava/lang/String;)Z", reinterpret_cast<bool *>(decodeQrCode)}
|
||||
};
|
||||
|
@ -165,6 +167,24 @@ void AndroidController::saveFile(const QString &fileName, const QString &data)
|
|||
QJniObject::fromString(data).object<jstring>());
|
||||
}
|
||||
|
||||
QString AndroidController::openFile(const QString &filter)
|
||||
{
|
||||
QEventLoop wait;
|
||||
QString fileName;
|
||||
connect(this, &AndroidController::fileOpened, this,
|
||||
[&fileName, &wait](const QString &uri) {
|
||||
qDebug() << "Android event: file opened; uri:" << uri;
|
||||
fileName = QQmlFile::urlToLocalFileOrQrc(uri);
|
||||
qDebug() << "Android opened filename:" << fileName;
|
||||
wait.quit();
|
||||
},
|
||||
static_cast<Qt::ConnectionType>(Qt::QueuedConnection | Qt::SingleShotConnection));
|
||||
callActivityMethod("openFile", "(Ljava/lang/String;)V",
|
||||
QJniObject::fromString(filter).object<jstring>());
|
||||
wait.exec();
|
||||
return fileName;
|
||||
}
|
||||
|
||||
void AndroidController::setNotificationText(const QString &title, const QString &message, int timerSec)
|
||||
{
|
||||
callActivityMethod("setNotificationText", "(Ljava/lang/String;Ljava/lang/String;I)V",
|
||||
|
@ -284,6 +304,22 @@ void AndroidController::onStatisticsUpdate(JNIEnv *env, jobject thiz, jlong rxBy
|
|||
emit AndroidController::instance()->statisticsUpdated((quint64) rxBytes, (quint64) txBytes);
|
||||
}
|
||||
|
||||
// static
|
||||
void AndroidController::onFileOpened(JNIEnv *env, jobject thiz, jstring uri)
|
||||
{
|
||||
Q_UNUSED(thiz);
|
||||
|
||||
const char *buffer = env->GetStringUTFChars(uri, nullptr);
|
||||
if (!buffer) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString lUri(buffer);
|
||||
env->ReleaseStringUTFChars(uri, buffer);
|
||||
|
||||
emit AndroidController::instance()->fileOpened(lUri);
|
||||
}
|
||||
|
||||
// static
|
||||
void AndroidController::onConfigImported(JNIEnv *env, jobject thiz, jstring data)
|
||||
{
|
||||
|
|
|
@ -31,6 +31,7 @@ public:
|
|||
void stop();
|
||||
void setNotificationText(const QString &title, const QString &message, int timerSec);
|
||||
void saveFile(const QString& fileName, const QString &data);
|
||||
QString openFile(const QString &filter);
|
||||
void startQrReaderActivity();
|
||||
|
||||
signals:
|
||||
|
@ -43,6 +44,7 @@ signals:
|
|||
void vpnDisconnected();
|
||||
void vpnReconnecting();
|
||||
void statisticsUpdated(quint64 rxBytes, quint64 txBytes);
|
||||
void fileOpened(QString uri);
|
||||
void configImported(QString config);
|
||||
void importConfigFromOutside(QString config);
|
||||
void initConnectionState(Vpn::ConnectionState state);
|
||||
|
@ -65,6 +67,7 @@ private:
|
|||
static void onVpnReconnecting(JNIEnv *env, jobject thiz);
|
||||
static void onStatisticsUpdate(JNIEnv *env, jobject thiz, jlong rxBytes, jlong txBytes);
|
||||
static void onConfigImported(JNIEnv *env, jobject thiz, jstring data);
|
||||
static void onFileOpened(JNIEnv *env, jobject thiz, jstring uri);
|
||||
static bool decodeQrCode(JNIEnv *env, jobject thiz, jstring data);
|
||||
|
||||
template <typename Ret, typename ...Args>
|
||||
|
|
|
@ -60,6 +60,11 @@ QString SystemController::getFileName(const QString &acceptLabel, const QString
|
|||
const QString &selectedFile, const bool isSaveMode, const QString &defaultSuffix)
|
||||
{
|
||||
QString fileName;
|
||||
#ifdef Q_OS_ANDROID
|
||||
Q_ASSERT(!isSaveMode);
|
||||
return AndroidController::instance()->openFile(nameFilter);
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_IOS
|
||||
|
||||
MobileUtils mobileUtils;
|
||||
|
@ -108,20 +113,6 @@ QString SystemController::getFileName(const QString &acceptLabel, const QString
|
|||
}
|
||||
|
||||
fileName = mainFileDialog->property("selectedFile").toString();
|
||||
|
||||
#ifdef Q_OS_ANDROID
|
||||
// patch for files containing spaces etc
|
||||
const QString sep { "raw%3A%2F" };
|
||||
if (fileName.startsWith("content://") && fileName.contains(sep)) {
|
||||
QString contentUrl = fileName.split(sep).at(0);
|
||||
QString rawUrl = fileName.split(sep).at(1);
|
||||
rawUrl.replace(" ", "%20");
|
||||
fileName = contentUrl + sep + rawUrl;
|
||||
}
|
||||
|
||||
return fileName;
|
||||
#endif
|
||||
|
||||
return QUrl(fileName).toLocalFile();
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue