mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-04 18:29:23 +02:00
dictionary code refactoring
This commit is contained in:
parent
520736b313
commit
7824ad3482
4 changed files with 100 additions and 74 deletions
|
@ -21,13 +21,8 @@ package org.geometerplus.android.fbreader;
|
|||
|
||||
import java.util.*;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.SearchManager;
|
||||
import android.content.ActivityNotFoundException;
|
||||
import android.content.ComponentName;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.app.*;
|
||||
import android.content.*;
|
||||
import android.net.Uri;
|
||||
|
||||
import org.geometerplus.zlibrary.core.options.ZLStringOption;
|
||||
|
@ -37,85 +32,111 @@ import org.geometerplus.android.util.UIUtil;
|
|||
import org.geometerplus.android.util.PackageUtil;
|
||||
|
||||
public abstract class DictionaryUtil {
|
||||
private static LinkedList<PackageInfo> ourDictionaryInfos = new LinkedList<PackageInfo>();
|
||||
// Map: dictionary info -> hide if package is not installed
|
||||
private static LinkedHashMap<PackageInfo,Boolean> ourDictionaryInfos =
|
||||
new LinkedHashMap<PackageInfo,Boolean>();
|
||||
private static ZLStringOption ourDictionaryOption;
|
||||
|
||||
public static List<PackageInfo> dictionaryInfos() {
|
||||
private static Map<PackageInfo,Boolean> infos() {
|
||||
if (ourDictionaryInfos.isEmpty()) {
|
||||
ourDictionaryInfos.add(new PackageInfo(
|
||||
"com.socialnmobile.colordict",
|
||||
"com.socialnmobile.colordict.activity.Main",
|
||||
"ColorDict",
|
||||
null,
|
||||
true
|
||||
));
|
||||
ourDictionaryInfos.add(new PackageInfo(
|
||||
"com.ngc.fora",
|
||||
"com.ngc.fora.ForaDictionary",
|
||||
"Fora Dictionary",
|
||||
null,
|
||||
true
|
||||
));
|
||||
ourDictionaryInfos.add(new PackageInfo(
|
||||
"com.slovoed.noreg.english_german.deluxe",
|
||||
"com.slovoed.noreg.english_german.deluxe.Start",
|
||||
"SlovoEd Deluxe German->English",
|
||||
"/808464950",
|
||||
false
|
||||
));
|
||||
ourDictionaryInfos.add(new PackageInfo(
|
||||
"com.slovoed.noreg.english_german.deluxe",
|
||||
"com.slovoed.noreg.english_german.deluxe.Start",
|
||||
"SlovoEd Deluxe English->German",
|
||||
"/808464949",
|
||||
false
|
||||
));
|
||||
ourDictionaryInfos.add(new PackageInfo(
|
||||
"org.freedictionary",
|
||||
"org.freedictionary.MainActivity",
|
||||
"Free Dictionary . org",
|
||||
"",
|
||||
false
|
||||
));
|
||||
ourDictionaryInfos.put(new PackageInfo(
|
||||
"ColorDict", // Id
|
||||
"com.socialnmobile.colordict", // Package
|
||||
"com.socialnmobile.colordict.activity.Main", // Class
|
||||
"ColorDict", // Title
|
||||
Intent.ACTION_SEARCH,
|
||||
"%s"
|
||||
), false);
|
||||
ourDictionaryInfos.put(new PackageInfo(
|
||||
"Fora Dictionary", // Id
|
||||
"com.ngc.fora", // Package
|
||||
"com.ngc.fora.ForaDictionary", // Class
|
||||
"Fora Dictionary", // Title
|
||||
Intent.ACTION_SEARCH,
|
||||
"%s"
|
||||
), false);
|
||||
ourDictionaryInfos.put(new PackageInfo(
|
||||
"Free Dictionary . org", // Id
|
||||
"org.freedictionary", // Package
|
||||
"org.freedictionary.MainActivity", // Class
|
||||
"Free Dictionary . org", // Title
|
||||
Intent.ACTION_VIEW,
|
||||
"%s"
|
||||
), false);
|
||||
ourDictionaryInfos.put(new PackageInfo(
|
||||
"SlovoEd Deluxe German->English", // Id
|
||||
"com.slovoed.noreg.english_german.deluxe", // Package
|
||||
"com.slovoed.noreg.english_german.deluxe.Start", // Class
|
||||
"SlovoEd Deluxe German->English", // Title
|
||||
Intent.ACTION_VIEW,
|
||||
"%s/808464950"
|
||||
), true);
|
||||
ourDictionaryInfos.put(new PackageInfo(
|
||||
"SlovoEd Deluxe English->German", // Id
|
||||
"com.slovoed.noreg.english_german.deluxe", // Package
|
||||
"com.slovoed.noreg.english_german.deluxe.Start", // Class
|
||||
"SlovoEd Deluxe English->German", // Title
|
||||
Intent.ACTION_VIEW,
|
||||
"%s/808464949"
|
||||
), true);
|
||||
}
|
||||
return ourDictionaryInfos;
|
||||
}
|
||||
|
||||
public static List<PackageInfo> dictionaryInfos(Context context) {
|
||||
final LinkedList<PackageInfo> list = new LinkedList<PackageInfo>();
|
||||
for (Map.Entry<PackageInfo,Boolean> entry : infos().entrySet()) {
|
||||
final PackageInfo info = entry.getKey();
|
||||
if (!entry.getValue() ||
|
||||
PackageUtil.canBeStarted(context, getDictionaryIntent(info, "test"))) {
|
||||
list.add(info);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static PackageInfo firstInfo() {
|
||||
for (Map.Entry<PackageInfo,Boolean> entry : infos().entrySet()) {
|
||||
if (!entry.getValue()) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("There are no available dictionary infos");
|
||||
}
|
||||
|
||||
public static ZLStringOption dictionaryOption() {
|
||||
if (ourDictionaryOption == null) {
|
||||
ourDictionaryOption =
|
||||
new ZLStringOption("Dictionary", "Title", dictionaryInfos().get(0).Title);
|
||||
ourDictionaryOption = new ZLStringOption("Dictionary", "Id", firstInfo().Id);
|
||||
}
|
||||
return ourDictionaryOption;
|
||||
}
|
||||
|
||||
private static PackageInfo getCurrentDictionaryInfo() {
|
||||
final String title = dictionaryOption().getValue();
|
||||
for (PackageInfo info : dictionaryInfos()) {
|
||||
if (info.Title.equals(title)) {
|
||||
final String id = dictionaryOption().getValue();
|
||||
for (PackageInfo info : infos().keySet()) {
|
||||
if (info.Id.equals(id)) {
|
||||
return info;
|
||||
}
|
||||
}
|
||||
return dictionaryInfos().get(0);
|
||||
return firstInfo();
|
||||
}
|
||||
|
||||
public static Intent getDictionaryIntent(String text) {
|
||||
final PackageInfo dictionaryInfo = getCurrentDictionaryInfo();
|
||||
if (dictionaryInfo.UseSearchIntend) {
|
||||
return new Intent(Intent.ACTION_SEARCH)
|
||||
.setComponent(new ComponentName(
|
||||
dictionaryInfo.PackageName,
|
||||
dictionaryInfo.ClassName
|
||||
))
|
||||
.putExtra(SearchManager.QUERY, text);
|
||||
private static Intent getDictionaryIntent(String text) {
|
||||
return getDictionaryIntent(getCurrentDictionaryInfo(), text);
|
||||
}
|
||||
|
||||
public static Intent getDictionaryIntent(PackageInfo dictionaryInfo, String text) {
|
||||
final Intent intent = new Intent(dictionaryInfo.IntentAction)
|
||||
.setComponent(new ComponentName(
|
||||
dictionaryInfo.PackageName,
|
||||
dictionaryInfo.ClassName
|
||||
))
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
text = dictionaryInfo.IntentDataPattern.replace("%s", text);
|
||||
if (Intent.ACTION_SEARCH.equals(dictionaryInfo.IntentAction)) {
|
||||
return intent.putExtra(SearchManager.QUERY, text);
|
||||
} else {
|
||||
return new Intent(Intent.ACTION_VIEW)
|
||||
.setComponent(new ComponentName(
|
||||
dictionaryInfo.PackageName,
|
||||
dictionaryInfo.ClassName
|
||||
))
|
||||
.setData(Uri.parse(text + dictionaryInfo.Argument))
|
||||
.addFlags(0x14000000);
|
||||
return intent.setData(Uri.parse(text));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue