1 package org.coolreader.crengine;
2 
3 import java.util.Collection;
4 import java.util.HashMap;
5 import java.util.Map;
6 
7 import org.coolreader.R;
8 import org.coolreader.genrescollection.GenresCollection;
9 
10 import android.view.LayoutInflater;
11 import android.view.View;
12 import android.widget.TableLayout;
13 import android.widget.TableRow;
14 import android.widget.TextView;
15 
16 public class BookInfoDialog extends BaseDialog {
17 	private final BaseActivity mCoolReader;
18 	private final LayoutInflater mInflater;
19 	private Map<String, Integer> mLabelMap;
fillMap()20 	private void fillMap() {
21 		mLabelMap = new HashMap<String, Integer>();
22 		mLabelMap.put("section.system", R.string.book_info_section_system);
23 		mLabelMap.put("system.version", R.string.book_info_system_version);
24 		mLabelMap.put("system.battery", R.string.book_info_system_battery);
25 		mLabelMap.put("system.time", R.string.book_info_system_time);
26 		mLabelMap.put("section.file", R.string.book_info_section_file_properties);
27 		mLabelMap.put("file.name", R.string.book_info_file_name);
28 		mLabelMap.put("file.path", R.string.book_info_file_path);
29 		mLabelMap.put("file.arcname", R.string.book_info_file_arcname);
30 		mLabelMap.put("file.arcpath", R.string.book_info_file_arcpath);
31 		mLabelMap.put("file.arcsize", R.string.book_info_file_arcsize);
32 		mLabelMap.put("file.size", R.string.book_info_file_size);
33 		mLabelMap.put("file.format", R.string.book_info_file_format);
34 		mLabelMap.put("section.position", R.string.book_info_section_current_position);
35 		mLabelMap.put("position.percent", R.string.book_info_position_percent);
36 		mLabelMap.put("position.page", R.string.book_info_position_page);
37 		mLabelMap.put("position.chapter", R.string.book_info_position_chapter);
38 		mLabelMap.put("section.book", R.string.book_info_section_book_properties);
39 		mLabelMap.put("book.authors", R.string.book_info_book_authors);
40 		mLabelMap.put("book.title", R.string.book_info_book_title);
41 		mLabelMap.put("book.series", R.string.book_info_book_series_name);
42 		mLabelMap.put("book.genres", R.string.book_info_genres);
43 		mLabelMap.put("book.language", R.string.book_info_book_language);
44 	}
45 
addItem(TableLayout table, String item)46 	private void addItem(TableLayout table, String item) {
47 		int p = item.indexOf("=");
48 		if ( p<0 )
49 			return;
50 		String name = item.substring(0, p).trim();
51 		String value = item.substring(p+1).trim();
52 		if ( name.length()==0 || value.length()==0 )
53 			return;
54 		boolean isSection = false;
55 		if ( "section".equals(name) ) {
56 			name = "";
57 			Integer id = mLabelMap.get(value);
58 			if ( id==null )
59 				return;
60 			String section = getContext().getString(id);
61 			if ( section!=null )
62 				value = section;
63 			isSection = true;
64 		} else if ("book.genres".equals(name)) {
65 			// genres ids separated by "|", see MainDB.READ_FILEINFO_FIELDS:
66 			StringBuilder genres = new StringBuilder();
67 			String[] parts = value.split("\\|");
68 			for (String code : parts) {
69 				code = code.trim();
70 				if (code.length() > 0) {
71 					if (genres.length() > 0)
72 						genres.append("\n");
73 					genres.append(Services.getGenresCollection().translate(code));
74 				}
75 			}
76 			value = genres.toString();
77 			Integer id = mLabelMap.get(name);
78 			String title = id!=null ? getContext().getString(id) : name;
79 			if ( title!=null )
80 				name = title;
81 		} else {
82 			Integer id = mLabelMap.get(name);
83 			String title = id!=null ? getContext().getString(id) : name;
84 			if ( title!=null )
85 				name = title;
86 		}
87 		TableRow tableRow = (TableRow)mInflater.inflate(isSection ? R.layout.book_info_section : R.layout.book_info_item, null);
88 		TextView nameView = (TextView)tableRow.findViewById(R.id.name);
89 		TextView valueView = (TextView)tableRow.findViewById(R.id.value);
90 		nameView.setText(name);
91 		valueView.setText(value);
92 		table.addView(tableRow);
93 	}
94 
BookInfoDialog( BaseActivity activity, Collection<String> items)95 	public BookInfoDialog( BaseActivity activity, Collection<String> items)
96 	{
97 		super(activity);
98 		mCoolReader = activity;
99 		setTitle(mCoolReader.getString(R.string.dlg_book_info));
100 		fillMap();
101 		mInflater = LayoutInflater.from(getContext());
102 		View view = mInflater.inflate(R.layout.book_info_dialog, null);
103 		TableLayout table = (TableLayout)view.findViewById(R.id.table);
104 		for ( String item : items ) {
105 			addItem(table, item);
106 		}
107 		setView( view );
108 	}
109 
110 }
111