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

dictionary code refactoring

This commit is contained in:
Nikolay Pultsin 2011-01-15 17:54:36 +00:00
parent 520736b313
commit 7824ad3482
4 changed files with 100 additions and 74 deletions

View file

@ -21,13 +21,8 @@ package org.geometerplus.android.fbreader;
import java.util.*; import java.util.*;
import android.app.Activity; import android.app.*;
import android.app.AlertDialog; import android.content.*;
import android.app.SearchManager;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import org.geometerplus.zlibrary.core.options.ZLStringOption; import org.geometerplus.zlibrary.core.options.ZLStringOption;
@ -37,85 +32,111 @@ import org.geometerplus.android.util.UIUtil;
import org.geometerplus.android.util.PackageUtil; import org.geometerplus.android.util.PackageUtil;
public abstract class DictionaryUtil { 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; private static ZLStringOption ourDictionaryOption;
public static List<PackageInfo> dictionaryInfos() { private static Map<PackageInfo,Boolean> infos() {
if (ourDictionaryInfos.isEmpty()) { if (ourDictionaryInfos.isEmpty()) {
ourDictionaryInfos.add(new PackageInfo( ourDictionaryInfos.put(new PackageInfo(
"com.socialnmobile.colordict", "ColorDict", // Id
"com.socialnmobile.colordict.activity.Main", "com.socialnmobile.colordict", // Package
"ColorDict", "com.socialnmobile.colordict.activity.Main", // Class
null, "ColorDict", // Title
true Intent.ACTION_SEARCH,
)); "%s"
ourDictionaryInfos.add(new PackageInfo( ), false);
"com.ngc.fora", ourDictionaryInfos.put(new PackageInfo(
"com.ngc.fora.ForaDictionary", "Fora Dictionary", // Id
"Fora Dictionary", "com.ngc.fora", // Package
null, "com.ngc.fora.ForaDictionary", // Class
true "Fora Dictionary", // Title
)); Intent.ACTION_SEARCH,
ourDictionaryInfos.add(new PackageInfo( "%s"
"com.slovoed.noreg.english_german.deluxe", ), false);
"com.slovoed.noreg.english_german.deluxe.Start", ourDictionaryInfos.put(new PackageInfo(
"SlovoEd Deluxe German->English", "Free Dictionary . org", // Id
"/808464950", "org.freedictionary", // Package
false "org.freedictionary.MainActivity", // Class
)); "Free Dictionary . org", // Title
ourDictionaryInfos.add(new PackageInfo( Intent.ACTION_VIEW,
"com.slovoed.noreg.english_german.deluxe", "%s"
"com.slovoed.noreg.english_german.deluxe.Start", ), false);
"SlovoEd Deluxe English->German", ourDictionaryInfos.put(new PackageInfo(
"/808464949", "SlovoEd Deluxe German->English", // Id
false "com.slovoed.noreg.english_german.deluxe", // Package
)); "com.slovoed.noreg.english_german.deluxe.Start", // Class
ourDictionaryInfos.add(new PackageInfo( "SlovoEd Deluxe German->English", // Title
"org.freedictionary", Intent.ACTION_VIEW,
"org.freedictionary.MainActivity", "%s/808464950"
"Free Dictionary . org", ), true);
"", ourDictionaryInfos.put(new PackageInfo(
false "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; 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() { public static ZLStringOption dictionaryOption() {
if (ourDictionaryOption == null) { if (ourDictionaryOption == null) {
ourDictionaryOption = ourDictionaryOption = new ZLStringOption("Dictionary", "Id", firstInfo().Id);
new ZLStringOption("Dictionary", "Title", dictionaryInfos().get(0).Title);
} }
return ourDictionaryOption; return ourDictionaryOption;
} }
private static PackageInfo getCurrentDictionaryInfo() { private static PackageInfo getCurrentDictionaryInfo() {
final String title = dictionaryOption().getValue(); final String id = dictionaryOption().getValue();
for (PackageInfo info : dictionaryInfos()) { for (PackageInfo info : infos().keySet()) {
if (info.Title.equals(title)) { if (info.Id.equals(id)) {
return info; return info;
} }
} }
return dictionaryInfos().get(0); return firstInfo();
} }
public static Intent getDictionaryIntent(String text) { private static Intent getDictionaryIntent(String text) {
final PackageInfo dictionaryInfo = getCurrentDictionaryInfo(); return getDictionaryIntent(getCurrentDictionaryInfo(), text);
if (dictionaryInfo.UseSearchIntend) { }
return new Intent(Intent.ACTION_SEARCH)
.setComponent(new ComponentName( public static Intent getDictionaryIntent(PackageInfo dictionaryInfo, String text) {
dictionaryInfo.PackageName, final Intent intent = new Intent(dictionaryInfo.IntentAction)
dictionaryInfo.ClassName .setComponent(new ComponentName(
)) dictionaryInfo.PackageName,
.putExtra(SearchManager.QUERY, text); 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 { } else {
return new Intent(Intent.ACTION_VIEW) return intent.setData(Uri.parse(text));
.setComponent(new ComponentName(
dictionaryInfo.PackageName,
dictionaryInfo.ClassName
))
.setData(Uri.parse(text + dictionaryInfo.Argument))
.addFlags(0x14000000);
} }
} }

View file

@ -20,17 +20,21 @@
package org.geometerplus.android.fbreader; package org.geometerplus.android.fbreader;
public class PackageInfo { public class PackageInfo {
public final String Id;
public final String PackageName; public final String PackageName;
public final String ClassName; public final String ClassName;
public final String Title; 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; PackageName = packageName;
ClassName = className; ClassName = className;
Title = title; Title = title;
Argument = argument;
UseSearchIntend = useSearchIntend; IntentAction = intentAction;
IntentDataPattern = intentDataPattern;
} }
} }

View file

@ -36,7 +36,7 @@ class DictionaryPreference extends ZLStringListPreference {
super(context, resource, resourceKey); super(context, resource, resourceKey);
myOption = DictionaryUtil.dictionaryOption(); 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[] values = new String[infos.size()];
final String[] texts = new String[infos.size()]; final String[] texts = new String[infos.size()];

View file

@ -23,6 +23,7 @@ import java.util.Map;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
@ -64,8 +65,8 @@ public abstract class PackageUtil {
); );
} }
public static boolean canBeStarted(Activity activity, Intent intent) { public static boolean canBeStarted(Context context, Intent intent) {
return activity.getApplicationContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null; return context.getApplicationContext().getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null;
} }
public static boolean installFromMarket(Activity activity, String pkg) { public static boolean installFromMarket(Activity activity, String pkg) {