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