OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.net; | 5 package org.chromium.net.impl; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.pm.PackageInfo; | 8 import android.content.pm.PackageInfo; |
9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
10 import android.content.pm.PackageManager.NameNotFoundException; | 10 import android.content.pm.PackageManager.NameNotFoundException; |
11 import android.os.Build; | 11 import android.os.Build; |
12 | 12 |
13 import java.util.Locale; | 13 import java.util.Locale; |
14 | 14 |
15 /** | 15 /** |
16 * Constructs a User-Agent string. | 16 * Constructs a User-Agent string. |
17 */ | 17 */ |
18 final class UserAgent { | 18 public final class UserAgent { |
19 private static final Object sLock = new Object(); | 19 private static final Object sLock = new Object(); |
20 | 20 |
21 private static final int VERSION_CODE_UNINITIALIZED = 0; | 21 private static final int VERSION_CODE_UNINITIALIZED = 0; |
22 private static int sVersionCode = VERSION_CODE_UNINITIALIZED; | 22 private static int sVersionCode = VERSION_CODE_UNINITIALIZED; |
23 | 23 |
24 private UserAgent() { | 24 private UserAgent() {} |
25 } | |
26 | 25 |
27 /** | 26 /** |
28 * Constructs a User-Agent string including application name and version, | 27 * Constructs a User-Agent string including application name and version, |
29 * system build version, model and Id, and Cronet version. | 28 * system build version, model and Id, and Cronet version. |
30 * @param context the context to fetch the application name and version | 29 * @param context the context to fetch the application name and version |
31 * from. | 30 * from. |
32 * @return User-Agent string. | 31 * @return User-Agent string. |
33 */ | 32 */ |
34 public static String from(Context context) { | 33 public static String from(Context context) { |
35 StringBuilder builder = new StringBuilder(); | 34 StringBuilder builder = new StringBuilder(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 | 79 |
81 return builder.toString(); | 80 return builder.toString(); |
82 } | 81 } |
83 | 82 |
84 private static int versionFromContext(Context context) { | 83 private static int versionFromContext(Context context) { |
85 synchronized (sLock) { | 84 synchronized (sLock) { |
86 if (sVersionCode == VERSION_CODE_UNINITIALIZED) { | 85 if (sVersionCode == VERSION_CODE_UNINITIALIZED) { |
87 PackageManager packageManager = context.getPackageManager(); | 86 PackageManager packageManager = context.getPackageManager(); |
88 String packageName = context.getPackageName(); | 87 String packageName = context.getPackageName(); |
89 try { | 88 try { |
90 PackageInfo packageInfo = packageManager.getPackageInfo( | 89 PackageInfo packageInfo = packageManager.getPackageInfo(pack
ageName, 0); |
91 packageName, 0); | |
92 sVersionCode = packageInfo.versionCode; | 90 sVersionCode = packageInfo.versionCode; |
93 } catch (NameNotFoundException e) { | 91 } catch (NameNotFoundException e) { |
94 throw new IllegalStateException( | 92 throw new IllegalStateException("Cannot determine package ve
rsion"); |
95 "Cannot determine package version"); | |
96 } | 93 } |
97 } | 94 } |
98 return sVersionCode; | 95 return sVersionCode; |
99 } | 96 } |
100 } | 97 } |
101 | 98 |
102 private static void appendCronetVersion(StringBuilder builder) { | 99 private static void appendCronetVersion(StringBuilder builder) { |
103 builder.append(" Cronet/"); | 100 builder.append(" Cronet/"); |
104 // TODO(pauljensen): This is the API version not the implementation | 101 builder.append(ImplVersion.CRONET_VERSION); |
105 // version. The implementation version may be more appropriate for the | |
106 // UserAgent but is not available until after the CronetEngine is | |
107 // instantiated. Down the road, if the implementation is loaded via | |
108 // other means, this should be replaced with the implementation version. | |
109 builder.append(ApiVersion.CRONET_VERSION); | |
110 } | 102 } |
111 } | 103 } |
OLD | NEW |