package org.evergreen_ils.auth;
+import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.location.Location;
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;
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.*;
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) {
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);
}
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);
}
}
finish();
}
- private void startTask() {
- new FetchConsortiumsTask().execute(libraries_directory_json_url);
- }
-
private void parseLibrariesJSON(String json) {
libraries.clear();
--- /dev/null
+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;
+ }
+}