1 package cz.cvut.fel.dce.qrscanner;
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;
17 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
18 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
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";
31 private ImageView mPreviewImg;
32 private ViewTreeObserver mPreviewImgObserver;
33 private String mComponentId;
34 private String mComponentRootPath;
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);
43 if (mPreviewImg != null) {
44 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
45 Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
46 mPreviewImgObserver.addOnGlobalLayoutListener(this);
49 Log.e(TAG, "ImageView for preview image could not be found in the resources.");
50 mPreviewImgObserver = null;
53 Intent intent = getIntent();
54 mComponentId = intent.getStringExtra("COMPONENT_ID");
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);
67 Log.i(TAG, "No component id received");
73 protected void onDestroy() {
75 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
76 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
77 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
82 public boolean onCreateOptionsMenu(Menu menu) {
83 // Inflate the menu; this adds items to the action bar if it is present.
88 public void onGlobalLayout() {
90 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
91 Log.i(TAG, "Path to component files: " + picturePath);
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);
113 /** Called when the user touches the button */
114 public void showContacts(View view) {
115 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
118 /** Called when the user touches the button */
119 public void showManufacturing(View view) {
120 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
123 /** Called when the user touches the button */
124 public void showManufactImages(View view) {
125 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
128 /** Called when the user touches the button */
129 public void showManufactGuide(View view) {
130 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
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);