summaryrefslogtreecommitdiff
path: root/src/org/fox/ttrss/ImageCacheService.java
diff options
context:
space:
mode:
authorAndrew Dolgov <fox@madoka.volgo-balt.ru>2011-12-07 14:59:30 +0300
committerAndrew Dolgov <fox@madoka.volgo-balt.ru>2011-12-07 14:59:30 +0300
commit42516faa2e2aa57114e824a262396e443563a140 (patch)
treefd5d3109b18503aeea14dbbec5f5b13325bf7331 /src/org/fox/ttrss/ImageCacheService.java
parent14461fa1466f52d37ae5bafffddd7d252bc34c85 (diff)
implement offline image caching
Diffstat (limited to 'src/org/fox/ttrss/ImageCacheService.java')
-rw-r--r--src/org/fox/ttrss/ImageCacheService.java181
1 files changed, 181 insertions, 0 deletions
diff --git a/src/org/fox/ttrss/ImageCacheService.java b/src/org/fox/ttrss/ImageCacheService.java
new file mode 100644
index 00000000..999b7437
--- /dev/null
+++ b/src/org/fox/ttrss/ImageCacheService.java
@@ -0,0 +1,181 @@
+package org.fox.ttrss;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.util.Date;
+
+import android.app.ActivityManager;
+import android.app.IntentService;
+import android.app.NotificationManager;
+import android.app.ActivityManager.RunningServiceInfo;
+import android.content.Intent;
+import android.os.Environment;
+import android.util.Log;
+
+public class ImageCacheService extends IntentService {
+
+ private final String TAG = this.getClass().getSimpleName();
+
+ public static final int NOTIFY_DOWNLOADING = 1;
+
+ private static final String CACHE_PATH = "/org.fox.ttrss/image-cache/";
+
+ private int m_imagesDownloaded = 0;
+
+ private NotificationManager m_nmgr;
+
+ public ImageCacheService() {
+ super("ImageCacheService");
+ }
+
+ private boolean isDownloadServiceRunning() {
+ ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
+ for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
+ if ("org.fox.ttrss.OfflineDownloadService".equals(service.service.getClassName())) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ m_nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
+ }
+
+ protected static boolean isUrlCached(String url) {
+ String hashedUrl = md5(url);
+
+ File storage = Environment.getExternalStorageDirectory();
+
+ File file = new File(storage.getAbsolutePath() + CACHE_PATH + "/" + hashedUrl + ".png");
+
+ return file.exists();
+ }
+
+ protected static String getCacheFileName(String url) {
+ String hashedUrl = md5(url);
+
+ File storage = Environment.getExternalStorageDirectory();
+
+ File file = new File(storage.getAbsolutePath() + CACHE_PATH + "/" + hashedUrl + ".png");
+
+ return file.getAbsolutePath();
+ }
+
+ protected static void cleanupCache(boolean deleteAll) {
+ if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
+ File storage = Environment.getExternalStorageDirectory();
+ File cachePath = new File(storage.getAbsolutePath() + CACHE_PATH);
+
+ long now = new Date().getTime();
+
+ if (cachePath.isDirectory()) {
+ for (File file : cachePath.listFiles()) {
+ if (deleteAll || now - file.lastModified() > 1000*60*60*24*7) {
+ file.delete();
+ }
+ }
+ }
+ }
+ }
+
+ protected static String md5(String s) {
+ try {
+ MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
+ digest.update(s.getBytes());
+ byte messageDigest[] = digest.digest();
+
+ StringBuffer hexString = new StringBuffer();
+ for (int i=0; i<messageDigest.length; i++)
+ hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
+
+ return hexString.toString();
+
+ } catch (NoSuchAlgorithmException e) {
+ e.printStackTrace();
+ }
+
+ return null;
+ }
+
+ private InputStream getStream(String urlString) {
+ try {
+ URL url = new URL(urlString);
+ URLConnection urlConnection = url.openConnection();
+ urlConnection.setConnectTimeout(250);
+ return urlConnection.getInputStream();
+ } catch (Exception ex) {
+ return null;
+ }
+ }
+ @Override
+ protected void onHandleIntent(Intent intent) {
+ String url = intent.getStringExtra("url");
+
+ //Log.d(TAG, "got request to download URL=" + url);
+
+ if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
+ return;
+
+ String hashedUrl = md5(url);
+
+ File storage = Environment.getExternalStorageDirectory();
+ File cachePath = new File(storage.getAbsolutePath() + CACHE_PATH);
+ if (!cachePath.exists()) cachePath.mkdirs();
+
+ if (cachePath.isDirectory() && hashedUrl != null) {
+ File outputFile = new File(cachePath.getAbsolutePath() + "/" + hashedUrl + ".png");
+
+ if (!outputFile.exists()) {
+
+ //Log.d(TAG, "downloading to " + outputFile.getAbsolutePath());
+
+ InputStream is = getStream(url);
+
+ if (is != null) {
+ try {
+ FileOutputStream fos = new FileOutputStream(outputFile);
+
+ byte[] buffer = new byte[1024];
+ int len = 0;
+ while ((len = is.read(buffer)) != -1) {
+ fos.write(buffer, 0, len);
+ }
+
+ fos.close();
+ is.close();
+
+ m_imagesDownloaded++;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+
+ if (!isDownloadServiceRunning()) {
+ m_nmgr.cancel(NOTIFY_DOWNLOADING);
+
+ Intent success = new Intent();
+ success.setAction(OfflineDownloadService.INTENT_ACTION_SUCCESS);
+ success.addCategory(Intent.CATEGORY_DEFAULT);
+ sendBroadcast(success);
+ }
+ }
+
+}