]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
077656f4dfaa481d06898968976744d1b22b4588
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / src / main / java / cz / cvut / fel / dce / qrscanner / PreviewActivity.java
1 package cz.cvut.fel.dce.qrscanner;
2
3 import android.content.Context;
4 import android.content.Intent;
5 import android.media.AudioManager;
6 import android.net.Uri;
7 import android.os.AsyncTask;
8 import android.support.v7.app.ActionBarActivity;
9 import android.os.Bundle;
10 import android.util.Log;
11 import android.view.View;
12 import android.view.ViewTreeObserver;
13 import android.widget.ImageView;
14 import android.widget.RelativeLayout;
15 import android.widget.TextView;
16 import android.widget.Toast;
17
18 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
19 import cz.cvut.fel.dce.qrscanner.pdfviewer.PdfPageView;
20
21 import java.io.BufferedReader;
22 import java.io.File;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
26
27 /**
28  * An activity for component picture preview with menu for selecting documents to be shown.
29  *
30  * The activity is designed to be called via an Intent with a component identifier string passed
31  * with the key COMPONENT_ID.
32  *
33  * The activity loads the component picture from the PDF file asynchronously to not block the UI.
34  */
35 public class PreviewActivity extends ActionBarActivity implements ViewTreeObserver.OnGlobalLayoutListener {
36         /**
37          * An activity tag for debug, error and info messages.
38          */
39         public static final String TAG = "PreviewActivity";
40         /**
41          * The path to the storage folder, where the application stores its data.
42          * <p>This path should not be hardcoded like this, but the Android API functions were returning
43          * paths tht were not valid on our Android devices.</p>
44          */
45         public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
46         /**
47          * The path in the application data folder, where the component database is.
48          */
49         public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
50         /**
51          * The name of the PDF file containing the pisture of the component.
52          */
53         public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
54         /**
55          * The name of the PDF file containing the manufacturing process description.
56          */
57         public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
58         /**
59          * The name of the PDF file containing the manufacturing process description with pictures.
60          */
61         public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
62         /**
63          * The name of the PDF file containing the contacts for the component manufacturer.
64          */
65         public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
66         /**
67          * The name of the PDF file containing the manufacturing guide.
68          */
69         public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
70         /**
71          * The Key of the Component Identifier value, passed as a data to the activity launch intent.
72          */
73         public static final String COMP_ID_INTENT_KEY = "COMPONENT_ID";
74         /**
75          * The name of the file containing aditional information about the component.
76          */
77         public static final String SKODA_COMPONENT_INFO_NAME = "iteminfo.txt";
78
79         /**
80          * The widget for showing the component preview image.
81          */
82         private ImageView mPreviewImg;
83         /**
84          * The container for progress bar widget and text view.
85          * <p>Those two are in the container to make them easily disappear and appear when needed.</p>
86          */
87         private RelativeLayout mProgressContainer;
88         /**
89          * The observer of the view tree for detection of the end of the View tree loading.
90          */
91         private ViewTreeObserver mPreviewImgObserver;
92         /**
93          * The path to the directory containing documents for the component.
94          */
95         private String mComponentRootPath;
96         /**
97          * Widget containing loaded page from a PDF file.
98          */
99         private PdfPageView mPdfView;
100         /**
101          * Flag signalling whether the PDF page has been loaded.
102          */
103         private boolean mPdfLoaded;
104         /**
105          * The widget showing the value of the components compartment.
106          */
107         private TextView mCompartmentValue;
108
109
110         @Override
111         protected void onCreate(Bundle savedInstanceState) {
112                 super.onCreate(savedInstanceState);
113                 setContentView(R.layout.activity_preview);
114                 mPdfLoaded = false;
115                 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
116                 mProgressContainer = (RelativeLayout) findViewById(R.id.progress_container);
117                 mCompartmentValue = (TextView) findViewById(R.id.compartment_value);
118
119                 if (mProgressContainer == null) {
120                         Log.e(TAG, "Progress container not found in the activity layout.");
121                         finish();
122                 }
123                 if (mPreviewImg == null) {
124                         Log.e(TAG, "ImageView for preview image could not be found in the activity layout.");
125                         finish();
126                 }
127                 if (mCompartmentValue == null) {
128                         Log.e(TAG, "TextView for compartment value could not be found in the activity layout.");
129                         finish();
130                 }
131
132
133                 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
134                 Log.d(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
135                 mPreviewImgObserver.addOnGlobalLayoutListener(this);
136
137                 Intent intent = getIntent();
138                 String mComponentId = intent.getStringExtra(COMP_ID_INTENT_KEY);
139
140                 if (mComponentId != null) {
141                         Log.i(TAG, "Received component id: " + mComponentId);
142                         mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
143                         File rootPath = new File(mComponentRootPath);
144                         if (!rootPath.isDirectory()) {
145                                 Log.e(TAG, "Component database root directory " + rootPath.getAbsolutePath() + " does not exist.");
146                                 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
147                                 toast.show();
148                                 finish();
149                         }
150                 }
151                 else {
152                         Log.e(TAG, "No component id received.");
153                         finish();
154                 }
155                 File infoFile = new File(mComponentRootPath + SKODA_COMPONENT_INFO_NAME);
156                 if (!infoFile.exists()) {
157                         Log.e(TAG, "Aditional information file " + infoFile.getAbsolutePath() + " does not exist.");
158                         Toast toast = Toast.makeText(getApplicationContext(), "Database not complete", Toast.LENGTH_LONG);
159                         toast.show();
160                         finish();
161                 }
162                 try {
163                         BufferedReader br = new BufferedReader(new FileReader(infoFile));
164                         String compartmentLine = br.readLine();
165                         if (compartmentLine != null) {
166                                 mCompartmentValue.setText(compartmentLine);
167                         }
168                         else {
169                                 mCompartmentValue.setText("-");
170                         }
171                 } catch (FileNotFoundException e) {
172                         Log.e(TAG, "Aditional information file " + infoFile.getAbsolutePath() + " does not exist.");
173                         Toast toast = Toast.makeText(getApplicationContext(), "Database not complete", Toast.LENGTH_LONG);
174                         toast.show();
175                         e.printStackTrace();
176                         finish();
177                 } catch (IOException e) {
178                         Log.e(TAG, "Error while reading the aditional information file " + infoFile.getAbsolutePath());
179                         Toast toast = Toast.makeText(getApplicationContext(), "Database reading failed", Toast.LENGTH_LONG);
180                         toast.show();
181                         e.printStackTrace();
182                         finish();
183                 }
184
185         }
186
187         @Override
188         protected void onDestroy() {
189                 super.onDestroy();
190                 Log.d(TAG, "Unregister mPreviewImgObserver OnGlobalLayoutListener.");
191                 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
192                         mPreviewImgObserver.removeOnGlobalLayoutListener(this);
193                 }
194         }
195
196         @Override
197         public void onGlobalLayout() {
198                 if (mPdfLoaded) {
199                         Log.d(TAG, "PDF file already loaded.");
200                         return;
201                 }
202                 try {
203                         String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
204                         Log.i(TAG, "Path to component files: " + picturePath);
205                         Log.d(TAG, "Loading PDF file " + picturePath);
206                         mPdfView = new PdfPageView(getApplicationContext(), picturePath);
207                         mPdfView.setPage(0);
208                         new LoadPageTask().execute();
209
210                         mPdfLoaded = true;
211
212                 } catch (Exception e) {
213                         Log.e(TAG, "Component picture loading from PDF file failed: " + e.getMessage());
214                         Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
215                         toast.show();
216                         e.printStackTrace();
217                 }
218         }
219
220         /**
221          * Called when the user touches the button Contacts.
222          */
223         public void showContacts(View view) {
224                 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
225         }
226
227         /**
228          * Called when the user touches the button Manufacturing.
229          */
230         public void showManufacturing(View view) {
231                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
232         }
233
234         /**
235          * Called when the user touches the button Manufacturing pictured
236          */
237         public void showManufactImages(View view) {
238                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
239         }
240
241         /**
242          * Called when the user touches the button Manufacture guide
243          */
244         public void showManufactGuide(View view) {
245                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
246         }
247
248         /**
249          * Launch an activity to show the PDF file.
250          * @param filePath Path to the PDF file to be shown
251          */
252         private void showPDF(String filePath) {
253                 Uri uri = Uri.parse(filePath);
254                 Intent pdfIntent = new Intent(this, MuPDFActivity.class);
255                 pdfIntent.setAction(Intent.ACTION_VIEW);
256                 pdfIntent.setData(uri);
257                 startActivity(pdfIntent);
258         }
259
260         /**
261          * Class for asynchronous loading of the PDF page to a bitmap.
262          */
263         private class LoadPageTask extends AsyncTask<Void, Void, Void> {
264
265                 @Override
266                 protected Void doInBackground(Void[] objects) {
267                         mPdfView.loadPage();
268                         return null;
269                 }
270
271                 @Override
272                 protected void onPreExecute() {
273                         super.onPreExecute();
274                         Log.d(TAG, "Starting loading of the PDF page asynchronously.");
275                         mProgressContainer.setVisibility(View.VISIBLE);
276                 }
277
278                 @Override
279                 protected void onPostExecute(Void aVoid) {
280                         super.onPostExecute(aVoid);
281                         mProgressContainer.setVisibility(View.INVISIBLE);
282                         mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
283                         mPreviewImg.setImageBitmap(mPdfView.getPageBitmap());
284                         mPreviewImg.invalidate();
285                         Log.d(TAG, "PDF page loaded.");
286                 }
287         }
288 }