]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blobdiff - QRScanner/glass/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
Clean and coment the code.
[hornmich/skoda-qr-demo.git] / QRScanner / glass / src / main / java / cz / cvut / fel / dce / qrscanner / PreviewActivity.java
index a8aafbf4ad4107ffc620eb5ab20afd8dea788b1d..17383b00fac16840f6a0e5deba4724c9f4a955c1 100644 (file)
@@ -3,10 +3,7 @@ package cz.cvut.fel.dce.qrscanner;
 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
-import android.graphics.Bitmap;
-import android.graphics.Color;
 import android.media.AudioManager;
-import android.net.Uri;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
@@ -23,28 +20,81 @@ import com.google.android.glass.media.Sounds;
 
 import java.io.File;
 
-import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
 import cz.cvut.fel.dce.qrscanner.pdfviewer.PdfPageView;
 import cz.cvut.fel.dce.qrscanner.pdfviewer.PdfViewActivity;
 
-
+/**
+ * An activity for component picture preview with menu for selecting documents to be shown.
+ *
+ * The activity is designed to be called via an Intent with a component identifier string passed
+ * with the key COMPONENT_ID.
+ *
+ * The activity loads the component picture from the PDF file asynchronously to not block the UI.
+ */
 public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener{
-
+       /**
+        * An activity tag for debug, error and info messages.
+        */
        public static final String TAG = "PreviewActivity";
+       /**
+        * The path to the storage folder, where the application stores its data.
+        * <p>This path should not be hardcoded like this, but the Android API functions were returning
+        * paths tht were not valid on our Android devices.</p>
+        */
        public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
+       /**
+        * The path in the application data folder, where the component database is.
+        */
        public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
+       /**
+        * The name of the PDF file containing the pisture of the component.
+        */
        public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
+       /**
+        * The name of the PDF file containing the manufacturing process description.
+        */
        public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
+       /**
+        * The name of the PDF file containing the manufacturing process description with pictures.
+        */
        public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
+       /**
+        * The name of the PDF file containing the contacts for the component manufacturer.
+        */
        public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
+       /**
+        * The name of the PDF file containing the manufacturing guide.
+        */
        public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
+       /**
+        * The Key of the Component Identifier value, passed as a data to the activity launch intent.
+        */
+       public static final String COMP_ID_INTENT_KEY = "COMPONENT_ID";
 
+       /**
+        * The widget for showing the component preview image.
+        */
        private ImageView mPreviewImg;
+       /**
+        * The container for progress bar widget and text view.
+        * <p>Those two are in the container to make them easily disappear and appear when needed.</p>
+        */
        private RelativeLayout mProgressContainer;
+       /**
+        * The observer of the view tree for detection of the end of the View tree loading.
+        */
        private ViewTreeObserver mPreviewImgObserver;
-       private String mComponentId;
+       /**
+        * The path to the directory containing documents for the component.
+        */
        private String mComponentRootPath;
+       /**
+        * Widget containing loaded page from a PDF file.
+        */
        private PdfPageView mPdfView;
+       /**
+        * Flag signalling whether the PDF page has been loaded.
+        */
        private boolean mPdfLoaded;
 
        @Override
@@ -55,27 +105,28 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
                mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
                mProgressContainer = (RelativeLayout) findViewById(R.id.progress_container);
 
-               if (mPreviewImg != null) {
-                       mPreviewImg.setMaxWidth(640);
-                       mPreviewImg.setMaxHeight(360);
-                       mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
-                       mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
-                       Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
-                       mPreviewImgObserver.addOnGlobalLayoutListener(this);
+               if (mProgressContainer == null) {
+                       Log.e(TAG, "Progress container not found in the activity layout.");
+                       finish();
                }
-               else {
-                       Log.e(TAG, "ImageView for preview image could not be found in the resources.");
-                       mPreviewImgObserver = null;
+               if (mPreviewImg == null) {
+                       Log.e(TAG, "ImageView for preview image could not be found in the activity layout.");
+                       finish();
                }
 
+               mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
+               Log.d(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
+               mPreviewImgObserver.addOnGlobalLayoutListener(this);
+
                Intent intent = getIntent();
-               mComponentId = intent.getStringExtra("COMPONENT_ID");
+               String mComponentId = intent.getStringExtra(COMP_ID_INTENT_KEY);
 
                if (mComponentId != null) {
                        Log.i(TAG, "Received component id: " + mComponentId);
                        mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
                        File rootPath = new File(mComponentRootPath);
                        if (!rootPath.isDirectory()) {
+                               Log.e(TAG, "Component database root directory " + rootPath.getAbsolutePath() + " does not exist.");
                                Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
                                toast.show();
                                AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
@@ -84,7 +135,7 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
                        }
                }
                else {
-                       Log.i(TAG, "No component id received");
+                       Log.e(TAG, "No component id received.");
                        finish();
                }
        }
@@ -92,7 +143,7 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
        @Override
        protected void onDestroy() {
                super.onDestroy();
-               Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
+               Log.d(TAG, "Unregister mPreviewImgObserver OnGlobalLayoutListener.");
                if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
                        mPreviewImgObserver.removeOnGlobalLayoutListener(this);
                }
@@ -100,7 +151,6 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
-               // Inflate the menu; this adds items to the action bar if it is present.
                getMenuInflater().inflate(R.menu.menu_preview, menu);
                return true;
        }
@@ -114,6 +164,7 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
                try {
                        String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
                        Log.i(TAG, "Path to component files: " + picturePath);
+                       Log.d(TAG, "Loading PDF file " + picturePath);
                        mPdfView = new PdfPageView(getApplicationContext(), picturePath);
                        mPdfView.setPage(0);
                        new LoadPageTask().execute();
@@ -121,6 +172,7 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
                        mPdfLoaded = true;
 
                } catch (Exception e) {
+                       Log.e(TAG, "Component picture loading from PDF file failed: " + e.getMessage());
                        Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
                        toast.show();
                        AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
@@ -129,29 +181,33 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
                }
        }
 
-       /** Called when the user touches the button */
+       /** Called when the user touches the button Contacts. */
        public void showContacts() {
                showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
        }
 
-       /** Called when the user touches the button */
+       /** Called when the user touches the button Manufacturing. */
        public void showManufacturing() {
                showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
        }
 
-       /** Called when the user touches the button */
+       /** Called when the user touches the button Manufacturing pictured. */
        public void showManufactImages() {
                showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
        }
 
-       /** Called when the user touches the button */
+       /** Called when the user touches the button Manufacture guide. */
        public void showManufactGuide() {
                showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
        }
 
+       /**
+        * Launch an activity to show the PDF file.
+        * @param filePath Path to the PDF file to be shown
+        */
        private void showPDF(String filePath) {
                Intent preview = new Intent(this, PdfViewActivity.class);
-               preview.putExtra("FILE_PATH", filePath);
+               preview.putExtra(PdfViewActivity.FILE_PATH_INTENT_KEY, filePath);
                startActivity(preview);
        }
 
@@ -168,9 +224,6 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
 
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
-               // Handle action bar item clicks here. The action bar will
-               // automatically handle clicks on the Home/Up button, so long
-               // as you specify a parent activity in AndroidManifest.xml.
                int id = item.getItemId();
 
                if (id == R.id.show_contacts) {
@@ -198,6 +251,9 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
                }
        }
 
+       /**
+        * Class for asynchronous loading of the PDF page to a bitmap.
+        */
        private class LoadPageTask extends AsyncTask<Void, Void, Void> {
 
                @Override
@@ -219,10 +275,10 @@ public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlob
                        mProgressContainer.setVisibility(View.INVISIBLE);
                        mPreviewImg.setMinimumWidth(640);
                        mPreviewImg.setMinimumHeight(360);
+                       mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                        mPreviewImg.setImageBitmap(mPdfView.getPageBitmap());
                        mPreviewImg.invalidate();
                        Log.d(TAG, "PDF page loaded.");
                }
        }
-
 }