1
0
Fork 0
mirror of https://github.com/geometer/FBReaderJ.git synced 2025-10-05 10:49:24 +02:00

refactoring: INetworkLink constants replaced by UrlInfo.Type elements

This commit is contained in:
Nikolay Pultsin 2011-04-16 00:21:27 +01:00
parent 5c2775b46e
commit aa590a5002
15 changed files with 119 additions and 86 deletions

View file

@ -39,6 +39,7 @@ import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.network.*; import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.opds.OPDSCustomLink; import org.geometerplus.fbreader.network.opds.OPDSCustomLink;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.util.UIUtil;
@ -89,7 +90,7 @@ public class AddCustomCatalogActivity extends Activity {
final Uri uri = intent.getData(); final Uri uri = intent.getData();
if (myLink != null) { if (myLink != null) {
setTextById(R.id.add_custom_catalog_url, myLink.getUrlInfo(INetworkLink.URL_MAIN).URL); setTextById(R.id.add_custom_catalog_url, myLink.getUrlInfo(UrlInfo.Type.Catalog).URL);
setTextById(R.id.add_custom_catalog_title, myLink.getTitle()); setTextById(R.id.add_custom_catalog_title, myLink.getTitle());
setTextById(R.id.add_custom_catalog_summary, myLink.getSummary()); setTextById(R.id.add_custom_catalog_summary, myLink.getSummary());
setExtraFieldsVisibility(true); setExtraFieldsVisibility(true);
@ -130,7 +131,7 @@ public class AddCustomCatalogActivity extends Activity {
} else { } else {
myLink.setTitle(title); myLink.setTitle(title);
myLink.setSummary(summary); myLink.setSummary(summary);
myLink.setUrl(INetworkLink.URL_MAIN, uri.toString()); myLink.setUrl(UrlInfo.Type.Catalog, uri.toString());
Intent intent = new Intent( Intent intent = new Intent(
NetworkLibraryActivity.ADD_CATALOG, NetworkLibraryActivity.ADD_CATALOG,
@ -240,8 +241,8 @@ public class AddCustomCatalogActivity extends Activity {
setErrorByKey("invalidUrl"); setErrorByKey("invalidUrl");
return; return;
} }
final HashMap<String,UrlInfoWithDate> infos = new HashMap<String,UrlInfoWithDate>(); final HashMap<UrlInfo.Type,UrlInfoWithDate> infos = new HashMap<UrlInfo.Type,UrlInfoWithDate>();
infos.put(INetworkLink.URL_MAIN, new UrlInfoWithDate(textUrl)); infos.put(UrlInfo.Type.Catalog, new UrlInfoWithDate(textUrl));
myLink = new OPDSCustomLink( myLink = new OPDSCustomLink(
ICustomNetworkLink.INVALID_ID, siteName, null, null, infos ICustomNetworkLink.INVALID_ID, siteName, null, null, infos
); );

View file

@ -41,6 +41,7 @@ import org.geometerplus.android.util.UIUtil;
import org.geometerplus.fbreader.network.*; import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.opds.OPDSCustomLink; import org.geometerplus.fbreader.network.opds.OPDSCustomLink;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
public class NetworkLibraryActivity extends NetworkBaseActivity { public class NetworkLibraryActivity extends NetworkBaseActivity {
@ -52,7 +53,7 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
private static final String ADD_CATALOG_URLS_MAP_KEY = "urls"; private static final String ADD_CATALOG_URLS_MAP_KEY = "urls";
static void addLinkToIntent(Intent intent, ICustomNetworkLink link) { static void addLinkToIntent(Intent intent, ICustomNetworkLink link) {
final String textUrl = link.getUrlInfo(INetworkLink.URL_MAIN).URL; final String textUrl = link.getUrlInfo(UrlInfo.Type.Catalog).URL;
intent.setData(Uri.parse(textUrl)); intent.setData(Uri.parse(textUrl));
intent intent
.putExtra(ADD_CATALOG_TITLE_KEY, link.getTitle()) .putExtra(ADD_CATALOG_TITLE_KEY, link.getTitle())
@ -72,7 +73,7 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
uri.getHost(), uri.getHost(),
intent.getStringExtra(ADD_CATALOG_TITLE_KEY), intent.getStringExtra(ADD_CATALOG_TITLE_KEY),
intent.getStringExtra(ADD_CATALOG_SUMMARY_KEY), intent.getStringExtra(ADD_CATALOG_SUMMARY_KEY),
(HashMap<String,UrlInfoWithDate>)intent.getSerializableExtra(ADD_CATALOG_URLS_MAP_KEY) (HashMap<UrlInfo.Type,UrlInfoWithDate>)intent.getSerializableExtra(ADD_CATALOG_URLS_MAP_KEY)
); );
} }

View file

@ -30,6 +30,7 @@ import org.geometerplus.zlibrary.ui.android.library.ZLAndroidApplication;
import org.geometerplus.fbreader.network.ICustomNetworkLink; import org.geometerplus.fbreader.network.ICustomNetworkLink;
import org.geometerplus.fbreader.network.NetworkDatabase; import org.geometerplus.fbreader.network.NetworkDatabase;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
import org.geometerplus.android.util.SQLiteUtil; import org.geometerplus.android.util.SQLiteUtil;
@ -77,7 +78,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
@Override @Override
protected void loadCustomLinks(ICustomLinksHandler handler) { protected void loadCustomLinks(ICustomLinksHandler handler) {
final Cursor cursor = myDatabase.rawQuery("SELECT link_id,title,site_name,summary FROM Links", null); final Cursor cursor = myDatabase.rawQuery("SELECT link_id,title,site_name,summary FROM Links", null);
final HashMap<String,UrlInfoWithDate> linksMap = new HashMap<String,UrlInfoWithDate>(); final HashMap<UrlInfo.Type,UrlInfoWithDate> linksMap = new HashMap<UrlInfo.Type,UrlInfoWithDate>();
while (cursor.moveToNext()) { while (cursor.moveToNext()) {
final int id = cursor.getInt(0); final int id = cursor.getInt(0);
final String title = cursor.getString(1); final String title = cursor.getString(1);
@ -88,7 +89,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url,update_time FROM LinkUrls WHERE link_id = " + id, null); final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url,update_time FROM LinkUrls WHERE link_id = " + id, null);
while (linksCursor.moveToNext()) { while (linksCursor.moveToNext()) {
linksMap.put( linksMap.put(
linksCursor.getString(0), UrlInfo.Type.fromFixedName(linksCursor.getString(0)),
new UrlInfoWithDate( new UrlInfoWithDate(
linksCursor.getString(1), linksCursor.getString(1),
SQLiteUtil.getDate(linksCursor, 2) SQLiteUtil.getDate(linksCursor, 2)
@ -134,7 +135,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
SQLiteUtil.bindString(statement, 3, link.getSummary()); SQLiteUtil.bindString(statement, 3, link.getSummary());
final long id; final long id;
final HashMap<String,UrlInfoWithDate> linksMap = new HashMap<String,UrlInfoWithDate>(); final HashMap<UrlInfo.Type,UrlInfoWithDate> linksMap = new HashMap<UrlInfo.Type,UrlInfoWithDate>();
if (statement == myInsertCustomLinkStatement) { if (statement == myInsertCustomLinkStatement) {
id = statement.executeInsert(); id = statement.executeInsert();
@ -147,7 +148,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url,update_time FROM LinkUrls WHERE link_id = " + link.getId(), null); final Cursor linksCursor = myDatabase.rawQuery("SELECT key,url,update_time FROM LinkUrls WHERE link_id = " + link.getId(), null);
while (linksCursor.moveToNext()) { while (linksCursor.moveToNext()) {
linksMap.put( linksMap.put(
linksCursor.getString(0), UrlInfo.Type.fromFixedName(linksCursor.getString(0)),
new UrlInfoWithDate( new UrlInfoWithDate(
linksCursor.getString(1), linksCursor.getString(1),
SQLiteUtil.getDate(linksCursor, 2) SQLiteUtil.getDate(linksCursor, 2)
@ -157,7 +158,7 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
linksCursor.close(); linksCursor.close();
} }
for (String key : link.getUrlKeys()) { for (UrlInfo.Type key : link.getUrlKeys()) {
final UrlInfoWithDate info = link.getUrlInfo(key); final UrlInfoWithDate info = link.getUrlInfo(key);
final UrlInfoWithDate dbInfo = linksMap.remove(key); final UrlInfoWithDate dbInfo = linksMap.remove(key);
final SQLiteStatement urlStatement; final SQLiteStatement urlStatement;
@ -179,16 +180,16 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
SQLiteUtil.bindString(urlStatement, 1, info.URL); SQLiteUtil.bindString(urlStatement, 1, info.URL);
SQLiteUtil.bindDate(urlStatement, 2, info.Updated); SQLiteUtil.bindDate(urlStatement, 2, info.Updated);
urlStatement.bindLong(3, id); urlStatement.bindLong(3, id);
urlStatement.bindString(4, key); urlStatement.bindString(4, key.getFixedName());
urlStatement.execute(); urlStatement.execute();
} }
for (String key: linksMap.keySet()) { for (UrlInfo.Type key: linksMap.keySet()) {
if (myDeleteCustomLinkUrlStatement == null) { if (myDeleteCustomLinkUrlStatement == null) {
myDeleteCustomLinkUrlStatement = myDatabase.compileStatement( myDeleteCustomLinkUrlStatement = myDatabase.compileStatement(
"DELETE FROM LinkUrls WHERE link_id = ? AND key = ?"); "DELETE FROM LinkUrls WHERE link_id = ? AND key = ?");
} }
myDeleteCustomLinkUrlStatement.bindLong(1, id); myDeleteCustomLinkUrlStatement.bindLong(1, id);
myDeleteCustomLinkUrlStatement.bindString(2, key); myDeleteCustomLinkUrlStatement.bindString(2, key.getFixedName());
myDeleteCustomLinkUrlStatement.execute(); myDeleteCustomLinkUrlStatement.execute();
} }
} }

View file

@ -32,6 +32,7 @@ import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.fbreader.network.*; import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager; import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
import org.geometerplus.fbreader.network.tree.NetworkBookTree; import org.geometerplus.fbreader.network.tree.NetworkBookTree;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.android.util.PackageUtil; import org.geometerplus.android.util.PackageUtil;
@ -53,7 +54,7 @@ abstract class Util implements UserRegistrationConstants {
return testService( return testService(
activity, activity,
REGISTRATION_ACTION, REGISTRATION_ACTION,
link.getUrlInfo(INetworkLink.URL_SIGN_UP).URL link.getUrlInfo(UrlInfo.Type.SignUp).URL
); );
} }
@ -61,12 +62,12 @@ abstract class Util implements UserRegistrationConstants {
try { try {
final Intent intent = new Intent( final Intent intent = new Intent(
REGISTRATION_ACTION, REGISTRATION_ACTION,
Uri.parse(link.getUrlInfo(INetworkLink.URL_SIGN_UP).URL) Uri.parse(link.getUrlInfo(UrlInfo.Type.SignUp).URL)
); );
if (PackageUtil.canBeStarted(activity, intent, true)) { if (PackageUtil.canBeStarted(activity, intent, true)) {
activity.startActivityForResult(new Intent( activity.startActivityForResult(new Intent(
REGISTRATION_ACTION, REGISTRATION_ACTION,
Uri.parse(link.getUrlInfo(INetworkLink.URL_SIGN_UP).URL) Uri.parse(link.getUrlInfo(UrlInfo.Type.SignUp).URL)
), USER_REGISTRATION_REQUEST_CODE); ), USER_REGISTRATION_REQUEST_CODE);
} }
} catch (ActivityNotFoundException e) { } catch (ActivityNotFoundException e) {
@ -99,7 +100,7 @@ abstract class Util implements UserRegistrationConstants {
return testService( return testService(
activity, activity,
action, action,
link.getUrlInfo(INetworkLink.URL_MAIN).URL link.getUrlInfo(UrlInfo.Type.Catalog).URL
); );
} }
@ -107,7 +108,7 @@ abstract class Util implements UserRegistrationConstants {
try { try {
final Intent intent = new Intent( final Intent intent = new Intent(
action, action,
Uri.parse(link.getUrlInfo(INetworkLink.URL_MAIN).URL) Uri.parse(link.getUrlInfo(UrlInfo.Type.Catalog).URL)
); );
final NetworkAuthenticationManager mgr = link.authenticationManager(); final NetworkAuthenticationManager mgr = link.authenticationManager();
if (mgr != null) { if (mgr != null) {
@ -123,7 +124,7 @@ abstract class Util implements UserRegistrationConstants {
} }
static boolean isBrowserTopupSupported(Activity activity, INetworkLink link) { static boolean isBrowserTopupSupported(Activity activity, INetworkLink link) {
return link.getUrlInfo(INetworkLink.URL_TOPUP).URL != null; return link.getUrlInfo(UrlInfo.Type.TopUp).URL != null;
} }
static void openInBrowser(Context context, String url) { static void openInBrowser(Context context, String url) {

View file

@ -24,6 +24,7 @@ import java.util.*;
import org.geometerplus.zlibrary.core.util.ZLMiscUtil; import org.geometerplus.zlibrary.core.util.ZLMiscUtil;
import org.geometerplus.zlibrary.core.options.ZLStringListOption; import org.geometerplus.zlibrary.core.options.ZLStringListOption;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
public abstract class AbstractNetworkLink implements INetworkLink, Basket { public abstract class AbstractNetworkLink implements INetworkLink, Basket {
@ -31,7 +32,7 @@ public abstract class AbstractNetworkLink implements INetworkLink, Basket {
protected String myTitle; protected String myTitle;
protected String mySummary; protected String mySummary;
protected final String myLanguage; protected final String myLanguage;
protected final TreeMap<String,UrlInfoWithDate> myInfos; protected final TreeMap<UrlInfo.Type,UrlInfoWithDate> myInfos;
private ZLStringListOption myBooksInBasketOption; private ZLStringListOption myBooksInBasketOption;
@ -42,14 +43,14 @@ public abstract class AbstractNetworkLink implements INetworkLink, Basket {
* @param title title of the corresponding library item. Must be not <code>null</code>. * @param title title of the corresponding library item. Must be not <code>null</code>.
* @param summary description of the corresponding library item. Can be <code>null</code>. * @param summary description of the corresponding library item. Can be <code>null</code>.
* @param language language of the catalog. If <code>null</code> we assume this catalog is multilanguage. * @param language language of the catalog. If <code>null</code> we assume this catalog is multilanguage.
* @param infos map contains URL infos with their identifiers; must always contain one URL with <code>URL_MAIN</code> identifier * @param infos collection of URL infos; must always contain one URL with <code>UrlInfo.Type.Catalog</code> identifier
*/ */
public AbstractNetworkLink(String siteName, String title, String summary, String language, Map<String,UrlInfoWithDate> infos) { public AbstractNetworkLink(String siteName, String title, String summary, String language, Map<UrlInfo.Type,UrlInfoWithDate> infos) {
mySiteName = siteName; mySiteName = siteName;
myTitle = title; myTitle = title;
mySummary = summary; mySummary = summary;
myLanguage = language != null ? language : "multi"; myLanguage = language != null ? language : "multi";
myInfos = new TreeMap<String,UrlInfoWithDate>(infos); myInfos = new TreeMap<UrlInfo.Type,UrlInfoWithDate>(infos);
} }
public final String getSiteName() { public final String getSiteName() {
@ -68,16 +69,16 @@ public abstract class AbstractNetworkLink implements INetworkLink, Basket {
return myLanguage; return myLanguage;
} }
public final HashMap<String,UrlInfoWithDate> urlInfoMap() { public final HashMap<UrlInfo.Type,UrlInfoWithDate> urlInfoMap() {
return new HashMap<String,UrlInfoWithDate>(myInfos); return new HashMap<UrlInfo.Type,UrlInfoWithDate>(myInfos);
} }
public final UrlInfoWithDate getUrlInfo(String urlKey) { public final UrlInfoWithDate getUrlInfo(UrlInfo.Type type) {
final UrlInfoWithDate info = myInfos.get(urlKey); final UrlInfoWithDate info = myInfos.get(type);
return info != null ? info : UrlInfoWithDate.NULL; return info != null ? info : UrlInfoWithDate.NULL;
} }
public final Set<String> getUrlKeys() { public final Set<UrlInfo.Type> getUrlKeys() {
return myInfos.keySet(); return myInfos.keySet();
} }
@ -136,7 +137,7 @@ public abstract class AbstractNetworkLink implements INetworkLink, Basket {
@Override @Override
public String toString() { public String toString() {
String icon = getUrlInfo(URL_ICON).URL; String icon = getUrlInfo(UrlInfo.Type.Catalog).URL;
if (icon != null) { if (icon != null) {
if (icon.length() > 64) { if (icon.length() > 64) {
icon = icon.substring(0, 61) + "..."; icon = icon.substring(0, 61) + "...";

View file

@ -23,6 +23,7 @@ import java.util.HashMap;
import org.geometerplus.zlibrary.core.network.ZLNetworkException; import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
public interface ICustomNetworkLink extends INetworkLink { public interface ICustomNetworkLink extends INetworkLink {
@ -35,9 +36,9 @@ public interface ICustomNetworkLink extends INetworkLink {
void setTitle(String title); void setTitle(String title);
void setSummary(String summary); void setSummary(String summary);
HashMap<String,UrlInfoWithDate> urlInfoMap(); HashMap<UrlInfo.Type,UrlInfoWithDate> urlInfoMap();
void setUrl(String urlKey, String url); void setUrl(UrlInfo.Type type, String url);
void removeUrl(String urlKey); void removeUrl(UrlInfo.Type type);
boolean isObsolete(long milliSeconds); boolean isObsolete(long milliSeconds);
void reloadInfo(boolean urlsOnly) throws ZLNetworkException; void reloadInfo(boolean urlsOnly) throws ZLNetworkException;

View file

@ -24,24 +24,16 @@ import java.util.*;
import org.geometerplus.zlibrary.core.network.ZLNetworkRequest; import org.geometerplus.zlibrary.core.network.ZLNetworkRequest;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager; import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
public interface INetworkLink { public interface INetworkLink {
String URL_MAIN = "main";
String URL_SEARCH = "search";
String URL_ICON = "icon";
String URL_SIGN_IN = "signIn";
String URL_SIGN_OUT = "signOut";
String URL_SIGN_UP = "signUp";
String URL_TOPUP = "topup";
String URL_RECOVER_PASSWORD = "recoverPassword";
String getSiteName(); String getSiteName();
String getTitle(); String getTitle();
String getSummary(); String getSummary();
UrlInfoWithDate getUrlInfo(String urlKey); UrlInfoWithDate getUrlInfo(UrlInfo.Type type);
Set<String> getUrlKeys(); Set<UrlInfo.Type> getUrlKeys();
/** /**
* @return 2-letters language code or special token "multi" * @return 2-letters language code or special token "multi"

View file

@ -21,6 +21,7 @@ package org.geometerplus.fbreader.network;
import java.util.Map; import java.util.Map;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
public abstract class NetworkDatabase { public abstract class NetworkDatabase {
@ -37,7 +38,7 @@ public abstract class NetworkDatabase {
protected abstract void executeAsATransaction(Runnable actions); protected abstract void executeAsATransaction(Runnable actions);
public interface ICustomLinksHandler { public interface ICustomLinksHandler {
void handleCustomLinkData(int id, String siteName, String title, String summary, Map<String,UrlInfoWithDate> infos); void handleCustomLinkData(int id, String siteName, String title, String summary, Map<UrlInfo.Type,UrlInfoWithDate> infos);
} }
protected abstract void loadCustomLinks(ICustomLinksHandler handler); protected abstract void loadCustomLinks(ICustomLinksHandler handler);

View file

@ -33,6 +33,7 @@ import org.geometerplus.fbreader.tree.FBTree;
import org.geometerplus.fbreader.network.tree.*; import org.geometerplus.fbreader.network.tree.*;
import org.geometerplus.fbreader.network.opds.OPDSCustomLink; import org.geometerplus.fbreader.network.opds.OPDSCustomLink;
import org.geometerplus.fbreader.network.opds.OPDSLinkReader; import org.geometerplus.fbreader.network.opds.OPDSLinkReader;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
public class NetworkLibrary { public class NetworkLibrary {
@ -180,10 +181,10 @@ public class NetworkLibrary {
db.loadCustomLinks( db.loadCustomLinks(
new NetworkDatabase.ICustomLinksHandler() { new NetworkDatabase.ICustomLinksHandler() {
public void handleCustomLinkData(int id, String siteName, public void handleCustomLinkData(int id, String siteName,
String title, String summary, Map<String,UrlInfoWithDate> infos) { String title, String summary, Map<UrlInfo.Type,UrlInfoWithDate> infos) {
if (title != null && if (title != null &&
siteName != null && siteName != null &&
infos.get(INetworkLink.URL_MAIN) != null) { infos.get(UrlInfo.Type.Catalog) != null) {
final ICustomNetworkLink link = new OPDSCustomLink( final ICustomNetworkLink link = new OPDSCustomLink(
id, siteName, title, summary, infos id, siteName, title, summary, infos
); );

View file

@ -90,7 +90,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
sid = mySidOption.getValue(); sid = mySidOption.getValue();
} }
String url = Link.getUrlInfo(INetworkLink.URL_SIGN_IN).URL; String url = Link.getUrlInfo(UrlInfo.Type.SignIn).URL;
if (url == null) { if (url == null) {
throw new ZLNetworkException(NetworkException.ERROR_UNSUPPORTED_OPERATION); throw new ZLNetworkException(NetworkException.ERROR_UNSUPPORTED_OPERATION);
} }
@ -120,7 +120,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
@Override @Override
public void authorise(String password) throws ZLNetworkException { public void authorise(String password) throws ZLNetworkException {
String url = Link.getUrlInfo(INetworkLink.URL_SIGN_IN).URL; String url = Link.getUrlInfo(UrlInfo.Type.SignIn).URL;
if (url == null) { if (url == null) {
throw new ZLNetworkException(NetworkException.ERROR_UNSUPPORTED_OPERATION); throw new ZLNetworkException(NetworkException.ERROR_UNSUPPORTED_OPERATION);
} }
@ -240,7 +240,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
if (sid.length() == 0) { if (sid.length() == 0) {
return null; return null;
} }
final String url = Link.getUrlInfo(INetworkLink.URL_TOPUP).URL; final String url = Link.getUrlInfo(UrlInfo.Type.TopUp).URL;
if (url == null) { if (url == null) {
return null; return null;
} }
@ -400,7 +400,7 @@ public class LitResAuthenticationManager extends NetworkAuthenticationManager {
@Override @Override
public void recoverPassword(String email) throws ZLNetworkException { public void recoverPassword(String email) throws ZLNetworkException {
String url = Link.getUrlInfo(INetworkLink.URL_RECOVER_PASSWORD).URL; String url = Link.getUrlInfo(UrlInfo.Type.RecoverPassword).URL;
if (url == null) { if (url == null) {
throw new ZLNetworkException(NetworkException.ERROR_UNSUPPORTED_OPERATION); throw new ZLNetworkException(NetworkException.ERROR_UNSUPPORTED_OPERATION);
} }

View file

@ -31,6 +31,7 @@ import org.geometerplus.zlibrary.core.util.ZLMiscUtil;
import org.geometerplus.fbreader.network.ICustomNetworkLink; import org.geometerplus.fbreader.network.ICustomNetworkLink;
import org.geometerplus.fbreader.network.NetworkException; import org.geometerplus.fbreader.network.NetworkException;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLink { public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLink {
@ -45,7 +46,7 @@ public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLin
return siteName; return siteName;
} }
public OPDSCustomLink(int id, String siteName, String title, String summary, Map<String,UrlInfoWithDate> infos) { public OPDSCustomLink(int id, String siteName, String title, String summary, Map<UrlInfo.Type,UrlInfoWithDate> infos) {
super(removeWWWPrefix(siteName), title, summary, null, infos, false); super(removeWWWPrefix(siteName), title, summary, null, infos, false);
myId = id; myId = id;
} }
@ -81,26 +82,26 @@ public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLin
myTitle = title; myTitle = title;
} }
public final void setUrl(String urlKey, String url) { public final void setUrl(UrlInfo.Type type, String url) {
myInfos.put(urlKey, new UrlInfoWithDate(url, new Date())); myInfos.put(type, new UrlInfoWithDate(url, new Date()));
myHasChanges = true; myHasChanges = true;
} }
public final void removeUrl(String urlKey) { public final void removeUrl(UrlInfo.Type type) {
final UrlInfoWithDate oldUrl = myInfos.remove(urlKey); final UrlInfoWithDate oldUrl = myInfos.remove(type);
myHasChanges = myHasChanges || oldUrl != null; myHasChanges = myHasChanges || oldUrl != null;
} }
public boolean isObsolete(long milliSeconds) { public boolean isObsolete(long milliSeconds) {
final long old = System.currentTimeMillis() - milliSeconds; final long old = System.currentTimeMillis() - milliSeconds;
final Date searchUpdateDate = getUrlInfo(URL_SEARCH).Updated; Date updateDate = getUrlInfo(UrlInfo.Type.Search).Updated;
if (searchUpdateDate == null || searchUpdateDate.getTime() < old) { if (updateDate == null || updateDate.getTime() < old) {
return true; return true;
} }
final Date iconUpdateDate = getUrlInfo(URL_ICON).Updated; updateDate = getUrlInfo(UrlInfo.Type.Image).Updated;
if (iconUpdateDate == null || iconUpdateDate.getTime() < old) { if (updateDate == null || updateDate.getTime() < old) {
return true; return true;
} }
@ -113,7 +114,7 @@ public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLin
ZLNetworkException error = null; ZLNetworkException error = null;
try { try {
ZLNetworkManager.Instance().perform(new ZLNetworkRequest(getUrlInfo(URL_MAIN).URL) { ZLNetworkManager.Instance().perform(new ZLNetworkRequest(getUrlInfo(UrlInfo.Type.Catalog).URL) {
@Override @Override
public void handleStream(URLConnection connection, InputStream inputStream) throws IOException, ZLNetworkException { public void handleStream(URLConnection connection, InputStream inputStream) throws IOException, ZLNetworkException {
final CatalogInfoReader info = new CatalogInfoReader(URL, OPDSCustomLink.this, opensearchDescriptionURLs); final CatalogInfoReader info = new CatalogInfoReader(URL, OPDSCustomLink.this, opensearchDescriptionURLs);
@ -125,7 +126,7 @@ public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLin
if (info.Title == null) { if (info.Title == null) {
throw new ZLNetworkException(NetworkException.ERROR_NO_REQUIRED_INFORMATION); throw new ZLNetworkException(NetworkException.ERROR_NO_REQUIRED_INFORMATION);
} }
setUrl(URL_ICON, info.Icon); setUrl(UrlInfo.Type.Image, info.Icon);
if (info.DirectOpenSearchDescription != null) { if (info.DirectOpenSearchDescription != null) {
descriptions.add(info.DirectOpenSearchDescription); descriptions.add(info.DirectOpenSearchDescription);
} }
@ -161,9 +162,9 @@ public class OPDSCustomLink extends OPDSNetworkLink implements ICustomNetworkLin
if (!descriptions.isEmpty()) { if (!descriptions.isEmpty()) {
// TODO: May be do not use '%s'??? Use Description instead??? (this needs to rewrite SEARCH engine logic a little) // TODO: May be do not use '%s'??? Use Description instead??? (this needs to rewrite SEARCH engine logic a little)
setUrl(URL_SEARCH, descriptions.get(0).makeQuery("%s")); setUrl(UrlInfo.Type.Search, descriptions.get(0).makeQuery("%s"));
} else { } else {
setUrl(URL_SEARCH, null); setUrl(UrlInfo.Type.Search, null);
} }
if (error != null) { if (error != null) {
throw error; throw error;

View file

@ -32,6 +32,7 @@ import org.geometerplus.fbreader.network.atom.ATOMLink;
import org.geometerplus.fbreader.network.atom.ATOMUpdated; import org.geometerplus.fbreader.network.atom.ATOMUpdated;
import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager; import org.geometerplus.fbreader.network.authentication.NetworkAuthenticationManager;
import org.geometerplus.fbreader.network.authentication.litres.LitResAuthenticationManager; import org.geometerplus.fbreader.network.authentication.litres.LitResAuthenticationManager;
import org.geometerplus.fbreader.network.urlInfo.UrlInfo;
import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate; import org.geometerplus.fbreader.network.urlInfo.UrlInfoWithDate;
class OPDSLinkXMLReader extends OPDSXMLReader implements OPDSConstants, MimeTypes { class OPDSLinkXMLReader extends OPDSXMLReader implements OPDSConstants, MimeTypes {
@ -97,7 +98,7 @@ class OPDSLinkXMLReader extends OPDSXMLReader implements OPDSConstants, MimeType
final String summary = entry.Content; final String summary = entry.Content;
final String language = entry.DCLanguage; final String language = entry.DCLanguage;
final HashMap<String,UrlInfoWithDate> infos = new HashMap<String,UrlInfoWithDate>(); final HashMap<UrlInfo.Type,UrlInfoWithDate> infos = new HashMap<UrlInfo.Type,UrlInfoWithDate>();
final HashMap<String,NetworkCatalogItem.Accessibility> urlConditions = final HashMap<String,NetworkCatalogItem.Accessibility> urlConditions =
new HashMap<String,NetworkCatalogItem.Accessibility>(); new HashMap<String,NetworkCatalogItem.Accessibility>();
for (ATOMLink link: entry.Links) { for (ATOMLink link: entry.Links) {
@ -106,35 +107,34 @@ class OPDSLinkXMLReader extends OPDSXMLReader implements OPDSConstants, MimeType
final String rel = link.getRel(); final String rel = link.getRel();
if (rel == REL_IMAGE_THUMBNAIL || rel == REL_THUMBNAIL) { if (rel == REL_IMAGE_THUMBNAIL || rel == REL_THUMBNAIL) {
if (type == MIME_IMAGE_PNG || type == MIME_IMAGE_JPEG) { if (type == MIME_IMAGE_PNG || type == MIME_IMAGE_JPEG) {
infos.put(INetworkLink.URL_ICON, new UrlInfoWithDate(href)); infos.put(UrlInfo.Type.Thumbnail, new UrlInfoWithDate(href));
} }
} else if ((rel != null && rel.startsWith(REL_IMAGE_PREFIX)) || rel == REL_COVER) { } else if ((rel != null && rel.startsWith(REL_IMAGE_PREFIX)) || rel == REL_COVER) {
if (infos.get(INetworkLink.URL_ICON) == null && if (type == MIME_IMAGE_PNG || type == MIME_IMAGE_JPEG) {
(type == MIME_IMAGE_PNG || type == MIME_IMAGE_JPEG)) { infos.put(UrlInfo.Type.Image, new UrlInfoWithDate(href));
infos.put(INetworkLink.URL_ICON, new UrlInfoWithDate(href));
} }
} else if (rel == null) { } else if (rel == null) {
if (type == MIME_APP_ATOM) { if (type == MIME_APP_ATOM) {
infos.put(INetworkLink.URL_MAIN, new UrlInfoWithDate(href)); infos.put(UrlInfo.Type.Catalog, new UrlInfoWithDate(href));
} }
} else if (rel == "search") { } else if (rel == "search") {
if (type == MIME_APP_ATOM) { if (type == MIME_APP_ATOM) {
final OpenSearchDescription descr = OpenSearchDescription.createDefault(href); final OpenSearchDescription descr = OpenSearchDescription.createDefault(href);
if (descr.isValid()) { if (descr.isValid()) {
// TODO: May be do not use '%s'??? Use Description instead??? (this needs to rewrite SEARCH engine logic a little) // TODO: May be do not use '%s'??? Use Description instead??? (this needs to rewrite SEARCH engine logic a little)
infos.put(INetworkLink.URL_SEARCH, new UrlInfoWithDate(descr.makeQuery("%s"))); infos.put(UrlInfo.Type.Search, new UrlInfoWithDate(descr.makeQuery("%s")));
} }
} }
} else if (rel == REL_LINK_SIGN_IN) { } else if (rel == REL_LINK_SIGN_IN) {
infos.put(INetworkLink.URL_SIGN_IN, new UrlInfoWithDate(href)); infos.put(UrlInfo.Type.SignIn, new UrlInfoWithDate(href));
} else if (rel == REL_LINK_SIGN_OUT) { } else if (rel == REL_LINK_SIGN_OUT) {
infos.put(INetworkLink.URL_SIGN_OUT, new UrlInfoWithDate(href)); infos.put(UrlInfo.Type.SignOut, new UrlInfoWithDate(href));
} else if (rel == REL_LINK_SIGN_UP) { } else if (rel == REL_LINK_SIGN_UP) {
infos.put(INetworkLink.URL_SIGN_UP, new UrlInfoWithDate(href)); infos.put(UrlInfo.Type.SignUp, new UrlInfoWithDate(href));
} else if (rel == REL_LINK_TOPUP) { } else if (rel == REL_LINK_TOPUP) {
infos.put(INetworkLink.URL_TOPUP, new UrlInfoWithDate(href)); infos.put(UrlInfo.Type.TopUp, new UrlInfoWithDate(href));
} else if (rel == REL_LINK_RECOVER_PASSWORD) { } else if (rel == REL_LINK_RECOVER_PASSWORD) {
infos.put(INetworkLink.URL_RECOVER_PASSWORD, new UrlInfoWithDate(href)); infos.put(UrlInfo.Type.RecoverPassword, new UrlInfoWithDate(href));
} else if (rel == REL_CONDITION_NEVER) { } else if (rel == REL_CONDITION_NEVER) {
urlConditions.put(href, NetworkCatalogItem.Accessibility.NEVER); urlConditions.put(href, NetworkCatalogItem.Accessibility.NEVER);
} else if (rel == REL_CONDITION_SIGNED_IN) { } else if (rel == REL_CONDITION_SIGNED_IN) {
@ -164,11 +164,11 @@ class OPDSLinkXMLReader extends OPDSXMLReader implements OPDSConstants, MimeType
String title, String title,
String summary, String summary,
String language, String language,
Map<String,UrlInfoWithDate> infos, Map<UrlInfo.Type,UrlInfoWithDate> infos,
HashMap<String,NetworkCatalogItem.Accessibility> urlConditions, HashMap<String,NetworkCatalogItem.Accessibility> urlConditions,
String sslCertificate String sslCertificate
) { ) {
if (siteName == null || title == null || infos.get(INetworkLink.URL_MAIN) == null) { if (siteName == null || title == null || infos.get(UrlInfo.Type.Catalog) == null) {
return null; return null;
} }

View file

@ -45,7 +45,7 @@ public class OPDSNetworkLink extends AbstractNetworkLink {
private final boolean myHasStableIdentifiers; private final boolean myHasStableIdentifiers;
OPDSNetworkLink(String siteName, String title, String summary, String language, OPDSNetworkLink(String siteName, String title, String summary, String language,
Map<String,UrlInfoWithDate> infos, boolean hasStableIdentifiers) { Map<UrlInfo.Type,UrlInfoWithDate> infos, boolean hasStableIdentifiers) {
super(siteName, title, summary, language, infos); super(siteName, title, summary, language, infos);
myHasStableIdentifiers = hasStableIdentifiers; myHasStableIdentifiers = hasStableIdentifiers;
} }
@ -118,7 +118,7 @@ public class OPDSNetworkLink extends AbstractNetworkLink {
} }
public ZLNetworkRequest simpleSearchRequest(String pattern, NetworkOperationData data) { public ZLNetworkRequest simpleSearchRequest(String pattern, NetworkOperationData data) {
final String url = getUrlInfo(URL_SEARCH).URL; final String url = getUrlInfo(UrlInfo.Type.Search).URL;
if (url == null) { if (url == null) {
return null; return null;
} }
@ -135,8 +135,8 @@ public class OPDSNetworkLink extends AbstractNetworkLink {
public NetworkCatalogItem libraryItem() { public NetworkCatalogItem libraryItem() {
final UrlInfoCollection urlMap = new UrlInfoCollection(); final UrlInfoCollection urlMap = new UrlInfoCollection();
urlMap.addInfo(new UrlInfo(UrlInfo.Type.Catalog, getUrlInfo(URL_MAIN).URL)); urlMap.addInfo(new UrlInfo(UrlInfo.Type.Catalog, getUrlInfo(UrlInfo.Type.Catalog).URL));
urlMap.addInfo(new UrlInfo(UrlInfo.Type.Image, getUrlInfo(URL_ICON).URL)); urlMap.addInfo(new UrlInfo(UrlInfo.Type.Image, getUrlInfo(UrlInfo.Type.Image).URL));
return new OPDSCatalogItem(this, getTitle(), getSummary(), urlMap, myExtraData); return new OPDSCatalogItem(this, getTitle(), getSummary(), urlMap, myExtraData);
} }

View file

@ -25,16 +25,45 @@ public class UrlInfo implements Serializable {
private static final long serialVersionUID = -893514485257788222L; private static final long serialVersionUID = -893514485257788222L;
public static enum Type { public static enum Type {
Catalog, Catalog("main"),
HtmlPage, HtmlPage,
Image, Image("icon"),
Thumbnail, Thumbnail,
Search("search"),
SignIn,
SignOut,
SignUp,
TopUp,
RecoverPassword,
Book, Book,
BookConditional, BookConditional,
BookDemo, BookDemo,
BookFullOrDemo, BookFullOrDemo,
BookBuy, BookBuy,
BookBuyInBrowser BookBuyInBrowser;
public static Type fromFixedName(String fixedName) {
for (Type t : values()) {
if (t.getFixedName().equals(fixedName)) {
return t;
}
}
return null;
}
private final String myFixedName;
Type(String fixedName) {
myFixedName = fixedName;
}
Type() {
myFixedName = null;
}
public String getFixedName() {
return myFixedName != null ? myFixedName : toString();
}
} }
public final Type InfoType; public final Type InfoType;

View file

@ -20,8 +20,11 @@
package org.geometerplus.fbreader.network.urlInfo; package org.geometerplus.fbreader.network.urlInfo;
import java.util.*; import java.util.*;
import java.io.Serializable;
public class UrlInfoCollection<T extends UrlInfo> implements Serializable {
private static final long serialVersionUID = -834589080548958222L;
public class UrlInfoCollection<T extends UrlInfo> {
private final LinkedList<T> myInfos = new LinkedList<T>(); private final LinkedList<T> myInfos = new LinkedList<T>();
public UrlInfoCollection() { public UrlInfoCollection() {