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

language filter (in progress)

This commit is contained in:
Nikolay Pultsin 2010-12-02 11:17:00 +00:00
parent a896bd4a05
commit 3370d413fa
7 changed files with 151 additions and 49 deletions

View file

@ -494,6 +494,9 @@
<node name="titleAlreadyExists" value="Specified title is already in use" />
<node name="siteAlreadyExists" value="Specified site is already in use" />
</node>
<node name="languageFilterDialog">
<node name="title" value="Language filter" />
</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

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/network_languages_list_dialog"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:id="@+id/network_languages_list_dialog_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</ScrollView>

View file

@ -19,6 +19,10 @@
package org.geometerplus.android.fbreader.network;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
@ -28,8 +32,10 @@ import android.os.Message;
import android.view.*;
import android.widget.BaseAdapter;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.resources.ZLResource;
import org.geometerplus.zlibrary.core.network.ZLNetworkException;
import org.geometerplus.zlibrary.core.language.ZLLanguageUtil;
import org.geometerplus.zlibrary.ui.android.R;
@ -39,7 +45,6 @@ import org.geometerplus.fbreader.network.NetworkTree;
import org.geometerplus.fbreader.network.NetworkLibrary;
public class NetworkLibraryActivity extends NetworkBaseActivity {
private NetworkTree myTree;
@Override
@ -85,7 +90,6 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
}
private static class Initializator extends Handler {
private NetworkLibraryActivity myActivity;
public Initializator(NetworkLibraryActivity activity) {
@ -165,7 +169,6 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
private final class LibraryAdapter extends BaseAdapter {
public final int getCount() {
if (!NetworkView.Instance().isInitialized()) {
return 0;
@ -178,7 +181,7 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
if (position == 0) {
return NetworkView.Instance().getSearchItemTree();
} else if (position > 0 && position <= size) {
return (NetworkTree) myTree.subTrees().get(position - 1);
return (NetworkTree)myTree.subTrees().get(position - 1);
} else if (position == size + 1) {
return NetworkView.Instance().getAddCustomCatalogItemTree();
}
@ -237,11 +240,45 @@ public class NetworkLibraryActivity extends NetworkBaseActivity {
refreshCatalogsList();
return true;
case MENU_LANGUAGES:
for (String langCode : NetworkLibrary.Instance().languages()) {
System.err.println("Language: " + ZLLanguageUtil.languageName(langCode));
{
final List<String> allLanguageCodes = NetworkLibrary.Instance().languageCodes();
final Collection<String> activeLanguageCodes = NetworkLibrary.Instance().activeLanguageCodes();
final CharSequence[] languageNames = new CharSequence[allLanguageCodes.size()];
final boolean[] checked = new boolean[allLanguageCodes.size()];
for (int i = 0; i < allLanguageCodes.size(); ++i) {
final String code = allLanguageCodes.get(i);
languageNames[i] = ZLLanguageUtil.languageName(code);
checked[i] = activeLanguageCodes.contains(code);
}
//showDialog(R.layout.network_languages_list_dialog);
final DialogInterface.OnMultiChoiceClickListener listener =
new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checked[which] = isChecked;
}
};
final ZLResource dialogResource = ZLResource.resource("dialog");
final AlertDialog dialog = new AlertDialog.Builder(this)
.setMultiChoiceItems(languageNames, checked, listener)
.setTitle(dialogResource.getResource("languageFilterDialog").getResource("title").getValue())
.setPositiveButton(dialogResource.getResource("button").getResource("ok").getValue(), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
final ArrayList<String> newActiveCodes = new ArrayList<String>();
for (int i = 0; i < checked.length; ++i) {
if (checked[i]) {
newActiveCodes.add(allLanguageCodes.get(i));
}
}
NetworkLibrary.Instance().setActiveLanguageCodes(newActiveCodes);
NetworkLibrary.Instance().invalidateChildren();
((BaseAdapter)getListAdapter()).notifyDataSetInvalidated();
}
})
.create();
dialog.show();
return true;
}
default:
return true;
}

View file

@ -199,7 +199,6 @@ class NetworkView {
*/
private static class MinPriorityThreadFactory implements ThreadFactory {
private final ThreadFactory myDefaultThreadFactory = Executors.defaultThreadFactory();
public Thread newThread(Runnable r) {

View file

@ -21,6 +21,7 @@ package org.geometerplus.fbreader.network;
import java.util.*;
import org.geometerplus.zlibrary.core.library.ZLibrary;
import org.geometerplus.zlibrary.core.util.ZLNetworkUtil;
import org.geometerplus.zlibrary.core.options.ZLStringOption;
import org.geometerplus.zlibrary.core.network.ZLNetworkManager;
@ -81,11 +82,67 @@ public class NetworkLibrary {
void onNewLink(INetworkLink link);
}
public final ZLStringOption NetworkSearchPatternOption = new ZLStringOption("NetworkSearch", "Pattern", "");
private final ArrayList<INetworkLink> myLinks = new ArrayList<INetworkLink>();
public List<String> languageCodes() {
final TreeSet<String> languageSet = new TreeSet<String>();
for (INetworkLink link : myLinks) {
languageSet.add(link.getLanguage());
}
return new ArrayList<String>(languageSet);
}
private ZLStringOption myActiveLanguageCodesOption;
private ZLStringOption activeLanguageCodesOption() {
if (myActiveLanguageCodesOption == null) {
myActiveLanguageCodesOption =
new ZLStringOption(
"Options",
"ActiveLanguages",
commaSeparatedString(ZLibrary.Instance().defaultLanguageCodes())
);
}
return myActiveLanguageCodesOption;
}
public Collection<String> activeLanguageCodes() {
return Arrays.asList(activeLanguageCodesOption().getValue().split(","));
}
public void setActiveLanguageCodes(Collection<String> codes) {
activeLanguageCodesOption().setValue(commaSeparatedString(codes));
}
private String commaSeparatedString(Collection<String> codes) {
final StringBuilder builder = new StringBuilder();
for (String code : codes) {
builder.append(code);
builder.append(",");
}
if (builder.length() > 0) {
builder.delete(builder.length() - 1, builder.length());
}
return builder.toString();
}
private List<INetworkLink> activeLinks() {
final LinkedList<INetworkLink> filteredList = new LinkedList<INetworkLink>();
final Collection<String> codes = activeLanguageCodes();
synchronized (myLinks) {
for (INetworkLink link : myLinks) {
if (link instanceof ICustomNetworkLink ||
codes.contains(link.getLanguage())) {
filteredList.add(link);
}
}
}
return filteredList;
}
private final RootTree myRootTree = new RootTree();
private boolean myUpdateChildren = true;
@ -157,7 +214,7 @@ public class NetworkLibrary {
toRemove.add(link);
}
}
myLinks.removeAll(myLinks);
myLinks.removeAll(toRemove);
}
}
@ -221,7 +278,7 @@ public class NetworkLibrary {
public String rewriteUrl(String url, boolean externalUrl) {
final String host = ZLNetworkUtil.hostFromUrl(url).toLowerCase();
synchronized (myLinks) {
for (INetworkLink link: myLinks) {
for (INetworkLink link : myLinks) {
if (host.contains(link.getSiteName())) {
url = link.rewriteUrl(url, externalUrl);
}
@ -271,12 +328,10 @@ public class NetworkLibrary {
int nodeCount = 0;
synchronized (myLinks) {
Collections.sort(myLinks, new LinksComparator());
for (int i = 0; i < myLinks.size(); ++i) {
INetworkLink link = myLinks.get(i);
/*if (!link.OnOption.getValue()) {
continue;
}*/
final ArrayList<INetworkLink> links = new ArrayList<INetworkLink>(activeLinks());
Collections.sort(links, new LinksComparator());
for (int i = 0; i < links.size(); ++i) {
INetworkLink link = links.get(i);
boolean processed = false;
while (currentNode != null || nodeIterator.hasNext()) {
if (currentNode == null) {
@ -299,8 +354,8 @@ public class NetworkLibrary {
break;
} else {
INetworkLink newNodeLink = null;
for (int j = i; j < myLinks.size(); ++j) {
final INetworkLink jlnk = myLinks.get(j);
for (int j = i; j < links.size(); ++j) {
final INetworkLink jlnk = links.get(j);
if (linksEqual(nodeLink, jlnk)) {
newNodeLink = jlnk;
break;
@ -334,13 +389,13 @@ public class NetworkLibrary {
currentNode = null;
}
for (FBTree tree: toRemove) {
for (FBTree tree : toRemove) {
tree.removeSelf();
}
}
private void updateVisibility() {
for (FBTree tree: myRootTree.subTrees()) {
for (FBTree tree : myRootTree.subTrees()) {
if (!(tree instanceof NetworkCatalogTree)) {
continue;
}
@ -382,10 +437,7 @@ public class NetworkLibrary {
};
synchronized (myLinks) {
for (INetworkLink link: myLinks) {
//if (link.OnOption.getValue()) {
// execute next code only if link is enabled
//}
for (INetworkLink link : activeLinks()) {
final NetworkOperationData data = link.createOperationData(link, synchronizedListener);
final ZLNetworkRequest request = link.simpleSearchRequest(pattern, data);
if (request != null) {
@ -403,7 +455,7 @@ public class NetworkLibrary {
if (listener.confirmInterrupt()) {
return;
}
for (NetworkOperationData data: dataList) {
for (NetworkOperationData data : dataList) {
ZLNetworkRequest request = data.resume();
if (request != null) {
requestList.add(request);
@ -440,7 +492,7 @@ public class NetworkLibrary {
public boolean hasCustomLinkTitle(String title, INetworkLink exceptFor) {
synchronized (myLinks) {
for (INetworkLink link: myLinks) {
for (INetworkLink link : myLinks) {
if (link != exceptFor && link.getTitle().equals(title)) {
return true;
}
@ -451,7 +503,7 @@ public class NetworkLibrary {
public boolean hasCustomLinkSite(String siteName, INetworkLink exceptFor) {
synchronized (myLinks) {
for (INetworkLink link: myLinks) {
for (INetworkLink link : myLinks) {
if (link != exceptFor && link.getSiteName().equals(siteName)) {
return true;
}
@ -459,12 +511,4 @@ public class NetworkLibrary {
}
return false;
}
public List<String> languages() {
final TreeSet<String> languageSet = new TreeSet<String>();
for (INetworkLink link : myLinks) {
languageSet.add(link.getLanguage());
}
return new ArrayList<String>(languageSet);
}
}

View file

@ -19,6 +19,8 @@
package org.geometerplus.zlibrary.core.library;
import java.util.Collection;
import org.geometerplus.zlibrary.core.filesystem.ZLResourceFile;
public abstract class ZLibrary {
@ -41,4 +43,5 @@ public abstract class ZLibrary {
abstract public int getScreenBrightness();
abstract public int getDisplayDPI();
abstract public void openInBrowser(String reference);
abstract public Collection<String> defaultLanguageCodes();
}

View file

@ -24,8 +24,10 @@ import java.util.*;
import android.app.Application;
import android.content.res.AssetFileDescriptor;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.telephony.TelephonyManager;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
@ -147,6 +149,33 @@ public final class ZLAndroidLibrary extends ZLibrary {
return (int)(160 * metrics.density);
}
@Override
public Collection<String> defaultLanguageCodes() {
final TreeSet<String> set = new TreeSet<String>();
set.add(Locale.getDefault().getLanguage());
final TelephonyManager manager = (TelephonyManager)myApplication.getSystemService(Context.TELEPHONY_SERVICE);
if (manager != null) {
final String country0 = manager.getSimCountryIso().toLowerCase();
final String country1 = manager.getNetworkCountryIso().toLowerCase();
for (Locale locale : Locale.getAvailableLocales()) {
final String country = locale.getCountry().toLowerCase();
if (country != null && country.length() > 0 &&
(country.equals(country0) || country.equals(country1))) {
set.add(locale.getLanguage());
}
}
if ("ru".equals(country0) || "ru".equals(country1)) {
set.add("ru");
} else if ("by".equals(country0) || "by".equals(country1)) {
set.add("ru");
} else if ("ua".equals(country0) || "ua".equals(country1)) {
set.add("ru");
}
}
set.add("multi");
return set;
}
private final class AndroidAssetsFile extends ZLResourceFile {
private final AndroidAssetsFile myParent;