Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor custom url downloaders. This will allow for the per download…
…er specific enabling/disabling of caching.
- Loading branch information
Showing
13 changed files
with
288 additions
and
140 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
UrlImageViewHelper/src/com/koushikdutta/urlimageviewhelper/ContactContentUrlDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,47 @@ | |||
package com.koushikdutta.urlimageviewhelper; | |||
|
|||
import java.io.InputStream; | |||
|
|||
import android.content.ContentResolver; | |||
import android.content.Context; | |||
import android.net.Uri; | |||
import android.os.AsyncTask; | |||
import android.provider.ContactsContract; | |||
|
|||
public class ContactContentUrlDownloader implements UrlDownloader { | |||
@Override | |||
public void download(final Context context, final String url, final String filename, final UrlDownloaderCallback callback, final Runnable completion) { | |||
final AsyncTask<Void, Void, Void> downloader = new AsyncTask<Void, Void, Void>() { | |||
@Override | |||
protected Void doInBackground(final Void... params) { | |||
try { | |||
final ContentResolver cr = context.getContentResolver(); | |||
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, Uri.parse(url)); | |||
callback.onDownloadComplete(ContactContentUrlDownloader.this, is, null); | |||
return null; | |||
} | |||
catch (final Throwable e) { | |||
e.printStackTrace(); | |||
return null; | |||
} | |||
} | |||
|
|||
@Override | |||
protected void onPostExecute(final Void result) { | |||
completion.run(); | |||
} | |||
}; | |||
|
|||
UrlImageViewHelper.executeTask(downloader); | |||
} | |||
|
|||
@Override | |||
public boolean allowCache() { | |||
return false; | |||
} | |||
|
|||
@Override | |||
public boolean canDownloadUrl(String url) { | |||
return url.startsWith(ContactsContract.Contacts.CONTENT_URI.toString()); | |||
} | |||
} |
46 changes: 46 additions & 0 deletions
46
UrlImageViewHelper/src/com/koushikdutta/urlimageviewhelper/ContentUrlDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,46 @@ | |||
package com.koushikdutta.urlimageviewhelper; | |||
|
|||
import java.io.InputStream; | |||
|
|||
import android.content.ContentResolver; | |||
import android.content.Context; | |||
import android.net.Uri; | |||
import android.os.AsyncTask; | |||
|
|||
public class ContentUrlDownloader implements UrlDownloader { | |||
@Override | |||
public void download(final Context context, final String url, final String filename, final UrlDownloaderCallback callback, final Runnable completion) { | |||
final AsyncTask<Void, Void, Void> downloader = new AsyncTask<Void, Void, Void>() { | |||
@Override | |||
protected Void doInBackground(final Void... params) { | |||
try { | |||
final ContentResolver cr = context.getContentResolver(); | |||
InputStream is = cr.openInputStream(Uri.parse(url)); | |||
callback.onDownloadComplete(ContentUrlDownloader.this, is, null); | |||
return null; | |||
} | |||
catch (final Throwable e) { | |||
e.printStackTrace(); | |||
return null; | |||
} | |||
} | |||
|
|||
@Override | |||
protected void onPostExecute(final Void result) { | |||
completion.run(); | |||
} | |||
}; | |||
|
|||
UrlImageViewHelper.executeTask(downloader); | |||
} | |||
|
|||
@Override | |||
public boolean allowCache() { | |||
return false; | |||
} | |||
|
|||
@Override | |||
public boolean canDownloadUrl(String url) { | |||
return url.startsWith(ContentResolver.SCHEME_CONTENT); | |||
} | |||
} |
43 changes: 43 additions & 0 deletions
43
UrlImageViewHelper/src/com/koushikdutta/urlimageviewhelper/FileUrlDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,43 @@ | |||
package com.koushikdutta.urlimageviewhelper; | |||
|
|||
import java.io.File; | |||
import java.net.URI; | |||
|
|||
import android.content.Context; | |||
import android.os.AsyncTask; | |||
|
|||
public class FileUrlDownloader implements UrlDownloader { | |||
@Override | |||
public void download(final Context context, final String url, final String filename, final UrlDownloaderCallback callback, final Runnable completion) { | |||
final AsyncTask<Void, Void, Void> downloader = new AsyncTask<Void, Void, Void>() { | |||
@Override | |||
protected Void doInBackground(final Void... params) { | |||
try { | |||
callback.onDownloadComplete(FileUrlDownloader.this, null, new File(new URI(url)).getAbsolutePath()); | |||
return null; | |||
} | |||
catch (final Throwable e) { | |||
e.printStackTrace(); | |||
return null; | |||
} | |||
} | |||
|
|||
@Override | |||
protected void onPostExecute(final Void result) { | |||
completion.run(); | |||
} | |||
}; | |||
|
|||
UrlImageViewHelper.executeTask(downloader); | |||
} | |||
|
|||
@Override | |||
public boolean allowCache() { | |||
return false; | |||
} | |||
|
|||
@Override | |||
public boolean canDownloadUrl(String url) { | |||
return url.startsWith("file:/"); | |||
} | |||
} |
89 changes: 89 additions & 0 deletions
89
UrlImageViewHelper/src/com/koushikdutta/urlimageviewhelper/HttpUrlDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,89 @@ | |||
package com.koushikdutta.urlimageviewhelper; | |||
|
|||
import java.io.FileOutputStream; | |||
import java.io.InputStream; | |||
import java.net.HttpURLConnection; | |||
import java.net.URL; | |||
import java.util.ArrayList; | |||
|
|||
import org.apache.http.NameValuePair; | |||
|
|||
import android.content.Context; | |||
import android.os.AsyncTask; | |||
|
|||
import com.koushikdutta.urlimageviewhelper.UrlImageViewHelper.RequestPropertiesCallback; | |||
|
|||
public class HttpUrlDownloader implements UrlDownloader { | |||
private RequestPropertiesCallback mRequestPropertiesCallback; | |||
|
|||
public RequestPropertiesCallback getRequestPropertiesCallback() { | |||
return mRequestPropertiesCallback; | |||
} | |||
|
|||
public void setRequestPropertiesCallback(final RequestPropertiesCallback callback) { | |||
mRequestPropertiesCallback = callback; | |||
} | |||
|
|||
|
|||
@Override | |||
public void download(final Context context, final String url, final String filename, final UrlDownloaderCallback callback, final Runnable completion) { | |||
final AsyncTask<Void, Void, Void> downloader = new AsyncTask<Void, Void, Void>() { | |||
@Override | |||
protected Void doInBackground(final Void... params) { | |||
try { | |||
InputStream is = null; | |||
|
|||
String thisUrl = url; | |||
HttpURLConnection urlConnection; | |||
while (true) { | |||
final URL u = new URL(thisUrl); | |||
urlConnection = (HttpURLConnection)u.openConnection(); | |||
urlConnection.setInstanceFollowRedirects(true); | |||
|
|||
if (mRequestPropertiesCallback != null) { | |||
final ArrayList<NameValuePair> props = mRequestPropertiesCallback.getHeadersForRequest(context, url); | |||
if (props != null) { | |||
for (final NameValuePair pair: props) { | |||
urlConnection.addRequestProperty(pair.getName(), pair.getValue()); | |||
} | |||
} | |||
} | |||
|
|||
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_MOVED_TEMP && urlConnection.getResponseCode() != HttpURLConnection.HTTP_MOVED_PERM) | |||
break; | |||
thisUrl = urlConnection.getHeaderField("Location"); | |||
} | |||
|
|||
if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { | |||
UrlImageViewHelper.clog("Response Code: " + urlConnection.getResponseCode()); | |||
return null; | |||
} | |||
is = urlConnection.getInputStream(); | |||
callback.onDownloadComplete(HttpUrlDownloader.this, is, null); | |||
return null; | |||
} | |||
catch (final Throwable e) { | |||
e.printStackTrace(); | |||
return null; | |||
} | |||
} | |||
|
|||
@Override | |||
protected void onPostExecute(final Void result) { | |||
completion.run(); | |||
} | |||
}; | |||
|
|||
UrlImageViewHelper.executeTask(downloader); | |||
} | |||
|
|||
@Override | |||
public boolean allowCache() { | |||
return true; | |||
} | |||
|
|||
@Override | |||
public boolean canDownloadUrl(String url) { | |||
return url.startsWith("http"); | |||
} | |||
} |
15 changes: 15 additions & 0 deletions
15
UrlImageViewHelper/src/com/koushikdutta/urlimageviewhelper/UrlDownloader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,15 @@ | |||
package com.koushikdutta.urlimageviewhelper; | |||
|
|||
import java.io.InputStream; | |||
|
|||
import android.content.Context; | |||
|
|||
public interface UrlDownloader { | |||
public static interface UrlDownloaderCallback { | |||
public void onDownloadComplete(UrlDownloader downloader, InputStream in, String filename); | |||
} | |||
|
|||
public void download(Context context, String url, String filename, UrlDownloaderCallback callback, Runnable completion); | |||
public boolean allowCache(); | |||
public boolean canDownloadUrl(String url); | |||
} |
Oops, something went wrong.