Stale cache management to address a P1 bug # 3296131.

Change-Id: I593f35a91c4a14c055828f8989fe01b9e7790039
diff --git a/Android.mk b/Android.mk
index 2ec003f..c2e58b6 100644
--- a/Android.mk
+++ b/Android.mk
@@ -113,11 +113,11 @@
 	rsVertexArray.cpp
 
 
-LOCAL_SHARED_LIBRARIES += libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libbcc
+LOCAL_SHARED_LIBRARIES += libz libcutils libutils libEGL libGLESv1_CM libGLESv2 libui libbcc
 
-LOCAL_STATIC_LIBRARIES := libft2 librslib_rt
+LOCAL_STATIC_LIBRARIES := libdex libft2 librslib_rt
 
-LOCAL_C_INCLUDES += external/freetype/include
+LOCAL_C_INCLUDES += external/freetype/include external/zlib dalvik
 
 LOCAL_LDLIBS := -lpthread -ldl
 LOCAL_MODULE:= libRS
diff --git a/rs.spec b/rs.spec
index cf94060..021ff92 100644
--- a/rs.spec
+++ b/rs.spec
@@ -321,6 +321,7 @@
 	}
 
 ScriptCCreate {
+        param const char * packageName
         param const char * resName
         param const char * cacheDir
 	ret RsScript
diff --git a/rsScriptC.cpp b/rsScriptC.cpp
index 0ae85cb..5197eeb 100644
--- a/rsScriptC.cpp
+++ b/rsScriptC.cpp
@@ -20,6 +20,9 @@
 #include "../../compile/libbcc/include/bcc/bcc.h"
 #include "utils/Timers.h"
 #include "utils/StopWatch.h"
+extern "C" {
+#include "libdex/ZipArchive.h"
+}
 
 #include <GLES/gl.h>
 #include <GLES/glext.h>
@@ -402,7 +405,12 @@
 extern const char rs_runtime_lib_bc[];
 extern unsigned rs_runtime_lib_bc_size;
 
-void ScriptCState::runCompiler(Context *rsc, ScriptC *s, const char *resName, const char *cacheDir) {
+void ScriptCState::runCompiler(Context *rsc,
+                               ScriptC *s,
+                               long modWhen,
+                               long crc32,
+                               const char *resName,
+                               const char *cacheDir) {
     {
         s->mBccScript = bccCreateScript();
         s->mEnviroment.mIsThreadable = true;
@@ -413,6 +421,8 @@
         if (bccReadBC(s->mBccScript,
                       s->mEnviroment.mScriptText,
                       s->mEnviroment.mScriptTextLength,
+                      modWhen,
+                      crc32,
                       resName,
                       cacheDir) >= 0) {
           //bccLinkBC(s->mBccScript, rs_runtime_lib_bc, rs_runtime_lib_bc_size);
@@ -424,6 +434,8 @@
             bccReadBC(s->mBccScript,
                       s->mEnviroment.mScriptText,
                       s->mEnviroment.mScriptTextLength,
+                      modWhen,
+                      crc32,
                       NULL,
                       cacheDir);
             bccCompileBC(s->mBccScript);
@@ -542,7 +554,11 @@
     ss->mScript->mEnviroment.mScriptTextLength = len;
 }
 
-RsScript rsi_ScriptCCreate(Context * rsc, const char *resName, const char *cacheDir)
+
+RsScript rsi_ScriptCCreate(Context *rsc,
+                           const char *packageName,
+                           const char *resName,
+                           const char *cacheDir)
 {
     ScriptCState *ss = &rsc->mScriptC;
 
@@ -550,7 +566,34 @@
     ss->mScript.clear();
     s->incUserRef();
 
-    ss->runCompiler(rsc, s.get(), resName, cacheDir);
+    // Open the apk and return the ZipArchive:
+    //  int dexZipOpenArchive(const char* fileName, ZipArchive* pArchive)
+    ZipArchive archive;
+    long modWhen;
+    long crc32;
+    if (!dexZipOpenArchive(packageName, &archive)) {  // Success
+      ZipEntry entry = dexZipFindEntry(&archive, resName);
+
+      int method;
+      size_t uncompLen;
+      size_t compLen;
+      off_t offset;
+      if (!dexZipGetEntryInfo(&archive,
+                              entry,
+                              &method,
+                              &uncompLen,
+                              &compLen,
+                              &offset,
+                              &modWhen,
+                              &crc32)) {
+      } else {
+        LOGI("Coudn't get entry info for the bitcode in an apk");
+      }
+    } else {
+      LOGI("Couldn't open the archive and read the bitcode");
+    }
+
+    ss->runCompiler(rsc, s.get(), modWhen, crc32, resName, cacheDir);
     ss->clear(rsc);
     return s.get();
 }
diff --git a/rsScriptC.h b/rsScriptC.h
index a714132..4cb5ade 100644
--- a/rsScriptC.h
+++ b/rsScriptC.h
@@ -83,7 +83,7 @@
     void init(Context *rsc);
 
     void clear(Context *rsc);
-    void runCompiler(Context *rsc, ScriptC *s, const char *resName, const char *cacheDir);
+    void runCompiler(Context *rsc, ScriptC *s, long modWhen, long crc32, const char *resName, const char *cacheDir);
 
     struct SymbolTable_t {
         const char * mName;