diff --git a/TODO.network b/TODO.network index 90ccc9765..aba8896ab 100644 --- a/TODO.network +++ b/TODO.network @@ -3,12 +3,12 @@ DONE нарисовать иконку (скопирована иконка ic_m NP улучшить определение размеров иконок ** сделать стартовые иконки прозрачными -** перенести User-Agent в connection.setRequestProperty(); DONE переместить в главном меню наверх (вместо Settings) DONE пункт меню "delete sample" DONE BookDownloaderService - i18n DONE указывать HTTP-agent = "FBReader/(java)" -- номер версии смотреть, как в диалоге About + DONE перенести User-Agent в connection.setRequestProperty(); DONE прятать Profile (прятать запрещенные каталоги) DONE при щелчке на книжку - сделать default action, и о нем спрашивать. diff --git a/src/org/geometerplus/android/fbreader/network/BookDownloaderService.java b/src/org/geometerplus/android/fbreader/network/BookDownloaderService.java index 14495167b..99564425b 100644 --- a/src/org/geometerplus/android/fbreader/network/BookDownloaderService.java +++ b/src/org/geometerplus/android/fbreader/network/BookDownloaderService.java @@ -38,6 +38,7 @@ import android.widget.RemoteViews; import android.widget.Toast; import org.geometerplus.zlibrary.core.resources.ZLResource; +import org.geometerplus.zlibrary.core.util.ZLNetworkUtil; import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.fbreader.network.BookReference; @@ -272,15 +273,22 @@ public class BookDownloaderService extends Service { try { final URL url = new URL(urlString); final URLConnection connection = url.openConnection(); - final int fileLength = connection.getContentLength(); - int downloadedPart = 0; - long progressTime = System.currentTimeMillis() + updateIntervalMillis; - if (fileLength <= 0) { - progressHandler.sendEmptyMessage(-1); + if (!(connection instanceof HttpURLConnection)) { + return; // TODO: return error/information message??? } - final HttpURLConnection httpConnection = (HttpURLConnection)connection; + final HttpURLConnection httpConnection = (HttpURLConnection) connection; + httpConnection.setConnectTimeout(15000); // FIXME: hardcoded timeout value!!! + httpConnection.setReadTimeout(30000); // FIXME: hardcoded timeout value!!! + httpConnection.setRequestProperty("Connection", "Close"); + httpConnection.setRequestProperty("User-Agent", ZLNetworkUtil.getUserAgent()); final int response = httpConnection.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { + final int fileLength = httpConnection.getContentLength(); + int downloadedPart = 0; + long progressTime = System.currentTimeMillis() + updateIntervalMillis; + if (fileLength <= 0) { + progressHandler.sendEmptyMessage(-1); + } OutputStream outStream = new FileOutputStream(file); try { InputStream inStream = httpConnection.getInputStream(); @@ -314,9 +322,12 @@ public class BookDownloaderService extends Service { downloadSuccess = true; } } catch (MalformedURLException e) { - // TODO: error message; remove file, don't start FBReader + // TODO: error message + } catch (SocketTimeoutException ex) { + // TODO: error message + // error message : NetworkErrors.errorMessage("operationTimedOutMessage"); } catch (IOException e) { - // TODO: error message; remove file, don't start FBReader + // TODO: error message } finally { downloadFinishHandler.sendEmptyMessage(downloadSuccess ? 1 : 0); if (!downloadSuccess) { diff --git a/src/org/geometerplus/fbreader/network/NetworkImage.java b/src/org/geometerplus/fbreader/network/NetworkImage.java index 2821b554b..adc87987a 100644 --- a/src/org/geometerplus/fbreader/network/NetworkImage.java +++ b/src/org/geometerplus/fbreader/network/NetworkImage.java @@ -23,6 +23,7 @@ import java.io.*; import java.net.*; import org.geometerplus.zlibrary.core.image.ZLSingleImage; +import org.geometerplus.zlibrary.core.util.ZLNetworkUtil; import org.geometerplus.fbreader.Constants; @@ -181,7 +182,15 @@ public final class NetworkImage extends ZLSingleImage { try { final URL url = new URL(myUrl); final URLConnection connection = url.openConnection(); - final HttpURLConnection httpConnection = (HttpURLConnection)connection; + if (!(connection instanceof HttpURLConnection)) { + // TODO: error message ??? + return; + } + final HttpURLConnection httpConnection = (HttpURLConnection) connection; + httpConnection.setConnectTimeout(15000); // FIXME: hardcoded timeout value!!! + httpConnection.setReadTimeout(30000); // FIXME: hardcoded timeout value!!! + httpConnection.setRequestProperty("Connection", "Close"); + httpConnection.setRequestProperty("User-Agent", ZLNetworkUtil.getUserAgent()); final int response = httpConnection.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { OutputStream outStream = new FileOutputStream(imageFile); diff --git a/src/org/geometerplus/fbreader/network/opds/OPDSCatalogItem.java b/src/org/geometerplus/fbreader/network/opds/OPDSCatalogItem.java index 8dc57abe6..ec34adf03 100644 --- a/src/org/geometerplus/fbreader/network/opds/OPDSCatalogItem.java +++ b/src/org/geometerplus/fbreader/network/opds/OPDSCatalogItem.java @@ -58,11 +58,13 @@ class OPDSCatalogItem extends NetworkCatalogItem { final URL url = new URL(urlString); final URLConnection connection = url.openConnection(); if (!(connection instanceof HttpURLConnection)) { - break; + return null; // TODO: return error/information message??? } final HttpURLConnection httpConnection = (HttpURLConnection) connection; httpConnection.setConnectTimeout(15000); // FIXME: hardcoded timeout value!!! + httpConnection.setReadTimeout(30000); // FIXME: hardcoded timeout value!!! httpConnection.setRequestProperty("Connection", "Close"); + httpConnection.setRequestProperty("User-Agent", ZLNetworkUtil.getUserAgent()); final int response = httpConnection.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { InputStream inStream = httpConnection.getInputStream(); diff --git a/src/org/geometerplus/zlibrary/core/util/ZLNetworkUtil.java b/src/org/geometerplus/zlibrary/core/util/ZLNetworkUtil.java index 8ed929f70..54bb3ce7a 100644 --- a/src/org/geometerplus/zlibrary/core/util/ZLNetworkUtil.java +++ b/src/org/geometerplus/zlibrary/core/util/ZLNetworkUtil.java @@ -19,6 +19,8 @@ package org.geometerplus.zlibrary.core.util; +import org.geometerplus.zlibrary.core.library.ZLibrary; + public class ZLNetworkUtil { public static String url(String baseUrl, String relativePath) { @@ -112,7 +114,7 @@ public class ZLNetworkUtil { return host; } - public static String getUserAgent(String versionName) { - return "FBReader/" + versionName + "(java)"; + public static String getUserAgent() { + return "FBReader/" + ZLibrary.Instance().getVersionName() + "(java)"; } } diff --git a/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidApplication.java b/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidApplication.java index b9a78afa2..d42296eba 100644 --- a/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidApplication.java +++ b/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidApplication.java @@ -23,12 +23,8 @@ import java.util.HashMap; import android.app.Application; -import android.content.ComponentName; -import android.content.pm.PackageInfo; - import org.geometerplus.zlibrary.core.options.ZLBooleanOption; import org.geometerplus.zlibrary.core.sqliteconfig.ZLSQLiteConfig; -import org.geometerplus.zlibrary.core.util.ZLNetworkUtil; import org.geometerplus.zlibrary.ui.android.application.ZLAndroidApplicationWindow; import org.geometerplus.zlibrary.ui.android.dialogs.ZLAndroidDialogManager; @@ -57,7 +53,6 @@ public class ZLAndroidApplication extends Application { new ZLAndroidImageManager(); new ZLAndroidDialogManager(); new ZLAndroidLibrary(this); - System.setProperty("http.agent", ZLNetworkUtil.getUserAgent(ZLAndroidApplication.Instance().getVersionName())); } public void onTerminate() { @@ -77,14 +72,4 @@ public class ZLAndroidApplication extends Application { } private final HashMap myData = new HashMap(); - - public String getVersionName() { - try { - ComponentName comp = new ComponentName(this, this.getClass()); - PackageInfo pinfo = getPackageManager().getPackageInfo(comp.getPackageName(), 0); - return pinfo.versionName; - } catch (android.content.pm.PackageManager.NameNotFoundException ex) { - return null; - } - } } diff --git a/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidLibrary.java b/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidLibrary.java index 2c016d4c0..6f3f93562 100644 --- a/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidLibrary.java +++ b/src/org/geometerplus/zlibrary/ui/android/library/ZLAndroidLibrary.java @@ -94,7 +94,7 @@ public final class ZLAndroidLibrary extends ZLibrary { @Override public String getVersionName() { try { - return myActivity.getPackageManager().getPackageInfo(myActivity.getPackageName(), 0).versionName; + return myApplication.getPackageManager().getPackageInfo(myApplication.getPackageName(), 0).versionName; } catch (Exception e) { return ""; }