]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blobdiff - QRScanner/mobile/src/main/java/cz/cvut/fel/dce/qrscanner/PreviewActivity.java
Fix bug with loading pdf preview
[hornmich/skoda-qr-demo.git] / QRScanner / mobile / src / main / java / cz / cvut / fel / dce / qrscanner / PreviewActivity.java
index 79ee599bf9cafbc4b408a962787cfad06b1f7b27..2ea62c447757aaed8e82d9ea7153553a71296abb 100644 (file)
@@ -2,15 +2,19 @@ package cz.cvut.fel.dce.qrscanner;
 
 import android.content.Intent;
 import android.graphics.Bitmap;
+import android.net.Uri;
 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
+import android.view.View;
 import android.view.ViewTreeObserver;
+import android.widget.Button;
 import android.widget.ImageView;
 import android.widget.Toast;
 
+import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
 import java.io.File;
 
@@ -19,6 +23,10 @@ public class PreviewActivity extends ActionBarActivity implements ViewTreeObserv
        public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
        public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
        public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
+       public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
+       public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
+       public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
+       public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
 
        private ImageView mPreviewImg;
        private ViewTreeObserver mPreviewImgObserver;
@@ -30,9 +38,17 @@ public class PreviewActivity extends ActionBarActivity implements ViewTreeObserv
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_preview);
                mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
-               mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
-               Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
-               mPreviewImgObserver.addOnGlobalLayoutListener(this);
+               mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
+
+               if (mPreviewImg != null) {
+                       mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
+                       Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
+                       mPreviewImgObserver.addOnGlobalLayoutListener(this);
+               }
+               else {
+                       Log.e(TAG, "ImageView for preview image could not be found in the resources.");
+                       mPreviewImgObserver = null;
+               }
 
                Intent intent = getIntent();
                mComponentId = intent.getStringExtra("COMPONENT_ID");
@@ -57,7 +73,7 @@ public class PreviewActivity extends ActionBarActivity implements ViewTreeObserv
        protected void onDestroy() {
                super.onDestroy();
                Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
-               if (mPreviewImgObserver.isAlive()) {
+               if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
                        mPreviewImgObserver.removeOnGlobalLayoutListener(this);
                }
        }
@@ -65,48 +81,62 @@ public class PreviewActivity extends ActionBarActivity implements ViewTreeObserv
        @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;
        }
 
        @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();
-
-               //noinspection SimplifiableIfStatement
-               if (id == R.id.action_settings) {
-                       return true;
+       public void onGlobalLayout() {
+               try {
+                       String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
+                       Log.i(TAG, "Path to component files: " + picturePath);
+
+                       MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
+                       Log.d(TAG, "numpages: "+ core.countPages());
+                       MuPDFCore.Cookie cookie = core.new Cookie();
+                       int pageW = (int)core.getPageSize(0).x;
+                       int pageH = (int)core.getPageSize(0).y;
+                       Log.d(TAG, "page size: " + pageW + ", " + pageH);
+                       Bitmap.Config conf = Bitmap.Config.ARGB_8888;
+                       Bitmap previewBitmap = Bitmap.createBitmap(pageW, pageH, conf);
+                       core.drawPage(previewBitmap, 0, pageW, pageH,0 , 0, pageW, pageH, cookie);
+                       mPreviewImg.setMaxWidth(mPreviewImg.getWidth());
+                       mPreviewImg.setMaxHeight(mPreviewImg.getHeight());
+                       mPreviewImg.setImageBitmap(previewBitmap);
+                       mPreviewImg.invalidate();
+               } catch (Exception e) {
+                       Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
+                       toast.show();
+                       e.printStackTrace();
                }
+       }
 
-               return super.onOptionsItemSelected(item);
+       /** Called when the user touches the button */
+       public void showContacts(View view) {
+               showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
        }
 
-       @Override
-       public void onGlobalLayout() {
-               if (mPreviewImg != null) {
-                       try {
-                               String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
-                               Log.i(TAG, "Path to component files: " + picturePath);
-
-                               MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
-                               MuPDFCore.Cookie cookie = core.new Cookie();
-                               int previewW = mPreviewImg.getWidth();
-                               int previewH = mPreviewImg.getHeight();
-                               Bitmap.Config conf = Bitmap.Config.ARGB_8888;
-                               Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
-                               core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
-                               mPreviewImg.setImageBitmap(previewBitmap);
-                       } catch (Exception e) {
-                               Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
-                               toast.show();
-                               e.printStackTrace();
-                       }
-               }
-               else {
-                       Log.e(TAG, "ImageView for preview image could not be found in the resources.");
-               }
+       /** Called when the user touches the button */
+       public void showManufacturing(View view) {
+               showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
+       }
+
+       /** Called when the user touches the button */
+       public void showManufactImages(View view) {
+               showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
        }
+
+       /** Called when the user touches the button */
+       public void showManufactGuide(View view) {
+               showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
+       }
+
+       private void showPDF(String filePath) {
+               Uri uri = Uri.parse(filePath);
+               Intent pdfIntent = new Intent(this,MuPDFActivity.class);
+               pdfIntent.setAction(Intent.ACTION_VIEW);
+               pdfIntent.setData(uri);
+               startActivity(pdfIntent);
+       }
+
+
 }