Skip to content

Commit

Permalink
Add support for isRequestRunning [amitshekhariitbhu#29]
Browse files Browse the repository at this point in the history
  • Loading branch information
amitshekhariitbhu committed Dec 20, 2017
1 parent 22298ae commit 132df35
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public class ANRequest<T extends ANRequest> {
private int mProgress;
private boolean isCancelled;
private boolean isDelivered;
private boolean isRunning;
private int mPercentageThresholdForCancelling = 0;
private JSONArrayRequestListener mJSONArrayRequestListener;
private JSONObjectRequestListener mJSONObjectRequestListener;
Expand Down Expand Up @@ -548,6 +549,7 @@ public void cancel(boolean forceCancel) {
if (forceCancel || mPercentageThresholdForCancelling == 0
|| mProgress < mPercentageThresholdForCancelling) {
isCancelled = true;
isRunning = false;
if (call != null) {
call.cancel();
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
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;

/**
* Created by amitshekhar on 22/03/16.
*/
public class ANRequestQueue {

private final static String TAG = ANRequestQueue.class.getSimpleName();
private final Set<ANRequest> mCurrentRequests = new HashSet<>();
private final Set<ANRequest> mCurrentRequests =
Collections.newSetFromMap(new ConcurrentHashMap<ANRequest, Boolean>());
private AtomicInteger mSequenceGenerator = new AtomicInteger();
private static ANRequestQueue sInstance = null;

Expand All @@ -55,40 +56,35 @@ public interface RequestFilter {
boolean apply(ANRequest request);
}


private void cancel(RequestFilter filter, boolean forceCancel) {
synchronized (mCurrentRequests) {
try {
for (Iterator<ANRequest> 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<ANRequest> 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<ANRequest> iterator = mCurrentRequests.iterator(); iterator.hasNext(); ) {
ANRequest request = iterator.next();
request.cancel(forceCancel);
if (request.isCanceled()) {
request.destroy();
iterator.remove();
}
try {
for (Iterator<ANRequest> 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();
}
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public InternalRunnable(ANRequest request) {

@Override
public void run() {
request.setRunning(true);
switch (request.getRequestType()) {
case SIMPLE:
executeSimpleRequest();
Expand All @@ -60,6 +61,7 @@ public void run() {
executeUploadRequest();
break;
}
request.setRunning(false);
}

private void executeSimpleRequest() {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/networking/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 132df35

Please sign in to comment.