1 package cz.cvut.fel.dce.qrscanner;
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.graphics.Bitmap;
6 import android.net.Uri;
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.ImageView;
14 import android.widget.Toast;
18 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFActivity;
19 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
22 public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener{
24 public static final String TAG = "PreviewActivity";
25 public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
26 public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
27 public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
28 public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
29 public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
30 public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
31 public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
33 private ImageView mPreviewImg;
34 private ViewTreeObserver mPreviewImgObserver;
35 private String mComponentId;
36 private String mComponentRootPath;
39 protected void onCreate(Bundle savedInstanceState) {
40 super.onCreate(savedInstanceState);
41 setContentView(R.layout.activity_preview);
42 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
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.
84 getMenuInflater().inflate(R.menu.menu_preview, menu);
89 public void onGlobalLayout() {
91 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
92 Log.i(TAG, "Path to component files: " + picturePath);
94 MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
95 MuPDFCore.Cookie cookie = core.new Cookie();
96 int previewW = mPreviewImg.getWidth();
97 int previewH = mPreviewImg.getHeight();
98 Bitmap.Config conf = Bitmap.Config.ARGB_8888;
99 Bitmap previewBitmap = Bitmap.createBitmap(previewW, previewH, conf);
100 core.updatePage(previewBitmap, 0, previewW, previewH, 0, 0, previewW, previewH, cookie);
101 mPreviewImg.setImageBitmap(previewBitmap);
102 } catch (Exception e) {
103 Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
109 /** Called when the user touches the button */
110 public void showContacts(View view) {
111 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
114 /** Called when the user touches the button */
115 public void showManufacturing(View view) {
116 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
119 /** Called when the user touches the button */
120 public void showManufactImages(View view) {
121 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
124 /** Called when the user touches the button */
125 public void showManufactGuide(View view) {
126 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
129 private void showPDF(String filePath) {
130 Uri uri = Uri.parse(filePath);
131 Intent pdfIntent = new Intent(this,MuPDFActivity.class);
132 pdfIntent.setAction(Intent.ACTION_VIEW);
133 pdfIntent.setData(uri);
134 startActivity(pdfIntent);
137 public boolean onOptionsItemSelected(MenuItem item) {
138 // Handle action bar item clicks here. The action bar will
139 // automatically handle clicks on the Home/Up button, so long
140 // as you specify a parent activity in AndroidManifest.xml.
141 int id = item.getItemId();
143 //noinspection SimplifiableIfStatement
144 if (id == R.id.action_settings) {
148 return super.onOptionsItemSelected(item);