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 if (mPreviewImg != null) {
42 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
43 Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
44 mPreviewImgObserver.addOnGlobalLayoutListener(this);
47 Log.e(TAG, "ImageView for preview image could not be found in the resources.");
48 mPreviewImgObserver = null;
51 Intent intent = getIntent();
52 mComponentId = intent.getStringExtra("COMPONENT_ID");
54 if (mComponentId != null) {
55 Log.i(TAG, "Received component id: " + mComponentId);
56 mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
57 File rootPath = new File(mComponentRootPath);
58 if (!rootPath.isDirectory()) {
59 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
65 Log.i(TAG, "No component id received");
71 protected void onDestroy() {
73 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
74 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
75 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
80 public boolean onCreateOptionsMenu(Menu menu) {
81 // Inflate the menu; this adds items to the action bar if it is present.
86 public void onGlobalLayout() {
88 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
89 Log.i(TAG, "Path to component files: " + picturePath);
91 MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
92 MuPDFCore.Cookie cookie = core.new Cookie();
93 int previewW = mPreviewImg.getWidth();
94 int previewH = mPreviewImg.getHeight();
95 Bitmap.Config conf = Bitmap.Config.ARGB_8888;
96 Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
97 core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
98 mPreviewImg.setImageBitmap(previewBitmap);
99 } catch (Exception e) {
100 Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
106 /** Called when the user touches the button */
107 public void showContacts(View view) {
108 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
111 /** Called when the user touches the button */
112 public void showManufacturing(View view) {
113 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
116 /** Called when the user touches the button */
117 public void showManufactImages(View view) {
118 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
121 /** Called when the user touches the button */
122 public void showManufactGuide(View view) {
123 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
126 private void showPDF(String filePath) {
127 Uri uri = Uri.parse(filePath);
128 Intent pdfIntent = new Intent(this,MuPDFActivity.class);
129 pdfIntent.setAction(Intent.ACTION_VIEW);
130 pdfIntent.setData(uri);
131 startActivity(pdfIntent);