From 43f1bf0d44b9e8f52c616cf444867204e2400716 Mon Sep 17 00:00:00 2001
From: Sergey Volkov <s.volkov@netris.ru>
Date: Thu, 11 Jan 2018 15:30:07 +0300
Subject: [PATCH 1/3] Android. Add HttpResponseCache module

---
 .../httpresponsecache/HttpResponseCacheModule.java | 144 +++++++++++++++++++++
 apidoc/Titanium/Android/HttpResponseCache.yml      | 125 ++++++++++++++++++
 2 files changed, 269 insertions(+)
 create mode 100644 android/modules/android/src/java/ti/modules/titanium/android/httpresponsecache/HttpResponseCacheModule.java
 create mode 100644 apidoc/Titanium/Android/HttpResponseCache.yml

diff --git a/android/modules/android/src/java/ti/modules/titanium/android/httpresponsecache/HttpResponseCacheModule.java b/android/modules/android/src/java/ti/modules/titanium/android/httpresponsecache/HttpResponseCacheModule.java
new file mode 100644
index 0000000..f55f611
--- /dev/null
+++ b/android/modules/android/src/java/ti/modules/titanium/android/httpresponsecache/HttpResponseCacheModule.java
@@ -0,0 +1,144 @@
+package ti.modules.titanium.android.httpresponsecache;
+
+import org.appcelerator.kroll.KrollModule;
+import org.appcelerator.kroll.annotations.Kroll;
+import org.appcelerator.titanium.TiApplication;
+
+import ti.modules.titanium.android.AndroidModule;
+import android.app.Activity;
+import android.app.Notification;
+import android.net.http.HttpResponseCache;
+
+import java.io.File;
+import java.io.IOException;
+
+@Kroll.module(parentModule=AndroidModule.class)
+public class HttpResponseCacheModule extends KrollModule
+{
+	private static final String CACHE_SIZE_KEY = "ti.android.cache.size.max";
+	private static final int DEFAULT_CACHE_SIZE = 25 * 1024; // 25MB
+
+	private String httpCachePath;
+	private long httpCacheSize;
+
+	public HttpResponseCacheModule()
+	{
+		super();
+		httpCachePath = "http";
+		httpCacheSize =
+				TiApplication.getInstance().getAppProperties().getInt(CACHE_SIZE_KEY, DEFAULT_CACHE_SIZE) * 1024;
+	}
+
+	@Kroll.method
+	public void install() throws IOException
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			cache.flush();
+			cache.close();
+		}
+		TiApplication tiApp = TiApplication.getInstance();
+		File dir = tiApp.getApplicationContext().getExternalCacheDir();
+		if (dir == null) {
+			dir = tiApp.getApplicationContext().getCacheDir();
+		}
+		File httpCacheDir = new File(dir, httpCachePath);
+		cache = HttpResponseCache.install(httpCacheDir, httpCacheSize);
+	}
+
+	@Kroll.method
+	public void flush()
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			cache.flush();
+		}
+	}
+
+	@Kroll.method
+	public void remove() throws IOException
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			cache.delete();
+		}
+	}
+
+	@Kroll.method
+	public void close() throws IOException
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			cache.close();
+		}
+	}
+
+	@Kroll.method
+	public int getHitCount()
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			return cache.getHitCount();
+		}
+		return 0;
+	}
+
+	@Kroll.method
+	public int getNetworkCount()
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			cache.getNetworkCount();
+		}
+		return 0;
+	}
+
+	@Kroll.method
+	public int getRequestCount()
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			return cache.getRequestCount();
+		}
+		return 0;
+	}
+
+	@Kroll.method
+	public long size()
+	{
+		HttpResponseCache cache = HttpResponseCache.getInstalled();
+		if (cache != null) {
+			return cache.size();
+
+		}
+		return 0;
+	}
+
+	@Kroll.method
+	public String getHttpCachePath()
+	{
+		return httpCachePath;
+	}
+
+	@Kroll.method
+	public long getHttpCacheSize()
+	{
+		return httpCacheSize;
+	}
+
+	@Kroll.method
+	public void setHttpCachePath(String value) {
+		httpCachePath = value;
+	}
+
+	@Kroll.method
+	public void setHttpCacheSize(long value) {
+		httpCacheSize = value;
+	}
+
+	@Override
+	public String getApiName()
+	{
+		return "Ti.Android.HttpResponseCache";
+	}
+}
diff --git a/apidoc/Titanium/Android/HttpResponseCache.yml b/apidoc/Titanium/Android/HttpResponseCache.yml
new file mode 100644
index 0000000..578cdc9
--- /dev/null
+++ b/apidoc/Titanium/Android/HttpResponseCache.yml
@@ -0,0 +1,125 @@
+---
+name: Titanium.Android.HttpResponseCache
+summary: The Titanium binding of an Android HttpResponseCache.
+description: |
+    Caches HTTP and HTTPS responses to the filesystem so they may be reused, saving time and bandwidth.
+
+    See also [android.net.http.HttpResponseCache](https://developer.android.com/reference/android/net/http/HttpResponseCache.html)
+    in the Android API Reference.
+
+    Cache will be saved in external storage if it present on the moment then
+    [install](Titanium.Android.HttpResponseCache.install) method is called.
+
+    #### Usage example
+
+            if(Ti.Platform.osname == "android") {
+                Ti.Android.HttpResponseCache.setHttpCachePath('_http_');
+                Ti.Android.HttpResponseCache.setHttpCacheSize(30 * 1024 * 1024);  // 30MB
+                Ti.Android.HttpResponseCache.install();
+            }
+
+extends: Titanium.Proxy
+platforms: [android]
+createable: false
+since: "8.0.0"
+methods:
+  - name: close
+    summary: Uninstalls the cache and releases any active resources.
+    description: |
+        See also:
+        [close](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#close())
+        in the Android API Reference.
+
+  - name: flush
+    summary: Force buffered operations to the filesystem.
+    description: |
+        See also:
+        [flush](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#flush())
+        in the Android API Reference.
+
+  - name: install
+    summary: Creates a new HTTP response cache and sets it as the system default cache.
+    description: |
+        See also:
+        [install](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#install(java.io.File, long))
+        in the Android API Reference.
+
+  - name: remove
+    summary: Uninstalls the cache and deletes all of its stored contents.
+    description: |
+        See also:
+        [delete](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#delete())
+        in the Android API Reference.
+
+  - name: getHitCount
+    summary: Returns the number of HTTP requests whose response was provided by the cache.
+    description: |
+        See also:
+        [getHitCount](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#getHitCount())
+        in the Android API Reference.
+    returns:
+        type: Number
+
+  - name: getNetworkCount
+    summary: Returns the number of HTTP requests that required the network to either supply a response or validate a locally cached response.
+    description: |
+        See also:
+        [getNetworkCount](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#getNetworkCount())
+        in the Android API Reference.
+    returns:
+        type: Number
+
+  - name: getRequestCount
+    summary: Returns the total number of HTTP requests that were made.
+    description: |
+        See also:
+        [getRequestCount](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#getRequestCount())
+        in the Android API Reference.
+    returns:
+        type: Number
+
+  - name: size
+    summary: Returns the number of bytes currently being used to store the values in this cache.
+    description: |
+        See also:
+        [size](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#size())
+        in the Android API Reference.
+    returns:
+        type: Number
+
+  - name: getHttpCachePath
+    summary: Gets relative path for cache.
+    description: |
+        Default value `http`.
+    returns:
+        type: String
+
+  - name: getHttpCacheSize
+    summary: Returns the maximum number of bytes that this cache should use to store its data.
+    description: |
+        Default value is 25MB. This could be redefined by
+        [setHttpCacheSize](Titanium.Android.HttpResponseCache.setHttpCacheSize) or by setting
+        `ti.android.cache.size.max` property in `tiapp.xml`
+        See also:
+        [maxSize](https://developer.android.com/reference/android/net/http/HttpResponseCache.html#maxSize())
+        in the Android API Reference.
+    returns:
+        type: Number
+
+  - name: setHttpCachePath
+    summary: Sets relative path for cache.
+    description: |
+        Override default cache path.
+    parameters:
+      - name: path
+        summary: Relative path for cache.
+        type: String
+
+  - name: SetHttpCacheSize
+    summary: Sets the maximum number of bytes that this cache should use to store its data.
+    description: |
+        Should be set before calling [install](Titanium.Android.HttpResponseCache.install) method
+    parameters:
+      - name: size
+        summary: Maximum cache size in bytes.
+        type: Number
-- 
2.7.4

