From e0aa262de8b3b59f49ec0194fcdc31b21ca81902 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Fri, 3 May 2019 08:15:50 +0200 Subject: [PATCH] hide loading delay warning if document is loaded Signed-off-by: tobiasKaminsky --- .../ui/activity/ExternalSiteWebView.java | 7 +- .../ui/activity/RichDocumentsWebView.java | 68 ++----------- .../android/ui/asynctasks/LoadUrlTask.java | 96 +++++++++++++++++++ 3 files changed, 110 insertions(+), 61 deletions(-) create mode 100644 src/main/java/com/owncloud/android/ui/asynctasks/LoadUrlTask.java diff --git a/src/main/java/com/owncloud/android/ui/activity/ExternalSiteWebView.java b/src/main/java/com/owncloud/android/ui/activity/ExternalSiteWebView.java index a111caf98b..ee6e9228a5 100644 --- a/src/main/java/com/owncloud/android/ui/activity/ExternalSiteWebView.java +++ b/src/main/java/com/owncloud/android/ui/activity/ExternalSiteWebView.java @@ -42,6 +42,7 @@ import java.io.InputStream; import androidx.appcompat.app.ActionBar; import androidx.drawerlayout.widget.DrawerLayout; +import lombok.Getter; /** * This activity shows an URL as a web view @@ -55,11 +56,11 @@ public class ExternalSiteWebView extends FileActivity { private static final String TAG = ExternalSiteWebView.class.getSimpleName(); - private boolean showSidebar; protected boolean showToolbar = true; - private int menuItemId; - protected WebView webview; protected int webViewLayout = R.layout.externalsite_webview; + private int menuItemId; + @Getter protected WebView webview; + private boolean showSidebar; String url; @SuppressLint("SetJavaScriptEnabled") diff --git a/src/main/java/com/owncloud/android/ui/activity/RichDocumentsWebView.java b/src/main/java/com/owncloud/android/ui/activity/RichDocumentsWebView.java index 6c30c8540d..81a7dd53d8 100644 --- a/src/main/java/com/owncloud/android/ui/activity/RichDocumentsWebView.java +++ b/src/main/java/com/owncloud/android/ui/activity/RichDocumentsWebView.java @@ -30,10 +30,8 @@ import android.content.ActivityNotFoundException; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; -import android.os.AsyncTask; import android.os.Build; import android.os.Bundle; -import android.os.Handler; import android.text.TextUtils; import android.view.View; import android.webkit.JavascriptInterface; @@ -56,7 +54,7 @@ import com.owncloud.android.datamodel.ThumbnailsCacheManager; import com.owncloud.android.lib.common.operations.RemoteOperationResult; import com.owncloud.android.lib.common.utils.Log_OC; import com.owncloud.android.operations.RichDocumentsCreateAssetOperation; -import com.owncloud.android.operations.RichDocumentsUrlOperation; +import com.owncloud.android.ui.asynctasks.LoadUrlTask; import com.owncloud.android.ui.fragment.OCFileListFragment; import com.owncloud.android.utils.DisplayUtils; import com.owncloud.android.utils.MimeTypeUtil; @@ -64,14 +62,14 @@ import com.owncloud.android.utils.glide.CustomGlideStreamLoader; import org.parceler.Parcels; -import java.lang.ref.WeakReference; - import javax.inject.Inject; import androidx.annotation.RequiresApi; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.Unbinder; +import lombok.Getter; +import lombok.Setter; /** * Opens document for editing via Richdocuments app in a web view @@ -88,6 +86,7 @@ public class RichDocumentsWebView extends ExternalSiteWebView { private Unbinder unbinder; private OCFile file; + @Getter @Setter private Snackbar loadingSnackbar; public ValueCallback uploadMessage; @@ -182,7 +181,7 @@ public class RichDocumentsWebView extends ExternalSiteWebView { // load url in background url = getIntent().getStringExtra(EXTRA_URL); if (TextUtils.isEmpty(url)) { - new LoadUrl(this, getAccount()).execute(file.getLocalId()); + new LoadUrlTask(this, getAccount()).execute(file.getLocalId()); } else { webview.loadUrl(url); } @@ -334,7 +333,7 @@ public class RichDocumentsWebView extends ExternalSiteWebView { super.onDestroy(); } - private void closeView() { + public void closeView() { webview.destroy(); finish(); } @@ -344,6 +343,10 @@ public class RichDocumentsWebView extends ExternalSiteWebView { fileName.setVisibility(View.GONE); progressBar.setVisibility(View.GONE); webview.setVisibility(View.VISIBLE); + + if (loadingSnackbar != null) { + loadingSnackbar.dismiss(); + } } private class RichDocumentsMobileInterface { @@ -368,56 +371,5 @@ public class RichDocumentsWebView extends ExternalSiteWebView { } } - private static class LoadUrl extends AsyncTask { - private Account account; - private WeakReference richDocumentsWebViewWeakReference; - - LoadUrl(RichDocumentsWebView richDocumentsWebView, Account account) { - this.account = account; - this.richDocumentsWebViewWeakReference = new WeakReference<>(richDocumentsWebView); - } - - @Override - protected String doInBackground(String... fileId) { - if (richDocumentsWebViewWeakReference.get() == null) { - return ""; - } - RichDocumentsUrlOperation richDocumentsUrlOperation = new RichDocumentsUrlOperation(fileId[0]); - RemoteOperationResult result = richDocumentsUrlOperation.execute(account, - richDocumentsWebViewWeakReference.get()); - - if (!result.isSuccess()) { - return ""; - } - - return (String) result.getData().get(0); - } - - @Override - protected void onPostExecute(String url) { - RichDocumentsWebView richDocumentsWebView = richDocumentsWebViewWeakReference.get(); - - if (richDocumentsWebView == null) { - return; - } - - if (!url.isEmpty()) { - richDocumentsWebView.webview.loadUrl(url); - - new Handler().postDelayed(() -> { - if (richDocumentsWebView.webview.getVisibility() != View.VISIBLE) { - DisplayUtils.createSnackbar(richDocumentsWebView.findViewById(android.R.id.content), - R.string.timeout_richDocuments, Snackbar.LENGTH_INDEFINITE) - .setActionTextColor(richDocumentsWebView.getResources().getColor(R.color.white)) - .setAction(R.string.fallback_weblogin_back, v -> richDocumentsWebView.closeView()).show(); - } - }, 10 * 1000); - } else { - Toast.makeText(richDocumentsWebView.getApplicationContext(), - R.string.richdocuments_failed_to_load_document, Toast.LENGTH_LONG).show(); - richDocumentsWebView.finish(); - } - } - } } diff --git a/src/main/java/com/owncloud/android/ui/asynctasks/LoadUrlTask.java b/src/main/java/com/owncloud/android/ui/asynctasks/LoadUrlTask.java new file mode 100644 index 0000000000..0ff52c283d --- /dev/null +++ b/src/main/java/com/owncloud/android/ui/asynctasks/LoadUrlTask.java @@ -0,0 +1,96 @@ +/* + * Nextcloud Android client application + * + * @author Tobias Kaminsky + * Copyright (C) 2019 Tobias Kaminsky + * Copyright (C) 2019 Nextcloud GmbH + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.owncloud.android.ui.asynctasks; + +import android.accounts.Account; +import android.os.AsyncTask; +import android.os.Build; +import android.os.Handler; +import android.view.View; +import android.widget.Toast; + +import com.google.android.material.snackbar.Snackbar; +import com.owncloud.android.R; +import com.owncloud.android.lib.common.operations.RemoteOperationResult; +import com.owncloud.android.operations.RichDocumentsUrlOperation; +import com.owncloud.android.ui.activity.RichDocumentsWebView; +import com.owncloud.android.utils.DisplayUtils; + +import java.lang.ref.WeakReference; + +import androidx.annotation.RequiresApi; + +@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) +public class LoadUrlTask extends AsyncTask { + + private Account account; + private WeakReference richDocumentsWebViewWeakReference; + + public LoadUrlTask(RichDocumentsWebView richDocumentsWebView, Account account) { + this.account = account; + this.richDocumentsWebViewWeakReference = new WeakReference<>(richDocumentsWebView); + } + + @Override + protected String doInBackground(String... fileId) { + if (richDocumentsWebViewWeakReference.get() == null) { + return ""; + } + RichDocumentsUrlOperation richDocumentsUrlOperation = new RichDocumentsUrlOperation(fileId[0]); + RemoteOperationResult result = richDocumentsUrlOperation.execute(account, + richDocumentsWebViewWeakReference.get()); + + if (!result.isSuccess()) { + return ""; + } + + return (String) result.getData().get(0); + } + + @Override + protected void onPostExecute(String url) { + RichDocumentsWebView richDocumentsWebView = richDocumentsWebViewWeakReference.get(); + + if (richDocumentsWebView == null) { + return; + } + + if (!url.isEmpty()) { + richDocumentsWebView.getWebview().loadUrl(url); + + new Handler().postDelayed(() -> { + if (richDocumentsWebView.getWebview().getVisibility() != View.VISIBLE) { + Snackbar snackbar = DisplayUtils.createSnackbar(richDocumentsWebView.findViewById(android.R.id.content), + R.string.timeout_richDocuments, Snackbar.LENGTH_INDEFINITE) + .setActionTextColor(richDocumentsWebView.getResources().getColor(R.color.white)) + .setAction(R.string.fallback_weblogin_back, v -> richDocumentsWebView.closeView()); + + richDocumentsWebView.setLoadingSnackbar(snackbar); + snackbar.show(); + } + }, 10 * 1000); + } else { + Toast.makeText(richDocumentsWebView.getApplicationContext(), + R.string.richdocuments_failed_to_load_document, Toast.LENGTH_LONG).show(); + richDocumentsWebView.finish(); + } + } +}