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