1919import android .content .res .Resources .NotFoundException ;
2020import androidx .annotation .RawRes ;
2121
22- import com .amplifyframework .AmplifyException ;
23-
2422import org .json .JSONException ;
2523import org .json .JSONObject ;
2624
3129 * A utility to read resource files.
3230 */
3331public final class Resources {
34-
35- private Resources () { }
32+ private Resources () {}
3633
3734 /**
3835 * Obtains the raw resource ID for the given resource identifier.
3936 * @param context An Android Context
4037 * @param identifier Name of a raw resource
4138 * @return ID of the raw resource
42- * @throws AmplifyException if the specified raw resource does not exist
39+ * @throws ResourceLoadingException if the specified raw resource does not exist
4340 */
4441 @ RawRes
45- public static int getRawResourceId (Context context , String identifier ) throws AmplifyException {
42+ public static int getRawResourceId (Context context , String identifier ) throws ResourceLoadingException {
4643 try {
4744 return context .getResources ().getIdentifier (identifier , "raw" , context .getPackageName ());
48- } catch (Exception exception ) {
49- throw new AmplifyException (
50- "Failed to find " + identifier + "." ,
51- exception , "Please check that it exists."
52- );
45+ } catch (Exception lookupError ) {
46+ throw new ResourceLoadingException ("No such resource with identifier " + identifier , lookupError );
5347 }
5448 }
5549
@@ -58,9 +52,10 @@ public static int getRawResourceId(Context context, String identifier) throws Am
5852 * @param context An Android Context
5953 * @param identifier Name of a raw resource
6054 * @return JSON Object equivalent of the raw resource
61- * @throws AmplifyException If resource with given ID does not exist or cannot be read
55+ * @throws ResourceLoadingException If resource with given ID does not exist or cannot be read
6256 */
63- public static JSONObject readJsonResource (Context context , String identifier ) throws AmplifyException {
57+ public static JSONObject readJsonResource (Context context , String identifier )
58+ throws ResourceLoadingException {
6459 return readJsonResourceFromId (context , getRawResourceId (context , identifier ));
6560 }
6661
@@ -69,18 +64,16 @@ public static JSONObject readJsonResource(Context context, String identifier) th
6964 * @param context An Android Context
7065 * @param resourceId ID of a raw resource
7166 * @return JSON Object equivalent of the raw resource
72- * @throws AmplifyException If resource with given ID does not exist or cannot be read
67+ * @throws ResourceLoadingException If resource with given ID does not exist or cannot be read
7368 */
74- public static JSONObject readJsonResourceFromId (Context context , @ RawRes int resourceId ) throws AmplifyException {
69+ public static JSONObject readJsonResourceFromId (Context context , @ RawRes int resourceId )
70+ throws ResourceLoadingException {
7571 InputStream inputStream ;
7672
7773 try {
7874 inputStream = context .getResources ().openRawResource (resourceId );
79- } catch (NotFoundException exception ) {
80- throw new AmplifyException (
81- "Failed to find the resource with ID " + resourceId + "." ,
82- exception , "Please check that it has been created."
83- );
75+ } catch (NotFoundException notFoundError ) {
76+ throw new ResourceLoadingException ("No such resource with ID = " + resourceId , notFoundError );
8477 }
8578
8679 final Scanner in = new Scanner (inputStream );
@@ -92,11 +85,26 @@ public static JSONObject readJsonResourceFromId(Context context, @RawRes int res
9285
9386 try {
9487 return new JSONObject (sb .toString ());
95- } catch (JSONException jsonError ) {
96- throw new AmplifyException (
97- "Failed to read the resource with ID " + resourceId + "." ,
98- jsonError , "Please check that it is correctly formed."
88+ } catch (JSONException badJsonError ) {
89+ throw new ResourceLoadingException (
90+ "Failed to read the resource with ID " + resourceId + "." , badJsonError
9991 );
10092 }
10193 }
94+
95+ /**
96+ * Indicates that a requested resource cannot be loaded.
97+ */
98+ public static final class ResourceLoadingException extends Exception {
99+ private static final long serialVersionUID = 1 ;
100+
101+ /**
102+ * Constructs a new resource loading exception.
103+ * @param failureRationale A rationale for the failure
104+ * @param rootCause A root cause for the failure
105+ */
106+ public ResourceLoadingException (String failureRationale , Throwable rootCause ) {
107+ super (failureRationale , rootCause );
108+ }
109+ }
102110}
0 commit comments