diff options
author | Christian Schneppe <christian@pix-art.de> | 2019-09-07 23:58:56 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2019-09-07 23:58:56 +0200 |
commit | fce79f08c182bcc3569f52799ba1e058a10597c4 (patch) | |
tree | 4eca8441c415a93ed17b0324d2203d8a758df36b | |
parent | 41aecb003dacd67d45ff73524a352472f470a978 (diff) |
improve webpreviews during offline times
4 files changed, 45 insertions, 29 deletions
diff --git a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java index caa428092..f7b874606 100644 --- a/src/main/java/de/pixart/messenger/services/XmppConnectionService.java +++ b/src/main/java/de/pixart/messenger/services/XmppConnectionService.java @@ -201,6 +201,7 @@ public class XmppConnectionService extends Service { private final SerialSingleThreadExecutor mNotificationExecutor = new SerialSingleThreadExecutor("NotificationExecutor"); public final SerialSingleThreadExecutor mUploadExecutor = new SerialSingleThreadExecutor("FileUpload"); public final SerialSingleThreadExecutor mDownloadExecutor = new SerialSingleThreadExecutor("FileDownload"); + public final SerialSingleThreadExecutor mWebPreviewExecutor = new SerialSingleThreadExecutor("WebPreview"); private final ReplacingTaskManager mRosterSyncTaskManager = new ReplacingTaskManager(); private final IBinder mBinder = new XmppConnectionBinder(); private final List<Conversation> conversations = new CopyOnWriteArrayList<>(); diff --git a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java index e87554818..d60478bb1 100644 --- a/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java +++ b/src/main/java/de/pixart/messenger/ui/adapter/MessageAdapter.java @@ -691,6 +691,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie viewHolder.audioPlayer.setVisibility(View.GONE); viewHolder.image.setVisibility(View.GONE); viewHolder.gifImage.setVisibility(View.GONE); + viewHolder.download_button.setVisibility(View.GONE); Editable body = new SpannableStringBuilder(message.getBody()); final boolean dataSaverDisabled = activity.xmppConnectionService.isDataSaverDisabled(); viewHolder.richlinkview.setVisibility(View.VISIBLE); @@ -715,7 +716,7 @@ public class MessageAdapter extends ArrayAdapter<Message> implements CopyTextVie } else { weburl = "http://" + removeTrailingBracket(url); } - viewHolder.richlinkview.setLink(weburl, message.getUuid(), dataSaverDisabled, new RichPreview.ViewListener() { + viewHolder.richlinkview.setLink(weburl, message.getUuid(), dataSaverDisabled, activity.xmppConnectionService, new RichPreview.ViewListener() { @Override public void onSuccess(boolean status) { diff --git a/src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java b/src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java index 110665001..bffc48473 100644 --- a/src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java +++ b/src/main/java/de/pixart/messenger/ui/widget/RichLinkView.java @@ -18,6 +18,7 @@ import android.widget.Toast; import com.squareup.picasso.Picasso; import de.pixart.messenger.R; +import de.pixart.messenger.services.XmppConnectionService; import de.pixart.messenger.utils.MetaData; import de.pixart.messenger.utils.RichPreview; @@ -163,7 +164,7 @@ public class RichLinkView extends RelativeLayout { return meta; } - public void setLink(final String url, final String filename, final boolean dataSaverDisabled, final RichPreview.ViewListener viewListener) { + public void setLink(final String url, final String filename, final boolean dataSaverDisabled, final XmppConnectionService mXmppConnectionService, final RichPreview.ViewListener viewListener) { main_url = url; RichPreview richPreview = new RichPreview(new RichPreview.ResponseListener() { @Override @@ -180,6 +181,6 @@ public class RichLinkView extends RelativeLayout { viewListener.onError(e); } }); - richPreview.getPreview(url, filename, context); + richPreview.getPreview(url, filename, context, mXmppConnectionService); } } diff --git a/src/main/java/de/pixart/messenger/utils/RichPreview.java b/src/main/java/de/pixart/messenger/utils/RichPreview.java index 7a2d2d723..0759b185b 100644 --- a/src/main/java/de/pixart/messenger/utils/RichPreview.java +++ b/src/main/java/de/pixart/messenger/utils/RichPreview.java @@ -24,6 +24,7 @@ import java.net.URLEncoder; import java.util.regex.Pattern; import de.pixart.messenger.Config; +import de.pixart.messenger.services.XmppConnectionService; /** * Created by ponna on 16-01-2018. @@ -43,14 +44,21 @@ public class RichPreview { metaData = new MetaData(); } - public void getPreview(final String url, final String filename, final Context context) { + public void getPreview(final String url, final String filename, final Context context, final XmppConnectionService mXmppConnectionService) { this.url = url; this.filename = filename; this.context = context; - new getData().execute(); + mXmppConnectionService.mWebPreviewExecutor.execute(() -> { + new getData(mXmppConnectionService).execute(); + }); } private class getData extends AsyncTask<Void, Void, Void> { + XmppConnectionService service; + + getData(XmppConnectionService xmppConnectionService) { + this.service = xmppConnectionService; + } @Override protected Void doInBackground(Void... params) { @@ -68,29 +76,33 @@ public class RichPreview { } String string = sb.substring(sb.indexOf("{"), sb.lastIndexOf("}") + 1); JSONObject json = new JSONObject(string); - if (json.has("url")) { - metaData.setUrl(json.getString("url")); - } - if (json.has("imageurl")) { - metaData.setImageurl(json.getString("imageurl")); - } - if (json.has("title")) { - metaData.setTitle(json.getString("title")); - } - if (json.has("description")) { - metaData.setDescription(json.getString("description")); - } - if (json.has("sitename")) { - metaData.setSitename(json.getString("sitename")); - } - if (json.has("mediatype")) { - metaData.setMediatype(json.getString("mediatype")); - } - if (json.has("favicon")) { - metaData.setFavicon(json.getString("favicon")); + if (json.has("offline") && json.getString("offline").equals("false")) { + if (json.has("url")) { + metaData.setUrl(json.getString("url")); + } + if (json.has("imageurl")) { + metaData.setImageurl(json.getString("imageurl")); + } + if (json.has("title")) { + metaData.setTitle(json.getString("title")); + } + if (json.has("description")) { + metaData.setDescription(json.getString("description")); + } + if (json.has("sitename")) { + metaData.setSitename(json.getString("sitename")); + } + if (json.has("mediatype")) { + metaData.setMediatype(json.getString("mediatype")); + } + if (json.has("favicon")) { + metaData.setFavicon(json.getString("favicon")); + } + } else { + retrieveMeta(url, context, service); } } catch (Exception e) { - retrieveMeta(url, context); + retrieveMeta(url, context, service); e.printStackTrace(); } finally { try { @@ -133,7 +145,7 @@ public class RichPreview { } } - private void saveMeta(MetaData metaData, Context context) { + private void saveMeta(MetaData metaData, Context context, final XmppConnectionService xmppConnectionService) { final File file = new File(context.getCacheDir(), RICH_LINK_METADATA + File.separator + filename); file.getParentFile().mkdirs(); FileOutputStream fos = null; @@ -150,6 +162,7 @@ public class RichPreview { json.put("sitename", metaData.getSitename()); json.put("mediatype", metaData.getMediatype()); json.put("favicon", metaData.getFavicon()); + json.put("offline", !xmppConnectionService.hasInternetConnection()); oos.writeObject(json.toString()); } catch (Exception e) { e.printStackTrace(); @@ -165,7 +178,7 @@ public class RichPreview { } } - private void retrieveMeta(String url, Context context) { + private void retrieveMeta(String url, Context context, XmppConnectionService xmppConnectionService) { Document doc = null; try { doc = Jsoup.connect(url) @@ -280,7 +293,7 @@ public class RichPreview { metaData.setUrl(uri != null ? uri.getHost() : null); } } - saveMeta(metaData, context); + saveMeta(metaData, context, xmppConnectionService); } public interface ResponseListener { |