1 package be
.nikiroo
.fanfix
.reader
.android
;
3 import android
.app
.Activity
;
4 import android
.app
.AlertDialog
;
5 import android
.app
.FragmentTransaction
;
6 import android
.content
.Context
;
7 import android
.content
.DialogInterface
;
8 import android
.os
.Bundle
;
9 import android
.os
.Environment
;
10 import android
.text
.InputType
;
11 import android
.view
.View
;
12 import android
.widget
.EditText
;
15 import java
.io
.IOException
;
18 import be
.nikiroo
.fanfix
.Instance
;
19 import be
.nikiroo
.fanfix
.data
.MetaData
;
20 import be
.nikiroo
.fanfix
.reader
.BasicReader
;
21 import be
.nikiroo
.fanfix
.reader
.Reader
;
22 import be
.nikiroo
.utils
.TraceHandler
;
24 public class AndroidReaderActivity
extends Activity
implements
25 AndroidReaderBook
.OnFragmentInteractionListener
{
26 private static Reader reader
= null;
29 protected void onCreate(Bundle savedInstanceState
) {
31 super.onCreate(savedInstanceState
);
32 setContentView(R
.layout
.activity_main
);
36 protected void onStart() {
41 private void refresh() {
42 AndroidReaderGroup group
= new AndroidReaderGroup();
44 FragmentTransaction trans
= getFragmentManager().beginTransaction();
45 trans
.replace(R
.id
.Main_pnlStories
, group
);
47 getFragmentManager().executePendingTransactions();
49 group
.fill(reader
.getLibrary().getList(), reader
);
52 public void onAdd(View view
) {
53 final View root
= findViewById(R
.id
.Main
);
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() {
60 public void onAnswer(final String answer
) {
61 root
.setEnabled(false);
62 new Thread(new Runnable() {
66 URL url
= new URL(answer
);
67 reader
.getLibrary().imprt(url
, null);
68 } catch (Throwable e
) {
69 // TODO: show error message correctly
71 for (String tab
= ""; e
!= null
72 && e
!= e
.getCause(); e
= e
75 + e
.getClass().getSimpleName()
76 + "] " + e
.getMessage() + "\n";
80 final String messf
= mess
;
81 AndroidReaderActivity
.this
82 .runOnUiThread(new Runnable() {
85 ask(AndroidReaderActivity
.this,
87 "Cannot import URL: \n"
95 AndroidReaderActivity
.this
96 .runOnUiThread(new Runnable() {
100 root
.setEnabled(true);
109 * Intent intent = new Intent(AndroidReaderActivity.this, SayIt.class);
110 * intent.putExtra(SayIt.MESSAGE, message); startActivity(intent);
115 public void onFragmentInteraction(MetaData meta
) {
116 AndroidReader reader
= new AndroidReader(this);
118 reader
.openExternal(Instance
.getLibrary(), meta
.getLuid());
119 } catch (IOException e
) {
124 private Reader
config() {
125 if (reader
!= null) {
129 String internal
= getExternalFilesDir(null).toString();
130 File user
= Environment
131 .getExternalStoragePublicDirectory(Environment
.DIRECTORY_DOCUMENTS
);
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())) {
141 } catch (Exception e
) {
142 // Fall back to Documents/Books
145 System
.setProperty("DEBUG", "1");
146 System
.setProperty("fanfix.home", internal
);
147 System
.setProperty("fanfix.libdir", new File(user
, "Books").toString());
149 Instance
.resetConfig(false);
150 Instance
.setTraceHandler(new TraceHandler(true, true, 2));
152 BasicReader
.setDefaultReaderType(Reader
.ReaderType
.ANDROID
);
153 return BasicReader
.getReader();
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
);
162 AlertDialog
.Builder alert
= new AlertDialog
.Builder(context
);
163 alert
.setTitle(title
);
164 alert
.setMessage(message
);
165 alert
.setCancelable(true);
166 alert
.setView(input
);
168 if (listener
!= null) {
169 alert
.setPositiveButton(okMessage
,
170 new DialogInterface
.OnClickListener() {
172 public void onClick(DialogInterface dialog
, int which
) {
173 listener
.onAnswer(input
.getText().toString());
177 alert
.setOnCancelListener(new DialogInterface
.OnCancelListener() {
179 public void onCancel(DialogInterface dialog
) {
180 listener
.onAnswer(null);
188 private interface AnswerListener
{
189 public void onAnswer(String answer
);