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:
parent
a896bd4a05
commit
3370d413fa
7 changed files with 151 additions and 49 deletions
|
@ -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 “%s” book?" />
|
||||
|
|
|
@ -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>
|
|
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -199,7 +199,6 @@ class NetworkView {
|
|||
*/
|
||||
|
||||
private static class MinPriorityThreadFactory implements ThreadFactory {
|
||||
|
||||
private final ThreadFactory myDefaultThreadFactory = Executors.defaultThreadFactory();
|
||||
|
||||
public Thread newThread(Runnable r) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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) {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue