From: kenstir Date: Thu, 3 Dec 2015 03:45:15 +0000 (-0500) Subject: First use of volley library to fetch libraries JSON X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=74004cbf39504c55f04991591a2c1122a8cfefae;p=working%2FEvergreen.git First use of volley library to fetch libraries JSON --- diff --git a/Open-ILS/src/Android/core/core.iml b/Open-ILS/src/Android/core/core.iml index bb3df034db..df63485834 100644 --- a/Open-ILS/src/Android/core/core.iml +++ b/Open-ILS/src/Android/core/core.iml @@ -46,6 +46,15 @@ + + + + + + + + + diff --git a/Open-ILS/src/Android/core/libs/volley.jar b/Open-ILS/src/Android/core/libs/volley.jar new file mode 100644 index 0000000000..8f3934c4ee Binary files /dev/null and b/Open-ILS/src/Android/core/libs/volley.jar differ diff --git a/Open-ILS/src/Android/core/src/org/evergreen_ils/auth/AuthenticatorActivity.java b/Open-ILS/src/Android/core/src/org/evergreen_ils/auth/AuthenticatorActivity.java index 297f2c5aa7..ade4b17aa5 100644 --- a/Open-ILS/src/Android/core/src/org/evergreen_ils/auth/AuthenticatorActivity.java +++ b/Open-ILS/src/Android/core/src/org/evergreen_ils/auth/AuthenticatorActivity.java @@ -1,5 +1,6 @@ package org.evergreen_ils.auth; +import android.app.DownloadManager; import android.content.Context; import android.content.pm.ApplicationInfo; import android.location.Location; @@ -8,6 +9,11 @@ import android.text.TextUtils; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Spinner; +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.Response; +import com.android.volley.VolleyError; +import com.android.volley.toolbox.StringRequest; import org.evergreen_ils.R; import android.accounts.Account; @@ -25,10 +31,10 @@ import android.widget.TextView; import org.evergreen_ils.accountAccess.AccountUtils; import org.evergreen_ils.globals.AppPrefs; import org.evergreen_ils.globals.Utils; +import org.evergreen_ils.net.VolleyWrangler; import org.evergreen_ils.searchCatalog.Library; import org.opensrf.util.JSONException; import org.opensrf.util.JSONReader; -import org.w3c.dom.Text; import java.util.*; @@ -55,6 +61,48 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity { Library selected_library = null; List libraries = new ArrayList(); public String libraries_directory_json_url; + private long start_ms; + + private void handleLibrariesJSON(String result) { + // parse the response + parseLibrariesJSON(result); + + // if the user has any existing accounts, then we can select a reasonable default library + Library default_library = null; + Location last_location = null; + Account[] existing_accounts = AccountUtils.getAccountsByType(AuthenticatorActivity.this); + Log.d(Const.AUTH_TAG, "there are " + existing_accounts.length + " existing accounts"); + if (existing_accounts.length > 0) { + default_library = AccountUtils.getLibraryForAccount(AuthenticatorActivity.this, existing_accounts[0]); + Log.d(Const.AUTH_TAG, "default_library=" + default_library); + } else { + LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); + if (lm != null) last_location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); + } + + // Build a List for use in the spinner adapter + // While we're at it choose a default library; first by prior account, second by proximity + Integer default_library_index = null; + float min_distance = Float.MAX_VALUE; + ArrayList l = new ArrayList(libraries.size()); + for (Library library : libraries) { + if (default_library != null && TextUtils.equals(default_library.url, library.url)) { + default_library_index = l.size(); + } else if (last_location != null && library.location != null) { + float distance = last_location.distanceTo(library.location); + if (distance < min_distance) { + default_library_index = l.size(); + min_distance = distance; + } + } + l.add(library.directory_name); + } + ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, l); + librarySpinner.setAdapter(adapter); + if (default_library_index != null) { + librarySpinner.setSelection(default_library_index); + } + } private class FetchConsortiumsTask extends AsyncTask { protected String doInBackground(String... params) { @@ -62,7 +110,7 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity { String result = null; try { Log.d(TAG, "fetching " + url); - result = Utils.getNetPageContent(url); + result = Utils.fetchUrl(url); //todo move json parsing to doInBackground } catch (Exception e) { Log.d(TAG, "error fetching", e); @@ -71,46 +119,35 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity { } protected void onPostExecute(String result) { - Log.d(TAG, "results available: " + result); - - // parse the response - parseLibrariesJSON(result); - - // if the user has any existing accounts, then we can select a reasonable default library - Library default_library = null; - Location last_location = null; - Account[] existing_accounts = AccountUtils.getAccountsByType(AuthenticatorActivity.this); - Log.d(Const.AUTH_TAG, "there are " + existing_accounts.length + " existing accounts"); - if (existing_accounts.length > 0) { - default_library = AccountUtils.getLibraryForAccount(AuthenticatorActivity.this, existing_accounts[0]); - Log.d(Const.AUTH_TAG, "default_library=" + default_library); - } else { - LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); - if (lm != null) last_location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); - } + long duration_ms = System.currentTimeMillis() - start_ms; + Log.d(TAG, "task fetch took " + duration_ms + "ms"); + handleLibrariesJSON(result); + } + } - // Build a List for use in the spinner adapter - // While we're at it choose a default library; first by prior account, second by proximity - Integer default_library_index = null; - float min_distance = Float.MAX_VALUE; - ArrayList l = new ArrayList(libraries.size()); - for (Library library : libraries) { - if (default_library != null && TextUtils.equals(default_library.url, library.url)) { - default_library_index = l.size(); - } else if (last_location != null && library.location != null) { - float distance = last_location.distanceTo(library.location); - if (distance < min_distance) { - default_library_index = l.size(); - min_distance = distance; - } - } - l.add(library.directory_name); - } - ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_spinner_dropdown_item, l); - librarySpinner.setAdapter(adapter); - if (default_library_index != null) { - librarySpinner.setSelection(default_library_index); - } + private void startTask() { + start_ms = System.currentTimeMillis(); + boolean use_volley = true; + if (use_volley) { + RequestQueue q = VolleyWrangler.getInstance(this).getRequestQueue(); + StringRequest stringRequest = new StringRequest(Request.Method.GET, libraries_directory_json_url, + new Response.Listener() { + @Override + public void onResponse(String response) { + long duration_ms = System.currentTimeMillis() - start_ms; + Log.d(TAG, "volley fetch took " + duration_ms + "ms"); + handleLibrariesJSON(response); + } + }, + new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + showAlert(error.getMessage()); + } + }); + q.add(stringRequest); + } else { + new FetchConsortiumsTask().execute(libraries_directory_json_url); } } @@ -330,10 +367,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity { finish(); } - private void startTask() { - new FetchConsortiumsTask().execute(libraries_directory_json_url); - } - private void parseLibrariesJSON(String json) { libraries.clear(); diff --git a/Open-ILS/src/Android/core/src/org/evergreen_ils/globals/Utils.java b/Open-ILS/src/Android/core/src/org/evergreen_ils/globals/Utils.java index 7268d486f1..d03349d495 100644 --- a/Open-ILS/src/Android/core/src/org/evergreen_ils/globals/Utils.java +++ b/Open-ILS/src/Android/core/src/org/evergreen_ils/globals/Utils.java @@ -49,7 +49,7 @@ public class Utils { * the url of the page to be retrieved * @return the net page content */ - public static String getNetPageContent(String url) { + public static String fetchUrl(String url) { String result = ""; diff --git a/Open-ILS/src/Android/core/src/org/evergreen_ils/net/VolleyWrangler.java b/Open-ILS/src/Android/core/src/org/evergreen_ils/net/VolleyWrangler.java new file mode 100644 index 0000000000..57f242a3e4 --- /dev/null +++ b/Open-ILS/src/Android/core/src/org/evergreen_ils/net/VolleyWrangler.java @@ -0,0 +1,67 @@ +package org.evergreen_ils.net; + +import android.content.Context; +import android.graphics.Bitmap; +import android.util.LruCache; + +import com.android.volley.Request; +import com.android.volley.RequestQueue; +import com.android.volley.toolbox.ImageLoader; +import com.android.volley.toolbox.Volley; + +/** + * Created by kenstir on 12/2/2015. + */ +// code from http://developer.android.com/training/volley/ +// or SallyPort or VolleyWrapper +public class VolleyWrangler { + private static VolleyWrangler mInstance; + private RequestQueue mRequestQueue; + private ImageLoader mImageLoader; + private static Context mCtx; + + private VolleyWrangler(Context context) { + mCtx = context; + mRequestQueue = getRequestQueue(); + + mImageLoader = new ImageLoader(mRequestQueue, + new ImageLoader.ImageCache() { + private final LruCache + cache = new LruCache(20); + + @Override + public Bitmap getBitmap(String url) { + return cache.get(url); + } + + @Override + public void putBitmap(String url, Bitmap bitmap) { + cache.put(url, bitmap); + } + }); + } + + public static synchronized VolleyWrangler getInstance(Context context) { + if (mInstance == null) { + mInstance = new VolleyWrangler(context); + } + return mInstance; + } + + public RequestQueue getRequestQueue() { + if (mRequestQueue == null) { + // getApplicationContext() is key, it keeps you from leaking the + // Activity or BroadcastReceiver if someone passes one in. + mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); + } + return mRequestQueue; + } + + public void addToRequestQueue(Request req) { + getRequestQueue().add(req); + } + + public ImageLoader getImageLoader() { + return mImageLoader; + } +}