Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

[android] Copy assets without blocking main thread

Carsten Hartenfels64a192fc3863
diff --git a/krita/main.cc b/krita/main.cc
index fe66601536..9f252d7f7b 100644
--- a/krita/main.cc
+++ b/krita/main.cc
@@ -58,7 +58,7 @@
#ifdef Q_OS_ANDROID
#include <QtAndroid>
#include <KisAndroidCrashHandler.h>
-#include <KisAndroidLogHandler.h>
+#include <KisAndroidUtils.h>
#endif
#if defined Q_OS_WIN
@@ -217,7 +217,7 @@ Java_org_krita_android_JNIWrappers_openFileFromIntent(JNIEnv* /*env*/,
extern "C" MAIN_EXPORT int MAIN_FN(int argc, char **argv)
{
#ifdef Q_OS_ANDROID
- KisAndroidLogHandler::handler_init();
+ KisAndroidUtils::performInitialSetup();
#endif
#ifdef Q_OS_WIN
diff --git a/libs/global/KisAndroidUtils.cpp b/libs/global/KisAndroidUtils.cpp
index 8e7191afe4..2e34c1cbc6 100644
--- a/libs/global/KisAndroidUtils.cpp
+++ b/libs/global/KisAndroidUtils.cpp
@@ -2,11 +2,26 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
#include "KisAndroidUtils.h"
+#include "KisAndroidLogHandler.h"
#include <QtAndroid>
namespace KisAndroidUtils
{
+void performInitialSetup()
+{
+ KisAndroidLogHandler::handler_init();
+
+ QAndroidJniObject activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",
+ "activity",
+ "()Landroid/app/Activity;");
+ if (activity.isValid()) {
+ activity.callMethod<void>("copyAssets", "()V");
+ } else {
+ qWarning("performInitialSetup: activity not valid");
+ }
+}
+
bool looksLikeXiaomiDevice()
{
// The device isn't going to change, so let's cache the slow JNI call.
diff --git a/libs/global/KisAndroidUtils.h b/libs/global/KisAndroidUtils.h
index de6bbda182..59c534b2ff 100644
--- a/libs/global/KisAndroidUtils.h
+++ b/libs/global/KisAndroidUtils.h
@@ -9,6 +9,9 @@
namespace KisAndroidUtils
{
+// Installs the Android log handler and copies assets.
+KRITAGLOBAL_EXPORT void performInitialSetup();
+
// Check whether we seem to be running on a Xiaomi device, which requires
// enabling several workarounds by default. If we need additional workarounds
// in the future, change this to return an enum or a flag set instead, depending
diff --git a/packaging/android/apk/src/org/krita/android/ConfigsManager.java b/packaging/android/apk/src/org/krita/android/ConfigsManager.java
index 6753aec4a5..d41fe4138e 100644
--- a/packaging/android/apk/src/org/krita/android/ConfigsManager.java
+++ b/packaging/android/apk/src/org/krita/android/ConfigsManager.java
@@ -8,6 +8,7 @@
package org.krita.android;
import android.app.Activity;
+import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
@@ -15,13 +16,22 @@ import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Environment;
+import android.view.Gravity;
+import android.view.Window;
+import android.view.WindowManager;
import android.util.Log;
+import android.widget.ProgressBar;
+
+import org.qtproject.qt5.android.QtNative;
+import org.qtproject.qt5.android.bindings.QtActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
+import java.util.HashSet;
+import java.util.Set;
/**
* Copies the default configurations from assets to INTERNAL_STORAGE
@@ -31,9 +41,16 @@ class ConfigsManager {
private final String LOG_TAG = "krita.ConfigsManager";
private final String LAST_UPDATE_TIME = "ORG_KRITA_LASTUPDATETIME";
- private long mLastUpdateTime;
- private String mStorageDir = null;
- private Activity mActivity;
+ private final Activity mActivity;
+ private final String mStorageDir;
+ private long mLastUpdateTime = 0L;
+ private AlertDialog mAlertDialog = null;
+ private boolean mAssetsCopied = false;
+
+ public ConfigsManager(Activity activity) {
+ mActivity = activity;
+ mStorageDir = getStorageDir();
+ }
private void updateLastUpdateTime() {
SharedPreferences sharedPref = mActivity.getPreferences(Context.MODE_PRIVATE);
@@ -47,8 +64,7 @@ class ConfigsManager {
.getLong(LAST_UPDATE_TIME, 0) != mLastUpdateTime;
}
- void handleAssets(Activity activity) {
- mActivity = activity;
+ public void handleAssets() {
try {
PackageInfo info = mActivity.getPackageManager().getPackageInfo(mActivity.getPackageName(), 0);
mLastUpdateTime = info.lastUpdateTime;
@@ -59,42 +75,116 @@ class ConfigsManager {
return;
}
- mStorageDir = getStorageDir();
- Log.i(LOG_TAG, mStorageDir);
- copyAssets();
- updateLastUpdateTime();
+ // Copying assets can take quite a while, so we show a spinner dialog
+ // during it. The functions relating to that are all synchronized,
+ // which means they take a mutex so that we don't end up with a race.
+ setAssetsCopied(false);
+ QtNative.activity().runOnUiThread(() -> {
+ try {
+ showDialog();
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Failed to show progress dialog", e);
+ }
+ });
+
+ Log.i(LOG_TAG, "storage dir: " + mStorageDir);
+ try {
+ copyAssets();
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Failed to copy assets", e);
+ }
+
+ try {
+ updateLastUpdateTime();
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Failed to update last update time", e);
+ }
+
+ setAssetsCopied(true);
+ QtNative.activity().runOnUiThread(() -> {
+ try {
+ dismissDialog();
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Failed to dismiss progress dialog", e);
+ }
+ });
}
- private void copyAssets() {
- recurse("");
+ private synchronized void setAssetsCopied(boolean assetsCopied) {
+ // Don't remove this function, it exists because it's synchronized! We
+ // have to take a lock to avoid races with the UI thread.
+ mAssetsCopied = assetsCopied;
}
- /**
- * Recurse into directories
- * Do not start path with `/` or end with `/`
- * @param path relative path to asset
- */
- private void recurse(String path) {
+ private synchronized void showDialog() {
+ // We might already be done copying the assets. This function is
+ // synchronized!
+ if (!mAssetsCopied) {
+ ProgressBar progressBar = new ProgressBar(mActivity);
+ progressBar.setPadding(100, 100, 100, 100);
+
+ mAlertDialog = new AlertDialog.Builder(mActivity, AlertDialog.THEME_DEVICE_DEFAULT_DARK)
+ .setCancelable(false)
+ .setView(progressBar)
+ .create();
+
+ // This dialog usually appears and disappears very quickly, so
+ // having animations looks pretty wonky. Turn those off.
+ Window window = mAlertDialog.getWindow();
+ if (window != null) {
+ window.setWindowAnimations(0);
+ }
+
+ mAlertDialog.show();
+
+ // By default, the alert dialog is full-width. This clamps the
+ // dialog bounds to the spinner so that it looks sensible.
+ window = mAlertDialog.getWindow();
+ if (window != null) {
+ window.setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
+ window.setGravity(Gravity.CENTER);
+ }
+ }
+ }
+
+ private synchronized void dismissDialog() {
+ // We may not have a dialog if we copied stuff fast enough. Again, note
+ // this function is synchronized!
+ if (mAlertDialog != null) {
+ mAlertDialog.dismiss();
+ mAlertDialog = null;
+ }
+ }
+
+ private void copyAssets() {
AssetManager assetManager = mActivity.getAssets();
- String[] assets;
+ Set<String> handledPaths = new HashSet<String>();
+ recurse(assetManager, handledPaths, "");
+ }
+
+ private void recurse(AssetManager assetManager, Set<String> handledPaths, String path) {
try {
- assets = assetManager.list(path);
+ String[] assets = assetManager.list(path);
if (assets == null) {
return;
}
// no assets inside, so a file
if (assets.length == 0) {
copyFile(path);
- }
- else {
+ handledPaths.add(path);
+ } else {
for (String asset : assets) {
if (asset.length() > 0) {
- recurse(toPath(path, asset));
+ String childPath = toPath(path, asset);
+ if (childPath != null) {
+ recurse(assetManager, handledPaths, childPath);
+ } else {
+ }
}
}
}
- } catch (IOException ex) {
- Log.e(LOG_TAG, "Could not access assets stream", ex);
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Error copying assets '" + path + "'", e);
}
}
@@ -105,25 +195,30 @@ class ConfigsManager {
* @return sanitized path
*/
private String toPath(String... args) {
- StringBuilder result = new StringBuilder();
- for (String item: args) {
- if (!item.isEmpty()) {
- if (result.length() != 0 && result.charAt(result.length() - 1) != '/') {
- result.append('/');
- }
- result.append(item);
- if (item.charAt(item.length() - 1) != '/') {
- result.append('/');
+ try {
+ StringBuilder result = new StringBuilder();
+ for (String item: args) {
+ if (!item.isEmpty()) {
+ if (result.length() != 0 && result.charAt(result.length() - 1) != '/') {
+ result.append('/');
+ }
+ result.append(item);
+ if (item.charAt(item.length() - 1) != '/') {
+ result.append('/');
+ }
}
}
- }
- // if last character is '/', then delete it
- if (result.length() != 0 && result.charAt(result.length() - 1) == '/') {
- result.deleteCharAt(result.length() - 1);
- }
+ // if last character is '/', then delete it
+ if (result.length() != 0 && result.charAt(result.length() - 1) == '/') {
+ result.deleteCharAt(result.length() - 1);
+ }
- return result.toString();
+ return result.toString();
+ } catch (Exception e) {
+ Log.e(LOG_TAG, "Error building path", e);
+ return null;
+ }
}
private void copyFile(String name) throws IOException {
@@ -146,11 +241,9 @@ class ConfigsManager {
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
- }
- catch (IOException e) {
- Log.w(LOG_TAG, "Could not copy file: " + name, e);
- }
- finally {
+ } catch (Exception e) {
+ Log.w(LOG_TAG, "Could not copy file '" + name + "'", e);
+ } finally {
if (in != null) {
in.close();
}
diff --git a/packaging/android/apk/src/org/krita/android/MainActivity.java b/packaging/android/apk/src/org/krita/android/MainActivity.java
index 59321dc408..08860855b0 100644
--- a/packaging/android/apk/src/org/krita/android/MainActivity.java
+++ b/packaging/android/apk/src/org/krita/android/MainActivity.java
@@ -54,7 +54,6 @@ public class MainActivity extends QtActivity {
Log.i(TAG, "TouchSlop: " + ViewConfiguration.get(this).getScaledTouchSlop());
Log.i(TAG, "LibsLoaded");
haveLibsLoaded = true;
- new ConfigsManager().handleAssets(this);
DonationHelper.getInstance();
}
@@ -185,6 +184,10 @@ public class MainActivity extends QtActivity {
public void onUserInteraction() {
}
+ public void copyAssets() {
+ new ConfigsManager(this).handleAssets();
+ }
+
public static int getLongPressTimeout() {
try {
return ViewConfiguration.get(QtNative.activity()).getLongPressTimeout();