diff --git a/android-networking/src/main/java/com/androidnetworking/AndroidNetworking.java b/android-networking/src/main/java/com/androidnetworking/AndroidNetworking.java index c1df3e01..84c50bfa 100644 --- a/android-networking/src/main/java/com/androidnetworking/AndroidNetworking.java +++ b/android-networking/src/main/java/com/androidnetworking/AndroidNetworking.java @@ -320,6 +320,16 @@ public static void setParserFactory(Parser.Factory parserFactory) { ParseUtil.setParserFactory(parserFactory); } + /** + * Method to find if the request is running or not + * + * @param tag The tag with which request running status is to be checked + * @return The request is running or not + */ + public static boolean isRequestRunning(Object tag) { + return ANRequestQueue.getInstance().isRequestRunning(tag); + } + /** * Shuts AndroidNetworking down */ diff --git a/android-networking/src/main/java/com/androidnetworking/common/ANRequest.java b/android-networking/src/main/java/com/androidnetworking/common/ANRequest.java index cf9c17d1..ab756177 100644 --- a/android-networking/src/main/java/com/androidnetworking/common/ANRequest.java +++ b/android-networking/src/main/java/com/androidnetworking/common/ANRequest.java @@ -111,6 +111,7 @@ public class ANRequest { private int mProgress; private boolean isCancelled; private boolean isDelivered; + private boolean isRunning; private int mPercentageThresholdForCancelling = 0; private JSONArrayRequestListener mJSONArrayRequestListener; private JSONObjectRequestListener mJSONObjectRequestListener; @@ -548,6 +549,7 @@ public void cancel(boolean forceCancel) { if (forceCancel || mPercentageThresholdForCancelling == 0 || mProgress < mPercentageThresholdForCancelling) { isCancelled = true; + isRunning = false; if (call != null) { call.cancel(); } @@ -567,6 +569,14 @@ public boolean isCanceled() { return isCancelled; } + public boolean isRunning() { + return isRunning; + } + + public void setRunning(boolean running) { + isRunning = running; + } + public Call getCall() { return call; } diff --git a/android-networking/src/main/java/com/androidnetworking/internal/ANRequestQueue.java b/android-networking/src/main/java/com/androidnetworking/internal/ANRequestQueue.java index b2cd8a62..ed7ff762 100644 --- a/android-networking/src/main/java/com/androidnetworking/internal/ANRequestQueue.java +++ b/android-networking/src/main/java/com/androidnetworking/internal/ANRequestQueue.java @@ -21,9 +21,10 @@ import com.androidnetworking.common.Priority; import com.androidnetworking.core.Core; -import java.util.HashSet; +import java.util.Collections; import java.util.Iterator; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; /** @@ -31,8 +32,8 @@ */ public class ANRequestQueue { - private final static String TAG = ANRequestQueue.class.getSimpleName(); - private final Set mCurrentRequests = new HashSet<>(); + private final Set mCurrentRequests = + Collections.newSetFromMap(new ConcurrentHashMap()); private AtomicInteger mSequenceGenerator = new AtomicInteger(); private static ANRequestQueue sInstance = null; @@ -55,40 +56,35 @@ public interface RequestFilter { boolean apply(ANRequest request); } - private void cancel(RequestFilter filter, boolean forceCancel) { - synchronized (mCurrentRequests) { - try { - for (Iterator iterator = mCurrentRequests.iterator(); iterator.hasNext(); ) { - ANRequest request = iterator.next(); - if (filter.apply(request)) { - request.cancel(forceCancel); - if (request.isCanceled()) { - request.destroy(); - iterator.remove(); - } + try { + for (Iterator iterator = mCurrentRequests.iterator(); iterator.hasNext(); ) { + ANRequest request = iterator.next(); + if (filter.apply(request)) { + request.cancel(forceCancel); + if (request.isCanceled()) { + request.destroy(); + iterator.remove(); } } - } catch (Exception e) { - e.printStackTrace(); } + } catch (Exception e) { + e.printStackTrace(); } } public void cancelAll(boolean forceCancel) { - synchronized (mCurrentRequests) { - try { - for (Iterator iterator = mCurrentRequests.iterator(); iterator.hasNext(); ) { - ANRequest request = iterator.next(); - request.cancel(forceCancel); - if (request.isCanceled()) { - request.destroy(); - iterator.remove(); - } + try { + for (Iterator iterator = mCurrentRequests.iterator(); iterator.hasNext(); ) { + ANRequest request = iterator.next(); + request.cancel(forceCancel); + if (request.isCanceled()) { + request.destroy(); + iterator.remove(); } - } catch (Exception e) { - e.printStackTrace(); } + } catch (Exception e) { + e.printStackTrace(); } } @@ -100,12 +96,7 @@ public void cancelRequestWithGivenTag(final Object tag, final boolean forceCance cancel(new RequestFilter() { @Override public boolean apply(ANRequest request) { - if (request.getTag() instanceof String && tag instanceof String) { - final String tempRequestTag = (String) request.getTag(); - final String tempTag = (String) tag; - return tempRequestTag.equals(tempTag); - } - return request.getTag().equals(tag); + return isRequestWithTheGivenTag(request, tag); } }, forceCancel); } catch (Exception e) { @@ -118,12 +109,10 @@ public int getSequenceNumber() { } public ANRequest addRequest(ANRequest request) { - synchronized (mCurrentRequests) { - try { - mCurrentRequests.add(request); - } catch (Exception e) { - e.printStackTrace(); - } + try { + mCurrentRequests.add(request); + } catch (Exception e) { + e.printStackTrace(); } try { request.setSequenceNumber(getSequenceNumber()); @@ -145,12 +134,36 @@ public ANRequest addRequest(ANRequest request) { } public void finish(ANRequest request) { - synchronized (mCurrentRequests) { - try { - mCurrentRequests.remove(request); - } catch (Exception e) { - e.printStackTrace(); + try { + mCurrentRequests.remove(request); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public boolean isRequestRunning(Object tag) { + try { + for (ANRequest request : mCurrentRequests) { + if (isRequestWithTheGivenTag(request, tag) && request.isRunning()) { + return true; + } } + } catch (Exception e) { + e.printStackTrace(); } + return false; } + + private boolean isRequestWithTheGivenTag(ANRequest request, Object tag) { + if (request.getTag() == null) { + return false; + } + if (request.getTag() instanceof String && tag instanceof String) { + final String tempRequestTag = (String) request.getTag(); + final String tempTag = (String) tag; + return tempRequestTag.equals(tempTag); + } + return request.getTag().equals(tag); + } + } diff --git a/android-networking/src/main/java/com/androidnetworking/internal/InternalRunnable.java b/android-networking/src/main/java/com/androidnetworking/internal/InternalRunnable.java index b784b9ca..6cb3f73f 100644 --- a/android-networking/src/main/java/com/androidnetworking/internal/InternalRunnable.java +++ b/android-networking/src/main/java/com/androidnetworking/internal/InternalRunnable.java @@ -49,6 +49,7 @@ public InternalRunnable(ANRequest request) { @Override public void run() { + request.setRunning(true); switch (request.getRequestType()) { case SIMPLE: executeSimpleRequest(); @@ -60,6 +61,7 @@ public void run() { executeUploadRequest(); break; } + request.setRunning(false); } private void executeSimpleRequest() { diff --git a/app/src/main/java/com/networking/MainActivity.java b/app/src/main/java/com/networking/MainActivity.java index 15b48812..c5a0bc38 100644 --- a/app/src/main/java/com/networking/MainActivity.java +++ b/app/src/main/java/com/networking/MainActivity.java @@ -148,7 +148,9 @@ public void makeRequests(View view) { } public void cancelAllRequests(View view) { + Log.d(TAG, "isRequestRunning before cancel : " + AndroidNetworking.isRequestRunning(this)); AndroidNetworking.cancel(this); + Log.d(TAG, "isRequestRunning after cancel : " + AndroidNetworking.isRequestRunning(this)); } public void loadImageDirect(View view) {