Merge "More RS exceptions cleanup. Remove some dead code."
diff --git a/graphics/java/android/renderscript/BaseObj.java b/graphics/java/android/renderscript/BaseObj.java
index 715e3fb..69907d9 100644
--- a/graphics/java/android/renderscript/BaseObj.java
+++ b/graphics/java/android/renderscript/BaseObj.java
@@ -33,7 +33,7 @@
 
     public int getID() {
         if (mDestroyed) {
-            throw new IllegalStateException("using a destroyed object.");
+            throw new RSInvalidStateException("using a destroyed object.");
         }
         return mID;
     }
@@ -43,13 +43,12 @@
     String mName;
     RenderScript mRS;
 
-    public void setName(String s) throws IllegalStateException, IllegalArgumentException
-    {
+    public void setName(String s) {
         if(s.length() < 1) {
-            throw new IllegalArgumentException("setName does not accept a zero length string.");
+            throw new RSIllegalArgumentException("setName does not accept a zero length string.");
         }
         if(mName != null) {
-            throw new IllegalArgumentException("setName object already has a name.");
+            throw new RSIllegalArgumentException("setName object already has a name.");
         }
 
         try {
@@ -61,8 +60,7 @@
         }
     }
 
-    protected void finalize() throws Throwable
-    {
+    protected void finalize() throws Throwable {
         if (!mDestroyed) {
             if(mID != 0 && mRS.isAlive()) {
                 mRS.nObjDestroy(mID);
@@ -78,7 +76,7 @@
 
     public void destroy() {
         if(mDestroyed) {
-            throw new IllegalStateException("Object already destroyed.");
+            throw new RSInvalidStateException("Object already destroyed.");
         }
         mDestroyed = true;
         mRS.nObjDestroy(mID);
diff --git a/graphics/java/android/renderscript/Element.java b/graphics/java/android/renderscript/Element.java
index d013886..f844b7e 100644
--- a/graphics/java/android/renderscript/Element.java
+++ b/graphics/java/android/renderscript/Element.java
@@ -93,6 +93,18 @@
         }
     }
 
+    public boolean isComplex() {
+        if (mElements == null) {
+            return false;
+        }
+        for (int ct=0; ct < mElements.length; ct++) {
+            if (mElements[ct].mElements != null) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     public static Element BOOLEAN(RenderScript rs) {
         if(rs.mElement_BOOLEAN == null) {
             rs.mElement_BOOLEAN = createUser(rs, DataType.BOOLEAN);
@@ -397,7 +409,7 @@
 
     }
 
-    public void destroy() throws IllegalStateException {
+    public void destroy() {
         super.destroy();
     }
 
@@ -412,7 +424,7 @@
 
     public static Element createVector(RenderScript rs, DataType dt, int size) {
         if (size < 2 || size > 4) {
-            throw new IllegalArgumentException("Bad size");
+            throw new RSIllegalArgumentException("Vector size out of rance 2-4.");
         }
         DataKind dk = DataKind.USER;
         boolean norm = false;
@@ -426,22 +438,22 @@
               dk == DataKind.PIXEL_LA ||
               dk == DataKind.PIXEL_RGB ||
               dk == DataKind.PIXEL_RGBA)) {
-            throw new IllegalArgumentException("Unsupported DataKind");
+            throw new RSIllegalArgumentException("Unsupported DataKind");
         }
         if (!(dt == DataType.UNSIGNED_8 ||
               dt == DataType.UNSIGNED_5_6_5 ||
               dt == DataType.UNSIGNED_4_4_4_4 ||
               dt == DataType.UNSIGNED_5_5_5_1)) {
-            throw new IllegalArgumentException("Unsupported DataType");
+            throw new RSIllegalArgumentException("Unsupported DataType");
         }
         if (dt == DataType.UNSIGNED_5_6_5 && dk != DataKind.PIXEL_RGB) {
-            throw new IllegalArgumentException("Bad kind and type combo");
+            throw new RSIllegalArgumentException("Bad kind and type combo");
         }
         if (dt == DataType.UNSIGNED_5_5_5_1 && dk != DataKind.PIXEL_RGBA) {
-            throw new IllegalArgumentException("Bad kind and type combo");
+            throw new RSIllegalArgumentException("Bad kind and type combo");
         }
         if (dt == DataType.UNSIGNED_4_4_4_4 && dk != DataKind.PIXEL_RGBA) {
-            throw new IllegalArgumentException("Bad kind and type combo");
+            throw new RSIllegalArgumentException("Bad kind and type combo");
         }
 
         int size = 1;
@@ -477,7 +489,7 @@
 
         public void add(Element element, String name, int arraySize) {
             if (arraySize < 1) {
-                throw new IllegalArgumentException("Array size cannot be less than 1.");
+                throw new RSIllegalArgumentException("Array size cannot be less than 1.");
             }
             if(mCount == mElements.length) {
                 Element[] e = new Element[mCount + 8];
diff --git a/graphics/java/android/renderscript/Program.java b/graphics/java/android/renderscript/Program.java
index ffcdbbc5..35236ca 100644
--- a/graphics/java/android/renderscript/Program.java
+++ b/graphics/java/android/renderscript/Program.java
@@ -47,6 +47,13 @@
     }
 
     public void bindConstants(Allocation a, int slot) {
+        if (slot < 0 || slot >= mConstants.length) {
+            throw new IllegalArgumentException("Slot ID out of range.");
+        }
+        if (a != null &&
+            a.getType().getID() != mConstants[slot].getID()) {
+            throw new IllegalArgumentException("Allocation type does not match slot type.");
+        }
         mRS.nProgramBindConstants(mID, slot, a.mID);
     }
 
@@ -141,7 +148,10 @@
         public void addInput(Element e) throws IllegalStateException {
             // Should check for consistant and non-conflicting names...
             if(mInputCount >= MAX_INPUT) {
-                throw new IllegalArgumentException("Max input count exceeded.");
+                throw new RSIllegalArgumentException("Max input count exceeded.");
+            }
+            if (e.isComplex()) {
+                throw new RSIllegalArgumentException("Complex elements not allowed.");
             }
             mInputs[mInputCount++] = e;
         }
@@ -149,7 +159,10 @@
         public void addOutput(Element e) throws IllegalStateException {
             // Should check for consistant and non-conflicting names...
             if(mOutputCount >= MAX_OUTPUT) {
-                throw new IllegalArgumentException("Max output count exceeded.");
+                throw new RSIllegalArgumentException("Max output count exceeded.");
+            }
+            if (e.isComplex()) {
+                throw new RSIllegalArgumentException("Complex elements not allowed.");
             }
             mOutputs[mOutputCount++] = e;
         }
@@ -164,7 +177,10 @@
         public int addConstant(Type t) throws IllegalStateException {
             // Should check for consistant and non-conflicting names...
             if(mConstantCount >= MAX_CONSTANT) {
-                throw new IllegalArgumentException("Max input count exceeded.");
+                throw new RSIllegalArgumentException("Max input count exceeded.");
+            }
+            if (t.getElement().isComplex()) {
+                throw new RSIllegalArgumentException("Complex elements not allowed.");
             }
             mConstants[mConstantCount] = t;
             return mConstantCount++;
diff --git a/graphics/java/android/renderscript/RenderScript.java b/graphics/java/android/renderscript/RenderScript.java
index 8f1f273..21efa39 100644
--- a/graphics/java/android/renderscript/RenderScript.java
+++ b/graphics/java/android/renderscript/RenderScript.java
@@ -156,10 +156,6 @@
     synchronized void nObjDestroy(int id) {
         rsnObjDestroy(mContext, id);
     }
-    native int  rsnFileOpen(int con, byte[] name);
-    synchronized int nFileOpen(byte[] name) {
-        return rsnFileOpen(mContext, name);
-    }
 
     native int  rsnElementCreate(int con, int type, int kind, boolean norm, int vecSize);
     synchronized int nElementCreate(int type, int kind, boolean norm, int vecSize) {
@@ -599,7 +595,7 @@
 
     void validate() {
         if (mContext == 0) {
-            throw new IllegalStateException("Calling RS with no Context active.");
+            throw new RSInvalidStateException("Calling RS with no Context active.");
         }
     }
 
diff --git a/graphics/java/android/renderscript/RenderScriptGL.java b/graphics/java/android/renderscript/RenderScriptGL.java
index 21cbb9a..cace063 100644
--- a/graphics/java/android/renderscript/RenderScriptGL.java
+++ b/graphics/java/android/renderscript/RenderScriptGL.java
@@ -68,10 +68,10 @@
 
         private void validateRange(int umin, int upref, int rmin, int rmax) {
             if (umin < rmin || umin > rmax) {
-                throw new IllegalArgumentException("Minimum value provided out of range.");
+                throw new RSIllegalArgumentException("Minimum value provided out of range.");
             }
             if (upref < umin) {
-                throw new IllegalArgumentException("Prefered must be >= Minimum.");
+                throw new RSIllegalArgumentException("Prefered must be >= Minimum.");
             }
         }
 
@@ -93,7 +93,7 @@
         public void setSamples(int minimum, int prefered, float Q) {
             validateRange(minimum, prefered, 0, 24);
             if (Q < 0.0f || Q > 1.0f) {
-                throw new IllegalArgumentException("Quality out of 0-1 range.");
+                throw new RSIllegalArgumentException("Quality out of 0-1 range.");
             }
             mSamplesMin = minimum;
             mSamplesPref = prefered;
@@ -188,33 +188,6 @@
         nContextBindProgramVertex(safeID(p));
     }
 
-
-
-
-    //////////////////////////////////////////////////////////////////////////////////
-    // File
-
-    public class File extends BaseObj {
-        File(int id) {
-            super(id, RenderScriptGL.this);
-        }
-    }
-
-    public File fileOpen(String s) throws IllegalStateException, IllegalArgumentException
-    {
-        if(s.length() < 1) {
-            throw new IllegalArgumentException("fileOpen does not accept a zero length string.");
-        }
-
-        try {
-            byte[] bytes = s.getBytes("UTF-8");
-            int id = nFileOpen(bytes);
-            return new File(id);
-        } catch (java.io.UnsupportedEncodingException e) {
-            throw new RuntimeException(e);
-        }
-    }
-
 }
 
 
diff --git a/graphics/java/android/renderscript/Type.java b/graphics/java/android/renderscript/Type.java
index 9a40d1f..0d65737 100644
--- a/graphics/java/android/renderscript/Type.java
+++ b/graphics/java/android/renderscript/Type.java
@@ -136,7 +136,7 @@
 
         public Builder(RenderScript rs, Element e) {
             if(e.mID == 0) {
-                throw new IllegalArgumentException("Invalid element.");
+                throw new RSIllegalArgumentException("Invalid element.");
             }
 
             mRS = rs;
@@ -147,7 +147,7 @@
 
         public void add(Dimension d, int value) {
             if(value < 1) {
-                throw new IllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
+                throw new RSIllegalArgumentException("Values of less than 1 for Dimensions are not valid.");
             }
             if(mDimensions.length >= mEntryCount) {
                 Dimension[] dn = new Dimension[mEntryCount + 8];
diff --git a/graphics/jni/android_renderscript_RenderScript.cpp b/graphics/jni/android_renderscript_RenderScript.cpp
index b0faacc..1cc6dc1 100644
--- a/graphics/jni/android_renderscript_RenderScript.cpp
+++ b/graphics/jni/android_renderscript_RenderScript.cpp
@@ -118,18 +118,6 @@
     rsObjDestroy(con, (void *)obj);
 }
 
-
-static jint
-nFileOpen(JNIEnv *_env, jobject _this, RsContext con, jbyteArray str)
-{
-    LOG_API("nFileOpen, con(%p)", con);
-    jint len = _env->GetArrayLength(str);
-    jbyte * cptr = (jbyte *) _env->GetPrimitiveArrayCritical(str, 0);
-    jint ret = (jint)rsFileOpen(con, (const char *)cptr, len);
-    _env->ReleasePrimitiveArrayCritical(str, cptr, JNI_ABORT);
-    return ret;
-}
-
 // ---------------------------------------------------------------------------
 
 static jint
@@ -1247,7 +1235,6 @@
 {"rsnGetName",                       "(II)Ljava/lang/String;",                (void*)nGetName },
 {"rsnObjDestroy",                    "(II)V",                                 (void*)nObjDestroy },
 
-{"rsnFileOpen",                      "(I[B)I",                                (void*)nFileOpen },
 {"rsnFileA3DCreateFromAssetStream",  "(II)I",                                 (void*)nFileA3DCreateFromAssetStream },
 {"rsnFileA3DGetNumIndexEntries",     "(II)I",                                 (void*)nFileA3DGetNumIndexEntries },
 {"rsnFileA3DGetIndexEntries",        "(III[I[Ljava/lang/String;)V",           (void*)nFileA3DGetIndexEntries },