Skip to content

Commit

Permalink
Merge pull request #7 from aerovulpe/dev-mess
Browse files Browse the repository at this point in the history
Dev mess
  • Loading branch information
aerovulpe committed Jun 9, 2015
2 parents ccb404a + 1ef18ee commit 69f1d70
Show file tree
Hide file tree
Showing 9 changed files with 182 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,14 @@ public void run() {
Request.removeAlbumRequestData(BaseActivity.this, accountID);
}
}, 5000);
getContentResolver().delete(CrawlerContract.AccountEntry.CONTENT_URI,
CrawlerContract.AccountEntry.COLUMN_ACCOUNT_ID + " == '" +
accountID + "'", null);
new Thread(new Runnable() {
@Override
public void run() {
getContentResolver().delete(CrawlerContract.AccountEntry.CONTENT_URI,
CrawlerContract.AccountEntry.COLUMN_ACCOUNT_ID + " == '" +
accountID + "'", null);
}
}).start();
}
});
builder.create().show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public void sharePhoto(Photo photo) {
public Uri savePhoto(Photo photo) {
Bitmap bitmap = ImageLoader.getInstance().loadImageSync(photo.getImageUrl());
return bitmap != null ? AndroidUtils.savePicture(getActivity(),
bitmap, photo.getName(),
bitmap, photo.getImageUrl(), photo.getName(),
photo.getTitle(), photo.getDescription()) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public boolean onPreferenceChange(Preference preference, Object newValue) {
public boolean onPreferenceChange(Preference preference, Object newValue) {
try {
int currentCacheValue = Integer.parseInt(newValue.toString());
if (currentCacheValue < 10) {
if (currentCacheValue < 10 || currentCacheValue > 2000) {
Toast.makeText(activity, getString(R.string.cache_size_guide),
Toast.LENGTH_SHORT).show();
return false;
Expand Down
27 changes: 10 additions & 17 deletions app/src/main/java/me/aerovulpe/crawler/ui/GifDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
Expand Down Expand Up @@ -253,24 +252,18 @@ public Bitmap getNextFrame() throws IllegalStateException {
* @param is containing GIF file.
* @return read status code (0 = no errors)
*/
public int read(InputStream is, int contentLength) {
public int read(InputStream is, int contentLength) throws IOException {
if (is != null) {
try {
int capacity = (contentLength > 0) ? (contentLength + 4096) : 4096;
ByteArrayOutputStream buffer = new ByteArrayOutputStream(capacity);
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();

read(buffer.toByteArray());
} catch (InterruptedIOException e) {
Log.i(TAG, "Show is over");
} catch (IOException e) {
Log.w(TAG, "Error reading data from stream", e);
int capacity = (contentLength > 0) ? (contentLength + 4096) : 4096;
ByteArrayOutputStream buffer = new ByteArrayOutputStream(capacity);
int nRead;
byte[] data = new byte[16384];
while ((nRead = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();

read(buffer.toByteArray());
} else {
status = STATUS_OPEN_ERROR;
}
Expand Down
130 changes: 74 additions & 56 deletions app/src/main/java/me/aerovulpe/crawler/ui/TouchImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
Expand All @@ -29,6 +28,7 @@
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
Expand All @@ -41,6 +41,8 @@
import android.widget.Scroller;

import java.io.IOException;
import java.io.InterruptedIOException;
import java.lang.ref.WeakReference;
import java.net.URL;

import me.aerovulpe.crawler.fragments.SettingsFragment;
Expand Down Expand Up @@ -95,7 +97,7 @@ public class TouchImageView extends ImageView {
private GestureDetector.OnDoubleTapListener doubleTapListener = null;
private OnTouchListener userTouchListener = null;
private OnTouchImageViewListener touchImageViewListener = null;
private Thread mGifThread;
private GifThread mGifThread;

public TouchImageView(Context context) {
super(context);
Expand Down Expand Up @@ -277,18 +279,15 @@ public void onRestoreInstanceState(Parcelable state) {
}

@Override
protected void onDraw(Canvas canvas) {
protected void onDraw(@NonNull Canvas canvas) {
onDrawReady = true;
imageRenderedAtLeastOnce = true;
if (delayedZoomVariables != null) {
setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType);
setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX,
delayedZoomVariables.focusY, delayedZoomVariables.scaleType);
delayedZoomVariables = null;
}
try {
super.onDraw(canvas);
} catch (RuntimeException e) {
Log.w(DEBUG, "Bitmap was recycled.");
}
super.onDraw(canvas);
}

@Override
Expand Down Expand Up @@ -1297,63 +1296,82 @@ public ZoomVariables(float scale, float focusX, float focusY, ScaleType scaleTyp
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mGifThread != null)
if (mGifThread != null) {
mGifThread.interrupt();
Drawable drawable = getDrawable();
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
Bitmap bitmap = bitmapDrawable.getBitmap();
if (bitmap != null && !bitmap.isRecycled())
bitmap.recycle();
}
}

public void playGif(final String url) {
public void playGif(String url) {
try {
final Handler handler = new Handler();
mGifThread = new Thread(new Runnable() {
public void run() {
boolean isConnectedToWifi = AndroidUtils.isConnectedToWifi(getContext());
boolean isConnectedToWired = AndroidUtils.isConnectedToWired(getContext());
if (!isConnectedToWifi && !isConnectedToWired &&
!SettingsFragment.downloadOffWifi(getContext()))
return;
try {
GifDecoder gifDecoder = new GifDecoder();
gifDecoder.read(new URL(url).openStream(), 0);
final int frameCount = gifDecoder.getFrameCount();
while (!Thread.currentThread().isInterrupted()) {
for (int i = 0; i < frameCount && !Thread.currentThread()
.isInterrupted(); i++) {
final Bitmap nextBitmap = gifDecoder.getNextFrame();
int delay = gifDecoder.getDelay(i);
delay = delay != 0 ? delay : 33;
handler.post(new Runnable() {
public void run() {
if (nextBitmap != null && !nextBitmap.isRecycled()) {
TouchImageView.this.setImageBitmap(nextBitmap);
}
}
});
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
Log.i(DEBUG, "Show is over");
return;
Handler handler = new Handler();
mGifThread = new GifThread(this, handler, url);
mGifThread.start();
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
}
}

static class GifThread extends Thread {
WeakReference<TouchImageView> mTouchImageViewRef;
Handler mHandler;
String mUrl;

public GifThread(TouchImageView touchImageView, Handler handler, String url) {
mTouchImageViewRef = new WeakReference<>(touchImageView);
mHandler = handler;
mUrl = url;
}

@Override
public void run() {
if (mTouchImageViewRef.get() == null)
return;
else {
Context context = mTouchImageViewRef.get().getContext();
boolean isConnectedToWifi = AndroidUtils.isConnectedToWifi(context);
boolean isConnectedToWired = AndroidUtils.isConnectedToWired(context);
if (!isConnectedToWifi && !isConnectedToWired &&
!SettingsFragment.downloadOffWifi(context))
return;
}

try {
GifDecoder gifDecoder = new GifDecoder();
gifDecoder.read(new URL(mUrl).openStream(), 0);
final int frameCount = gifDecoder.getFrameCount();
while (!Thread.currentThread().isInterrupted()
&& mTouchImageViewRef.get() != null) {
for (int i = 0; i < frameCount; i++) {
if (Thread.currentThread().isInterrupted()
|| mTouchImageViewRef.get() == null)
return;

final Bitmap nextBitmap = gifDecoder.getNextFrame();
int delay = gifDecoder.getDelay(i);
delay = delay != 0 ? delay : 33;
mHandler.post(new Runnable() {
public void run() {
if (nextBitmap != null && !nextBitmap.isRecycled()) {
TouchImageView touchImageView;
if ((touchImageView = mTouchImageViewRef.get()) != null)
touchImageView.setImageBitmap(nextBitmap);
}
}
});
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
return;
}
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
Log.w(DEBUG, "Bitmap was recycled.");
}
}
});
mGifThread.start();
} catch (OutOfMemoryError e) {
e.printStackTrace();
System.gc();
} catch (InterruptedIOException ignored) {
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
Log.w(DEBUG, "Bitmap was not ready.");
}
}
}
}
Loading

0 comments on commit 69f1d70

Please sign in to comment.