mirror of
https://github.com/nuclearfog/Shitter.git
synced 2025-01-30 19:05:02 +01:00
removed JVM proxy, added proxy for image loader, layout fix
This commit is contained in:
parent
380958165a
commit
117c89b81d
@ -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();
|
||||
}
|
||||
|
||||
|
@ -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<String, Bitmap, Boolean> {
|
||||
|
||||
@Nullable
|
||||
private EngineException err;
|
||||
private WeakReference<MediaViewer> callback;
|
||||
private TwitterEngine mTwitter;
|
||||
|
||||
|
||||
/**
|
||||
@ -28,6 +33,7 @@ public class ImageLoader extends AsyncTask<String, Bitmap, Boolean> {
|
||||
*/
|
||||
public ImageLoader(@NonNull MediaViewer callback) {
|
||||
this.callback = new WeakReference<>(callback);
|
||||
mTwitter = TwitterEngine.getInstance(callback);
|
||||
}
|
||||
|
||||
|
||||
@ -37,9 +43,7 @@ public class ImageLoader extends AsyncTask<String, Bitmap, Boolean> {
|
||||
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<String, Bitmap, Boolean> {
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} catch (EngineException err) {
|
||||
this.err = err;
|
||||
} catch (Exception exception) {
|
||||
exception.printStackTrace();
|
||||
}
|
||||
@ -68,7 +74,7 @@ public class ImageLoader extends AsyncTask<String, Bitmap, Boolean> {
|
||||
if (success) {
|
||||
callback.get().onSuccess();
|
||||
} else {
|
||||
callback.get().onError();
|
||||
callback.get().onError(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
@ -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" />
|
||||
|
||||
|
@ -146,4 +146,5 @@
|
||||
<string name="info_cant_add_video">Video konnte nicht hinzugefügt werden!</string>
|
||||
<string name="error_file_not_found">Datei nicht gefunden!</string>
|
||||
<string name="item_load_more">Mehr laden</string>
|
||||
<string name="error_image_loading">Bild konnte nicht geladen werden!</string>
|
||||
</resources>
|
@ -149,4 +149,5 @@
|
||||
<string name="badge_twitter4j" translatable="false">Twitter4J Badge</string>
|
||||
<string name="error_file_not_found">File not found!</string>
|
||||
<string name="item_load_more">Load more</string>
|
||||
<string name="error_image_loading">Could not load image!</string>
|
||||
</resources>
|
Loading…
x
Reference in New Issue
Block a user