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.graphics.Color;
7 import android.net.Uri;
8 import android.os.Bundle;
9 import android.util.Log;
10 import android.view.KeyEvent;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.view.View;
14 import android.view.ViewTreeObserver;
15 import android.widget.ImageView;
16 import android.widget.Toast;
20 import cz.cvut.fel.dce.qrscanner.mupdf.MuPDFCore;
21 import cz.cvut.fel.dce.qrscanner.pdfviewer.PdfViewActivity;
24 public class PreviewActivity extends Activity implements ViewTreeObserver.OnGlobalLayoutListener{
26 public static final String TAG = "PreviewActivity";
27 public static final String STORAGE_PATH = "/storage/sdcard0/Pictures";
28 public static final String SKODA_DOCS_PATH_EXTENSION = "/skoda/components/";
29 public static final String SKODA_COMP_PICTURE_NAME = "Abbildung.pdf";
30 public static final String SKODA_COMP_MANUFACTURING = "Arbeitseinleitung.pdf";
31 public static final String SKODA_COMP_MANUFACT_IMAGES = "Bild_Arbeitseinleitung.pdf";
32 public static final String SKODA_COMP_CONTACTS = "Angaben.pdf";
33 public static final String SKODA_COMP_MANUFACT_GUIDE = "Werkstatt_Einleitung.pdf";
35 private ImageView mPreviewImg;
36 private ViewTreeObserver mPreviewImgObserver;
37 private String mComponentId;
38 private String mComponentRootPath;
41 protected void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 setContentView(R.layout.activity_preview);
44 mPreviewImg = (ImageView) findViewById(R.id.imgComponent);
46 if (mPreviewImg != null) {
47 mPreviewImg.setMaxWidth(640);
48 mPreviewImg.setMaxHeight(360);
49 mPreviewImg.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
50 mPreviewImgObserver = mPreviewImg.getViewTreeObserver();
51 Log.i(TAG, "Registering mPreviewImgObserver OnGlobalLayoutListener.");
52 mPreviewImgObserver.addOnGlobalLayoutListener(this);
55 Log.e(TAG, "ImageView for preview image could not be found in the resources.");
56 mPreviewImgObserver = null;
59 Intent intent = getIntent();
60 mComponentId = intent.getStringExtra("COMPONENT_ID");
62 if (mComponentId != null) {
63 Log.i(TAG, "Received component id: " + mComponentId);
64 mComponentRootPath = STORAGE_PATH + SKODA_DOCS_PATH_EXTENSION + mComponentId + "/";
65 File rootPath = new File(mComponentRootPath);
66 if (!rootPath.isDirectory()) {
67 Toast toast = Toast.makeText(getApplicationContext(), "Component not found", Toast.LENGTH_LONG);
73 Log.i(TAG, "No component id received");
79 protected void onDestroy() {
81 Log.i(TAG, "Unregistering mPreviewImgObserver OnGlobalLayoutListener.");
82 if (mPreviewImgObserver != null && mPreviewImgObserver.isAlive()) {
83 mPreviewImgObserver.removeOnGlobalLayoutListener(this);
88 public boolean onCreateOptionsMenu(Menu menu) {
89 // Inflate the menu; this adds items to the action bar if it is present.
90 getMenuInflater().inflate(R.menu.menu_preview, menu);
95 public void onGlobalLayout() {
97 String picturePath = mComponentRootPath + SKODA_COMP_PICTURE_NAME;
98 Log.i(TAG, "Path to component files: " + picturePath);
100 MuPDFCore core = new MuPDFCore(getApplicationContext(), picturePath);
101 Log.d(TAG, "numpages: "+ core.countPages());
102 MuPDFCore.Cookie cookie = core.new Cookie();
103 int pageW = (int)core.getPageSize(0).x;
104 int pageH = (int)core.getPageSize(0).y;
105 Log.d(TAG, "page size: " + pageW + ", " + pageH);
106 Bitmap.Config conf = Bitmap.Config.ARGB_8888;
107 Bitmap previewBitmap = Bitmap.createBitmap(pageW, pageH, conf);
108 core.drawPage(previewBitmap, 0, pageW, pageH, 0, 0, pageW, pageH, cookie);
109 mPreviewImg.setImageBitmap(previewBitmap);
110 mPreviewImg.invalidate();
111 } catch (Exception e) {
112 Toast toast = Toast.makeText(getApplicationContext(), "Component preview could not be loaded.", Toast.LENGTH_LONG);
118 /** Called when the user touches the button */
119 public void showContacts() {
120 showPDF(mComponentRootPath + SKODA_COMP_CONTACTS);
123 /** Called when the user touches the button */
124 public void showManufacturing() {
125 showPDF(mComponentRootPath + SKODA_COMP_MANUFACTURING);
128 /** Called when the user touches the button */
129 public void showManufactImages() {
130 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_IMAGES);
133 /** Called when the user touches the button */
134 public void showManufactGuide() {
135 showPDF(mComponentRootPath + SKODA_COMP_MANUFACT_GUIDE);
138 private void showPDF(String filePath) {
139 Intent preview = new Intent(this, PdfViewActivity.class);
140 preview.putExtra("FILE_PATH", filePath);
141 startActivity(preview);
145 public boolean onKeyDown(int keyCode, KeyEvent event) {
146 if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
150 return super.onKeyDown(keyCode, event);
154 public boolean onOptionsItemSelected(MenuItem item) {
155 // Handle action bar item clicks here. The action bar will
156 // automatically handle clicks on the Home/Up button, so long
157 // as you specify a parent activity in AndroidManifest.xml.
158 int id = item.getItemId();
160 if (id == R.id.show_contacts) {
164 else if (id == R.id.show_manufacturing) {
168 else if (id == R.id.show_pictured_manufacturing) {
169 showManufactImages();
172 else if (id == R.id.show_workshop) {
176 else if (id == R.id.stop) {
181 return super.onOptionsItemSelected(item);