From d6d10b5d83ca8e282bd2436556bc13b930fd6009 Mon Sep 17 00:00:00 2001
From: Sergey Volkov <s.volkov@netris.ru>
Date: Mon, 18 Dec 2017 19:08:07 +0300
Subject: [PATCH 3/3] Android. Add ImageView.cache property

---
 .../ti/modules/titanium/ui/ImageViewProxy.java     |  2 ++
 .../appcelerator/titanium/util/TiFileHelper.java   | 24 ++++++++++++++++------
 .../titanium/view/TiDrawableReference.java         | 23 ++++++++++++++++++---
 apidoc/Titanium/UI/ImageView.yml                   |  9 ++++++++
 4 files changed, 49 insertions(+), 9 deletions(-)

diff --git a/android/modules/ui/src/java/ti/modules/titanium/ui/ImageViewProxy.java b/android/modules/ui/src/java/ti/modules/titanium/ui/ImageViewProxy.java
index 10ad048..3a8f25a 100644
--- a/android/modules/ui/src/java/ti/modules/titanium/ui/ImageViewProxy.java
+++ b/android/modules/ui/src/java/ti/modules/titanium/ui/ImageViewProxy.java
@@ -15,6 +15,7 @@ import ti.modules.titanium.ui.widget.TiUIImageView;
 import android.app.Activity;
 
 @Kroll.proxy(creatableInModule=UIModule.class, propertyAccessors = {
+	TiC.PROPERTY_CACHE,
 	TiC.PROPERTY_DECODE_RETRIES,
 	TiC.PROPERTY_AUTOROTATE,
 	TiC.PROPERTY_DEFAULT_IMAGE,
@@ -30,6 +31,7 @@ public class ImageViewProxy extends ViewProxy
 	public ImageViewProxy()
 	{
 		super();
+		defaultValues.put(TiC.PROPERTY_CACHE, true);
 	}
 
 	@Override
diff --git a/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java b/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java
index 24c09bf..59d9ba1 100644
--- a/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java
+++ b/android/titanium/src/java/org/appcelerator/titanium/util/TiFileHelper.java
@@ -17,6 +17,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.lang.ref.SoftReference;
+import java.net.HttpURLConnection;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.URL;
@@ -45,6 +46,7 @@ import android.os.Environment;
 import android.os.Handler;
 import android.os.Message;
 import android.webkit.URLUtil;
+import org.appcelerator.titanium.TiC;
 
 @SuppressWarnings("deprecation")
 public class TiFileHelper implements Handler.Callback
@@ -146,7 +148,12 @@ public class TiFileHelper implements Handler.Callback
 		return _instance;
 	}
 
-	public InputStream openInputStream(String path, boolean report)
+	public InputStream openInputStream(String path, boolean report) throws IOException
+	{
+		return openInputStream(path, report, false);
+	}
+
+	public InputStream openInputStream(String path, boolean report, boolean useCaches)
 		throws IOException
 	{
 		InputStream is = null;
@@ -179,9 +186,11 @@ public class TiFileHelper implements Handler.Callback
 				}
 			} else if (URLUtil.isNetworkUrl(path)) {
 				if (TiApplication.isUIThread()) {
-					is = (InputStream) TiMessenger.sendBlockingRuntimeMessage(getRuntimeHandler().obtainMessage(MSG_NETWORK_URL), path);
+					Message message = getRuntimeHandler().obtainMessage(MSG_NETWORK_URL);
+					message.getData().putBoolean(TiC.PROPERTY_CACHE, useCaches);
+					is = (InputStream) TiMessenger.sendBlockingRuntimeMessage(message, path);
 				} else {
-					is = handleNetworkURL(path);
+					is = handleNetworkURL(path, useCaches);
 				}
 			} else if (path.startsWith(RESOURCE_ROOT_ASSETS)) {
 				int len = "file:///android_asset/".length();
@@ -226,7 +235,7 @@ public class TiFileHelper implements Handler.Callback
 		return is;
 	}
 
-	private InputStream handleNetworkURL(String path) throws IOException
+	private InputStream handleNetworkURL(String path, boolean useCaches) throws IOException
 	{
 		InputStream is = null;
 		try {
@@ -242,7 +251,9 @@ public class TiFileHelper implements Handler.Callback
 		}
 
 		URL u = new URL(path);
-		InputStream lis = u.openStream();
+		HttpURLConnection client = (HttpURLConnection) u.openConnection();
+		client.setUseCaches(useCaches);
+		InputStream lis = client.getInputStream();
 		ByteArrayOutputStream bos = null;
 		try {
 			bos = new ByteArrayOutputStream(8192);
@@ -780,7 +791,8 @@ public class TiFileHelper implements Handler.Callback
 			case MSG_NETWORK_URL:
 				AsyncResult result = (AsyncResult) msg.obj;
 				try {
-					result.setResult(handleNetworkURL(TiConvert.toString(result.getArg())));
+					result.setResult(handleNetworkURL(TiConvert.toString(result.getArg()),
+							msg.getData().getBoolean(TiC.PROPERTY_CACHE)));
 				} catch (IOException e) {
 					e.printStackTrace();
 				}
diff --git a/android/titanium/src/java/org/appcelerator/titanium/view/TiDrawableReference.java b/android/titanium/src/java/org/appcelerator/titanium/view/TiDrawableReference.java
index 4bfe7e6..16fd98b 100644
--- a/android/titanium/src/java/org/appcelerator/titanium/view/TiDrawableReference.java
+++ b/android/titanium/src/java/org/appcelerator/titanium/view/TiDrawableReference.java
@@ -23,6 +23,7 @@ import org.appcelerator.kroll.KrollProxy;
 import org.appcelerator.kroll.common.Log;
 import org.appcelerator.titanium.TiApplication;
 import org.appcelerator.titanium.TiBlob;
+import org.appcelerator.titanium.TiC;
 import org.appcelerator.titanium.TiDimension;
 import org.appcelerator.titanium.TiFileProxy;
 import org.appcelerator.titanium.io.TiBaseFile;
@@ -84,6 +85,7 @@ public class TiDrawableReference
 	private boolean anyDensityFalse = false;
 	private boolean autoRotate;
 	private int orientation = -1;
+	public boolean useCaches = false;
 
 	// TIMOB-3599: A bug in Gingerbread forces us to retry decoding bitmaps when they initially fail
 	public static final int DEFAULT_DECODE_RETRIES = 5;
@@ -166,7 +168,14 @@ public class TiDrawableReference
 		if (url == null || url.length() == 0 || url.trim().length() == 0) {
 			return new TiDrawableReference(proxy.getActivity(), DrawableReferenceType.NULL);
 		}
-		return fromUrl(proxy.getActivity(), proxy.resolveUrl(null, url));
+		boolean useCaches = false;
+		Object cacheProperty = proxy.getProperty(TiC.PROPERTY_CACHE);
+		if (cacheProperty != null) {
+			useCaches = TiConvert.toBoolean(cacheProperty);
+		}
+		TiDrawableReference ref = fromUrl(proxy.getActivity(), proxy.resolveUrl(null, url));
+		ref.useCaches = useCaches;
+		return ref;
 	}
 
 	/**
@@ -241,8 +250,15 @@ public class TiDrawableReference
 			object = proxy.resolveUrl(null, (String)object);
 		}
 
+		boolean useCaches = false;
+		Object cacheProperty = proxy.getProperty(TiC.PROPERTY_CACHE);
+		if (cacheProperty != null) {
+			useCaches = TiConvert.toBoolean(cacheProperty);
+		}
 		// Create a drawable reference from the given object.
-		return TiDrawableReference.fromObject(activity, object);
+		TiDrawableReference ref = TiDrawableReference.fromObject(activity, object);
+		ref.useCaches = useCaches;
+		return ref;
 	}
 
 	/**
@@ -412,6 +428,7 @@ public class TiDrawableReference
 				    try {
 				        URL mURL = new URL(url);
 				        connection = (HttpURLConnection) mURL.openConnection();
+				        connection.setUseCaches(useCaches);
 				        connection.setInstanceFollowRedirects(true);
 				        connection.setDoInput(true);
 				        connection.connect();
@@ -889,7 +906,7 @@ public class TiDrawableReference
 
 		if (isTypeUrl() && url != null) {
 			try {
-				stream = TiFileHelper.getInstance().openInputStream(url, false);
+				stream = TiFileHelper.getInstance().openInputStream(url, false, useCaches);
 				
 			} catch (IOException e) {
 				Log.e(TAG, "Problem opening stream with url " + url + ": " + e.getMessage(), e);
diff --git a/apidoc/Titanium/UI/ImageView.yml b/apidoc/Titanium/UI/ImageView.yml
index ebcaf85..aefdbd1 100644
--- a/apidoc/Titanium/UI/ImageView.yml
+++ b/apidoc/Titanium/UI/ImageView.yml
@@ -150,6 +150,15 @@ properties:
     since: 3.0.0
     platforms: [android, iphone, ipad]
 
+  - name: cache
+    summary: |
+        Indicates whether the remote image should be stored in cache.
+    type: Boolean
+    default: true.
+    availability: creation
+    since: 8.0.0
+    platforms: [android]
+
   - name: decodeRetries
     summary: |
         Number of times to retry decoding the bitmap at a URL.
-- 
2.7.4

