11package org .coolreader ;
22
33import org .coolreader .crengine .BaseActivity ;
4+ import org .coolreader .crengine .BookInfo ;
45import org .coolreader .crengine .DeviceInfo ;
56import org .coolreader .crengine .L ;
67import org .coolreader .crengine .Logger ;
1112import android .content .ComponentName ;
1213import android .content .Intent ;
1314import android .content .pm .PackageManager ;
15+ import android .util .DisplayMetrics ;
1416
17+ import com .abbyy .mobile .lingvo .api .MinicardContract ;
1518import java .util .ArrayList ;
1619import java .util .List ;
1720
1821public class Dictionaries {
1922
23+ public static class PopupFrameMetric {
24+ public final int Height ;
25+ public final int Gravity ;
26+
27+ PopupFrameMetric (DisplayMetrics metrics , int selectionTop , int selectionBottom ) {
28+ final int screenHeight = metrics .heightPixels ;
29+ final int topSpace = selectionTop ;
30+ final int bottomSpace = metrics .heightPixels - selectionBottom ;
31+ final boolean showAtBottom = bottomSpace >= topSpace ;
32+ final int space = (showAtBottom ? bottomSpace : topSpace ) - metrics .densityDpi / 12 ;
33+ final int maxHeight = Math .min (metrics .densityDpi * 20 / 12 , screenHeight * 2 / 3 );
34+ final int minHeight = Math .min (metrics .densityDpi * 10 / 12 , screenHeight * 2 / 3 );
35+
36+ Height = Math .max (minHeight , Math .min (maxHeight , space ));
37+ Gravity = showAtBottom ? android .view .Gravity .BOTTOM : android .view .Gravity .TOP ;
38+ }
39+ }
40+
2041 private Activity mActivity ;
2142
2243 public Integer isiDic2IsActive () {
@@ -82,6 +103,7 @@ public DictInfo ( String id, String name, String packageName, String className,
82103 new DictInfo ("Dictan" , "Dictan Dictionary" , "info.softex.dictan" , null , Intent .ACTION_VIEW , 2 ),
83104 new DictInfo ("FreeDictionary.org" , "Free Dictionary . org" , "org.freedictionary" , "org.freedictionary.MainActivity" , "android.intent.action.VIEW" , 0 ),
84105 new DictInfo ("ABBYYLingvo" , "ABBYY Lingvo" , "com.abbyy.mobile.lingvo.market" , null /*com.abbyy.mobile.lingvo.market.MainActivity*/ , "com.abbyy.mobile.lingvo.intent.action.TRANSLATE" , 0 ).setDataKey ("com.abbyy.mobile.lingvo.intent.extra.TEXT" ),
106+ new DictInfo ("ABBYYLingvo (minicard)" , "ABBYY Lingvo (minicard)" , "com.abbyy.mobile.lingvo.market" , null , "com.abbyy.mobile.lingvo.intent.action.TRANSLATE" , 5 ).setDataKey ("com.abbyy.mobile.lingvo.intent.extra.TEXT" ),
85107 //new DictInfo("ABBYYLingvoLive", "ABBYY Lingvo Live", "com.abbyy.mobile.lingvolive", null, "com.abbyy.mobile.lingvo.intent.action.TRANSLATE", 0).setDataKey("com.abbyy.mobile.lingvo.intent.extra.TEXT"),
86108 new DictInfo ("LingoQuizLite" , "Lingo Quiz Lite" , "mnm.lite.lingoquiz" , "mnm.lite.lingoquiz.ExchangeActivity" , "lingoquiz.intent.action.ADD_WORD" , 0 ).setDataKey ("EXTRA_WORD" ),
87109 new DictInfo ("LingoQuiz" , "Lingo Quiz" , "mnm.lingoquiz" , "mnm.lingoquiz.ExchangeActivity" , "lingoquiz.intent.action.ADD_WORD" , 0 ).setDataKey ("EXTRA_WORD" ),
@@ -266,18 +288,6 @@ public void findInDictionary(String s) throws DictionaryException {
266288 intent4 .setType ("text/plain" );
267289 intent4 .putExtra (android .content .Intent .EXTRA_SUBJECT , "" );
268290 intent4 .putExtra (android .content .Intent .EXTRA_TEXT , s );
269- //List<ResolveInfo> resInfo = mActivity.getPackageManager().queryIntentActivities(intent4, 0);
270- //for (resInfo : mActivity.getPackageManager().queryIntentActivities(intent4, 0)) {
271- // if (resInfo.
272- //};
273- //startActivity(Intent.createChooser(intent4, null));
274- //intent4.setAction(Intent.ACTION_VIEW);
275- //intent4.putExtra("key_text_input", "What time is it?");
276- //intent4.putExtra("key_text_output", "");
277- //intent4.putExtra("key_language_from", "en");
278- //intent4.putExtra("key_language_to", "es");
279- //intent4.putExtra("key_suggest_translation", "");
280- //intent4.putExtra("key_from_floating_window", false);
281291 intent4 .setComponent (new ComponentName (curDict .packageName , curDict .className ));
282292 try
283293 {
@@ -286,8 +296,72 @@ public void findInDictionary(String s) throws DictionaryException {
286296 throw new DictionaryException ("Dictionary \" " + curDict .name + "\" is not installed" );
287297 }
288298 break ;
299+ case 5 :
300+ Intent intent5 = new Intent (curDict .action );
301+ final DisplayMetrics metrics = new DisplayMetrics ();
302+ mActivity .getWindowManager ().getDefaultDisplay ().getMetrics (metrics );
303+ int selectionTop = 0 ;
304+ int selectionBottom = 0 ;
305+ if (mActivity instanceof CoolReader ) {
306+ CoolReader cr = (CoolReader ) mActivity ;
307+ if (cr .getReaderView ()!=null ) {
308+ if (cr .getReaderView ().lastSelection != null ) {
309+ selectionTop = cr .getReaderView ().lastSelection .startY ;
310+ selectionBottom = cr .getReaderView ().lastSelection .endY ;
311+ }
312+ if (cr .getReaderView ().getBookInfo ()!=null ) {
313+ BookInfo book = cr .getReaderView ().getBookInfo ();
314+ String lang = book .getFileInfo ().lang_to ;
315+ if (lang ==null ) lang = "" ;
316+ if (lang .equals ("" )) lang = "ru" ;
317+ String langf = book .getFileInfo ().lang_from ;
318+ if (langf ==null ) langf = "" ;
319+ if (langf .equals ("" )) langf = book .getFileInfo ().language ;
320+ if (langf ==null ) langf = "" ;
321+ if (lang .equals ("" )) lang = "en" ;
322+ intent5 .putExtra (MinicardContract .EXTRA_LANGUAGE_TO , lang );
323+ intent5 .putExtra (MinicardContract .EXTRA_LANGUAGE_FROM , langf );
324+ }
325+ }
326+ }
327+ if (selectionBottom <selectionTop ) {
328+ int dummy = selectionBottom ;
329+ selectionBottom = selectionTop ;
330+ selectionTop = dummy ;
331+ }
332+ final PopupFrameMetric frameMetrics =
333+ new PopupFrameMetric (metrics , selectionTop , selectionBottom );
334+ intent5 .putExtra (MinicardContract .EXTRA_GRAVITY , frameMetrics .Gravity );
335+ intent5 .putExtra (MinicardContract .EXTRA_HEIGHT , frameMetrics .Height );
336+ intent5 .putExtra (MinicardContract .EXTRA_FORCE_LEMMATIZATION , true );
337+ intent5 .putExtra (MinicardContract .EXTRA_TRANSLATE_VARIANTS , true );
338+ intent5 .putExtra (MinicardContract .EXTRA_ENABLE_SUGGESTIONS , true );
339+ //intent5.putExtra(MinicardContract.EXTRA_LIGHT_THEME, true);
340+ intent5 .addFlags (Intent .FLAG_ACTIVITY_NO_HISTORY );
341+ intent5 .addFlags (Intent .FLAG_ACTIVITY_NO_ANIMATION );
342+ if (curDict .className != null || DeviceInfo .getSDKLevel () == 3 ) {
343+ intent5 .setComponent (new ComponentName (
344+ curDict .packageName , curDict .className ));
345+ } else {
346+ intent5 .setPackage (curDict .packageName );
347+ }
348+ intent5 .addFlags (DeviceInfo .getSDKLevel () >= 7 ? Intent .FLAG_ACTIVITY_CLEAR_TASK : Intent .FLAG_ACTIVITY_NEW_TASK );
349+ if (s !=null )
350+ intent5 .putExtra (curDict .dataKey , s );
351+ try {
352+ mActivity .startActivity ( intent5 );
353+ } catch ( ActivityNotFoundException e ) {
354+ throw new DictionaryException ("Dictionary \" " + curDict .name + "\" is not installed" );
355+ } catch ( Exception e ) {
356+ throw new DictionaryException ("Can't open dictionary \" " + curDict .name + "\" " );
357+ }
358+ break ;
359+ //final String targetLanguage = DictionaryUtil.TargetLanguageOption.getValue();
360+ //if (!Language.ANY_CODE.equals(targetLanguage)) {
361+ // intent.putExtra(MinicardContract.EXTRA_LANGUAGE_TO, targetLanguage);
362+ //}
363+ //InternalUtil.startDictionaryActivity(fbreader, intent, this);
289364 }
290-
291365 }
292366
293367 public void onActivityResult (int requestCode , int resultCode , Intent intent ) throws DictionaryException {
0 commit comments