mirror of
https://github.com/geometer/FBReaderJ.git
synced 2025-10-05 02:39: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));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,17 +20,21 @@
|
|||
package org.geometerplus.android.fbreader;
|
||||
|
||||
public class PackageInfo {
|
||||
public final String Id;
|
||||
public final String PackageName;
|
||||
public final String ClassName;
|
||||
public final String Title;
|
||||
public final String Argument;
|
||||
public final boolean UseSearchIntend;
|
||||
|
||||
PackageInfo(String packageName, String className, String title, String argument, boolean useSearchIntend) {
|
||||
public final String IntentAction;
|
||||
public final String IntentDataPattern;
|
||||
|
||||
PackageInfo(String id, String packageName, String className, String title, String intentAction, String intentDataPattern) {
|
||||
Id = id;
|
||||
PackageName = packageName;
|
||||
ClassName = className;
|
||||
Title = title;
|
||||
Argument = argument;
|
||||
UseSearchIntend = useSearchIntend;
|
||||
|
||||
IntentAction = intentAction;
|
||||
IntentDataPattern = intentDataPattern;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ class DictionaryPreference extends ZLStringListPreference {
|
|||
super(context, resource, resourceKey);
|
||||
|
||||
myOption = DictionaryUtil.dictionaryOption();
|
||||
final List<PackageInfo> infos = DictionaryUtil.dictionaryInfos();
|
||||
final List<PackageInfo> infos = DictionaryUtil.dictionaryInfos(context);
|
||||
|
||||
final String[] values = new String[infos.size()];
|
||||
final String[] texts = new String[infos.size()];
|
||||
|
|
|
@ -23,6 +23,7 @@ import java.util.Map;
|
|||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.ActivityNotFoundException;
|
||||
|
@ -64,8 +65,8 @@ public abstract class PackageUtil {
|
|||
);
|
||||
}
|
||||
|
||||
public static boolean canBeStarted(Activity activity, Intent intent) {
|
||||
return activity.getApplicationContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null;
|
||||
public static boolean canBeStarted(Context context, Intent intent) {
|
||||
return context.getApplicationContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null;
|
||||
}
|
||||
|
||||
public static boolean installFromMarket(Activity activity, String pkg) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue