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

CustomCatalog UI

git-svn-id: https://only.mawhrin.net/repos/FBReaderJ/trunk@1523 6a642e6f-84f6-412e-ac94-c4a38d5a04b0
This commit is contained in:
Vasiliy Bout 2010-06-29 20:26:08 +00:00
parent bfd0db46ce
commit 6174b7fc04
11 changed files with 418 additions and 19 deletions

View file

@ -42,6 +42,8 @@
<node name="search" value="Search" />
<node name="searchSummary" value="Search for books in catalogs" />
<node name="searchResults" value="Search results for: %s" />
<node name="editCustomCatalog" value="Edit catalog"/>
<node name="removeCustomCatalog" value="Remove catalog"/>
<node name="confirmQuestions">
<node name="read" value="Do you want to read local copy?"/>
<node name="download" value="Do you want to download this book?"/>
@ -58,6 +60,7 @@
<node name="signIn" value="Sign in"/>
<node name="signOut" value="Sign out (%s)"/>
<node name="refillAccount" value="Refill account"/>
<node name="addCustomCatalog" value="Add catalog"/>
</node>
</node>
<node name="networkBookView">
@ -302,6 +305,16 @@
<node name="email" value="E-mail" />
<node name="differentPasswords" value="Password and password confirmation must be the same." />
</node>
<node name="CustomCatalogDialog">
<node name="title" value="Custom catalog" />
<node name="catalogTitle" value="Title" />
<node name="catalogUrl" value="URL" />
<node name="catalogSummary" value="Summary" />
<node name="titleIsEmpty" value="Title should be non-empty" />
<node name="urlIsEmpty" value="URL should be non-empty" />
<node name="invalidUrl" value="Invalid URL" />
<node name="alreadyExists" value="Specified title already exists" />
</node>
<node name="purchaseConfirmBox">
<node name="title" value="Purchase book" />
<node name="message" value="Are you sure you want to buy&#10;&#8220;%s&#8221;&#10;book?" />

View file

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/network_custom_catalog_dialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingRight="12dp"
>
<TextView
android:id="@+id/network_catalog_title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:maxLines="2"
/>
<TextView
android:id="@+id/network_catalog_url_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:maxLines="2"
/>
<TextView
android:id="@+id/network_catalog_summary_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_vertical"
android:maxLines="2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:id="@+id/network_catalog_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
<EditText
android:id="@+id/network_catalog_url"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginTop="5dp"
/>
<EditText
android:id="@+id/network_catalog_summary"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginTop="5dp"
/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/network_catalog_error"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textColor="#ff0000"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
</LinearLayout>
</ScrollView>

View file

@ -61,8 +61,21 @@ class AuthenticationDialog extends NetworkDialog {
library.synchronize();
NetworkView.Instance().fireModelChanged();
if (message.what < 0) {
if (message.what == -2) {
final ZLResource dialogResource = ZLResource.resource("dialog");
final ZLResource boxResource = dialogResource.getResource("networkError");
final ZLResource buttonResource = dialogResource.getResource("button");
new AlertDialog.Builder(activity)
.setTitle(boxResource.getResource("title").getValue())
.setMessage((String) message.obj)
.setIcon(0)
.setPositiveButton(buttonResource.getResource("ok").getValue(), null)
.create().show();
} else {
myErrorMessage = (String) message.obj;
activity.showDialog(NetworkDialog.DIALOG_AUTHENTICATION);
return;
}
} else if (message.what > 0) {
if (myOnSuccessRunnable != null) {
myOnSuccessRunnable.run();
@ -147,6 +160,11 @@ class AuthenticationDialog extends NetworkDialog {
.setTitle(myResource.getResource("title").getValue())
.setPositiveButton(buttonResource.getResource("ok").getValue(), listener)
.setNegativeButton(buttonResource.getResource("cancel").getValue(), listener)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
listener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
})
.create();
}

View file

@ -0,0 +1,205 @@
/*
* Copyright (C) 2010 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
package org.geometerplus.android.fbreader.network;
import java.util.HashMap;
import android.app.Activity;
import android.app.Dialog;
import android.app.AlertDialog;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;
import android.content.DialogInterface;
import org.geometerplus.zlibrary.ui.android.R;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.fbreader.network.*;
import org.geometerplus.fbreader.network.opds.OPDSLinkReader;
class CustomCatalogDialog extends NetworkDialog {
private String myTitle;
private String myUrl;
private String mySummary;
public CustomCatalogDialog() {
super("CustomCatalogDialog");
}
private void clearData() {
myTitle = myUrl = mySummary = null;
}
public Dialog createDialog(final Activity activity) {
final View layout = activity.getLayoutInflater().inflate(R.layout.network_custom_catalog_dialog, null);
setupLabel(layout, R.id.network_catalog_title_text, "catalogTitle", R.id.network_catalog_title);
setupLabel(layout, R.id.network_catalog_url_text, "catalogUrl", R.id.network_catalog_url);
setupLabel(layout, R.id.network_catalog_summary_text, "catalogSummary", R.id.network_catalog_summary);
final Handler handler = new Handler() {
public void handleMessage(Message message) {
if (!NetworkView.Instance().isInitialized()) {
return;
}
final NetworkLibrary library = NetworkLibrary.Instance();
library.invalidate();
library.invalidateVisibility();
library.synchronize();
NetworkView.Instance().fireModelChanged();
if (message.what < 0) {
if (message.what == -2) {
final ZLResource dialogResource = ZLResource.resource("dialog");
final ZLResource boxResource = dialogResource.getResource("networkError");
final ZLResource buttonResource = dialogResource.getResource("button");
new AlertDialog.Builder(activity)
.setTitle(boxResource.getResource("title").getValue())
.setMessage((String) message.obj)
.setIcon(0)
.setPositiveButton(buttonResource.getResource("ok").getValue(), null)
.create().show();
} else {
myErrorMessage = (String) message.obj;
activity.showDialog(NetworkDialog.DIALOG_CUSTOM_CATALOG);
return;
}
} else if (message.what > 0) {
if (myOnSuccessRunnable != null) {
myOnSuccessRunnable.run();
}
}
clearData();
}
};
final DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if (which == DialogInterface.BUTTON_POSITIVE) {
AlertDialog alert = (AlertDialog) dialog;
myTitle = ((TextView) alert.findViewById(R.id.network_catalog_title)).getText().toString().trim();
myUrl = ((TextView) alert.findViewById(R.id.network_catalog_url)).getText().toString().trim();
mySummary = ((TextView) alert.findViewById(R.id.network_catalog_summary)).getText().toString().trim();
if (myTitle.length() == 0) {
myTitle = null;
final String err = myResource.getResource("titleIsEmpty").getValue();
handler.sendMessage(handler.obtainMessage(-1, err));
return;
}
if (myUrl.length() == 0) {
myUrl = null;
final String err = myResource.getResource("urlIsEmpty").getValue();
handler.sendMessage(handler.obtainMessage(-1, err));
return;
}
if (mySummary.length() == 0) {
mySummary = null;
}
Uri uri = Uri.parse(myUrl);
if (uri.getScheme() == null) {
myUrl = "http://" + myUrl;
uri = Uri.parse(myUrl);
}
String siteName = uri.getHost();
if (siteName == null) {
final String err = myResource.getResource("invalidUrl").getValue();
handler.sendMessage(handler.obtainMessage(-1, err));
return;
}
if (siteName.startsWith("www.")) {
siteName = siteName.substring(4);
}
if (myLink == null) {
final OPDSLinkReader reader = new OPDSLinkReader();
final HashMap<String, String> links = new HashMap<String, String>();
links.put(INetworkLink.URL_MAIN, myUrl);
final ICustomNetworkLink link = reader.createCustomLink(ICustomNetworkLink.INVALID_ID,
siteName, myTitle, mySummary, null, links);
myLink = link;
if (link != null) {
if (!NetworkLibrary.Instance().addCustomLink(link)) {
final String err = myResource.getResource("alreadyExists").getValue();
handler.sendMessage(handler.obtainMessage(-1, err));
return;
}
} else {
throw new RuntimeException("Unable to create link!!! Impossible!!!");
}
} else {
final ICustomNetworkLink link = (ICustomNetworkLink) myLink;
link.setSiteName(siteName);
link.setTitle(myTitle);
link.setSummary(mySummary);
link.setLink(INetworkLink.URL_MAIN, myUrl);
link.saveLink();
}
handler.sendEmptyMessage(1);
} else {
handler.sendEmptyMessage(0);
}
}
};
final ZLResource buttonResource = ZLResource.resource("dialog").getResource("button");
return new AlertDialog.Builder(activity)
.setView(layout)
.setTitle(myResource.getResource("title").getValue())
.setPositiveButton(buttonResource.getResource("ok").getValue(), listener)
.setNegativeButton(buttonResource.getResource("cancel").getValue(), listener)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
listener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
})
.create();
}
public void prepareDialog(Dialog dialog) {
if (myLink != null) {
if (myTitle == null) myTitle = myLink.getTitle();
if (myUrl == null) myUrl = myLink.getLink(INetworkLink.URL_MAIN);
if (mySummary == null) mySummary = myLink.getSummary();
}
((TextView) dialog.findViewById(R.id.network_catalog_title)).setText((myTitle != null) ? myTitle : "");
((TextView) dialog.findViewById(R.id.network_catalog_url)).setText((myUrl != null) ? myUrl : "");
((TextView) dialog.findViewById(R.id.network_catalog_summary)).setText((mySummary != null) ? mySummary : "");
final TextView error = (TextView) dialog.findViewById(R.id.network_catalog_error);
if (myErrorMessage == null) {
error.setVisibility(View.GONE);
error.setText("");
} else {
error.setVisibility(View.VISIBLE);
error.setText(myErrorMessage);
}
View dlgView = dialog.findViewById(R.id.network_custom_catalog_dialog);
dlgView.invalidate();
dlgView.requestLayout();
}
}

View file

@ -48,6 +48,8 @@ class NetworkCatalogActions extends NetworkTreeActions {
public static final int SIGNOUT_ITEM_ID = 5;
public static final int REFILL_ACCOUNT_ITEM_ID = 6;
public static final int CUSTOM_CATALOG_EDIT = 7;
public static final int CUSTOM_CATALOG_REMOVE = 8;
@Override
public boolean canHandleTree(NetworkTree tree) {
@ -98,6 +100,11 @@ class NetworkCatalogActions extends NetworkTreeActions {
}
}
}
INetworkLink link = catalogTree.Item.Link;
if (link instanceof ICustomNetworkLink) {
addMenuItem(menu, CUSTOM_CATALOG_EDIT, "editCustomCatalog");
addMenuItem(menu, CUSTOM_CATALOG_REMOVE, "removeCustomCatalog");
}
} else {
if (item.URLByType.get(NetworkCatalogItem.URL_HTML_PAGE) != null) {
addMenuItem(menu, OPEN_IN_BROWSER_ITEM_ID, "openInBrowser");
@ -113,7 +120,6 @@ class NetworkCatalogActions extends NetworkTreeActions {
}
break;
}
return;
}
}
@ -247,6 +253,12 @@ class NetworkCatalogActions extends NetworkTreeActions {
((NetworkCatalogTree)tree).Item.Link.authenticationManager().refillAccountLink()
);
return true;
case CUSTOM_CATALOG_EDIT:
NetworkDialog.show(activity, NetworkDialog.DIALOG_CUSTOM_CATALOG, ((NetworkCatalogTree)tree).Item.Link, null);
return true;
case CUSTOM_CATALOG_REMOVE:
removeCustomLink((ICustomNetworkLink)((NetworkCatalogTree)tree).Item.Link);
return true;
}
return false;
}
@ -430,4 +442,12 @@ class NetworkCatalogActions extends NetworkTreeActions {
};
((ZLAndroidDialogManager)ZLAndroidDialogManager.Instance()).wait("signOut", runnable, activity);
}
private void removeCustomLink(ICustomNetworkLink link) {
final NetworkLibrary library = NetworkLibrary.Instance();
library.removeCustomLink(link);
library.invalidate();
library.synchronize();
NetworkView.Instance().fireModelChanged();
}
}

View file

@ -37,6 +37,7 @@ abstract class NetworkDialog {
// dialog identifiers
public static final int DIALOG_AUTHENTICATION = 0;
public static final int DIALOG_REGISTER_USER = 1;
public static final int DIALOG_CUSTOM_CATALOG = 2;
private static final TreeMap<Integer, NetworkDialog> ourInstances = new TreeMap<Integer, NetworkDialog>();
@ -50,6 +51,9 @@ abstract class NetworkDialog {
case DIALOG_REGISTER_USER:
dlg = new RegisterUserDialog();
break;
case DIALOG_CUSTOM_CATALOG:
dlg = new CustomCatalogDialog();
break;
}
if (dlg != null) {
ourInstances.put(Integer.valueOf(id), dlg);

View file

@ -116,6 +116,7 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
addMenuItem(menu, 1, "networkSearch", R.drawable.ic_menu_networksearch);
addMenuItem(menu, 2, "addCustomCatalog", 0);
return true;
}
@ -132,6 +133,9 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
switch (item.getItemId()) {
case 1:
return onSearchRequested();
case 2:
NetworkDialog.show(this, NetworkDialog.DIALOG_CUSTOM_CATALOG, null, null);
return true;
default:
return true;
}

View file

@ -50,7 +50,7 @@ class RegisterUserDialog extends NetworkDialog {
super("RegisterUserDialog");
}
private void clearUserInfo() {
private void clearData() {
myLogin = myPassword = myEmail = null;
}
@ -64,12 +64,13 @@ class RegisterUserDialog extends NetworkDialog {
final Handler handler = new Handler() {
public void handleMessage(Message message) {
if (!NetworkView.Instance().isInitialized()) {
return;
}
final NetworkLibrary library = NetworkLibrary.Instance();
library.invalidateVisibility();
library.synchronize();
if (NetworkView.Instance().isInitialized()) {
NetworkView.Instance().fireModelChanged();
}
if (message.what < 0) {
if (message.what == -2) {
final ZLResource dialogResource = ZLResource.resource("dialog");
@ -91,7 +92,7 @@ class RegisterUserDialog extends NetworkDialog {
myOnSuccessRunnable.run();
}
}
clearUserInfo();
clearData();
}
};
@ -161,6 +162,11 @@ class RegisterUserDialog extends NetworkDialog {
.setTitle(myResource.getResource("title").getValue())
.setPositiveButton(buttonResource.getResource("ok").getValue(), listener)
.setNegativeButton(buttonResource.getResource("cancel").getValue(), listener)
.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
listener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
}
})
.create();
}

View file

@ -189,6 +189,36 @@ class SQLiteNetworkDatabase extends NetworkDatabase {
});
}
private SQLiteStatement myDeleteAllCustomLinksStatement;
private SQLiteStatement myDeleteCustomLinkStatement;
@Override
protected void deleteCustomLink(final ICustomNetworkLink link) {
if (link.getId() == ICustomNetworkLink.INVALID_ID) {
return;
}
executeAsATransaction(new Runnable() {
public void run() {
final long id = link.getId();
if (myDeleteAllCustomLinksStatement == null) {
myDeleteAllCustomLinksStatement = myDatabase.compileStatement(
"DELETE FROM CustomLinkUrls WHERE link_id = ?");
}
myDeleteAllCustomLinksStatement.bindLong(1, id);
myDeleteAllCustomLinksStatement.execute();
if (myDeleteCustomLinkStatement == null) {
myDeleteCustomLinkStatement = myDatabase.compileStatement(
"DELETE FROM CustomLinks WHERE link_id = ?"
);
}
myDeleteCustomLinkStatement.bindLong(1, id);
myDeleteCustomLinkStatement.execute();
link.setId(ICustomNetworkLink.INVALID_ID);
}
});
}
private void createTables() {
myDatabase.execSQL(
"CREATE TABLE CustomLinks(" +

View file

@ -41,4 +41,5 @@ public abstract class NetworkDatabase {
protected abstract void loadCustomLinks(List<ICustomNetworkLink> links, ICustomLinksFactory factory);
protected abstract void saveCustomLink(ICustomNetworkLink link);
protected abstract void deleteCustomLink(ICustomNetworkLink link);
}

View file

@ -45,17 +45,11 @@ public class NetworkLibrary {
private static class CompositeList extends AbstractSequentialList<INetworkLink> {
private final ArrayList<ArrayList<? extends INetworkLink>> myLists;
private final int mySize;
private Comparator<INetworkLink> myComparator;
public CompositeList(ArrayList<ArrayList<? extends INetworkLink>> lists,
Comparator<INetworkLink> comparator) {
int size = 0;
for (ArrayList<? extends INetworkLink> list: lists) {
size += list.size();
}
myLists = lists;
mySize = size;
myComparator = comparator;
}
@ -87,7 +81,7 @@ public class NetworkLibrary {
}
public boolean hasNext() {
return myIndex < mySize;
return myIndex < size();
}
public boolean hasPrevious() {
@ -165,7 +159,7 @@ public class NetworkLibrary {
@Override
public ListIterator<INetworkLink> listIterator(int location) {
if (location < 0 || location > mySize) {
if (location < 0 || location > size()) {
throw new IndexOutOfBoundsException();
}
Iterator it = new Iterator();
@ -182,7 +176,11 @@ public class NetworkLibrary {
@Override
public int size() {
return mySize;
int size = 0;
for (ArrayList<? extends INetworkLink> list: myLists) {
size += list.size();
}
return size;
}
}
@ -332,9 +330,13 @@ public class NetworkLibrary {
}
final INetworkLink nodeLink = ((NetworkCatalogTree) currentNode).Item.Link;
if (nodeLink == link) {
if (link instanceof ICustomNetworkLink) {
toRemove.add(currentNode);
} else {
processed = true;
}
currentNode = null;
++nodeCount;
processed = true;
break;
} else {
boolean found = false;
@ -354,8 +356,8 @@ public class NetworkLibrary {
}
}
}
final int nextIndex = nodeIterator.nextIndex();
if (!processed) {
final int nextIndex = nodeIterator.nextIndex();
new NetworkCatalogRootTree(myRootTree, link, nodeCount++).Item.onDisplayItem();
nodeIterator = myRootTree.subTrees().listIterator(nextIndex + 1);
}
@ -474,4 +476,14 @@ public class NetworkLibrary {
public ICustomNetworkLink getCustomLink(int index) {
return myCustomLinks.get(index);
}
public void removeCustomLink(ICustomNetworkLink link) {
final int index = Collections.binarySearch(myCustomLinks, link, new LinksComparator());
if (index < 0) {
return;
}
myCustomLinks.remove(index);
NetworkDatabase.Instance().deleteCustomLink(link);
link.setSaveLinkListener(null);
}
}