blob: 0e04e87698657c6960653ac17ce5b6e10eea31bb [file] [log] [blame]
Evan Charlton0acc7932012-11-06 13:58:19 -08001/*
2 * Copyright (C) 2012 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
17package com.android.volley.toolbox;
18
19import android.content.Context;
20import android.content.pm.PackageInfo;
21import android.content.pm.PackageManager.NameNotFoundException;
22import android.net.http.AndroidHttpClient;
23import android.os.Build;
24
25import com.android.volley.Network;
26import com.android.volley.RequestQueue;
27
28import java.io.File;
29
30public class Volley {
31
32 /** Default on-disk cache directory. */
33 private static final String DEFAULT_CACHE_DIR = "volley";
34
35 /**
36 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
37 *
38 * @param context A {@link Context} to use for creating the cache dir.
Ficus Kirkpatrickf5bea0d2013-05-21 19:17:57 -070039 * @param stack An {@link HttpStack} to use for the network, or null for default.
Evan Charlton0acc7932012-11-06 13:58:19 -080040 * @return A started {@link RequestQueue} instance.
41 */
Ficus Kirkpatrickf5bea0d2013-05-21 19:17:57 -070042 public static RequestQueue newRequestQueue(Context context, HttpStack stack) {
Evan Charlton0acc7932012-11-06 13:58:19 -080043 File cacheDir = new File(context.getCacheDir(), DEFAULT_CACHE_DIR);
44
45 String userAgent = "volley/0";
46 try {
47 String packageName = context.getPackageName();
Evan Charlton0acc7932012-11-06 13:58:19 -080048 PackageInfo info = context.getPackageManager().getPackageInfo(packageName, 0);
49 userAgent = packageName + "/" + info.versionCode;
50 } catch (NameNotFoundException e) {
51 }
52
Ficus Kirkpatrickf5bea0d2013-05-21 19:17:57 -070053 if (stack == null) {
54 if (Build.VERSION.SDK_INT >= 9) {
55 stack = new HurlStack();
56 } else {
57 // Prior to Gingerbread, HttpUrlConnection was unreliable.
58 // See: http://android-developers.blogspot.com/2011/09/androids-http-clients.html
59 stack = new HttpClientStack(AndroidHttpClient.newInstance(userAgent));
60 }
Evan Charlton0acc7932012-11-06 13:58:19 -080061 }
62
63 Network network = new BasicNetwork(stack);
64
65 RequestQueue queue = new RequestQueue(new DiskBasedCache(cacheDir), network);
66 queue.start();
67
68 return queue;
69 }
Ficus Kirkpatrickf5bea0d2013-05-21 19:17:57 -070070
71 /**
72 * Creates a default instance of the worker pool and calls {@link RequestQueue#start()} on it.
73 *
74 * @param context A {@link Context} to use for creating the cache dir.
75 * @return A started {@link RequestQueue} instance.
76 */
77 public static RequestQueue newRequestQueue(Context context) {
78 return newRequestQueue(context, null);
79 }
Evan Charlton0acc7932012-11-06 13:58:19 -080080}