First use of volley library to fetch libraries JSON
authorkenstir <kenstir@gmail.com>
Thu, 3 Dec 2015 03:45:15 +0000 (22:45 -0500)
committerkenstir <kenstir@gmail.com>
Thu, 3 Dec 2015 03:45:15 +0000 (22:45 -0500)
Open-ILS/src/Android/core/core.iml
Open-ILS/src/Android/core/libs/volley.jar [new file with mode: 0644]
Open-ILS/src/Android/core/src/org/evergreen_ils/auth/AuthenticatorActivity.java
Open-ILS/src/Android/core/src/org/evergreen_ils/globals/Utils.java
Open-ILS/src/Android/core/src/org/evergreen_ils/net/VolleyWrangler.java [new file with mode: 0644]

index bb3df03..df63485 100644 (file)
     <orderEntry type="module-library" exported="">
       <library>
         <CLASSES>
+          <root url="jar://$MODULE_DIR$/libs/volley.jar!/" />
+        </CLASSES>
+        <JAVADOC />
+        <SOURCES />
+      </library>
+    </orderEntry>
+    <orderEntry type="module-library" exported="">
+      <library>
+        <CLASSES>
           <root url="jar://$MODULE_DIR$/libs/zxing_barcode.jar!/" />
         </CLASSES>
         <JAVADOC />
diff --git a/Open-ILS/src/Android/core/libs/volley.jar b/Open-ILS/src/Android/core/libs/volley.jar
new file mode 100644 (file)
index 0000000..8f3934c
Binary files /dev/null and b/Open-ILS/src/Android/core/libs/volley.jar differ
index 297f2c5..ade4b17 100644 (file)
@@ -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<Library> libraries = new ArrayList<Library>();
     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<String> 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<String> l = new ArrayList<String>(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<String> adapter = new ArrayAdapter<String>(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<String, Integer, String> {
         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<String> 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<String> l = new ArrayList<String>(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<String> adapter = new ArrayAdapter<String>(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<String>() {
+                        @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();
 
index 7268d48..d03349d 100644 (file)
@@ -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 (file)
index 0000000..57f242a
--- /dev/null
@@ -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<String, Bitmap>
+                            cache = new LruCache<String, Bitmap>(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 <T> void addToRequestQueue(Request<T> req) {
+        getRequestQueue().add(req);
+    }
+
+    public ImageLoader getImageLoader() {
+        return mImageLoader;
+    }
+}