Bug 994189 - Factor out method to load raw resource (r=margaret)

This commit is contained in:
Lucas Rocha 2014-04-15 16:12:03 +01:00
parent a08dea41d3
commit edb88c478a
3 changed files with 29 additions and 14 deletions

View File

@ -5,7 +5,6 @@
package org.mozilla.gecko.db;
import java.io.IOException;
import java.io.InputStream;
import org.json.JSONArray;
import org.json.JSONException;
@ -13,6 +12,7 @@ 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;
@ -83,7 +83,8 @@ public class HomeProvider extends SQLiteBridgeContentProvider {
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;
@ -122,18 +123,6 @@ public class HomeProvider extends SQLiteBridgeContentProvider {
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
*/

View File

@ -66,6 +66,7 @@ gujar.sources += [
'util/NativeJSObject.java',
'util/NonEvictingLruCache.java',
'util/ProxySelector.java',
'util/RawResource.java',
'util/StringUtils.java',
'util/ThreadUtils.java',
'util/UiAsyncTask.java',

View File

@ -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();
}
}