aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/com/android/volley/RequestQueue.java
diff options
context:
space:
mode:
authorAnonymous <no-reply@google.com>2021-07-09 17:40:10 +0000
committerAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>2021-07-09 17:40:10 +0000
commitfd5d34f4fc6ffdf0b9c31381ffee78cf73f007d3 (patch)
treecf5d56ccdb862fd9676c855f919408ef940bc019 /core/src/main/java/com/android/volley/RequestQueue.java
parent904ab87ee280c7545a908a8ea10a833385a7cf3f (diff)
parentba6983e134f8f64ced7921f506862a7fcee59d19 (diff)
downloadvolley-bc303189c0a935d2f803f031e030b3e23a4363ec.tar.gz
Import of Volley from GitHub to AOSP. am: 212e7004ac am: f77c53c3a3 am: f33c2e45dc am: ba6983e134t_frc_odp_330442040t_frc_odp_330442000t_frc_con_330443020t_frc_cbr_330443000t_frc_ase_330444010t_frc_art_330443060t_frc_adb_330444000frc_340821000frc_340819280frc_340819220frc_340819190frc_340819030frc_340819020frc_340819010frc_340818170frc_340818110android-vts-14.0_r4android-vts-14.0_r3android-vts-14.0_r2android-vts-14.0_r1android-u-beta-1-gplandroid-security-14.0.0_r9android-security-14.0.0_r8android-security-14.0.0_r7android-security-14.0.0_r6android-security-14.0.0_r5android-security-14.0.0_r4android-security-14.0.0_r3android-security-14.0.0_r2android-security-14.0.0_r1android-platform-14.0.0_r8android-platform-14.0.0_r7android-platform-14.0.0_r6android-platform-14.0.0_r5android-platform-14.0.0_r4android-platform-14.0.0_r3android-platform-14.0.0_r2android-platform-14.0.0_r1android-cts-14.0_r4android-cts-14.0_r3android-cts-14.0_r2android-cts-14.0_r1android-14.0.0_r9android-14.0.0_r8android-14.0.0_r7android-14.0.0_r6android-14.0.0_r51android-14.0.0_r50android-14.0.0_r5android-14.0.0_r45android-14.0.0_r44android-14.0.0_r43android-14.0.0_r42android-14.0.0_r41android-14.0.0_r40android-14.0.0_r4android-14.0.0_r39android-14.0.0_r38android-14.0.0_r37android-14.0.0_r36android-14.0.0_r35android-14.0.0_r34android-14.0.0_r33android-14.0.0_r32android-14.0.0_r31android-14.0.0_r30android-14.0.0_r3android-14.0.0_r29android-14.0.0_r28android-14.0.0_r27android-14.0.0_r26android-14.0.0_r25android-14.0.0_r24android-14.0.0_r23android-14.0.0_r22android-14.0.0_r21android-14.0.0_r20android-14.0.0_r2android-14.0.0_r19android-14.0.0_r18android-14.0.0_r17android-14.0.0_r16android-14.0.0_r15android-14.0.0_r14android-14.0.0_r13android-14.0.0_r12android-14.0.0_r11android-14.0.0_r10android-14.0.0_r1android-13.0.0_r83android-13.0.0_r82android-13.0.0_r81android-13.0.0_r80android-13.0.0_r79android-13.0.0_r78android-13.0.0_r77android-13.0.0_r76android-13.0.0_r75android-13.0.0_r74android-13.0.0_r73android-13.0.0_r72android-13.0.0_r71android-13.0.0_r70android-13.0.0_r69android-13.0.0_r68android-13.0.0_r67android-13.0.0_r66android-13.0.0_r65android-13.0.0_r64android-13.0.0_r63android-13.0.0_r62android-13.0.0_r61android-13.0.0_r60android-13.0.0_r59android-13.0.0_r58android-13.0.0_r57android-13.0.0_r56android-13.0.0_r55android-13.0.0_r54android-13.0.0_r53android-13.0.0_r52android-13.0.0_r51android-13.0.0_r50android-13.0.0_r49android-13.0.0_r48android-13.0.0_r47android-13.0.0_r46android-13.0.0_r45android-13.0.0_r44android-13.0.0_r43android-13.0.0_r42android-13.0.0_r41android-13.0.0_r40android-13.0.0_r39android-13.0.0_r38android-13.0.0_r37android-13.0.0_r36android-13.0.0_r35android-13.0.0_r34android-13.0.0_r33android-13.0.0_r32android-13.0.0_r30android-13.0.0_r29android-13.0.0_r28android-13.0.0_r27android-13.0.0_r24android-13.0.0_r23android-13.0.0_r22android-13.0.0_r21android-13.0.0_r20android-13.0.0_r19android-13.0.0_r18android-13.0.0_r17android-13.0.0_r16aml_uwb_341710010aml_uwb_341513070aml_uwb_341511050aml_uwb_341310300aml_uwb_341310030aml_uwb_341111010aml_uwb_341011000aml_uwb_331910010aml_uwb_331820070aml_uwb_331613010aml_uwb_331611010aml_uwb_331410010aml_uwb_331310030aml_uwb_331115000aml_uwb_331015040aml_uwb_330810010aml_tz5_341510070aml_tz5_341510050aml_tz5_341510010aml_tz4_332714070aml_tz4_332714050aml_tz4_332714010aml_tz4_331910000aml_tz4_331314030aml_tz4_331314020aml_tz4_331314010aml_tz4_331012050aml_tz4_331012040aml_tz4_331012000aml_rkp_341510000aml_rkp_341311000aml_rkp_341114000aml_rkp_341015010aml_rkp_341012000aml_odp_341717000aml_odp_341610000aml_hef_341717050aml_hef_341613000aml_hef_341512030aml_hef_341415040aml_hef_341311010aml_hef_341114030aml_go_wif_330911000aml_go_uwb_330912000aml_go_tz4_330912000aml_go_tet_330914010aml_go_swc_330913000aml_go_sta_330911000aml_go_sdk_330810000aml_go_sch_330911000aml_go_res_330912000aml_go_per_330912000aml_go_odp_330913000aml_go_odp_330912000aml_go_neu_330912000aml_go_net_330913000aml_go_mpr_330912000aml_go_ase_330913000aml_go_ads_330915100aml_go_ads_330915000aml_go_ads_330913000aml_go_adb_330913000aml_cfg_341510000aml_ase_341510000aml_ase_341410000aml_ase_341310010aml_ase_341113000aml_ase_340913000aml_ase_331311020aml_ase_331112000aml_ase_331011020aml_ads_341720000aml_ads_341615050aml_ads_341517040aml_ads_341413000aml_ads_341316030aml_ads_341131050aml_ads_341027030aml_ads_340915050aml_ads_331920180aml_ads_331814200aml_ads_331710270aml_ads_331611190aml_ads_331511020aml_ads_331418080aml_ads_331131000main-16k-with-phonesmain-16kandroid14-tests-releaseandroid14-security-releaseandroid14-s2-releaseandroid14-s1-releaseandroid14-releaseandroid14-qpr3-releaseandroid14-qpr2-s5-releaseandroid14-qpr2-s4-releaseandroid14-qpr2-s3-releaseandroid14-qpr2-s2-releaseandroid14-qpr2-s1-releaseandroid14-qpr2-releaseandroid14-qpr1-s2-releaseandroid14-qpr1-releaseandroid14-platform-releaseandroid14-mainline-uwb-releaseandroid14-mainline-healthfitness-releaseandroid14-mainline-appsearch-releaseandroid14-mainline-adservices-releaseandroid14-gsiandroid14-devandroid14-d2-s5-releaseandroid14-d2-s4-releaseandroid14-d2-s3-releaseandroid14-d2-s2-releaseandroid14-d2-s1-releaseandroid14-d2-releaseandroid14-d1-s7-releaseandroid14-d1-s6-releaseandroid14-d1-s5-releaseandroid14-d1-s4-releaseandroid14-d1-s3-releaseandroid14-d1-s2-releaseandroid14-d1-s1-releaseandroid14-d1-releaseandroid13-qpr3-s9-releaseandroid13-qpr3-s8-releaseandroid13-qpr3-s7-releaseandroid13-qpr3-s6-releaseandroid13-qpr3-s5-releaseandroid13-qpr3-s4-releaseandroid13-qpr3-s3-releaseandroid13-qpr3-s2-releaseandroid13-qpr3-s14-releaseandroid13-qpr3-s13-releaseandroid13-qpr3-s12-releaseandroid13-qpr3-s11-releaseandroid13-qpr3-s10-releaseandroid13-qpr3-s1-releaseandroid13-qpr3-releaseandroid13-qpr3-c-s8-releaseandroid13-qpr3-c-s7-releaseandroid13-qpr3-c-s6-releaseandroid13-qpr3-c-s5-releaseandroid13-qpr3-c-s4-releaseandroid13-qpr3-c-s3-releaseandroid13-qpr3-c-s2-releaseandroid13-qpr3-c-s12-releaseandroid13-qpr3-c-s11-releaseandroid13-qpr3-c-s10-releaseandroid13-qpr3-c-s1-releaseandroid13-qpr2-s9-releaseandroid13-qpr2-s8-releaseandroid13-qpr2-s7-releaseandroid13-qpr2-s6-releaseandroid13-qpr2-s5-releaseandroid13-qpr2-s3-releaseandroid13-qpr2-s2-releaseandroid13-qpr2-s12-releaseandroid13-qpr2-s11-releaseandroid13-qpr2-s10-releaseandroid13-qpr2-s1-releaseandroid13-qpr2-releaseandroid13-qpr2-b-s1-releaseandroid13-qpr1-s8-releaseandroid13-qpr1-s7-releaseandroid13-qpr1-s6-releaseandroid13-qpr1-s5-releaseandroid13-qpr1-s4-releaseandroid13-qpr1-s3-releaseandroid13-qpr1-s2-releaseandroid13-qpr1-s1-releaseandroid13-qpr1-releaseandroid13-mainline-uwb-releaseandroid13-mainline-tzdata4-releaseandroid13-mainline-go-wifi-releaseandroid13-mainline-go-uwb-releaseandroid13-mainline-go-tzdata4-releaseandroid13-mainline-go-tethering-releaseandroid13-mainline-go-sdkext-releaseandroid13-mainline-go-scheduling-releaseandroid13-mainline-go-resolv-releaseandroid13-mainline-go-permission-releaseandroid13-mainline-go-os-statsd-releaseandroid13-mainline-go-odp-releaseandroid13-mainline-go-neuralnetworks-releaseandroid13-mainline-go-networking-releaseandroid13-mainline-go-mediaprovider-releaseandroid13-mainline-go-media-swcodec-releaseandroid13-mainline-go-appsearch-releaseandroid13-mainline-go-adservices-releaseandroid13-mainline-go-adbd-releaseandroid13-mainline-appsearch-releaseandroid13-mainline-adservices-releaseandroid13-frc-odp-releaseandroid13-frc-conscrypt-releaseandroid13-frc-cellbroadcast-releaseandroid13-frc-art-releaseandroid13-frc-adbd-releaseandroid13-devandroid13-d4-s2-releaseandroid13-d4-s1-releaseandroid13-d4-releaseandroid13-d3-s1-releaseandroid13-d2-releaseaml_tz5_341510010aml_tz4_332714010
Original change: https://android-review.googlesource.com/c/platform/external/volley/+/1758419 Change-Id: I7191d1e2ebb6c416909593386dbbd3481241bf8c
Diffstat (limited to 'core/src/main/java/com/android/volley/RequestQueue.java')
-rw-r--r--core/src/main/java/com/android/volley/RequestQueue.java342
1 files changed, 342 insertions, 0 deletions
diff --git a/core/src/main/java/com/android/volley/RequestQueue.java b/core/src/main/java/com/android/volley/RequestQueue.java
new file mode 100644
index 0000000..6db0b1c
--- /dev/null
+++ b/core/src/main/java/com/android/volley/RequestQueue.java
@@ -0,0 +1,342 @@
+/*
+ * Copyright (C) 2011 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.volley;
+
+import android.os.Handler;
+import android.os.Looper;
+import androidx.annotation.IntDef;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.PriorityBlockingQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * A request dispatch queue with a thread pool of dispatchers.
+ *
+ * <p>Calling {@link #add(Request)} will enqueue the given Request for dispatch, resolving from
+ * either cache or network on a worker thread, and then delivering a parsed response on the main
+ * thread.
+ */
+public class RequestQueue {
+
+ /** Callback interface for completed requests. */
+ // TODO: This should not be a generic class, because the request type can't be determined at
+ // compile time, so all calls to onRequestFinished are unsafe. However, changing this would be
+ // an API-breaking change. See also: https://github.com/google/volley/pull/109
+ @Deprecated // Use RequestEventListener instead.
+ public interface RequestFinishedListener<T> {
+ /** Called when a request has finished processing. */
+ void onRequestFinished(Request<T> request);
+ }
+
+ /** Request event types the listeners {@link RequestEventListener} will be notified about. */
+ @Retention(RetentionPolicy.SOURCE)
+ @IntDef({
+ RequestEvent.REQUEST_QUEUED,
+ RequestEvent.REQUEST_CACHE_LOOKUP_STARTED,
+ RequestEvent.REQUEST_CACHE_LOOKUP_FINISHED,
+ RequestEvent.REQUEST_NETWORK_DISPATCH_STARTED,
+ RequestEvent.REQUEST_NETWORK_DISPATCH_FINISHED,
+ RequestEvent.REQUEST_FINISHED
+ })
+ public @interface RequestEvent {
+ /** The request was added to the queue. */
+ public static final int REQUEST_QUEUED = 0;
+ /** Cache lookup started for the request. */
+ public static final int REQUEST_CACHE_LOOKUP_STARTED = 1;
+ /**
+ * Cache lookup finished for the request and cached response is delivered or request is
+ * queued for network dispatching.
+ */
+ public static final int REQUEST_CACHE_LOOKUP_FINISHED = 2;
+ /** Network dispatch started for the request. */
+ public static final int REQUEST_NETWORK_DISPATCH_STARTED = 3;
+ /** The network dispatch finished for the request and response (if any) is delivered. */
+ public static final int REQUEST_NETWORK_DISPATCH_FINISHED = 4;
+ /**
+ * All the work associated with the request is finished and request is removed from all the
+ * queues.
+ */
+ public static final int REQUEST_FINISHED = 5;
+ }
+
+ /** Callback interface for request life cycle events. */
+ public interface RequestEventListener {
+ /**
+ * Called on every request lifecycle event. Can be called from different threads. The call
+ * is blocking request processing, so any processing should be kept at minimum or moved to
+ * another thread.
+ */
+ void onRequestEvent(Request<?> request, @RequestEvent int event);
+ }
+
+ /** Used for generating monotonically-increasing sequence numbers for requests. */
+ private final AtomicInteger mSequenceGenerator = new AtomicInteger();
+
+ /**
+ * The set of all requests currently being processed by this RequestQueue. A Request will be in
+ * this set if it is waiting in any queue or currently being processed by any dispatcher.
+ */
+ private final Set<Request<?>> mCurrentRequests = new HashSet<>();
+
+ /** The cache triage queue. */
+ private final PriorityBlockingQueue<Request<?>> mCacheQueue = new PriorityBlockingQueue<>();
+
+ /** The queue of requests that are actually going out to the network. */
+ private final PriorityBlockingQueue<Request<?>> mNetworkQueue = new PriorityBlockingQueue<>();
+
+ /** Number of network request dispatcher threads to start. */
+ private static final int DEFAULT_NETWORK_THREAD_POOL_SIZE = 4;
+
+ /** Cache interface for retrieving and storing responses. */
+ private final Cache mCache;
+
+ /** Network interface for performing requests. */
+ private final Network mNetwork;
+
+ /** Response delivery mechanism. */
+ private final ResponseDelivery mDelivery;
+
+ /** The network dispatchers. */
+ private final NetworkDispatcher[] mDispatchers;
+
+ /** The cache dispatcher. */
+ private CacheDispatcher mCacheDispatcher;
+
+ private final List<RequestFinishedListener> mFinishedListeners = new ArrayList<>();
+
+ /** Collection of listeners for request life cycle events. */
+ private final List<RequestEventListener> mEventListeners = new ArrayList<>();
+
+ /**
+ * Creates the worker pool. Processing will not begin until {@link #start()} is called.
+ *
+ * @param cache A Cache to use for persisting responses to disk
+ * @param network A Network interface for performing HTTP requests
+ * @param threadPoolSize Number of network dispatcher threads to create
+ * @param delivery A ResponseDelivery interface for posting responses and errors
+ */
+ public RequestQueue(
+ Cache cache, Network network, int threadPoolSize, ResponseDelivery delivery) {
+ mCache = cache;
+ mNetwork = network;
+ mDispatchers = new NetworkDispatcher[threadPoolSize];
+ mDelivery = delivery;
+ }
+
+ /**
+ * Creates the worker pool. Processing will not begin until {@link #start()} is called.
+ *
+ * @param cache A Cache to use for persisting responses to disk
+ * @param network A Network interface for performing HTTP requests
+ * @param threadPoolSize Number of network dispatcher threads to create
+ */
+ public RequestQueue(Cache cache, Network network, int threadPoolSize) {
+ this(
+ cache,
+ network,
+ threadPoolSize,
+ new ExecutorDelivery(new Handler(Looper.getMainLooper())));
+ }
+
+ /**
+ * Creates the worker pool. Processing will not begin until {@link #start()} is called.
+ *
+ * @param cache A Cache to use for persisting responses to disk
+ * @param network A Network interface for performing HTTP requests
+ */
+ public RequestQueue(Cache cache, Network network) {
+ this(cache, network, DEFAULT_NETWORK_THREAD_POOL_SIZE);
+ }
+
+ /** Starts the dispatchers in this queue. */
+ public void start() {
+ stop(); // Make sure any currently running dispatchers are stopped.
+ // Create the cache dispatcher and start it.
+ mCacheDispatcher = new CacheDispatcher(mCacheQueue, mNetworkQueue, mCache, mDelivery);
+ mCacheDispatcher.start();
+
+ // Create network dispatchers (and corresponding threads) up to the pool size.
+ for (int i = 0; i < mDispatchers.length; i++) {
+ NetworkDispatcher networkDispatcher =
+ new NetworkDispatcher(mNetworkQueue, mNetwork, mCache, mDelivery);
+ mDispatchers[i] = networkDispatcher;
+ networkDispatcher.start();
+ }
+ }
+
+ /** Stops the cache and network dispatchers. */
+ public void stop() {
+ if (mCacheDispatcher != null) {
+ mCacheDispatcher.quit();
+ }
+ for (final NetworkDispatcher mDispatcher : mDispatchers) {
+ if (mDispatcher != null) {
+ mDispatcher.quit();
+ }
+ }
+ }
+
+ /** Gets a sequence number. */
+ public int getSequenceNumber() {
+ return mSequenceGenerator.incrementAndGet();
+ }
+
+ /** Gets the {@link Cache} instance being used. */
+ public Cache getCache() {
+ return mCache;
+ }
+
+ /**
+ * A simple predicate or filter interface for Requests, for use by {@link
+ * RequestQueue#cancelAll(RequestFilter)}.
+ */
+ public interface RequestFilter {
+ boolean apply(Request<?> request);
+ }
+
+ /**
+ * Cancels all requests in this queue for which the given filter applies.
+ *
+ * @param filter The filtering function to use
+ */
+ public void cancelAll(RequestFilter filter) {
+ synchronized (mCurrentRequests) {
+ for (Request<?> request : mCurrentRequests) {
+ if (filter.apply(request)) {
+ request.cancel();
+ }
+ }
+ }
+ }
+
+ /**
+ * Cancels all requests in this queue with the given tag. Tag must be non-null and equality is
+ * by identity.
+ */
+ public void cancelAll(final Object tag) {
+ if (tag == null) {
+ throw new IllegalArgumentException("Cannot cancelAll with a null tag");
+ }
+ cancelAll(
+ new RequestFilter() {
+ @Override
+ public boolean apply(Request<?> request) {
+ return request.getTag() == tag;
+ }
+ });
+ }
+
+ /**
+ * Adds a Request to the dispatch queue.
+ *
+ * @param request The request to service
+ * @return The passed-in request
+ */
+ public <T> Request<T> add(Request<T> request) {
+ // Tag the request as belonging to this queue and add it to the set of current requests.
+ request.setRequestQueue(this);
+ synchronized (mCurrentRequests) {
+ mCurrentRequests.add(request);
+ }
+
+ // Process requests in the order they are added.
+ request.setSequence(getSequenceNumber());
+ request.addMarker("add-to-queue");
+ sendRequestEvent(request, RequestEvent.REQUEST_QUEUED);
+
+ beginRequest(request);
+ return request;
+ }
+
+ <T> void beginRequest(Request<T> request) {
+ // If the request is uncacheable, skip the cache queue and go straight to the network.
+ if (!request.shouldCache()) {
+ sendRequestOverNetwork(request);
+ } else {
+ mCacheQueue.add(request);
+ }
+ }
+
+ /**
+ * Called from {@link Request#finish(String)}, indicating that processing of the given request
+ * has finished.
+ */
+ @SuppressWarnings("unchecked") // see above note on RequestFinishedListener
+ <T> void finish(Request<T> request) {
+ // Remove from the set of requests currently being processed.
+ synchronized (mCurrentRequests) {
+ mCurrentRequests.remove(request);
+ }
+ synchronized (mFinishedListeners) {
+ for (RequestFinishedListener<T> listener : mFinishedListeners) {
+ listener.onRequestFinished(request);
+ }
+ }
+ sendRequestEvent(request, RequestEvent.REQUEST_FINISHED);
+ }
+
+ /** Sends a request life cycle event to the listeners. */
+ void sendRequestEvent(Request<?> request, @RequestEvent int event) {
+ synchronized (mEventListeners) {
+ for (RequestEventListener listener : mEventListeners) {
+ listener.onRequestEvent(request, event);
+ }
+ }
+ }
+
+ /** Add a listener for request life cycle events. */
+ public void addRequestEventListener(RequestEventListener listener) {
+ synchronized (mEventListeners) {
+ mEventListeners.add(listener);
+ }
+ }
+
+ /** Remove a listener for request life cycle events. */
+ public void removeRequestEventListener(RequestEventListener listener) {
+ synchronized (mEventListeners) {
+ mEventListeners.remove(listener);
+ }
+ }
+
+ @Deprecated // Use RequestEventListener instead.
+ public <T> void addRequestFinishedListener(RequestFinishedListener<T> listener) {
+ synchronized (mFinishedListeners) {
+ mFinishedListeners.add(listener);
+ }
+ }
+
+ /** Remove a RequestFinishedListener. Has no effect if listener was not previously added. */
+ @Deprecated // Use RequestEventListener instead.
+ public <T> void removeRequestFinishedListener(RequestFinishedListener<T> listener) {
+ synchronized (mFinishedListeners) {
+ mFinishedListeners.remove(listener);
+ }
+ }
+
+ public ResponseDelivery getResponseDelivery() {
+ return mDelivery;
+ }
+
+ <T> void sendRequestOverNetwork(Request<T> request) {
+ mNetworkQueue.add(request);
+ }
+}