Adding User-Agent to URI photo requests in Dialer.
- Adding stub UserAgentGenerator implementation in ContactsCommon; this
returns null to retain existing functionality.
- Modify ContactPhotoManager to include User Agent in requests for Uri
photos.
Bug: 15189476
Change-Id: I911e3a521587409bd729a218738985ff2232bf1e
diff --git a/src/com/android/contacts/common/ContactPhotoManager.java b/src/com/android/contacts/common/ContactPhotoManager.java
index 12faadb..271136b 100644
--- a/src/com/android/contacts/common/ContactPhotoManager.java
+++ b/src/com/android/contacts/common/ContactPhotoManager.java
@@ -57,14 +57,17 @@
import com.android.contacts.common.lettertiles.LetterTileDrawable;
import com.android.contacts.common.util.BitmapUtil;
import com.android.contacts.common.util.UriUtils;
+import com.android.contacts.commonbind.util.UserAgentGenerator;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
+import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
@@ -674,6 +677,11 @@
/** For debug: How many times we had to reload cached photo for a fresh entry. Should be 0. */
private final AtomicInteger mFreshCacheOverwrite = new AtomicInteger();
+ /**
+ * The user agent string to use when loading URI based photos.
+ */
+ private String mUserAgent;
+
public ContactPhotoManagerImpl(Context context) {
mContext = context;
@@ -713,6 +721,12 @@
mThumbnailSize = context.getResources().getDimensionPixelSize(
R.dimen.contact_browser_list_item_photo_size);
+
+ // Get a user agent string to use for URI photo requests.
+ mUserAgent = UserAgentGenerator.getUserAgent(context);
+ if (mUserAgent == null) {
+ mUserAgent = "";
+ }
}
/** Converts bytes to K bytes, rounding up. Used only for debug log. */
@@ -1515,7 +1529,19 @@
final String scheme = uri.getScheme();
InputStream is = null;
if (scheme.equals("http") || scheme.equals("https")) {
- is = new URL(uri.toString()).openStream();
+ final HttpURLConnection connection =
+ (HttpURLConnection) new URL(uri.toString()).openConnection();
+
+ // Include the user agent if it is specified.
+ if (!TextUtils.isEmpty(mUserAgent)) {
+ connection.setRequestProperty("User-Agent", mUserAgent);
+ }
+ try {
+ is = connection.getInputStream();
+ } catch (IOException e) {
+ connection.disconnect();
+ is = null;
+ }
} else {
is = mResolver.openInputStream(uri);
}
diff --git a/src/com/android/contacts/commonbind/util/UserAgentGenerator.java b/src/com/android/contacts/commonbind/util/UserAgentGenerator.java
new file mode 100644
index 0000000..13bcaca
--- /dev/null
+++ b/src/com/android/contacts/commonbind/util/UserAgentGenerator.java
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.android.contacts.commonbind.util;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.Build;
+
+/**
+ * Generates a user agent string for the application.
+ */
+public class UserAgentGenerator {
+ /**
+ * Builds a user agent string for the current application. No default implementation.
+ *
+ * @param context The context.
+ * @return The user agent string.
+ */
+ public static String getUserAgent(Context context) {
+ return null;
+ }
+}