Bug 792992 - Switch URL usage to URI to prevent unnecessary network calls. r=rnewman

This commit is contained in:
Eugen Sawin 2015-02-09 22:06:22 +01:00
Родитель cb0eb48887
Коммит e0db0f1b3a
2 изменённых файлов: 31 добавлений и 22 удалений

Просмотреть файл

@ -44,6 +44,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
@ -348,32 +349,32 @@ public class UpdateService extends IntentService {
}
}
private URLConnection openConnectionWithProxy(URL url) throws java.net.URISyntaxException, java.io.IOException {
Log.i(LOGTAG, "opening connection with url: " + url);
private URLConnection openConnectionWithProxy(URI uri) throws java.net.MalformedURLException, java.io.IOException {
Log.i(LOGTAG, "opening connection with URI: " + uri);
ProxySelector ps = ProxySelector.getDefault();
Proxy proxy = Proxy.NO_PROXY;
if (ps != null) {
List<Proxy> proxies = ps.select(url.toURI());
List<Proxy> proxies = ps.select(uri);
if (proxies != null && !proxies.isEmpty()) {
proxy = proxies.get(0);
}
}
return url.openConnection(proxy);
return uri.toURL().openConnection(proxy);
}
private UpdateInfo findUpdate(boolean force) {
try {
URL url = getUpdateUrl(force);
URI uri = getUpdateURI(force);
if (url == null) {
Log.e(LOGTAG, "failed to get update URL");
if (uri == null) {
Log.e(LOGTAG, "failed to get update URI");
return null;
}
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document dom = builder.parse(openConnectionWithProxy(url).getInputStream());
Document dom = builder.parse(openConnectionWithProxy(uri).getInputStream());
NodeList nodes = dom.getElementsByTagName("update");
if (nodes == null || nodes.getLength() == 0)
@ -401,7 +402,7 @@ public class UpdateService extends IntentService {
// Fill in UpdateInfo from the XML data
UpdateInfo info = new UpdateInfo();
info.url = new URL(urlNode.getTextContent());
info.uri = new URI(urlNode.getTextContent());
info.buildID = buildIdNode.getTextContent();
info.hashFunction = hashFunctionNode.getTextContent();
info.hashValue = hashValueNode.getTextContent();
@ -505,9 +506,17 @@ public class UpdateService extends IntentService {
}
private File downloadUpdatePackage(UpdateInfo info, boolean overwriteExisting) {
URL url = null;
try {
url = info.uri.toURL();
} catch (java.net.MalformedURLException e) {
Log.e(LOGTAG, "failed to read URL: ", e);
return null;
}
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
path.mkdirs();
String fileName = new File(info.url.getFile()).getName();
String fileName = new File(url.getFile()).getName();
File downloadFile = new File(path, fileName);
if (!overwriteExisting && info.buildID.equals(getLastBuildID()) && downloadFile.exists()) {
@ -546,7 +555,7 @@ public class UpdateService extends IntentService {
mWifiLock.acquire();
}
URLConnection conn = openConnectionWithProxy(info.url);
URLConnection conn = openConnectionWithProxy(info.uri);
int length = conn.getContentLength();
output = new BufferedOutputStream(new FileOutputStream(downloadFile));
@ -709,8 +718,8 @@ public class UpdateService extends IntentService {
editor.commit();
}
private URL getUpdateUrl(boolean force) {
return UpdateServiceHelper.expandUpdateUrl(this, mPrefs.getString(KEY_UPDATE_URL, null), force);
private URI getUpdateURI(boolean force) {
return UpdateServiceHelper.expandUpdateURI(this, mPrefs.getString(KEY_UPDATE_URL, null), force);
}
private void setUpdateUrl(String url) {
@ -729,7 +738,7 @@ public class UpdateService extends IntentService {
}
private class UpdateInfo {
public URL url;
public URI uri;
public String buildID;
public String hashFunction;
public String hashValue;
@ -740,13 +749,13 @@ public class UpdateService extends IntentService {
}
public boolean isValid() {
return url != null && isNonEmpty(buildID) &&
return uri != null && isNonEmpty(buildID) &&
isNonEmpty(hashFunction) && isNonEmpty(hashValue) && size > 0;
}
@Override
public String toString() {
return "url = " + url + ", buildID = " + buildID + ", hashFunction = " + hashFunction + ", hashValue = " + hashValue + ", size = " + size;
return "uri = " + uri + ", buildID = " + buildID + ", hashFunction = " + hashFunction + ", hashValue = " + hashValue + ", size = " + size;
}
}
}

Просмотреть файл

@ -17,7 +17,7 @@ import android.content.pm.ApplicationInfo;
import android.os.Build;
import android.util.Log;
import java.net.URL;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
@ -87,8 +87,8 @@ public class UpdateServiceHelper {
isEnabled = enabled;
}
public static URL expandUpdateUrl(Context context, String updateUrl, boolean force) {
if (updateUrl == null) {
public static URI expandUpdateURI(Context context, String updateUri, boolean force) {
if (updateUri == null) {
return null;
}
@ -112,7 +112,7 @@ public class UpdateServiceHelper {
Log.i(LOGTAG, "Failed to read update locale file, falling back to " + locale);
}
String url = updateUrl.replace("%PRODUCT%", AppConstants.MOZ_APP_BASENAME)
String url = updateUri.replace("%PRODUCT%", AppConstants.MOZ_APP_BASENAME)
.replace("%VERSION%", AppConstants.MOZ_APP_VERSION)
.replace("%BUILD_ID%", force ? "0" : AppConstants.MOZ_APP_BUILDID)
.replace("%BUILD_TARGET%", "Android_" + AppConstants.MOZ_APP_ABI + pkgSpecial)
@ -124,8 +124,8 @@ public class UpdateServiceHelper {
.replace("%MOZ_VERSION%", AppConstants.MOZILLA_VERSION);
try {
return new URL(url);
} catch (java.net.MalformedURLException e) {
return new URI(url);
} catch (java.net.URISyntaxException e) {
Log.e(LOGTAG, "Failed to create update url: ", e);
return null;
}