Bug 994189 - Factor out method to load raw resource (r=margaret)
authorLucas Rocha <lucasr@mozilla.com>
Tue, 15 Apr 2014 16:12:03 +0100
changeset 197062 2d750966825dca5e5f72758b356872a5636679d4
parent 197061 7e929146003ea2422238e11b88ba3a8090b008b6
child 197063 f6e8cdd02ef78723d1bd369551c5ca157b7d2e37
push id3624
push userasasaki@mozilla.com
push dateMon, 09 Jun 2014 21:49:01 +0000
treeherdermozilla-beta@b1a5da15899a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmargaret
bugs994189
milestone31.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 994189 - Factor out method to load raw resource (r=margaret)
mobile/android/base/db/HomeProvider.java
mobile/android/base/moz.build
mobile/android/base/util/RawResource.java
--- a/mobile/android/base/db/HomeProvider.java
+++ b/mobile/android/base/db/HomeProvider.java
@@ -1,23 +1,23 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 package org.mozilla.gecko.db;
 
 import java.io.IOException;
-import java.io.InputStream;
 
 import org.json.JSONArray;
 import org.json.JSONException;
 import org.json.JSONObject;
 import org.mozilla.gecko.R;
 import org.mozilla.gecko.db.BrowserContract.HomeItems;
 import org.mozilla.gecko.sqlite.SQLiteBridge;
+import org.mozilla.gecko.util.RawResource;
 
 import android.content.ContentValues;
 import android.content.UriMatcher;
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.net.Uri;
 import android.util.Log;
 
@@ -78,17 +78,18 @@ public class HomeProvider extends SQLite
     }
 
     /**
      * Returns a cursor populated with static fake data.
      */
     private Cursor queryFakeItems(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
         JSONArray items = null;
         try {
-            items = new JSONArray(getRawFakeItems());
+            final String jsonString = RawResource.get(getContext(), R.raw.fake_home_items);
+            items = new JSONArray(jsonString);
         } catch (IOException e) {
             Log.e(LOGTAG, "Error getting fake home items", e);
             return null;
         } catch (JSONException e) {
             Log.e(LOGTAG, "Error parsing fake_home_items.json", e);
             return null;
         }
 
@@ -117,28 +118,16 @@ public class HomeProvider extends SQLite
                 });
             } catch (JSONException e) {
                 Log.e(LOGTAG, "Error creating cursor row for fake home item", e);
             }
         }
         return c;
     }
 
-    private String getRawFakeItems() throws IOException {
-        final InputStream inputStream = getContext().getResources().openRawResource(R.raw.fake_home_items);
-        final byte[] buffer = new byte[1024];
-        StringBuilder s = new StringBuilder();
-        int count;
-
-        while ((count = inputStream.read(buffer)) != -1) {
-            s.append(new String(buffer, 0, count));
-        }
-        return s.toString();
-    }
-
     /**
      * SQLiteBridgeContentProvider implementation
      */
 
     @Override
     protected String getDBName(){
         return DB_FILENAME;
     }
--- a/mobile/android/base/moz.build
+++ b/mobile/android/base/moz.build
@@ -61,16 +61,17 @@ gujar.sources += [
     'util/INISection.java',
     'util/JSONUtils.java',
     'util/MenuUtils.java',
     'util/NativeEventListener.java',
     'util/NativeJSContainer.java',
     'util/NativeJSObject.java',
     'util/NonEvictingLruCache.java',
     'util/ProxySelector.java',
+    'util/RawResource.java',
     'util/StringUtils.java',
     'util/ThreadUtils.java',
     'util/UiAsyncTask.java',
 ]
 gujar.extra_jars = [
     'gecko-mozglue.jar'
 ]
 gujar.javac_flags += ['-Xlint:all,-deprecation']
new file mode 100644
--- /dev/null
+++ b/mobile/android/base/util/RawResource.java
@@ -0,0 +1,25 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+package org.mozilla.gecko.util;
+
+import android.content.Context;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+public final class RawResource {
+    public static String get(Context context, int id) throws IOException {
+        final InputStream inputStream = context.getResources().openRawResource(id);
+        final byte[] buffer = new byte[1024];
+        StringBuilder s = new StringBuilder();
+        int count;
+
+        while ((count = inputStream.read(buffer)) != -1) {
+            s.append(new String(buffer, 0, count));
+        }
+
+        return s.toString();
+    }
+}
\ No newline at end of file