1 package cz.cvut.fel.dce.qrscanner;
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;
18 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
19 import cz.cvut.fel.dce.qrscanner.pdfviewer.PdfPageView;
21 import java.io.BufferedReader;
23 import java.io.FileNotFoundException;
24 import java.io.FileReader;
25 import java.io.IOException;
28 * An activity for component picture preview with menu for selecting documents to be shown.
30 * The activity is designed to be called via an Intent with a component identifier string passed
31 * with the key COMPONENT_ID.
33 * The activity loads the component picture from the PDF file asynchronously to not block the UI.
35 public class PreviewActivity extends ActionBarActivity implements ViewTreeObserver.OnGlobalLayoutListener {
37 * An activity tag for debug, error and info messages.
39 public static final String TAG = "PreviewActivity";
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>
45 public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
47 * The path in the application data folder, where the component database is.
49 public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
51 * The name of the PDF file containing the pisture of the component.
53 public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
55 * The name of the PDF file containing the manufacturing process description.
57 public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
59 * The name of the PDF file containing the manufacturing process description with pictures.
61 public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
63 * The name of the PDF file containing the contacts for the component manufacturer.
65 public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
67 * The name of the PDF file containing the manufacturing guide.
69 public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
71 * The Key of the Component Identifier value, passed as a data to the activity launch intent.
73 public static final String COMP_ID_INTENT_KEY = "COMPONENT_ID";
75 * The name of the file containing aditional information about the component.
77 public static final String SKODA_COMPONENT_INFO_NAME = "iteminfo.txt";
80 * The widget for showing the component preview image.
82 private ImageView mPreviewImg;
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>
87 private RelativeLayout mProgressContainer;
89 * The observer of the view tree for detection of the end of the View tree loading.
91 private ViewTreeObserver mPreviewImgObserver;
93 * The path to the directory containing documents for the component.
95 private String mComponentRootPath;
97 * Widget containing loaded page from a PDF file.
99 private PdfPageView mPdfView;
101 * Flag signalling whether the PDF page has been loaded.
103 private boolean mPdfLoaded;
105 * The widget showing the value of the components compartment.
107 private TextView mCompartmentValue;
111 protected void onCreate(Bundle savedInstanceState) {
112 super.onCreate(savedInstanceState);
113 setContentView(R.layout.activity_preview);
115 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
116 mProgressContainer = (RelativeLayout) findViewById(R.id.progress_container);
117 mCompartmentValue = (TextView) findViewById(R.id.compartment_value);
119 if (mProgressContainer == null) {
120 Log.e(TAG, "Progress container not found in the activity layout.");
123 if (mPreviewImg == null) {
124 Log.e(TAG, "ImageView for preview image could not be found in the activity layout.");
127 if (mCompartmentValue == null) {
128 Log.e(TAG, "TextView for compartment value could not be found in the activity layout.");
133 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
134 Log.d(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
135 mPreviewImgObserver.addOnGlobalLayoutListener(this);
137 Intent intent = getIntent();
138 String mComponentId = intent.getStringExtra(COMP_ID_INTENT_KEY);
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);
152 Log.e(TAG, "No component id received.");
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);
163 BufferedReader br = new BufferedReader(new FileReader(infoFile));
164 String compartmentLine = br.readLine();
165 if (compartmentLine != null) {
166 mCompartmentValue.setText(compartmentLine);
169 mCompartmentValue.setText("-");
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);
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);
188 protected void onDestroy() {
190 Log.d(TAG, "Unregister mPreviewImgObserver OnGlobalLayoutListener.");
191 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
192 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
197 public void onGlobalLayout() {
199 Log.d(TAG, "PDF file already loaded.");
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);
208 new LoadPageTask().execute();
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);
221 * Called when the user touches the button Contacts.
223 public void showContacts(View view) {
224 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
228 * Called when the user touches the button Manufacturing.
230 public void showManufacturing(View view) {
231 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
235 * Called when the user touches the button Manufacturing pictured
237 public void showManufactImages(View view) {
238 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
242 * Called when the user touches the button Manufacture guide
244 public void showManufactGuide(View view) {
245 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
249 * Launch an activity to show the PDF file.
250 * @param filePath Path to the PDF file to be shown
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);
261 * Class for asynchronous loading of the PDF page to a bitmap.
263 private class LoadPageTask extends AsyncTask<Void, Void, Void> {
266 protected Void doInBackground(Void[] objects) {
272 protected void onPreExecute() {
273 super.onPreExecute();
274 Log.d(TAG, "Starting loading of the PDF page asynchronously.");
275 mProgressContainer.setVisibility(View.VISIBLE);
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.");