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

fixed custom catalog search

This commit is contained in:
Nikolay Pultsin 2011-03-04 17:37:29 +00:00
parent 16fd2dd17e
commit ad05ea1664
9 changed files with 90 additions and 95 deletions

View file

@ -19,6 +19,8 @@
package org.geometerplus.android.fbreader.network; package org.geometerplus.android.fbreader.network;
import java.util.HashMap;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
@ -35,15 +37,14 @@ import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.ui.android.R; import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.fbreader.network.ICustomNetworkLink; import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.opds.OPDSLinkReader; import org.geometerplus.fbreader.network.opds.OPDSLinkReader;
import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.util.UIUtil;
public class AddCustomCatalogActivity extends Activity { public class AddCustomCatalogActivity extends Activity {
private ZLResource myResource; private ZLResource myResource;
private Integer myCatalogId; private volatile ICustomNetworkLink myLink;
private String myIcon;
@Override @Override
public void onCreate(Bundle icicle) { public void onCreate(Bundle icicle) {
@ -83,21 +84,16 @@ public class AddCustomCatalogActivity extends Activity {
); );
final Intent intent = getIntent(); final Intent intent = getIntent();
myLink = NetworkLibraryActivity.getLinkFromIntent(intent);
final Uri uri = intent.getData(); final Uri uri = intent.getData();
myCatalogId = ICustomNetworkLink.INVALID_ID;
if (uri != null) { if (myLink != null) {
myCatalogId = intent.getIntExtra( setTextById(R.id.add_custom_catalog_url, myLink.getUrlInfo(INetworkLink.URL_MAIN).URL);
NetworkLibraryActivity.ADD_CATALOG_ID_KEY, myCatalogId setTextById(R.id.add_custom_catalog_title, myLink.getTitle());
); setTextById(R.id.add_custom_catalog_summary, myLink.getSummary());
if (myCatalogId != ICustomNetworkLink.INVALID_ID) {
setTextById(R.id.add_custom_catalog_url, uri.toString());
setTextById(R.id.add_custom_catalog_title, intent.getStringExtra(NetworkLibraryActivity.ADD_CATALOG_TITLE_KEY));
setTextById(R.id.add_custom_catalog_summary, intent.getStringExtra(NetworkLibraryActivity.ADD_CATALOG_SUMMARY_KEY));
myIcon = intent.getStringExtra(NetworkLibraryActivity.ADD_CATALOG_ICON_KEY);
} else {
loadInfoByUri(uri);
}
setExtraFieldsVisibility(true); setExtraFieldsVisibility(true);
} else if (uri != null) {
loadInfoByUri(uri);
} else { } else {
setExtraFieldsVisibility(false); setExtraFieldsVisibility(false);
} }
@ -126,24 +122,22 @@ public class AddCustomCatalogActivity extends Activity {
setErrorByKey("invalidUrl"); setErrorByKey("invalidUrl");
return; return;
} }
if (!getExtraFieldsVisibility()) { if (myLink == null) {
loadInfoByUri(uri); loadInfoByUri(uri);
} else if (isEmptyString(title)) { } else if (isEmptyString(title)) {
setErrorByKey("titleIsEmpty"); setErrorByKey("titleIsEmpty");
} else { } else {
startActivity( myLink.setTitle(title);
new Intent( myLink.setSummary(summary);
NetworkLibraryActivity.ADD_CATALOG,
uri, Intent intent = new Intent(
AddCustomCatalogActivity.this, NetworkLibraryActivity.ADD_CATALOG,
NetworkLibraryActivity.class uri,
) AddCustomCatalogActivity.this,
.putExtra(NetworkLibraryActivity.ADD_CATALOG_TITLE_KEY, title) NetworkLibraryActivity.class
.putExtra(NetworkLibraryActivity.ADD_CATALOG_SUMMARY_KEY, summary) ).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
.putExtra(NetworkLibraryActivity.ADD_CATALOG_ICON_KEY, myIcon) NetworkLibraryActivity.addLinkToIntent(intent, myLink);
.putExtra(NetworkLibraryActivity.ADD_CATALOG_ID_KEY, myCatalogId) startActivity(intent);
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP)
);
finish(); finish();
} }
} }
@ -152,10 +146,6 @@ public class AddCustomCatalogActivity extends Activity {
return s == null || s.length() == 0; return s == null || s.length() == 0;
} }
private boolean getExtraFieldsVisibility() {
return findViewById(R.id.add_custom_catalog_title_group).getVisibility() == View.VISIBLE;
}
private void setExtraFieldsVisibility(boolean show) { private void setExtraFieldsVisibility(boolean show) {
final int visibility = show ? View.VISIBLE : View.GONE; final int visibility = show ? View.VISIBLE : View.GONE;
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@ -253,8 +243,11 @@ public class AddCustomCatalogActivity extends Activity {
if (siteName.startsWith("www.")) { if (siteName.startsWith("www.")) {
siteName = siteName.substring(4); siteName = siteName.substring(4);
} }
final ICustomNetworkLink link = final HashMap<String,UrlInfo> infos = new HashMap<String,UrlInfo>();
OPDSLinkReader.createCustomLink(siteName, null, null, null, textUrl); infos.put(INetworkLink.URL_MAIN, new UrlInfo(textUrl));
myLink = OPDSLinkReader.createCustomLink(
ICustomNetworkLink.INVALID_ID, siteName, null, null, null, infos
);
final Runnable loadInfoRunnable = new Runnable() { final Runnable loadInfoRunnable = new Runnable() {
private String myError; private String myError;
@ -262,20 +255,19 @@ public class AddCustomCatalogActivity extends Activity {
public void run() { public void run() {
try { try {
myError = null; myError = null;
link.reloadInfo(); myLink.reloadInfo();
} catch (ZLNetworkException e) { } catch (ZLNetworkException e) {
myError = e.getMessage(); myError = e.getMessage();
} }
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {
if (myError == null) { if (myError == null) {
setTextById(R.id.add_custom_catalog_title, link.getTitle()); setTextById(R.id.add_custom_catalog_title, myLink.getTitle());
setTextById(R.id.add_custom_catalog_summary, link.getSummary()); setTextById(R.id.add_custom_catalog_summary, myLink.getSummary());
myIcon = link.getIcon();
setExtraFieldsVisibility(true); setExtraFieldsVisibility(true);
} else { } else {
runErrorDialog(myError); runErrorDialog(myError);
myIcon = null; myLink = null;
} }
} }
}); });

View file

@ -248,19 +248,12 @@ class NetworkCatalogActions extends NetworkTreeActions {
return true; return true;
case CUSTOM_CATALOG_EDIT: case CUSTOM_CATALOG_EDIT:
{ {
final ICustomNetworkLink link = final Intent intent = new Intent(activity, AddCustomCatalogActivity.class);
(ICustomNetworkLink)((NetworkCatalogTree)tree).Item.Link; NetworkLibraryActivity.addLinkToIntent(
final String textUrl = link.getUrlInfo(INetworkLink.URL_MAIN).URL; intent,
if (textUrl != null) { (ICustomNetworkLink)((NetworkCatalogTree)tree).Item.Link
activity.startActivity( );
new Intent(activity, AddCustomCatalogActivity.class) activity.startActivity(intent);
.setData(Uri.parse(textUrl))
.putExtra(NetworkLibraryActivity.ADD_CATALOG_TITLE_KEY, link.getTitle())
.putExtra(NetworkLibraryActivity.ADD_CATALOG_SUMMARY_KEY, link.getSummary())
.putExtra(NetworkLibraryActivity.ADD_CATALOG_ICON_KEY, link.getIcon())
.putExtra(NetworkLibraryActivity.ADD_CATALOG_ID_KEY, link.getId())
);
}
return true; return true;
} }
case CUSTOM_CATALOG_REMOVE: case CUSTOM_CATALOG_REMOVE:

View file

@ -39,19 +39,43 @@ import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.android.util.UIUtil; import org.geometerplus.android.util.UIUtil;
import org.geometerplus.fbreader.network.NetworkTree; import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.NetworkLibrary;
import org.geometerplus.fbreader.network.ICustomNetworkLink;
import org.geometerplus.fbreader.network.opds.OPDSLinkReader; import org.geometerplus.fbreader.network.opds.OPDSLinkReader;
public class NetworkLibraryActivity extends NetworkBaseActivity { public class NetworkLibraryActivity extends NetworkBaseActivity {
final static String ADD_CATALOG = "android.fbreader.action.ADD_CATALOG"; static final String ADD_CATALOG = "android.fbreader.action.ADD_CATALOG";
final static String ADD_CATALOG_TITLE_KEY = "title"; private static final String ADD_CATALOG_TITLE_KEY = "title";
final static String ADD_CATALOG_SUMMARY_KEY = "summary"; private static final String ADD_CATALOG_SUMMARY_KEY = "summary";
final static String ADD_CATALOG_ICON_KEY = "icon"; private static final String ADD_CATALOG_ICON_KEY = "icon";
final static String ADD_CATALOG_ID_KEY = "id"; private static final String ADD_CATALOG_ID_KEY = "id";
final static String ADD_CATALOG_URLS_BUNDLE_KEY = "urls"; private static final String ADD_CATALOG_URLS_MAP_KEY = "urls";
static void addLinkToIntent(Intent intent, ICustomNetworkLink link) {
final String textUrl = link.getUrlInfo(INetworkLink.URL_MAIN).URL;
intent.setData(Uri.parse(textUrl));
intent
.putExtra(ADD_CATALOG_TITLE_KEY, link.getTitle())
.putExtra(ADD_CATALOG_SUMMARY_KEY, link.getSummary())
.putExtra(ADD_CATALOG_ICON_KEY, link.getIcon())
.putExtra(ADD_CATALOG_ID_KEY, link.getId())
.putExtra(ADD_CATALOG_URLS_MAP_KEY, link.urlInfoMap());
}
static ICustomNetworkLink getLinkFromIntent(Intent intent) {
final Uri uri = intent.getData();
if (uri == null || !intent.hasExtra(ADD_CATALOG_ID_KEY)) {
return null;
}
return OPDSLinkReader.createCustomLink(
intent.getIntExtra(ADD_CATALOG_ID_KEY, ICustomNetworkLink.INVALID_ID),
uri.getHost(),
intent.getStringExtra(ADD_CATALOG_TITLE_KEY),
intent.getStringExtra(ADD_CATALOG_SUMMARY_KEY),
intent.getStringExtra(ADD_CATALOG_ICON_KEY),
(HashMap<String,UrlInfo>)intent.getSerializableExtra(ADD_CATALOG_URLS_MAP_KEY)
);
}
private NetworkTree myTree; private NetworkTree myTree;
private volatile Intent myIntent; private volatile Intent myIntent;
@ -74,19 +98,9 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
} }
private void processIntent(Intent intent) { private void processIntent(Intent intent) {
final String action = intent.getAction(); if (ADD_CATALOG.equals(intent.getAction())) {
if (ADD_CATALOG.equals(action)) { final ICustomNetworkLink link = getLinkFromIntent(intent);
final Uri uri = intent.getData(); System.err.println("LINK = " + link);
final String title = intent.getStringExtra(ADD_CATALOG_TITLE_KEY);
final String summary = intent.getStringExtra(ADD_CATALOG_SUMMARY_KEY);
final String icon = intent.getStringExtra(ADD_CATALOG_ICON_KEY);
final int id = intent.getIntExtra(ADD_CATALOG_ID_KEY, ICustomNetworkLink.INVALID_ID);
if (uri == null || title == null) {
return;
}
final ICustomNetworkLink link = OPDSLinkReader.createCustomLink(
id, uri.getHost(), title, summary, icon, uri.toString()
);
if (link != null) { if (link != null) {
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
public void run() { public void run() {

View file

@ -90,10 +90,12 @@ public abstract class AbstractNetworkLink implements INetworkLink {
@Override @Override
public String toString() { public String toString() {
String icon = myIcon; String icon = myIcon;
if (icon.length() > 64) { if (icon != null) {
icon = icon.substring(0, 61) + "..."; if (icon.length() > 64) {
icon = icon.substring(0, 61) + "...";
}
icon = icon.replaceAll("\n", "");
} }
icon = icon.replaceAll("\n", "");
return "AbstractNetworkLink: {" return "AbstractNetworkLink: {"
+ "siteName=" + mySiteName + "siteName=" + mySiteName
+ "; title=" + myTitle + "; title=" + myTitle

View file

@ -21,6 +21,8 @@ package org.geometerplus.fbreader.network;
import org.geometerplus.zlibrary.core.network.ZLNetworkException; import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import java.util.HashMap;
public interface ICustomNetworkLink extends INetworkLink { public interface ICustomNetworkLink extends INetworkLink {
public static final int INVALID_ID = -1; public static final int INVALID_ID = -1;
@ -32,6 +34,7 @@ public interface ICustomNetworkLink extends INetworkLink {
void setSummary(String summary); void setSummary(String summary);
void setIcon(String icon); void setIcon(String icon);
HashMap<String,UrlInfo> urlInfoMap();
void setUrl(String urlKey, String url); void setUrl(String urlKey, String url);
void removeUrl(String urlKey); void removeUrl(String urlKey);

View file

@ -40,7 +40,6 @@ public interface INetworkLink {
String getSummary(); String getSummary();
String getIcon(); String getIcon();
HashMap<String,UrlInfo> urlInfoMap();
UrlInfo getUrlInfo(String urlKey); UrlInfo getUrlInfo(String urlKey);
Set<String> getUrlKeys(); Set<String> getUrlKeys();

View file

@ -180,8 +180,12 @@ public class NetworkLibrary {
new NetworkDatabase.ICustomLinksHandler() { new NetworkDatabase.ICustomLinksHandler() {
public void handleCustomLinkData(int id, String siteName, public void handleCustomLinkData(int id, String siteName,
String title, String summary, String icon, Map<String,UrlInfo> infos) { String title, String summary, String icon, Map<String,UrlInfo> infos) {
final ICustomNetworkLink link = OPDSLinkReader.createCustomLink(id, siteName, title, summary, icon, infos); if (title != null &&
if (link != null) { siteName != null &&
infos.get(INetworkLink.URL_MAIN) != null) {
final ICustomNetworkLink link = OPDSLinkReader.createCustomLink(
id, siteName, title, summary, icon, infos
);
addLinkInternal(link); addLinkInternal(link);
} }
} }

View file

@ -40,22 +40,9 @@ public class OPDSLinkReader {
static final String CATALOGS_URL = "http://data.fbreader.org/catalogs/generic-1.2.xml"; static final String CATALOGS_URL = "http://data.fbreader.org/catalogs/generic-1.2.xml";
public static ICustomNetworkLink createCustomLink(int id, String siteName, String title, String summary, String icon, Map<String,UrlInfo> infos) { public static ICustomNetworkLink createCustomLink(int id, String siteName, String title, String summary, String icon, Map<String,UrlInfo> infos) {
if (siteName == null || title == null || infos.get(INetworkLink.URL_MAIN) == null) {
return null;
}
return new OPDSCustomLink(id, siteName, title, summary, icon, infos); return new OPDSCustomLink(id, siteName, title, summary, icon, infos);
} }
public static ICustomNetworkLink createCustomLink(int id, String siteName, String title, String summary, String icon, String url) {
final HashMap<String,UrlInfo> infos = new HashMap<String,UrlInfo>();
infos.put(INetworkLink.URL_MAIN, new UrlInfo(url));
return new OPDSCustomLink(id, siteName, title, summary, icon, infos);
}
public static ICustomNetworkLink createCustomLink(String siteName, String title, String summary, String icon, String url) {
return createCustomLink(ICustomNetworkLink.INVALID_ID, siteName, title, summary, icon, url);
}
public static final int CACHE_LOAD = 0; public static final int CACHE_LOAD = 0;
public static final int CACHE_UPDATE = 1; public static final int CACHE_UPDATE = 1;
public static final int CACHE_CLEAR = 2; public static final int CACHE_CLEAR = 2;

View file

@ -127,6 +127,7 @@ public class OPDSNetworkLink extends AbstractNetworkLink {
if (url == null) { if (url == null) {
return null; return null;
} }
System.err.println("URL = " + url);
try { try {
pattern = URLEncoder.encode(pattern, "utf-8"); pattern = URLEncoder.encode(pattern, "utf-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {