diff --git a/app/src/main/java/org/nuclearfog/twidda/activity/MediaViewer.java b/app/src/main/java/org/nuclearfog/twidda/activity/MediaViewer.java index 3d96ba7f..e4b92f31 100644 --- a/app/src/main/java/org/nuclearfog/twidda/activity/MediaViewer.java +++ b/app/src/main/java/org/nuclearfog/twidda/activity/MediaViewer.java @@ -24,6 +24,8 @@ import org.nuclearfog.twidda.R; import org.nuclearfog.twidda.adapter.ImageAdapter; import org.nuclearfog.twidda.adapter.ImageAdapter.OnImageClickListener; import org.nuclearfog.twidda.backend.ImageLoader; +import org.nuclearfog.twidda.backend.engine.EngineException; +import org.nuclearfog.twidda.backend.helper.ErrorHandler; import org.nuclearfog.zoomview.ZoomView; import java.text.SimpleDateFormat; @@ -189,8 +191,12 @@ public class MediaViewer extends AppCompatActivity implements OnImageClickListen } - public void onError() { - Toast.makeText(this, R.string.error_image_download, Toast.LENGTH_SHORT).show(); + public void onError(@Nullable EngineException err) { + if (err != null) { + ErrorHandler.handleFailure(getApplicationContext(), err); + } else { + Toast.makeText(getApplicationContext(), R.string.error_image_download, Toast.LENGTH_SHORT).show(); + } finish(); } diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/ImageLoader.java b/app/src/main/java/org/nuclearfog/twidda/backend/ImageLoader.java index ceff7634..e20fb6e7 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/ImageLoader.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/ImageLoader.java @@ -5,20 +5,25 @@ import android.graphics.BitmapFactory; import android.os.AsyncTask; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import org.nuclearfog.twidda.activity.MediaViewer; +import org.nuclearfog.twidda.backend.engine.EngineException; +import org.nuclearfog.twidda.backend.engine.TwitterEngine; -import java.io.InputStream; import java.lang.ref.WeakReference; -import java.net.URL; /** * Background task to load images from twitter and storage + * * @see MediaViewer */ public class ImageLoader extends AsyncTask { + @Nullable + private EngineException err; private WeakReference callback; + private TwitterEngine mTwitter; /** @@ -28,6 +33,7 @@ public class ImageLoader extends AsyncTask { */ public ImageLoader(@NonNull MediaViewer callback) { this.callback = new WeakReference<>(callback); + mTwitter = TwitterEngine.getInstance(callback); } @@ -37,9 +43,7 @@ public class ImageLoader extends AsyncTask { for (String link : links) { Bitmap image; if (link.startsWith("https://")) { - URL url = new URL(link); - InputStream stream = url.openStream(); - image = BitmapFactory.decodeStream(stream); + image = mTwitter.getImage(link); } else { image = BitmapFactory.decodeFile(link); } @@ -48,6 +52,8 @@ public class ImageLoader extends AsyncTask { } } return true; + } catch (EngineException err) { + this.err = err; } catch (Exception exception) { exception.printStackTrace(); } @@ -68,7 +74,7 @@ public class ImageLoader extends AsyncTask { if (success) { callback.get().onSuccess(); } else { - callback.get().onError(); + callback.get().onError(err); } } } diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/engine/EngineException.java b/app/src/main/java/org/nuclearfog/twidda/backend/engine/EngineException.java index a1ddc3da..dc6b50d2 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/engine/EngineException.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/engine/EngineException.java @@ -8,9 +8,6 @@ import static org.nuclearfog.twidda.backend.engine.EngineException.ErrorType.RES public class EngineException extends Exception { - static final int FILENOTFOUND = 600; - static final int TOKENNOTSET = 601; - public enum ErrorType { RATE_LIMIT_EX, USER_NOT_FOUND, @@ -26,15 +23,23 @@ public class EngineException extends Exception { NO_MEDIA_FOUND, NO_LINK_DEFINED, NO_CONNECTION, + IMAGE_NOT_LOADED, ERROR_NOT_DEFINED } - private final ErrorType errorType; + enum InternalErrorType { + FILENOTFOUND, + TOKENNOTSET, + BITMAP_FAILURE + } + + private ErrorType errorType; private int retryAfter; /** * Constructor for Twitter4J errors + * * @param error Twitter4J Exception */ EngineException(TwitterException error) { @@ -105,9 +110,10 @@ public class EngineException extends Exception { /** * Constructor for non Twitter4J errors + * * @param errorCode custom error code */ - EngineException(int errorCode) { + EngineException(InternalErrorType errorCode) { switch (errorCode) { case FILENOTFOUND: errorType = ErrorType.NO_MEDIA_FOUND; @@ -117,6 +123,9 @@ public class EngineException extends Exception { errorType = ErrorType.NO_LINK_DEFINED; break; + case BITMAP_FAILURE: + errorType = ErrorType.IMAGE_NOT_LOADED; + default: errorType = ErrorType.ERROR_NOT_DEFINED; break; diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/engine/TwitterEngine.java b/app/src/main/java/org/nuclearfog/twidda/backend/engine/TwitterEngine.java index 51cabd20..5cfcdc82 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/engine/TwitterEngine.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/engine/TwitterEngine.java @@ -1,6 +1,8 @@ package org.nuclearfog.twidda.backend.engine; import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import androidx.annotation.Nullable; @@ -21,8 +23,11 @@ import org.nuclearfog.twidda.database.GlobalSettings; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.net.Authenticator; -import java.net.PasswordAuthentication; +import java.io.IOException; +import java.io.InputStream; +import java.net.InetSocketAddress; +import java.net.Proxy; +import java.net.URL; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; @@ -83,39 +88,10 @@ public class TwitterEngine { } } TwitterFactory factory = new TwitterFactory(builder.build()); - if (aToken != null) + if (aToken != null) { twitter = factory.getInstance(aToken); - else + } else { twitter = factory.getInstance(); - initJVMProxy(); - } - - /** - * Initialize App proxy - */ - private void initJVMProxy() { - try { - if (settings.isProxyServerSet()) { - System.setProperty("https.proxyHost", settings.getProxyHost()); - System.setProperty("https.proxyPort", settings.getProxyPort()); - if (settings.isProxyLoginSet()) { - System.setProperty("https.proxyUser", settings.getProxyUser()); - System.setProperty("https.proxyPassword", settings.getProxyPass()); - Authenticator.setDefault(new Authenticator() { - @Override - protected PasswordAuthentication getPasswordAuthentication() { - return new PasswordAuthentication(settings.getProxyUser(), settings.getProxyPass().toCharArray()); - } - }); - } - } else { - System.clearProperty("https.proxyHost"); - System.clearProperty("https.proxyPort"); - System.clearProperty("https.proxyUser"); - System.clearProperty("https.proxyPassword"); - } - } catch (SecurityException sErr) { - sErr.printStackTrace(); } } @@ -185,7 +161,7 @@ public class TwitterEngine { twitterID = twitter.getId(); settings.setConnection(key1, key2, twitterID); } else { - throw new EngineException(EngineException.TOKENNOTSET); + throw new EngineException(EngineException.InternalErrorType.TOKENNOTSET); } } catch (TwitterException err) { throw new EngineException(err); @@ -1065,6 +1041,33 @@ public class TwitterEngine { } + /** + * download image from Twitter + * + * @param link link of the image + * @return bitmap image + * @throws EngineException if image loading failed + */ + public Bitmap getImage(String link) throws EngineException { + try { + Proxy proxy; + URL url = new URL(link); + if (settings.isProxyServerSet()) { + String proxyAddress = settings.getProxyHost(); + int proxyPort = Integer.parseInt(settings.getProxyPort()); + InetSocketAddress socket = new InetSocketAddress(proxyAddress, proxyPort); + proxy = new Proxy(Proxy.Type.HTTP, socket); + } else { + proxy = Proxy.NO_PROXY; + } + InputStream stream = url.openConnection(proxy).getInputStream(); + return BitmapFactory.decodeStream(stream); + } catch (IOException err) { + throw new EngineException(EngineException.InternalErrorType.BITMAP_FAILURE); + } + } + + /** * convert #twitter4j.User to TwitterUser List * @@ -1131,7 +1134,7 @@ public class TwitterEngine { } catch (TwitterException err) { throw new EngineException(err); } catch (FileNotFoundException err) { - throw new EngineException(EngineException.FILENOTFOUND); + throw new EngineException(EngineException.InternalErrorType.FILENOTFOUND); } } @@ -1151,7 +1154,7 @@ public class TwitterEngine { } catch (TwitterException err) { throw new EngineException(err); } catch (FileNotFoundException err) { - throw new EngineException(EngineException.FILENOTFOUND); + throw new EngineException(EngineException.InternalErrorType.FILENOTFOUND); } } } \ No newline at end of file diff --git a/app/src/main/java/org/nuclearfog/twidda/backend/helper/ErrorHandler.java b/app/src/main/java/org/nuclearfog/twidda/backend/helper/ErrorHandler.java index 44d41506..5cebc2de 100644 --- a/app/src/main/java/org/nuclearfog/twidda/backend/helper/ErrorHandler.java +++ b/app/src/main/java/org/nuclearfog/twidda/backend/helper/ErrorHandler.java @@ -87,6 +87,10 @@ public abstract class ErrorHandler { Toast.makeText(context, R.string.error_connection_failed, Toast.LENGTH_SHORT).show(); break; + case IMAGE_NOT_LOADED: + Toast.makeText(context, R.string.error_image_loading, Toast.LENGTH_SHORT).show(); + break; + case ERROR_NOT_DEFINED: if (error.getMessage() != null) Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/res/layout/item_tweet.xml b/app/src/main/res/layout/item_tweet.xml index d1d1b42a..154518e5 100644 --- a/app/src/main/res/layout/item_tweet.xml +++ b/app/src/main/res/layout/item_tweet.xml @@ -47,9 +47,9 @@ android:id="@+id/username" android:layout_width="0dp" android:layout_height="wrap_content" - android:drawablePadding="@dimen/padding_drawable" android:layout_gravity="start" - android:layout_weight="2" + android:layout_weight="8" + android:drawablePadding="@dimen/padding_drawable" android:gravity="start" android:singleLine="true" /> @@ -58,8 +58,9 @@ android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" - android:layout_weight="1" + android:layout_weight="3" android:gravity="end" + android:singleLine="true" android:textAlignment="gravity" android:textSize="@dimen/textsize_date" /> diff --git a/app/src/main/res/values-de-rDE/strings.xml b/app/src/main/res/values-de-rDE/strings.xml index 2304e63f..99c52444 100644 --- a/app/src/main/res/values-de-rDE/strings.xml +++ b/app/src/main/res/values-de-rDE/strings.xml @@ -146,4 +146,5 @@ Video konnte nicht hinzugefĆ¼gt werden! Datei nicht gefunden! Mehr laden + Bild konnte nicht geladen werden! \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 30121fbc..7899d49b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -149,4 +149,5 @@ Twitter4J Badge File not found! Load more + Could not load image! \ No newline at end of file