gui: use submenus for subdirs
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / android / AndroidReaderActivity.java
... / ...
CommitLineData
1package be.nikiroo.fanfix.reader.android;
2
3import android.app.Activity;
4import android.app.AlertDialog;
5import android.app.FragmentTransaction;
6import android.content.Context;
7import android.content.DialogInterface;
8import android.os.Bundle;
9import android.os.Environment;
10import android.text.InputType;
11import android.view.View;
12import android.widget.EditText;
13
14import java.io.File;
15import java.io.IOException;
16import java.net.URL;
17
18import be.nikiroo.fanfix.Instance;
19import be.nikiroo.fanfix.data.MetaData;
20import be.nikiroo.fanfix.reader.BasicReader;
21import be.nikiroo.fanfix.reader.Reader;
22import be.nikiroo.utils.TraceHandler;
23
24public class AndroidReaderActivity extends Activity implements
25 AndroidReaderBook.OnFragmentInteractionListener {
26 private static Reader reader = null;
27
28 @Override
29 protected void onCreate(Bundle savedInstanceState) {
30 reader = config();
31 super.onCreate(savedInstanceState);
32 setContentView(R.layout.activity_main);
33 }
34
35 @Override
36 protected void onStart() {
37 super.onStart();
38 refresh();
39 }
40
41 private void refresh() {
42 AndroidReaderGroup group = new AndroidReaderGroup();
43
44 FragmentTransaction trans = getFragmentManager().beginTransaction();
45 trans.replace(R.id.Main_pnlStories, group);
46 trans.commit();
47 getFragmentManager().executePendingTransactions();
48
49 group.fill(reader.getLibrary().getList(), reader);
50 }
51
52 public void onAdd(View view) {
53 final View root = findViewById(R.id.Main);
54
55 ask(this,
56 "Import new story",
57 "Enter the story URL (the program will then download it -- the interface will not be usable until it is downloaded",
58 "Download", new AnswerListener() {
59 @Override
60 public void onAnswer(final String answer) {
61 root.setEnabled(false);
62 new Thread(new Runnable() {
63 @Override
64 public void run() {
65 try {
66 URL url = new URL(answer);
67 reader.getLibrary().imprt(url, null);
68 } catch (Throwable e) {
69 // TODO: show error message correctly
70 String mess = "";
71 for (String tab = ""; e != null
72 && e != e.getCause(); e = e
73 .getCause()) {
74 mess += tab + "["
75 + e.getClass().getSimpleName()
76 + "] " + e.getMessage() + "\n";
77 tab += "\t";
78 }
79
80 final String messf = mess;
81 AndroidReaderActivity.this
82 .runOnUiThread(new Runnable() {
83 @Override
84 public void run() {
85 ask(AndroidReaderActivity.this,
86 "Error",
87 "Cannot import URL: \n"
88 + messf,
89 "OK", null);
90 }
91 });
92
93 }
94
95 AndroidReaderActivity.this
96 .runOnUiThread(new Runnable() {
97 @Override
98 public void run() {
99 refresh();
100 root.setEnabled(true);
101 }
102 });
103 }
104 }).start();
105 }
106 });
107
108 /*
109 * Intent intent = new Intent(AndroidReaderActivity.this, SayIt.class);
110 * intent.putExtra(SayIt.MESSAGE, message); startActivity(intent);
111 */
112 }
113
114 @Override
115 public void onFragmentInteraction(MetaData meta) {
116 AndroidReader reader = new AndroidReader(this);
117 try {
118 reader.openExternal(Instance.getLibrary(), meta.getLuid());
119 } catch (IOException e) {
120 e.printStackTrace();
121 }
122 }
123
124 private Reader config() {
125 if (reader != null) {
126 return reader;
127 }
128
129 String internal = getExternalFilesDir(null).toString();
130 File user = Environment
131 .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
132
133 try {
134 File parent = user.getParentFile();
135 if (parent.exists() || parent.mkdirs()) {
136 File test = new File(parent, "test");
137 if (test.exists() || (test.createNewFile() && test.delete())) {
138 user = parent;
139 }
140 }
141 } catch (Exception e) {
142 // Fall back to Documents/Books
143 }
144
145 System.setProperty("DEBUG", "1");
146 System.setProperty("fanfix.home", internal);
147 System.setProperty("fanfix.libdir", new File(user, "Books").toString());
148
149 Instance.resetConfig(false);
150 Instance.setTraceHandler(new TraceHandler(true, true, 2));
151
152 BasicReader.setDefaultReaderType(Reader.ReaderType.ANDROID);
153 return BasicReader.getReader();
154 }
155
156 public static void ask(Context context, String title, String message,
157 String okMessage, final AnswerListener listener) {
158 final EditText input = new EditText(context);
159 input.setFocusable(true);
160 input.setInputType(InputType.TYPE_CLASS_TEXT);
161
162 AlertDialog.Builder alert = new AlertDialog.Builder(context);
163 alert.setTitle(title);
164 alert.setMessage(message);
165 alert.setCancelable(true);
166 alert.setView(input);
167
168 if (listener != null) {
169 alert.setPositiveButton(okMessage,
170 new DialogInterface.OnClickListener() {
171 @Override
172 public void onClick(DialogInterface dialog, int which) {
173 listener.onAnswer(input.getText().toString());
174 }
175 });
176
177 alert.setOnCancelListener(new DialogInterface.OnCancelListener() {
178 @Override
179 public void onCancel(DialogInterface dialog) {
180 listener.onAnswer(null);
181 }
182 });
183 }
184
185 alert.show();
186 }
187
188 private interface AnswerListener {
189 public void onAnswer(String answer);
190 }
191}