1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-03 17:59:33 +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 улучшить определение размеров иконок
** сделать стартовые иконки прозрачными
** перенести User-Agent в connection.setRequestProperty();
DONE переместить в главном меню наверх (вместо Settings)
DONE пункт меню "delete sample"
DONE BookDownloaderService - i18n
DONE указывать HTTP-agent = "FBReader/<version>(java)" -- номер версии смотреть, как в диалоге About
DONE перенести User-Agent в connection.setRequestProperty();
DONE прятать Profile (прятать запрещенные каталоги)
DONE при щелчке на книжку - сделать default action, и о нем спрашивать.

View file

@ -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) {

View file

@ -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);

View file

@ -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();

View file

@ -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)";
}
}

View file

@ -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;
}
}
}

View file

@ -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 "";
}