1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-04 18:29:23 +02:00

Network code changes

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1185 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Vasiliy Bout 2010-04-06 15:59:13 +00:00
parent 33a151e34e
commit 0be03fa5ad
7 changed files with 38 additions and 29 deletions

View file

@ -3,12 +3,12 @@ DONE нарисовать иконку (скопирована иконка ic_m
NP улучшить определение размеров иконок NP улучшить определение размеров иконок
** сделать стартовые иконки прозрачными ** сделать стартовые иконки прозрачными
** перенести User-Agent в connection.setRequestProperty();
DONE переместить в главном меню наверх (вместо Settings) DONE переместить в главном меню наверх (вместо Settings)
DONE пункт меню "delete sample" DONE пункт меню "delete sample"
DONE BookDownloaderService - i18n DONE BookDownloaderService - i18n
DONE указывать HTTP-agent = "FBReader/<version>(java)" -- номер версии смотреть, как в диалоге About DONE указывать HTTP-agent = "FBReader/<version>(java)" -- номер версии смотреть, как в диалоге About
DONE перенести User-Agent в connection.setRequestProperty();
DONE прятать Profile (прятать запрещенные каталоги) DONE прятать Profile (прятать запрещенные каталоги)
DONE при щелчке на книжку - сделать default action, и о нем спрашивать. DONE при щелчке на книжку - сделать default action, и о нем спрашивать.

View file

@ -38,6 +38,7 @@ import android.widget.RemoteViews;
import android.widget.Toast; import android.widget.Toast;
import org.geometerplus.zlibrary.core.resources.ZLResource; import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.util.ZLNetworkUtil;
import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.network.BookReference; import org.geometerplus.fbreader.network.BookReference;
@ -272,15 +273,22 @@ public class BookDownloaderService extends Service {
try { try {
final URL url = new URL(urlString); final URL url = new URL(urlString);
final URLConnection connection = url.openConnection(); final URLConnection connection = url.openConnection();
final int fileLength = connection.getContentLength(); if (!(connection instanceof HttpURLConnection)) {
return; // 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) {
final int fileLength = httpConnection.getContentLength();
int downloadedPart = 0; int downloadedPart = 0;
long progressTime = System.currentTimeMillis() + updateIntervalMillis; long progressTime = System.currentTimeMillis() + updateIntervalMillis;
if (fileLength <= 0) { if (fileLength <= 0) {
progressHandler.sendEmptyMessage(-1); progressHandler.sendEmptyMessage(-1);
} }
final HttpURLConnection httpConnection = (HttpURLConnection)connection;
final int response = httpConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
OutputStream outStream = new FileOutputStream(file); OutputStream outStream = new FileOutputStream(file);
try { try {
InputStream inStream = httpConnection.getInputStream(); InputStream inStream = httpConnection.getInputStream();
@ -314,9 +322,12 @@ public class BookDownloaderService extends Service {
downloadSuccess = true; downloadSuccess = true;
} }
} catch (MalformedURLException e) { } 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) { } catch (IOException e) {
// TODO: error message; remove file, don't start FBReader // TODO: error message
} finally { } finally {
downloadFinishHandler.sendEmptyMessage(downloadSuccess ? 1 : 0); downloadFinishHandler.sendEmptyMessage(downloadSuccess ? 1 : 0);
if (!downloadSuccess) { if (!downloadSuccess) {

View file

@ -23,6 +23,7 @@ import java.io.*;
import java.net.*; import java.net.*;
import org.geometerplus.zlibrary.core.image.ZLSingleImage; import org.geometerplus.zlibrary.core.image.ZLSingleImage;
import org.geometerplus.zlibrary.core.util.ZLNetworkUtil;
import org.geometerplus.fbreader.Constants; import org.geometerplus.fbreader.Constants;
@ -181,7 +182,15 @@ public final class NetworkImage extends ZLSingleImage {
try { try {
final URL url = new URL(myUrl); final URL url = new URL(myUrl);
final URLConnection connection = url.openConnection(); final URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection)) {
// TODO: error message ???
return;
}
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(); final int response = httpConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) { if (response == HttpURLConnection.HTTP_OK) {
OutputStream outStream = new FileOutputStream(imageFile); OutputStream outStream = new FileOutputStream(imageFile);

View file

@ -58,11 +58,13 @@ class OPDSCatalogItem extends NetworkCatalogItem {
final URL url = new URL(urlString); final URL url = new URL(urlString);
final URLConnection connection = url.openConnection(); final URLConnection connection = url.openConnection();
if (!(connection instanceof HttpURLConnection)) { if (!(connection instanceof HttpURLConnection)) {
break; return null; // TODO: return error/information message???
} }
final HttpURLConnection httpConnection = (HttpURLConnection) connection; final HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setConnectTimeout(15000); // FIXME: hardcoded timeout value!!! httpConnection.setConnectTimeout(15000); // FIXME: hardcoded timeout value!!!
httpConnection.setReadTimeout(30000); // FIXME: hardcoded timeout value!!!
httpConnection.setRequestProperty("Connection", "Close"); httpConnection.setRequestProperty("Connection", "Close");
httpConnection.setRequestProperty("User-Agent", ZLNetworkUtil.getUserAgent());
final int response = httpConnection.getResponseCode(); final int response = httpConnection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) { if (response == HttpURLConnection.HTTP_OK) {
InputStream inStream = httpConnection.getInputStream(); InputStream inStream = httpConnection.getInputStream();

View file

@ -19,6 +19,8 @@
package org.geometerplus.zlibrary.core.util; package org.geometerplus.zlibrary.core.util;
import org.geometerplus.zlibrary.core.library.ZLibrary;
public class ZLNetworkUtil { public class ZLNetworkUtil {
public static String url(String baseUrl, String relativePath) { public static String url(String baseUrl, String relativePath) {
@ -112,7 +114,7 @@ public class ZLNetworkUtil {
return host; return host;
} }
public static String getUserAgent(String versionName) { public static String getUserAgent() {
return "FBReader/" + versionName + "(java)"; return "FBReader/" + ZLibrary.Instance().getVersionName() + "(java)";
} }
} }

View file

@ -23,12 +23,8 @@ import java.util.HashMap;
import android.app.Application; 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.options.ZLBooleanOption;
import org.geometerplus.zlibrary.core.sqliteconfig.ZLSQLiteConfig; 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.application.ZLAndroidApplicationWindow;
import org.geometerplus.zlibrary.ui.android.dialogs.ZLAndroidDialogManager; import org.geometerplus.zlibrary.ui.android.dialogs.ZLAndroidDialogManager;
@ -57,7 +53,6 @@ public class ZLAndroidApplication extends Application {
new ZLAndroidImageManager(); new ZLAndroidImageManager();
new ZLAndroidDialogManager(); new ZLAndroidDialogManager();
new ZLAndroidLibrary(this); new ZLAndroidLibrary(this);
System.setProperty("http.agent", ZLNetworkUtil.getUserAgent(ZLAndroidApplication.Instance().getVersionName()));
} }
public void onTerminate() { public void onTerminate() {
@ -77,14 +72,4 @@ public class ZLAndroidApplication extends Application {
} }
private final HashMap myData = new HashMap(); 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;
}
}
} }

View file

@ -94,7 +94,7 @@ public final class ZLAndroidLibrary extends ZLibrary {
@Override @Override
public String getVersionName() { public String getVersionName() {
try { try {
return myActivity.getPackageManager().getPackageInfo(myActivity.getPackageName(), 0).versionName; return myApplication.getPackageManager().getPackageInfo(myApplication.getPackageName(), 0).versionName;
} catch (Exception e) { } catch (Exception e) {
return ""; return "";
} }