1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.volley.toolbox; 18 19 import android.os.Handler; 20 import android.os.Looper; 21 import com.android.volley.Cache; 22 import com.android.volley.NetworkResponse; 23 import com.android.volley.Request; 24 import com.android.volley.Response; 25 26 /** A synthetic request used for clearing the cache. */ 27 public class ClearCacheRequest extends Request<Object> { 28 private final Cache mCache; 29 private final Runnable mCallback; 30 31 /** 32 * Creates a synthetic request for clearing the cache. 33 * 34 * @param cache Cache to clear 35 * @param callback Callback to make on the main thread once the cache is clear, or null for none 36 */ ClearCacheRequest(Cache cache, Runnable callback)37 public ClearCacheRequest(Cache cache, Runnable callback) { 38 super(Method.GET, null, null); 39 mCache = cache; 40 mCallback = callback; 41 } 42 43 @Override isCanceled()44 public boolean isCanceled() { 45 // This is a little bit of a hack, but hey, why not. 46 mCache.clear(); 47 if (mCallback != null) { 48 Handler handler = new Handler(Looper.getMainLooper()); 49 handler.postAtFrontOfQueue(mCallback); 50 } 51 return true; 52 } 53 54 @Override getPriority()55 public Priority getPriority() { 56 return Priority.IMMEDIATE; 57 } 58 59 @Override parseNetworkResponse(NetworkResponse response)60 protected Response<Object> parseNetworkResponse(NetworkResponse response) { 61 return null; 62 } 63 64 @Override deliverResponse(Object response)65 protected void deliverResponse(Object response) {} 66 } 67