]> rtime.felk.cvut.cz Git - hornmich/skoda-qr-demo.git/blob - 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
1 package cz.cvut.fel.dce.qrscanner;
2
3 import android.content.Intent;
4 import android.graphics.Bitmap;
5 import android.net.Uri;
6 import android.support.v7.app.ActionBarActivity;
7 import android.os.Bundle;
8 import android.util.Log;
9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.view.View;
12 import android.view.ViewTreeObserver;
13 import android.widget.Button;
14 import android.widget.ImageView;
15 import android.widget.Toast;
16
17 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
18 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
19 import java.io.File;
20
21 public class PreviewActivity extends ActionBarActivity implements ViewTreeObserver.OnGlobalLayoutListener {
22         public static final String TAG = "PreviewActivity";
23         public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
24         public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
25         public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
26         public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
27         public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
28         public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
29         public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
30
31         private ImageView mPreviewImg;
32         private ViewTreeObserver mPreviewImgObserver;
33         private String mComponentId;
34         private String mComponentRootPath;
35
36         @Override
37         protected void onCreate(Bundle savedInstanceState) {
38                 super.onCreate(savedInstanceState);
39                 setContentView(R.layout.activity_preview);
40                 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
41                 mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
42
43                 if (mPreviewImg != null) {
44                         mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
45                         Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
46                         mPreviewImgObserver.addOnGlobalLayoutListener(this);
47                 }
48                 else {
49                         Log.e(TAG, "ImageView for preview image could not be found in the resources.");
50                         mPreviewImgObserver = null;
51                 }
52
53                 Intent intent = getIntent();
54                 mComponentId = intent.getStringExtra("COMPONENT_ID");
55
56                 if (mComponentId != null) {
57                         Log.i(TAG, "Received component id: " + mComponentId);
58                         mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
59                         File rootPath = new File(mComponentRootPath);
60                         if (!rootPath.isDirectory()) {
61                                 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
62                                 toast.show();
63                                 finish();
64                         }
65                 }
66                 else {
67                         Log.i(TAG, "No component id received");
68                         finish();
69                 }
70         }
71
72         @Override
73         protected void onDestroy() {
74                 super.onDestroy();
75                 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
76                 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
77                         mPreviewImgObserver.removeOnGlobalLayoutListener(this);
78                 }
79         }
80
81         @Override
82         public boolean onCreateOptionsMenu(Menu menu) {
83                 // Inflate the menu; this adds items to the action bar if it is present.
84                 return true;
85         }
86
87         @Override
88         public void onGlobalLayout() {
89                 try {
90                         String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
91                         Log.i(TAG, "Path to component files: " + picturePath);
92
93                         MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
94                         Log.d(TAG, "numpages: "+ core.countPages());
95                         MuPDFCore.Cookie cookie = core.new Cookie();
96                         int pageW = (int)core.getPageSize(0).x;
97                         int pageH = (int)core.getPageSize(0).y;
98                         Log.d(TAG, "page size: " + pageW + ", " + pageH);
99                         Bitmap.Config conf = Bitmap.Config.ARGB_8888;
100                         Bitmap previewBitmap = Bitmap.createBitmap(pageW, pageH, conf);
101                         core.drawPage(previewBitmap, 0, pageW, pageH,0 , 0, pageW, pageH, cookie);
102                         mPreviewImg.setMaxWidth(mPreviewImg.getWidth());
103                         mPreviewImg.setMaxHeight(mPreviewImg.getHeight());
104                         mPreviewImg.setImageBitmap(previewBitmap);
105                         mPreviewImg.invalidate();
106                 } catch (Exception e) {
107                         Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
108                         toast.show();
109                         e.printStackTrace();
110                 }
111         }
112
113         /** Called when the user touches the button */
114         public void showContacts(View view) {
115                 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
116         }
117
118         /** Called when the user touches the button */
119         public void showManufacturing(View view) {
120                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
121         }
122
123         /** Called when the user touches the button */
124         public void showManufactImages(View view) {
125                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
126         }
127
128         /** Called when the user touches the button */
129         public void showManufactGuide(View view) {
130                 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
131         }
132
133         private void showPDF(String filePath) {
134                 Uri uri = Uri.parse(filePath);
135                 Intent pdfIntent = new Intent(this,MuPDFActivity.class);
136                 pdfIntent.setAction(Intent.ACTION_VIEW);
137                 pdfIntent.setData(uri);
138                 startActivity(pdfIntent);
139         }
140
141
142 }