Owen Lin | a2fba68 | 2011-08-17 22:07:43 +0800 | [diff] [blame] | 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.gallery3d.common; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.pm.PackageInfo; |
| 21 | import android.content.pm.PackageManager.NameNotFoundException; |
| 22 | import android.os.Build; |
| 23 | import android.util.Log; |
| 24 | |
| 25 | import org.apache.http.HttpVersion; |
| 26 | import org.apache.http.client.HttpClient; |
| 27 | import org.apache.http.conn.params.ConnManagerParams; |
| 28 | import org.apache.http.params.CoreProtocolPNames; |
| 29 | import org.apache.http.params.HttpParams; |
| 30 | |
| 31 | import java.lang.reflect.InvocationTargetException; |
| 32 | import java.lang.reflect.Method; |
| 33 | |
| 34 | /** |
| 35 | * Constructs {@link HttpClient} instances and isolates client code from API |
| 36 | * level differences. |
| 37 | */ |
| 38 | public final class HttpClientFactory { |
| 39 | // TODO: migrate GDataClient to use this util method instead of apache's |
| 40 | // DefaultHttpClient. |
| 41 | /** |
| 42 | * Creates an HttpClient with the userAgent string constructed from the |
| 43 | * package name contained in the context. |
| 44 | * @return the client |
| 45 | */ |
| 46 | public static HttpClient newHttpClient(Context context) { |
| 47 | return HttpClientFactory.newHttpClient(getUserAgent(context)); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Creates an HttpClient with the specified userAgent string. |
| 52 | * @param userAgent the userAgent string |
| 53 | * @return the client |
| 54 | */ |
| 55 | public static HttpClient newHttpClient(String userAgent) { |
| 56 | // AndroidHttpClient is available on all platform releases, |
| 57 | // but is hidden until API Level 8 |
| 58 | try { |
| 59 | Class<?> clazz = Class.forName("android.net.http.AndroidHttpClient"); |
| 60 | Method newInstance = clazz.getMethod("newInstance", String.class); |
| 61 | Object instance = newInstance.invoke(null, userAgent); |
| 62 | |
| 63 | HttpClient client = (HttpClient) instance; |
| 64 | |
| 65 | // ensure we default to HTTP 1.1 |
| 66 | HttpParams params = client.getParams(); |
| 67 | params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); |
| 68 | |
| 69 | // AndroidHttpClient sets these two parameters thusly by default: |
| 70 | // HttpConnectionParams.setSoTimeout(params, 60 * 1000); |
| 71 | // HttpConnectionParams.setConnectionTimeout(params, 60 * 1000); |
| 72 | |
| 73 | // however it doesn't set this one... |
| 74 | ConnManagerParams.setTimeout(params, 60 * 1000); |
| 75 | |
| 76 | return client; |
| 77 | } catch (InvocationTargetException e) { |
| 78 | throw new RuntimeException(e); |
| 79 | } catch (ClassNotFoundException e) { |
| 80 | throw new RuntimeException(e); |
| 81 | } catch (NoSuchMethodException e) { |
| 82 | throw new RuntimeException(e); |
| 83 | } catch (IllegalAccessException e) { |
| 84 | throw new RuntimeException(e); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Closes an HttpClient. |
| 90 | */ |
| 91 | public static void close(HttpClient client) { |
| 92 | // AndroidHttpClient is available on all platform releases, |
| 93 | // but is hidden until API Level 8 |
| 94 | try { |
| 95 | Class<?> clazz = client.getClass(); |
| 96 | Method method = clazz.getMethod("close", (Class<?>[]) null); |
| 97 | method.invoke(client, (Object[]) null); |
| 98 | } catch (InvocationTargetException e) { |
| 99 | throw new RuntimeException(e); |
| 100 | } catch (NoSuchMethodException e) { |
| 101 | throw new RuntimeException(e); |
| 102 | } catch (IllegalAccessException e) { |
| 103 | throw new RuntimeException(e); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private static String sUserAgent = null; |
| 108 | |
| 109 | private static String getUserAgent(Context context) { |
| 110 | if (sUserAgent == null) { |
| 111 | PackageInfo pi; |
| 112 | try { |
| 113 | pi = context.getPackageManager().getPackageInfo( |
| 114 | context.getPackageName(), 0); |
| 115 | } catch (NameNotFoundException e) { |
| 116 | throw new IllegalStateException("getPackageInfo failed"); |
| 117 | } |
| 118 | sUserAgent = String.format("%s/%s; %s/%s/%s/%s; %s/%s/%s", |
| 119 | pi.packageName, |
| 120 | pi.versionName, |
| 121 | Build.BRAND, |
| 122 | Build.DEVICE, |
| 123 | Build.MODEL, |
| 124 | Build.ID, |
| 125 | Build.VERSION.SDK, |
| 126 | Build.VERSION.RELEASE, |
| 127 | Build.VERSION.INCREMENTAL); |
| 128 | } |
| 129 | return sUserAgent; |
| 130 | } |
| 131 | |
| 132 | private HttpClientFactory() { |
| 133 | } |
| 134 | } |