opening html files was not OK anymore
[nikiroo-utils.git] / src / be / nikiroo / fanfix / reader / android / AndroidReaderBook.java
CommitLineData
64968418
NR
1package be.nikiroo.fanfix.reader.android;
2
3import android.app.Activity;
4import android.app.Fragment;
5import android.graphics.Bitmap;
6import android.os.AsyncTask;
7import android.os.Bundle;
8import android.view.LayoutInflater;
9import android.view.View;
10import android.view.ViewGroup;
11import android.widget.FrameLayout;
12import android.widget.ImageView;
13import android.widget.TextView;
14
15import java.io.IOException;
16
64968418
NR
17import be.nikiroo.fanfix.data.MetaData;
18import be.nikiroo.fanfix.reader.Reader;
19import be.nikiroo.utils.Image;
20import be.nikiroo.utils.android.ImageUtilsAndroid;
21
22public class AndroidReaderBook extends Fragment {
64968418 23 private OnFragmentInteractionListener listener;
64968418
NR
24
25 /**
26 * This interface must be implemented by activities that contain this
27 * fragment to allow an interaction in this fragment to be communicated to
28 * the activity and potentially other fragments contained in that activity.
29 * <p>
30 * See the Android Training lesson <a href=
31 * "http://developer.android.com/training/basics/fragments/communicating.html"
32 * >Communicating with Other Fragments</a> for more information.
33 */
34 public interface OnFragmentInteractionListener {
35 void onFragmentInteraction(MetaData meta);
36 }
37
38 public AndroidReaderBook() {
39 // Required empty public constructor
40 }
41
42 @Override
43 public View onCreateView(LayoutInflater inflater, ViewGroup container,
44 Bundle savedInstanceState) {
45 return inflater.inflate(R.layout.fragment_android_reader_book,
46 container, false);
47 }
48
49 @Override
50 public void onAttach(Activity context) {
51 super.onAttach(context);
52 if (context instanceof OnFragmentInteractionListener) {
53 listener = (OnFragmentInteractionListener) context;
54 }
55 }
56
57 @Override
58 public void onDetach() {
59 super.onDetach();
60 listener = null;
61 }
62
a18ba536
NR
63 public void fill(final MetaData meta, final Reader reader) {
64 ViewHolder viewHolder = new ViewHolder(getView());
64968418 65
a18ba536
NR
66 viewHolder.title.setText(meta.getTitle());
67 viewHolder.author.setText(meta.getAuthor());
68 viewHolder.frame.setClickable(true);
69 viewHolder.frame.setFocusable(true);
70 viewHolder.frame.setOnClickListener(new View.OnClickListener() {
64968418 71 @Override
a18ba536
NR
72 public void onClick(View v) {
73 OnFragmentInteractionListener llistener = listener;
74 if (llistener != null) {
75 llistener.onFragmentInteraction(meta);
76 }
64968418 77 }
a18ba536 78 });
64968418 79
a18ba536 80 new AsyncTask<MetaData, Void, Image>() {
64968418 81 @Override
a18ba536
NR
82 protected Image doInBackground(MetaData[] metas) {
83 if (metas[0].getCover() != null) {
84 return metas[0].getCover();
64968418
NR
85 }
86
a18ba536
NR
87 return reader.getLibrary().getCover(metas[0].getLuid());
88 }
89
90 @Override
91 protected void onPostExecute(Image coverImage) {
92 ViewHolder viewHolder = new ViewHolder(getView());
93
94 try {
95 if (coverImage != null) {
96 Bitmap coverBitmap = ImageUtilsAndroid
97 .fromImage(coverImage);
98 coverBitmap = Bitmap.createScaledBitmap(coverBitmap,
99 128, 128, true);
100 viewHolder.cover.setImageBitmap(coverBitmap);
64968418 101 }
a18ba536
NR
102 } catch (IOException e) {
103 e.printStackTrace();
104 }
64968418 105 }
a18ba536
NR
106 }.execute(meta);
107 }
108
109 private class ViewHolder {
110 public FrameLayout frame;
111 public TextView title;
112 public TextView author;
113 public ImageView cover;
114
115 public ViewHolder(View book) {
116 frame = book.findViewById(R.id.Book);
117 title = book.findViewById(R.id.Book_lblTitle);
118 author = book.findViewById(R.id.Book_lblAuthor);
119 cover = book.findViewById(R.id.Book_imgCover);
120 }
64968418
NR
121 }
122}